题目一:
//JNU Orion 233
//created by Wang Yuchen @ 2018/03/14
//第五次试实验 人员类Staff
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
class Date{
public:
Date(int y = 0, int m = 0, int d = 0);
Date(Date &d);
void setDate(int y, int m, int d);
void showDate();
~Date(){};
private:
int year;
int month;
int day;
};
class Staff{
public:
Staff(string newName, int newAge, Date newBirthday);
void setStaff(string newName, int newAge, Date newBirthday);
void showStaff();
~Staff(){};
private:
string name;
int age;
Date birthday;
};
Date::Date(int y,int m,int d){//Date构造函数
year = y;
month = m;
day = d;
}
Date::Date(Date &d){//Date复制构造函数
year = d.year;
month = d.month;
day = d.day;
cout<< "[sys] Calling the Copy Constructor of Staff ..." << endl;
}
void Date::setDate(int y,int m,int d){//Date设置日期
year = y;
month = m;
day = d;
}
void Date::showDate(){//Date显示日期
cout<< year << "" << month << "" << day <<endl;
}
Staff::Staff(string newName,int newAge,Date newBirthday){
name = newName;
age = newAge;
birthday = newBirthday;
}
void Staff::setStaff(string newName,int newAge,Date newBirthday){
name = newName;
age = newAge;
birthday = newBirthday;
}
void Staff::showStaff(){
cout<< "[sys] Staff Info: "<<endl;
cout<< " Name:" << name <<" Age:" << age <<" Birthday:";
birthday.showDate();
}
int main()
{
string name;
int age;
int y,m,d;//年 月 日
char choice;
while(true){
cout<< "[sys] Input Staff's Name: ";
cin>> name;
cout<< "[sys] Input Staff's Age: ";
cin>>age;
cout<< "[sys] Input Staff's Birthday(xxxx-xx-xx): ";
cin>>y>>m>>d;
cout<< endl;
Date birthday(y,m,d);
Staff staff(name,age,birthday);
staff.showStaff();
cout<< "[sys] continue ?(Y/N)" ;
cin>> choice;
if(choice == 'N')
break;
}
return 0;
}
题目二:
。。。。
代码如下:
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
class Complex{
public:
Complex(float na ,float nb);
Complex(float na);
//Complex(Complex &c)
void addComplex(Complex &c2);
void showComplex();
~Complex(){};
private:
float a,b;
};
Complex::Complex(float na,float nb){
a = na;
b = nb;
}
Complex::Complex(float na){
a = na;
}
/*
Complex::Complex(Complex &c2){
a = c2.a;
b = c2.b;
}*/
void Complex::addComplex(Complex &c2){
a += c2.a;
b += c2.b;
}
void Complex::showComplex(){
if(b == 0){
cout<< a <<endl;
}
else {
if(a == 0)
cout<< b << "i" <<endl;
else
cout<< a << "+" << b << "i" <<endl;
}
}
int main(){
Complex c1(3,5);
Complex c2(4.5);
c1.addComplex(c2);
c1.showComplex();
return 0;
}