【解】
#include
using namespace std;
struct
{
int year; //结构体变量date中的成员对应于输入的年、月、日
int month;
int day;
}date;
int main()
{
int days; //days为天数
cout << "input year,month,day:";
cin >> date.year >> date.month >> date.day;
switch (date.month)
{
case 1: days = date.day; break;
case 2: days = date.day + 31; break;
case 3: days = date.day + 59; break;
case 4: days = date.day + 90; break;
case 5: days = date.day + 120; break;
case 6: days = date.day + 151; break;
case 7: days = date.day + 181; break;
case 8: days = date.day + 212; break;
case 9: days = date.day + 243; break;
case 10: days = date.day + 273; break;
case 11: days = date.day + 304; break;
case 12: days = date.day + 334; break;
}
if (((date.year % 4 == 0 && date.year % 100 != 0) || date.year % 400 == 0) && date.month >= 3)
days += 1;
cout << date.month << "/" << date.day << " is the " << days << "th day in " << date.year << "." << endl;
return 0;
}
//(2)程序二
#include
using namespace std;
struct
{
int year;
int month;
int day;
}date;
int main()
{
int i, days;
int day_tab[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
cout << "input year,month,day:";
cin >> date.year >> date.month >> date.day;
days = 0;
for (i = 1; i < date.month; i++)
days += day_tab[i];
days += date.day;
if ((date.year % 4 == 0 && date.year % 100 != 0 || date.year % 400 == 0) && date.month >= 3)
days += 1;
cout << date.month << "/" << date.day << " is the " << days << "th day in " << date.year << "." << endl;
return 0;
}
运行结果:
//① input year, month, day:2005 10 1↙
//10 / 1 is the 274th day in 2005.
//② input year, month, day : 2008 10 1↙
//10 / 1 is the 275th day in 2008
【解】
#include
using namespace std;
struct y_m_d
{
int year;
int month;
int day;
};
int main()
{
y_m_d date;
int days(int, int, int); //对days函数的声明
int day_sum;
cout << "input year,month,day:";
cin >> date.year >> date.month >> date.day;
day_sum = days(date.year, date.month, date.day);
cout << date.month << "/" << date.day << " is the " << day_sum << "th day in " << date.year << endl;
return 0;
}
int days(int year, int month, int day) //定义days函数
{
int day_sum, i;
int day_tab[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
day_sum = 0;
for (i = 1; i < month; i++)
day_sum += day_tab[i];
day_sum += day;
if ((year % 4 == 0 && year % 100 != 0 || year % 4 == 0) && month >= 3)
day_sum += 1;
return(day_sum);
}
运行结果:
//input year, month, day:2005, 7, 1↙
//10 / 1 is the 182th day in 2005.
【解】
#include
#include
using namespace std;
const int n = 5;
struct student
{
char num[6];
char name[8];
int score[4];
}stu[n];
int main()
{
void print(student stu[]);
int i, j;
for (i = 0; i < n; i++)
{
cout << "input scores of student " << i + 1 << ":" << endl;
cout << "NO.: ";
cin >> stu[i].num;
cout << "name: ";
cin >> stu[i].name;
for (j = 0; j < 3; j++)
{
cout << "score " << j + 1 << ":";
cin >> stu[i].score[j];
}
cout << endl;
}
print(stu);
return 0;
}
void print(student stu[])
{
int i, j;
cout << " NO. name score1 score2 score3" << endl;
for (i = 0; i < n; i++)
{
cout << stu[i].num << " " << setw(10) << stu[i].name << " ";
for (j = 0; j < 3; j++)
cout << setw(3) << stu[i].score[j] << " ";
cout << endl;
}
}
运行结果:
//input scores of student 1:
//NO. : 101↙
//NAME : Li↙
//score 1 : 90↙
//score 2 : 79↙
//score 3 : 89↙
//
//input scores of student 2 :
//NO. : 102↙
//NAME : Ma↙
//score 1 : 97↙
//score 2 : 90↙
//score 3 : 68↙
//
//input scores of student 3 :
//NO. : 103↙
//NAME : Wang↙
//score 1 : 77↙
//score 2 : 70↙
//score 3 : 78↙
//
//input scores of student 4 :
//NO. : 104↙
//NAME : Fun↙
//score 1 : 67↙
//score 2 : 89↙
//score 3 : 56↙
//
//input scores of student 5 :
//NO. : 105↙
//NAME : Xue↙
//score 1 : 87↙
//score 2 : 65↙
//score 3 : 69↙
//
//NO.name score1 score2 score3
//101 Li 90 79 89
//102 Ma 97 90 68
//103 Wang 77 70 78
//104 Fun 67 89 56
//105 Xue 87 65 69
【解】
#include
#include
using namespace std;
const int n = 5;
struct student
{
char num[6];
char name[8];
int score[4];
}stu[n];
int main()
{
void input(student stu[]);
void print(student stu[]);
input(stu);
print(stu);
return 0;
}
void input(student stu[])
{
int i, j;
for (i = 0; i < n; i++)
{
cout << "input scores of student " << i + 1 << ":" << endl;
cout << "NO.: ";
cin >> stu[i].num;
cout << "name: ";
cin >> stu[i].name;
for (j = 0; j < 3; j++)
{
cout << "score " << j + 1 << ":";
cin >> stu[i].score[j];
}
}
}
void print(student stu[])
{
int i, j;
cout << " NO. name score1 score2 score3" << endl;
for (i = 0; i < n; i++)
{
cout << stu[i].num << " " << setw(10) << stu[i].name << " ";
for (j = 0; j < 3; j++)
cout << setw(3) << stu[i].score[j] << " ";
cout << endl;
}
}
【解】
#include
#include
using namespace std;
const int n = 3;
struct student
{
char num[6];
char name[8];
int score[4];
float avr;
} stu[n];
int main()
{
int i, j, max, maxi, sum;
float average;
for (i = 0; i < n; i++)
{
cout << "input scores of student " << i + 1 << endl;
cout << "NO.:";
cin >> stu[i].num;
cout << "name:";
cin >> stu[i].name;
for (j = 0; j < 3; j++)
{
cout << "score " << j + 1 << ":";
cin >> stu[i].score[j];
}
cout << endl;
}
average = 0;
max = 0;
maxi = 0;
for (i = 0; i < n; i++)
{
sum = 0;
for (j = 0; j < 3; j++)
sum += stu[i].score[j];
stu[i].avr = sum / 3.0;
average += stu[i].avr;
if (sum > max)
{
max = sum;
maxi = i;
}
}
average /= n;
cout << " NO. name score1 score2 score3 average" << endl;
for (i = 0; i < n; i++)
{
cout << setw(8) << stu[i].num << " " << setw(10) << stu[i].name << " ";
for (j = 0; j < 3; j++)
cout << setw(3) << stu[i].score[j] << " ";
cout << stu[i].avr << endl;
}
cout << "average=" << average << endl;
cout << "The highest score is :" << stu[maxi].name << ", score total:" << max << endl;
return 0;
}
运行结果:
//input scores of student 1:
//NO. : 101↙
//name : Wang↙
//score1 : 93↙
//score2 : 89↙
//score3 : 87↙
//
//input scores of student 2 :
//NO. : 102↙
//name : Li↙
//score1 : 85↙
//score2 : 80↙
//score3 : 78↙
//
//input scores of student 3 :
//NO. : 103↙
//name : Zhao↙
//score1 : 65↙
//score2 : 70↙
//score3 : 59↙
//
//input scores of student 4 :
//NO. : 104↙
//name : Ma↙
//score1 : 77↙
//score2 : 70↙
//score3 : 83↙
//
//input scores of student 5 :
//NO. : 105↙
//name : Han↙
//score1 : 70↙
//score2 : 67↙
//score3 : 60↙
//
//input scores of student 6 :
//NO. : 106↙
//name : Zhang↙
//score1 : 99↙
//score2 : 97↙
//score3 : 95↙
//
//input scores of student 7 :
//NO. : 107↙
//name : Zhou↙
//score1 : 88↙
//score2 : 89↙
//score3 : 88↙
//
//input scores of student 8 :
//NO. : 108↙
//name : Chen↙
//score1 : 87↙
//score2 : 88↙
//score3 : 85↙
//
//input scores of student 9 :
//NO. : 109↙
//name : Yang↙
//score1 : 72↙
//score2 : 70↙
//score3 : 69↙
//
//input scores of student 10 :
//NO. : 110↙
//name : Liu↙
//score1 : 78↙
//score2 : 80↙
//score3 : 83↙
//
//NO.Name score1 score2 score3 average
//101 Wang 93 89 87 89.67
//102 Li 85 80 78 81.00
//103 Zhao 65 70 59 64.67
//104 Ma 77 70 83 76.67
//105 Han 70 67 60 65.67
//106 Zhang 99 97 95 97.00
//107 Zhou 88 89 88 88.33
//108 Chen 87 88 85 86.67
//109 Yang 72 70 69 70.33
//110 Liu 78 80 83 80.33
//average = 80.03
//The highest score is : Zhang, score total : 291.
【解】
#include
using namespace std;
#define NULL 0
struct student
{long num;
float score;
struct student *next;
};
int main()
{student a,b,c,*head,*p;
a.num=10001; a.score=89.5;
b.num=10003; b.score=90;
c.num=10007; c.score=85; //对结点的num和score成员赋值
head=&a; //将结点a的起始地址赋给头指针head
a.next=&b; //将结点b的起始地址赋给a结点的next成员
b.next=&c; //将结点c的起始地址赋给b结点的next成员
c.next=NULL; //c结点的next成员不存放其他结点地址
p=head; //使p指针指向a结点
do
{cout<num<<" "<score<next; //使p指向下一结点
}while(p!=NULL); //输出完c结点后p的值为NULL
return 0;
}
#include
using namespace std;
#define NULL 0
struct student
{long num;
float score;
student *next;
};
//n为全局变量,本文件模块中各函数均可使用它
student *creat(void) //定义函数。此函数带回一个指向链表头的指针
{student *head;
student *p1,*p2;
int n=0;
p1=p2=new student; //开辟一个新单元,并使p1,p2指向它
cin>>p1->num>>p1->score;
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=new student;
cin>>p1->num>>p1->score;
}
p2->next=NULL;
return(head);
}
【解】
#include
using namespace std;
#define NULL 0
struct student
{
long num;
float score;
student *next;
};
int n;
student *del(student *head, long num)
{
student *p1, *p2;
if (head == NULL) //是空表
{
cout << "list null!" << endl; return(head);
}
p1 = head; //使p1指向第1个结点
while (num != p1->num && p1->next != NULL) //p1指向的不是所要找的结点且后面还有结点
{
p2 = p1; p1 = p1->next;
} //p1后移一个结点
if (num == p1->num) //找到了
{
if (p1 == head) head = p1->next; //若p1指向的是首结点,把第2个结点地址赋予head
else p2->next = p1->next; //否则将下一结点地址赋给前一结点地址
cout << "delete:" << num << endl;
n = n + 1;
}
else cout << "cannot find " << num; //找不到该结点
return(head);
}
【解】
#include
using namespace std;
#define NULL 0
struct student
{
long num;
float score;
student *next;
};
int n;
student *del(student *head, long num)
{
student *p1, *p2;
if (head == NULL) //是空表
{
cout << "list null!" << endl; return(head);
}
p1 = head; //使p1指向第一个结点
while (num != p1-> num && p1-> next != NULL) //p1指向的不是所要找的结点且后面还有结点
{
p2 = p1; p1 = p1-> next;
} //p1后移一个结点
if (num == p1-> num) //找到了
{
if (p1 == head) head = p1-> next; //若p1指向的是首结点,把第二个结点地址赋予head
else p2-> next = p1-> next; //否则将下一结点地址赋给前一结点地址
cout << "delete:" << num << endl;
n = n - 1;
}
else cout << "cannot find " << num; //找不到该结点
return(head);
}
运行结果:
【解】
student *insert(student *head, student *stud)
{
student *p0, *p1, *p2;
p1 = head; //使p1指向第1个结点
p0 = stud; //指向要插入的结点
if (head == NULL) //原来的链表是空表
{
head = p0; p0->next = NULL;
} //使p0指向的结点作为头结点
else
{
while ((p0->num > p1->num) && (p1->next != NULL))
{
p2 = p1; //使p2指向刚才p1指向的结点
p1 = p1->next;
} //p1后移1个结点
if (p0->num <= p1->num)
{
if (head == p1) head = p0; //插到原来第1个结点之前
else p2->next = p0; //插到p2指向的结点之后
p0->next = p1;
}
else
{
p1->next = p0; p0->next = NULL;
} //插到最后的结点之后
}
n = n + 1; //结点数加1
return (head);
}
运行结果:
【解】
#include
using namespace std;
#define NULL 0
struct student
{
long num;
float score;
student *next;
};
int n;
int main()
{
student *creat(void);
student *del(student *, long);
student *insert(student *, student *);
void print(student *);
student *head, stu;
long del_num;
cout << "input records:" << endl;
head = creat(); //返回头指针
print(head); //输出全部结点
cout << endl << "input the deleted number:";
cin >> del_num; //输入要删除的学号
head = del(head, del_num); //删除后链表的头地址
print(head); //输出全部结点
cout << endl << "input the inserted record:"; //输入要插入的结点
cin >> stu.num >> stu.score;
head = insert(head, &stu); //返回地址
print(head); //输出全部结点
return 0;
}
//input records: (建立链表)
//10101 90↙
//10103 98↙
//10105 87↙
//0 0↙
//
//Now,These 3 records are:
//10101 90
//10103 98
//10105 87
//
//input the deleted number : 10103↙ (删除学号为10103的点)
//delete : 10003
//10101 90
//10105 87
//
//input the inserted record : 10102 95↙ (插入一个结点)
//
//Now, These 3 records are :
//10101 90
//10102 95
//10105 87
//
//input the inserted record : 10104 76↙ (再插入一个结点)
//Now, These 4 records are :
//10101 90
//10104 76
//10104 76
//10104 76
//修改后的整个程序如下:
#include
using namespace std;
#define NULL 0
struct student
{
long num;
float score;
student*next;
};
int n;
int main()
{
student *creat(void);
student *del(student *, long);
student *insert(student *, student *);
void print(student *);
student *head, *stu;
long del_num;
cout << "input records:" << endl;
head = creat(); //返回头指针
print(head); //输出全部结点
cout << endl << "input the deleted number:"; //请用户输入要删除的结点
cin >> del_num; //输入要删除的学号
while (del_num != 0) //当输入的学号为0时结束循环
{
head = del(head, del_num); //删除结点后的链表的头地址
print(head); //输出全部结点
cout << "input the deleted number:"; //请用户输入要删除的结点
cin >> del_num; //输入要删除的学号
}
cout << endl << "input the inserted record:"; //输入要插入的结点
stu = new student; //开辟一个新结点
cin >> stu->num >> stu->score;
while (stu->num != 0) //当输入的学号为0时结束循环
{
head = insert(head, stu); //返回链表的头地址, 赋给head
print(head); //输出全部结点
cout << endl << "input the inserted record:"; //请用户输入要插入的结点
stu = new student; //开辟一个新结点
cin >> stu->num >> stu->score; //输入插入结点的数据
}
return 0;
}
student *creat(void) //建立链表的函数
{
student *head;
student *p1, *p2;
n = 0;
p1 = p2 = new student; //开辟一个新单元, 并使p1,p2指向它
cin >> p1->num >> p1->score;
head = NULL;
while (p1->num != 0)
{
n = n + 1;
if (n == 1) head = p1;
else p2->next = p1;
p2 = p1;
p1 = new student;
cin >> p1->num >> p1->score;
}
p2->next = NULL;
return(head);
}
student *del(student *head, long num) //删除结点的函数
{
student *p1, *p2;
if (head == NULL) //是空表
{
cout << "list null!" << endl; return(head);
}
p1 = head; //使p1指向第一个结点
while (num != p1->num && p1->next != NULL) //p1指向的不是所要找的结点且后面还有结点
{
p2 = p1; p1 = p1->next;
} //p1后移一个结点
if (num == p1->num) //找到了
{
if (p1 == head) head = p1->next; //若p1指向的是首结点, 把第二个结点地址赋予head
else p2->next = p1->next; //否则将下一结点地址赋给前一结点地址
cout << "delete:" << num << endl;
n = n - 1;
}
else cout << "cannot find " << num; //找不到该结点
return(head);
}
student *insert(student *head, student *stud) //插入结点的函数
{
student *p0, *p1, *p2;
p1 = head; //使p1指向第一个结点
p0 = stud; //指向要插入的结点
if (head == NULL) //原来的链表是空表
{
head = p0; p0->next = NULL;
} //使p0指向的结点作为头结点
else
{
while ((p0->num > p1->num) && (p1->next != NULL))
{
p2 = p1; //使p2指向刚才p1指向的结点
p1 = p1->next;
} //p1后移一个结点
if (p0->num <= p1->num)
{
if (head == p1) head = p0; //插到原来第一个结点之前
else p2->next = p0; //插到p2指向的结点之后
p0->next = p1;
}
else
{
p1->next = p0; p0->next = NULL;
} //插到最后的结点之后
}
n = n + 1; //结点数加1
return(head);
}
void print(student *head) //输出链表的函数
{
student *p;
cout << "Now,These " << n << " records are:" << endl;
p = head;
if (head != NULL)
do
{
cout << p->num << " " << p->score << endl;
p = p->next;
} while (p != NULL);
}
//input records: (建立链表)
//10101 90↙
//10103 98↙
//10105 87↙
//0 0↙
//
//Now, These 3 records are :
//10101 90
//10103 98
//10105 87
//
//input the deleted number : 10103↙ (删除学号为10103的点)
//delete : 10103
//10101 90
//10105 87
//input the deleted number : 0↙ (输入的学号为0, 结束删除操作)
//
//input the inserted record : 10102 95↙ (插入一个结点)
//
//Now, These 3 records are :
//10101 90
//10102 95
//10105 87
//
//input the inserted record : 10104 76↙ (再插入一个结点)
//Now, These 4 records are:
//10101 90
//10102 95
//10104 76
//10105 87
//input the inserted record : 0 0↙ (输入的学号为0, 结束插入操作)
【解】
#include
using namespace std;
int main()
{
enum weekday { sun, mon, tue, wed, thu, fri, sat }; //声明枚举类型weekday
enum weekday day; //定义枚举变量day
int a, b, c, d, e, f, g, loop; //定义整型变量a,b,c,d,e,f,g,loop
char ch = 'A'; //定义字符变量c, 以便输出字符A,B,C,D,E,F,G
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,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;
//将a强制转换为weekday类型, 是A医生的值班日
case 2: day = weekday(b); break; //是B医生的值班日
case 3: day = weekday(c); break; //是C医生的值班日
case 4: day = weekday(d); break; //是D医生的值班日
case 5: day = weekday(e); break; //是E医生的值班日
case 6: day = weekday(f); break; //是F医生的值班日
case 7: day = weekday(g); //是G医生的值班日
}
switch (day) //以英文单词形式输出星期几
{
case sun: cout << "Sunday" << endl; break;
case mon: cout << "Monday" << endl; break;
case tue: cout << "Tuesday" << endl; break;
case wed: cout << "Wednesday" << endl; break;
case thu: cout << "Thursday" << endl; break;
case fri: cout << "Friday" << endl; break;
case sat: cout << "Saturday" << endl;
}
}
return 0;
}
运行结果:
//Doctor A : Saturday
//Doctor B : Monday
//Doctor C : Friday
//Doctor D : Tuesday
//Doctor E : Sunday
//Doctor F : Thursday
//Doctor G : Wednesday