习题1
#include<iostream>
using namespace std;
struct student
{
int num;
char name[50];
int english;
int math;
int chinese;
};
void print(student &stu)//改成void print(student stu)也行
{
cout<<"学号"<<stu.num<<endl;
cout<<"名字"<<stu.name<<endl;
cout<<"英语"<<stu.english<<endl;
cout<<"数学"<<stu.math<<endl;
cout<<"语文"<<stu.chinese<<endl;
}
int main()
{
student stu;
cout<<"输入学生信息:"<<endl<<"学号 姓名 英语 数学 语文"<<endl;
cin>>stu.num>>stu.name>>stu.math>>stu.english>>stu.chinese;
print(stu);//
return 0;
}
习题2
#include<iostream>
using namespace std;
struct student
{
int num;
char name[50];
int english;
int math;
int chinese;
};
void print(student* p)
{
cout<<"学号"<<p->num<<endl;
cout<<"名字"<<p->name<<endl;
cout<<"英语"<<p->english<<endl;
cout<<"数学"<<p->math<<endl;
cout<<"语文"<<p->chinese<<endl;
}
int main()
{
student stu,*p=&stu;
cout<<"输入学生信息:"<<endl<<"学号 姓名 英语 数学 语文"<<endl;
cin>>p->num>>p->name>>p->math>>p->english>>p->chinese;
print(p);
return 0;
}
习题2 版本2
#include<iostream>
using namespace std;
struct stu
{
int english;
int math;
int chinese;
};
struct student
{
int num;
char name[50];
stu chenji;
};
void print(student* p)
{
cout<<"学号"<<p->num<<endl;
cout<<"名字"<<p->name<<endl;
cout<<"英语"<<p->chenji.english<<endl;
cout<<"数学"<<p->chenji.math<<endl;
cout<<"语文"<<p->chenji.chinese<<endl;
}
int main()
{
student stu,*p=&stu;
cout<<"输入学生信息:"<<endl<<"学号 姓名 英语 数学 语文"<<endl;
cin>>p->num>>p->name>>p->chenji.math>>p->chenji.english>>p->chenji.chinese;
print(p);
return 0;
}
习题3
#include<iostream>
using namespace std;
union asc
{
int i;
char ch[2];
};
int main()
{
cout<<"输入一个整数,大于127则退出"<<endl;
asc password;
do
{
cin>>password.i;
cout<<password.i<<"对应字符为:"<<password.ch<<endl;
}while(password.i<128);
return 0;
}
习题4
#include<iostream>
using namespace std;
struct pay
{
int basepay;
int workpay;
};
struct worker
{
int num;
char name[20];
pay all;
};
int main()
{
int max=0;
int j;
worker wor[10]=
{
{1000,"ming",{9000,1200}},
{1001,"hao",{20000,10000}},
{1002,"toph",{80000,8000}},
{1003,"anhy",{6000,15000}},
{1004,"ruby",{50000,20000}},
{1005,"yang",{60000,20000}},
{1006,"yadou",{6500,4700}},
{1007,"xingking",{5200,1300}},
{1008,"hong",{25000,1000}},
{1009,"goudang",{20000,1800}}
};
int minpay=wor[0].all.basepay+wor[0].all.workpay;;
for(int i=1;i<10;i++)
{
if(minpay>wor[i].all.basepay+wor[i].all.workpay)
{
minpay=wor[i].all.basepay+wor[i].all.workpay;
j=i-1;
}
}
cout<<"总工资最小的职工的信息为:"<<endl
<<"编号: "<<wor[j].num<<endl
<<"姓名: "<<wor[j].name<<endl
<<"基本工资:"<<wor[j].all.basepay<<endl
<<"职务工资:"<<wor[j].all.workpay<<endl;
return 0;
}
习题5
#include<iostream>
using namespace std;
struct time
{
int year;
int month;
int day;
};
int main()
{
int i,daynum=0;
time date;
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
cout<<"输入。年。月。日。:"<<endl;
cin>>date.year>>date.month>>date.day;
if(date.year%4==0) //改润年
days[1]=29;
for(i=1;i<date.month;i++)
daynum+=days[i];
daynum+=date.day;
cout<<"该日是一年中的第"<<daynum<<"天"<<endl;
return 0;
}