C++练习实验(上)

实验一

#include<iostream>
#include<cstring>
using namespace std;
int max1 = 100; 
class Book{
	public :
		Book();
		void display();
		void borrow();
		void restore();
	private :
		string bookname;
		string author;
		double price;
		int number;
};
Book::Book(){
	bookname = "NEVer";
	author = "Dyb";
	price = 50.5;
	number = max1;
}
void Book :: display(){
	cout << "bookname : " << bookname << endl;
	cout << "author : " << author << endl;
	cout << "price : " << price << endl;
	cout << "number : " << number << endl; 
}
void Book :: borrow(){ 

	if(number <= 0)
		cout << "借书失败,库存不足,请等待他人还书" << endl; 
	else{
		--number;
		cout << "借书成功。"<< endl;
		cout << "此书当前剩余:" << number << "本" << endl;
	}
}
void Book :: restore(){
	if(number < max1){
		number ++;
		cout << "还书成功~" << endl; 
	} 
	else{
		cout << "本馆该书库存已满:" << endl;
		cout << "你可以选择把该书送给本馆 或 归还至其他图书馆 :" << endl;
		cout << "送给本馆 请输入1 :: 归还至其他图书馆请输入2 " << endl;
		string ch;
		while(1){
			cin >> ch;
			if(ch == "1"){
				max1++;
				number++;
				cout << "谢谢您 !祝您新年快乐。" << endl; 
				break;
			} 
			else if(ch == "2")
			{
				cout << "欢迎下次光临~~~ 祝您新年快乐。" << endl; 
				break;
			}
			else{
				cout << "输入违法 请重新输入~" << endl; 
			}
		}
		
	}
	cout << "当前图书剩余为:" << number << endl; 
} 
void help(){
	cout << "~~~~~~		欢迎来到图书馆管理系统		~~~~~~" << endl;
	cout << "~~~~~~		借书请输入b			~~~~~~" << endl;
	cout << "~~~~~~		还书请输入r			~~~~~~" << endl;
	cout << "~~~~~~		显示当前图书信息请输入s		~~~~~~" << endl; 
}
int main (){
	Book book1;
		help();
		char oper;
		while(1){
		cin >> oper;
			switch (oper){
				case 'b':
					book1.borrow();
					break;
				case 'r':
					book1.restore();
					break;
				case 's':
					book1.display();
					break;
				case 'e': 
					cout << "结束程序~" << endl;
					exit(0);
				default:
					cout << "输入违法,请重新输入!" << endl;
			}	
		}
	return 0;
} 

实验二

 

#include<iostream>
using namespace std;
class Shop{
	public :
		Shop(int, double);
		static void Average();
		static void Display();
	private:
		static int n;
		double price;
		static double discount;
		static double average;
		static double sum;
};

double Shop::sum = 0;
double Shop::average = 0;
double Shop::discount = 0.98;
int Shop :: n = 0;
Shop :: Shop(int number, double p){
	n = number;
	if(number > 10)
		price = p * discount;
	else
		price = p;
	sum += price * number;
}
void Shop::Average()
{
	average = sum / n;
}
void Shop::Display(){
	cout << "The total number of sale is : " << sum << endl;
	cout << "The average is: " << average << endl;
}
int main(){
	Shop Salesperson[3] = {Shop(5, 23.5), Shop(12, 24.56), Shop(100, 21.5)};
	Shop::Average();
	Shop::Display();
	return 0;
} 

实验三

#include<iostream>
using namespace std;
class Complex{
	public:
		Complex(){
			real = 0;
			imag = 0;
		}
		Complex(double, double);
		Complex operator +(Complex);
		Complex operator +(double);
		friend Complex operator +(double, Complex);
		void display();
	private :
		double real;
		double imag;
};
Complex::Complex(double r, double i){
	 real = r;
	 imag = i;
}
Complex Complex::operator +(Complex b){
	Complex c;
	c.real = real + b.real;
	c.imag = imag + b.imag;
	return c; 
}
Complex Complex::operator +(double b){
	Complex c;
	c.real = real + b;
	c.imag = imag;
	return c; 
}
Complex operator +(double a, Complex b){
	Complex c;
	c.real = b.real + a;
	c.imag = b.imag;
	return c; 
}
void Complex::display(){
	cout << "(" << real << "+" << imag << ")" << endl;
}
int main(){
	Complex c,d,e;
	Complex a(11,2),b(3,5);
	cout << "a = "; 
	a.display();
	cout << "b = ";
	b.display();
	
	c = a + b;
	cout << "a + b = " ;
	c.display();

	d = a + 10;
	cout << "a + 10 = ";
	d.display();
	
	e = 11 + b;
	cout << "11 + b = ";
	e.display();
	return 0;

} 

猜你喜欢

转载自blog.csdn.net/Harington/article/details/86479672