7-1
#include<iostream>
using namespace std;
int leap(int y);
int days(int y,int m,int d);
struct Date{
int year;int month;int day;
};
int main(){
int n;
Date d;
cout<<"Please enter year,month,day:";
cin>>d.year>>d.month>>d.day;
cout<<"该日期是该年的第"<<days(d.year,d.month,d.day)<<"天"<<endl;
return 0;
}
int days(int y,int m,int d){
int dt[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int sum=0;
for(int i=1;i<m;i++)
sum+=dt[i];
sum+=d;
if(!leap(y)&&m>2)
sum++;
return sum;
}
int leap(int y){
if((y%100==0&&y%4==0)||y%400==0)
return 1;
return 0;
}
7-2
同上
7-3
#include<iostream>
#include<iomanip>
using namespace std;
struct Student{
int num;string name;int score[3];
};
void print(Student stu[5]);
int main(){
int n;
Student stu[5];
for(int i=0;i<5;i++){
cin>>stu[i].num>>stu[i].name;
for(int j=0;j<3;j++)
cin>>stu[i].score[j];
}
print(stu);
return 0;
}
void print(Student stu[5]){
for(int i=0;i<5;i++){
cout<<stu[i].num<<" "<<setw(10)<<stu[i].name<<" ";
for(int j=0;j<3;j++)
cout<<setw(3)<<stu[i].score[j]<<" ";
cout<<endl;
}
}
//01 Zhang 91 92 93 02 zhou 81 82 83 03 Tang 99 98 97 04 Li 77 76 78 05 Men 89 89 89
7-4
#include<iostream>
#include<iomanip>
using namespace std;
struct Student{
int num;string name;int score[3];
};
void input(Student stu[5]);
void print(Student stu[5]);
int main(){
int n;
Student stu[5];
input(stu);
print(stu);
return 0;
}
void input(Student stu[5]){
for(int i=0;i<5;i++){
cin>>stu[i].num>>stu[i].name;
for(int j=0;j<3;j++)
cin>>stu[i].score[j];
}
}
void print(Student stu[5]){
for(int i=0;i<5;i++){
cout<<stu[i].num<<" "<<setw(10)<<stu[i].name<<" ";
for(int j=0;j<3;j++)
cout<<setw(3)<<stu[i].score[j]<<" ";
cout<<endl;
}
}
//01 Zhang 91 92 93 02 zhou 81 82 83 03 Tang 99 98 97 04 Li 77 76 78 05 Men 89 89 89
7-5
#include<iostream>
#include<iomanip>
using namespace std;
struct Student{
int num;string name;int score[3];
};
void input(Student stu[5]);
void print(Student stu[5]);
int find_high(int *s);
int main(){
int n;
Student stu[5];
input(stu);
print(stu);
return 0;
}
void input(Student stu[5]){
for(int i=0;i<5;i++){
cin>>stu[i].num>>stu[i].name;
for(int j=0;j<3;j++)
cin>>stu[i].score[j];
}
}
void print(Student stu[5]){
int sum[3],k;
for(int i=0;i<5;i++)
for(int j=0;j<3;j++)
sum[i]+=stu[i].score[j];
k=find_high(sum);
cout<<"The Best student is :"<<endl;
cout<<stu[k].num<<" "<<stu[k].name<<" "<<stu[k].score[0]<<" "<<stu[k].score[1]<<" "<<stu[k].score[2]<<endl;
}
int find_high(int *s){
int max_m=s[0],k=0;
for(int i=1;i<5;i++)
if(max_m<s[i]){
max_m=s[i];k=i;
}
return k;
}
//01 Zhang 91 92 93 02 zhou 81 82 83 03 Tang 99 98 97 04 Li 77 76 78 05 Men 89 89 89
7-6~7-10
#include<iostream>
using namespace std;
struct Student{
int num;
float score;
struct Student *next;
};
Student *creat(void);
void print(Student *p);
void my_delete(Student *head,int m);
void my_insert(Student *head,Student *stu);
int main(){
Student *p,*stu;
int m;
p=creat();
print(p);
cout<<"Please enter the number you want to delete:";
cin>>m;
my_delete(p,m);
print(p);
stu=new Student;
cout<<"Please enter the inserted record:";
cin>>stu->num>>stu->score;
while(stu->num!=0){
my_insert(p,stu);
cout<<"Please enter the inserted record:";
stu=new Student;
cin>>stu->num>>stu->score;
}
print(p);
return 0;
}
Student *creat(void){
Student *head;
Student *p1,*p2;
int n=0;
p1=p2=new Student;
cout<<"Please enter :";
cin>>p1->num>>p1->score;
head=NULL;
while(p1->num!=0){
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=new Student;
cout<<"Please enter :";
cin>>p1->num>>p1->score;
}
p2->next=NULL;
return head;
}
void print(Student *p){
cout<<"Results are:"<<endl;
//cout<<p->num<<" "<<p->score<<endl;
while(p!=NULL){
cout<<p->num<<" "<<p->score<<endl;
p=p->next;
}
}
void my_delete(Student *head,int m){
Student *p1=NULL,*p2=NULL;
if(head==NULL)
cout<<"It is null."<<endl;
p1=head;p2=p1->next;
while(p1->next!=NULL&&m!=p1->num){
p2=p1;p1=p1->next;
}
if(m==p1->num){
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
cout<<"Delete:"<<m<<endl;
}
else
cout<<"Cannot find "<<m;
}
void my_insert(Student *head,Student *stu){
Student *p0,*p1,*p2;
p0=stu;p1=head;
//空链表
if(head==NULL){
head=p0;
p0->next=NULL;
}
else{
//未找到插入位置
while((p0->num>p1->num)&&(p1->next!=NULL)){
p2=p1;p1=p1->next;
}
if(p0->num<=p1->num){
if(head==p1)
head=p0;
else
p2->next=p0;
}
else{
p1->next=p0;p0->next=NULL;
}
}
}
7-11
//非自写,参考网上代码
#include<iostream>
using namespace std;
int main()
{
enum weekday{sun,mon,tus,wed,thu,fri,sat}; //声明枚举类型
enum weekday day; //定义枚举变量
int a,b,c,d,e,f,g,loop; //定义整形变量
char ch='A'; //定义字符变量
f=thu; //按照题意,F医生是星期四值班
for(a=sun;a<=sat;a++) //需要逐个检查A医生星期几符合条件
if(a!=f) //A医生值班日子不应该与F医生相同
for(b=sun;b<=sat;b++) //逐个检查B医生星期几符合条件
if((a!=b)&&(f>b)) //B医生值班日子不应该与A医生相同,且F在B之后
for(c=sun;c<=sat;c++) //逐个检查C医生星期几符合条件
if((c!=a)&&(c!=b)&&(c!=f)&&(a==c+1)&&(f<c)) //C医生值班日子不应该与A,B,F医生相同,且A比C晚一天
for(d=sun;d<=sat;d++) //逐个检查D医生星期几符合条件
if((d!=a)&&(d!=b)&&(d!=c)&&(d!=f)&&(c==d+3)) //D医生值班日子不应该与A,B,C,F医生相同,且C与D之后3天
for(e=sun;e<=sat;e++) //逐个检查E医生星期几符合条件
if((e!=a)&&(e!=b)&&(e!=c)&&(e!=d)&&(e!=f)&&(d==e+2)) //E值班不应该与A,B,C,D,E,F相同,且E与D前2天
for(g=sun;g<=sat;g++) //逐个检查G医生条件
if((g!=a)&&(g!=b)&&(g!=c)&&(g!=d)&&(g!=e)&&(g!=f)&&(g==b+2)) //G值班不应该与A,B,C,D,E,F相同,且G与B后2天
//符合以上条件才能执行以下工作
for(loop=0;loop<7;loop++)
{ cout<<"Doctor"<<char(ch+loop)<<":";
switch(loop+1)
{
case 1:day=weekday(a);break;
case 2:day=weekday(b);break;
case 3:day=weekday(c);break;
case 4:day=weekday(d);break;
case 5:day=weekday(e);break;
case 6:day=weekday(f);break;
case 7:day=weekday(g);break;
}
switch(day)
{
case sun:cout<<"Sunday"<<endl;break;
case mon:cout<<"Monday"<<endl;break;
case tus:cout<<"Thusday"<<endl;break;
case wed:cout<<"Wednesday"<<endl;break;
case thu:cout<<"Thurday"<<endl;break;
case fri:cout<<"Friday"<<endl;break;
case sat:cout<<"Saturday"<<endl;break;
}
}
return 0;
}
8-1
#include<iostream>
using namespace std;
class Time{
public:
void set_time(void);
void show_time(void);
private:
int hour;
int minute;
int sec;
};
Time t;
int main(){
t.set_time();
t.show_time();
return 0;
}
void Time::set_time(void){
cin>>t.hour;cin>>t.minute;cin>>t.sec;
}
void Time::show_time(void){
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
8-2
#include<iostream>
using namespace std;
class Time{
public:
void set_time(void){
cin>>hour;cin>>minute;cin>>sec;
}
void show_time(void){
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
private:
int hour;
int minute;
int sec;
};
Time t;
int main(){
t.set_time();
t.show_time();
return 0;
}
8-3同8-1
8-4
student.h中
class Student{
public:
void display();
void set_value();
private:
int num;
string name;
char sec;
};
Student.cpp中
#include<iostream>
using namespace std;
#include "student.h"
Student stu;
void Student::display(){
cout<<"num:"<<stu.num<<endl;
cout<<"name:"<<stu.name<<endl;
cout<<"sex:"<<stu.sec<<endl;
}
void Student::set_value(){
cin>>stu.num;
cin>>stu.name;
cin>>stu.sec;
}
main.cpp中
#include<iostream>
#include<string>
using namespace std;
#include "student.h"
int main(){
Student stud;
stud.set_value();
stud.display();
return 0;
}
8-5
arraymax.h中
class Array_max{
public:
void set_value();
void max_value();
void show_value();
private:
int array_a[10];
int max_m;
};
arraymax.cpp中
#include<iostream>
using namespace std;
#include "arraymax.h"
Array_max arrmax;
void Array_max::set_value(){
int i=0;
for(i=0;i<10;i++)
cin>>arrmax.array_a[i];
}
void Array_max::max_value(){
int i;
arrmax.max_m=arrmax.array_a[0],i;
for(i=1;i<10;i++)
if(arrmax.array_a[i]>arrmax.max_m)
arrmax.max_m=arrmax.array_a[i];
}
void Array_max::show_value(){
cout<<"max = "<<arrmax.max_m;
}
main.cpp中
#include<iostream>
#include<string>
using namespace std;
#include "arraymax.h"
int main(){
Array_max arrmax;
arrmax.set_value();
arrmax.max_value();
arrmax.show_value();
return 0;
}
//21 31 56 24 79 12 3 9 90 10
8-6
#include<iostream>
using namespace std;
class Rectangular{
public:
void set_value(){
cin>>length>>width>>height;
}
void show_value(){
cout<<"Volume is "<<length*width*height;
}
private:
int length;
int width;
int height;
};
int main(){
Rectangular rec;
rec.set_value();
rec.show_value();
return 0;
}