C++实验(中)

实验四

#include <iostream>
using namespace std;
class Date
{
	private:
		int year;
		int mouth;
		int day;
	public:
		
		friend istream &operator >>(istream &, Date &);
		friend ostream &operator <<(ostream &, Date &);
		
		
		Date operator+(int a);
		friend Date operator+(int a, Date b);
		Date operator+(Date b);
		
		Date operator++();
		Date operator++(int);
		
};
Date Date::operator+(int a)
{
	day += a;
	if(day > 30)
	{
		int temp;
		temp = day/30;
		day %= 30;
		mouth += temp;
		if(mouth > 12)
		{
			temp = mouth/12;
			mouth %= 12;
			year += temp;
		}	
	}
	return *this;
}

Date Date::operator+(Date b)
{
	Date c;
	c.day = day + b.day;
	c.mouth = mouth + b.mouth;
	c.year = year + b.year;
	if(c.day > 30)
	{
		int temp;
		temp = c.day/30;
		c.day %= 30;
		c.mouth += temp;
		if(c.mouth > 12)
		{
			temp = c.mouth/12;
			c.mouth %= 12;
			c.year += temp;
		}	
	}
	return c;
} 

Date operator+(int a, Date b)
{
	b.day += a;
	if(b.day > 30)
	{
		int temp;
		temp = b.day/30;
		b.day %= 30;
		b.mouth += temp;
		if(b.mouth > 12)
		{
			temp = b.mouth/12;
			b.mouth %= 12;
			b.year += temp;
		}	
	}
	return b;
	
}

istream &operator >>(istream &input, Date &c)
{
	cout << "请输入 年 月 日 中间用空格隔开" << endl;
	input >> c.year >> c.mouth >> c.day;
	if( c.mouth < 1 || c.mouth > 12)
	{
		cout << "输入 月份 错误,请重新输入" << endl;
		 input >> c.year >> c.mouth >> c.day;
	} 
	if(c.day < 1 || c.day >30)
	{
		cout << "输入 日期 错误,请重新输入" << endl;
		input >> c.year >> c.mouth >> c.day;
	} 
	return input; 
}
ostream& operator << (ostream &output, Date &tempdata)
{
	output << tempdata.year << "年 " << tempdata.mouth << "月 " << tempdata.day << "日" << endl;
	return output;
}

Date Date::operator++()
{
	if(++day > 30)
	{
		day = 1;
		++mouth;
		if(mouth >= 12)
		{
			mouth = 1;
			++year;
		}
	}
	return *this;
}

Date Date::operator++(int)
{
	
	Date temp = *this;
	if(++day > 30)
	{
		day = 1;
		++mouth;
		if(mouth > 12)
		{
			mouth = 1;
			++year;
		}
	}
	return temp;
	
}

int main(int argc, char** argv) {
	Date today, nextday;
	cin >> today; 
	cout << today ;
	cout << "请输入你要加的日期" << endl;
	int number;
	cin >>  number;
	nextday = number + today;
	//today = today + nextday;
	cout << "day + " << number << " = " << nextday ;
	nextday++;
	cout << "day++ = " << nextday << endl;
	//cout << today;
	return 0;
}

 

#include<iostream>
using namespace std;
class CFraction
{
	public:
		friend istream &operator >> (istream &, CFraction &);
		friend ostream &operator << (ostream &, CFraction &);
	private:
		int nume;
		int deno;	
};
istream &operator >>(istream &input, CFraction &c)
{
	cout << "请输入分子和分母:" << endl;
	input >> c.nume >> c.deno;
	if(c.deno == 0)
	{
		cout << "分母不能为0,请重新输入。" << endl;
		input >> c.nume >> c.deno;
	}
	return input;
}
ostream &operator << (ostream &output, CFraction &c)
{
	output << c.nume << "/" << c.deno;
	return output;
}
int main()
{
	CFraction grade;
	cin >> grade;
	cout << grade;
	return 0;
}

实验五

#include<iostream>
#include<cstring>
using namespace std;
class Student{
	public:
		void ReadData();
		void OutputData();
	protected:
		string num;
		string name;
		string sex;
};
void Student::ReadData()
{
	cout << "请输入学号, 姓名,性别:"; 
	cin >> num >> name >> sex;
}
void Student::OutputData(){
	cout << "num = " << num << endl;
	cout << "name = " << name << endl;
	cout << "sex = " << sex << endl; 
}
class Graduate:public Student{
	public :
		void Set_Research();
		void ResearchWork();
	private : 
	string R_Interest;
};
void Graduate::Set_Research()
{
	cout << "请输入研究方向:" ;
	cin >>  R_Interest;
}
void Graduate::ResearchWork()
{
	cout << "num = " << num << endl;
	cout << "R_Interest = " << R_Interest << endl; 
	
}
int main(){
	Graduate grad;
	grad.ReadData();
	grad.OutputData();
	grad.Set_Research();
	grad.ResearchWork();
	return 0;
} 

实验六

 

#include<iostream>
#include<math.h>
using namespace std;
const double PI = 3.14157;
class Point
{
	public:
		Point(double, double);
		~Point();
	protected :
		double x;
		double y;
};

class Circle : public Point
{
	public:
		Circle(double, double, double);
		~Circle();
	protected :
		double radious;
};
Point::Point(double a, double b){
	x = a;
	y = b;
}
Point::~Point(){
	cout << "Point的析构函数~" << endl;
}

Circle::Circle(double a, double b, double r):Point(a, b)
{
	radious = r;
}
Circle:: ~Circle (){
	cout << "Circle的析构函数~" << endl;
}
class Cylinder : public Circle{
	public:
		Cylinder(double, double, double, double);
		~Cylinder();
		void Volume();
	private :
		double height;
};
Cylinder:: Cylinder(double a, double b, double r, double h)
:Circle(a, b, r), height(h){}

void Cylinder::Volume(){
		double volume = pow( radious , 2.0) * PI * height;
		cout << "圆柱体的体积为: " << volume << endl;
}
Cylinder::~Cylinder(){
	cout << "Cylinder的析构函数~" << endl;
}
int main (){
	Cylinder cy11(1, 1, 10, 100);
	cy11.Volume();
	return 0;
	return 0;
} 

#include<iostream>
#include<cstring>
#include<string.h>
using namespace std;
class Teacher{
	public:
		Teacher(string, int, string, string, string, string);
		~Teacher(){}
		void display();
	protected:
		string name;
		int age;
		string sex;
		string address;
		string phonenumber;
		string title;
};
Teacher::Teacher(string nam, int a, string s, string add, string phone, string t){
	name = nam;
	age = a;
	sex = s;
	address = add;
	phonenumber = phone;
	title = t;
}
void Teacher::display(){
	cout << "name : " << name << endl;
	cout << "age : " << age << endl;
	cout << "sex : " << sex << endl;
	cout << "title : " << title << endl;
	cout << "address : " << address << endl;
	cout << "phonenumber : " << phonenumber << endl;  
}
class Cadre{
	public:
		Cadre(string nam, int a, string s, string add, string ph, string pos) ;
		~Cadre(){}
	protected:
		string name;
		int age;
		string sex;
		string address;
		string phonenumber;
		string post;
};
Cadre::Cadre(string nam, int a, string s, string add, string ph, string pos) {
	name = nam;
	age = a;
	sex = s;
	address = add;
	phonenumber = ph;
	post = pos;
}
class Teacher_Cadre : public Teacher, public Cadre
{
	public:
		Teacher_Cadre(string nam, int a, string s, string add, string ph, string ti, string pos, double wa);
		void show();
	protected:
		double wages;	
};
Teacher_Cadre::Teacher_Cadre(string nam, int a, string s, string add, string ph, string ti, string pos, double wa) 
: Teacher(nam, a, s, add, ph, ti),Cadre(nam, a, s, add, ph, pos)
{
	wages = wa;
}
void Teacher_Cadre::show()
{
	display();
	cout << "post : " << post << endl;
	cout << "wages : " << wages << endl;
}
int main(){
	Teacher_Cadre tc1("MR.甘", 28, "男", "河南大学金明校区", "123456789", "优秀老师", "软件教师", 8000.0);
	tc1.show();
	return 0;
	return 0;
} 

猜你喜欢

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