谢谢
[此贴子已经被作者于2006-7-13 19:24:10编辑过]
第四道
#include<iostream>
#include<string>
using namespace std;
class Data
{
private:
int day;
int month;
int year;
public:
Data()
{ day=0;
month=0;
year=0;
}
void get_data(int d,int m,int y=2006)
{ if ((d<=30 && d>=0) && (m<=12 && m>=0))
{ day=d;
month=m;
year=y;
}
}
void out_data()
{ cout << "日期为:" << year << "年" << month << "月" << day << "日" <<endl ;
}
};
int main()
{ Data data;
data.get_data(7,7);
data.out_data();
return 0;
}
第5个题目(可以输入中文):
#include<iostream>
#include<string>
using namespace std;
class Person
{
private:
int age;
string name;
int sex; //0表示男,1表示女,输入时为也为整数0/1
public:
Person()
{ age=0;
name=" ";
sex=0; //初始为男
}
void get_person(int a, string n,int s=0)
{ age=a;
name=n;
sex=s;
}
void out_person()
{ cout << "年龄:" << age <<endl;
cout << "姓名:" << name << endl;
if (sex==0)
cout << "性别:男" <<endl;
if (sex==1)
cout << "性别:女" <<endl;
}
};
class Student : public Person
{ private:
unsigned long num;
char *pmajor;
char *pschool;
public:
Student():Person()
{ num=0;
pmajor=NULL;
pschool=NULL;
}
void get_student(unsigned long nu, char *pm,char *ps)
{
num=nu;
pmajor=pm;
pschool=ps;
}
void out_student()
{ out_person();
cout << "学号:" << num <<endl;
cout << "专业:" << pmajor <<endl;
cout << "学院:" << pschool <<endl;
}
};
int main()
{ Student a_student;
int a_age;
string a_name;
int a_sex;
unsigned long a_number;
char a_pmajor[100];
char a_pschool[100];
cout << "请输入姓名:" << endl;
getline(cin,a_name);
cout << "请输入年龄:" << endl;
cin >> a_age;
cout << "请输入性别:" << endl;
cin >> a_sex;
cout << "请输入学号:" << endl;
cin >> a_number;
cout << "请输入专业:" << endl;
cin >> a_pmajor;
cout << "请输入所属学院:" << endl;
cin >> a_pschool;
a_student.get_person(a_age,a_name);
a_student.get_student(a_number,a_pmajor,a_pschool);
a_student.out_student();
return 0;
}
#include<iostream.h>
main()
{
int a,b,c,k;
int sum=0;
cout<<"输入小于5位的数:";
cin>>a;
k=1000;
if(a>=10000||a<0)
cout<<"the error number"<<endl;
else if (a<10000)
{
c=a/k;
b=a%k;
k/=10;
sum+=c;
a=b;
c=a/k;
b=a%k;
k/=10;
sum+=c;
a=b;
c=a/k;
b=a%k;
k/=10;
sum+=c;
a=b;
c=a/k;
b=a%k;
k/=10;
sum+=c;
a=b;
}
cout<<"和为:"<<sum<<endl;
}