/这个周过后同学们应该掌握多变量类型定义的结构体了,在这基础上能应该多做练习题巩固用法,计算机专业的学生更应该把C语言学的更加的牢固,为以后的学习奠定坚实的基础,大一我们没有过多的专业课,所以自己就学了两遍,希望各位同学可以以此为参考自己的学习。/
1
计算时间差V2.0(4分)
题目内容:
用结构体定义时钟类型,编程从键盘任意输入两个时间(例如4时55分和1时25分),计算并输出这两个时间之间的间隔。要求不输出时间差的负号。结构体类型定义如下:
typedef struct clock
{
int hour;
int minute;
int second;
} CLOCK;
函数原型: CLOCK CalculateTime(CLOCK t1, CLOCK t2);
函数功能:计算并返回两个时间t1和t2之间的差
程序运行结果示例1:
Input time one:(hour,minute):4,55↙
Input time two: (hour,minute):1,25↙
3hour,30minute
程序运行结果示例2:
Input time one:(hour,minute):1,33↙
Input time two: (hour,minute):5,21↙
3hour,48minute
输入提示: “Input time one:(hour,minute):”
“Input time two: (hour,minute):”
输入格式: “%d,%d”
输出格式:"%dhour,%dminute\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include
#include
typedef struct clock
{
int hour;
int minute;
int second;
} CLOCK;
CLOCK CalculateTime(CLOCK t1, CLOCK t2)
{
int sum[2],Alltime;
sum[1]=t1.hour*60+t1.minute;
sum[2]=t2.hour*60+t2.minute;
Alltime=fabs(sum[1]-sum[2]);
printf("%dhour,%dminute\n",Alltime/60,Alltime%60);
}
int main()
{
CLOCK time[2];
printf("Input time one:(hour,minute):");
scanf("%d,%d",&time[0].hour,&time[0].minute);
printf("Input time two: (hour,minute):");
scanf("%d,%d",&time[1].hour,&time[1].minute);
CalculateTime(time[0],time[1]);
return 0;
}
2
奖学金发放(4分)
题目内容:
某校的惯例是在每学期的期末考试之后发放奖学金。发放的奖学金共有五种,每项奖学金获取的条件分别如下:
函数原型:void Addup(WIN stu[], int n);
函数原型:int FindMax(WIN student[], int n);
程序运行结果示例:
Input n:4↙
Input name:YaoMing↙
Input final score:87↙
Input class score:82↙
Class cadre or not?(Y/N):Y↙
Students from the West or not?(Y/N):N↙
Input the number of published papers:0↙
name:YaoMing,scholarship:4850
Input name:ChenRuiyi↙
Input final score:88↙
Input class score:78↙
Class cadre or not?(Y/N):N↙
Students from the West or not?(Y/N):Y↙
Input the number of published papers:1↙
name:ChenRuiyi,scholarship:9000
Input name:LiXin↙
Input final score:92↙
Input class score:88↙
Class cadre or not?(Y/N):N↙
Students from the West or not?(Y/N):N↙
Input the number of published papers:0↙
name:LiXin,scholarship:6000
Input name:ZhangQin↙
Input final score:83↙
Input class score:87↙
Class cadre or not?(Y/N):Y↙
Students from the West or not?(Y/N):N↙
Input the number of published papers:1↙
name:ZhangQin,scholarship:8850
ChenRuiyi get the highest scholarship 9000
输入学生人数提示:“Input n:”
输入学生姓名提示:“Input name:”
输入学生期末平均成绩提示:“Input final score:”
输入学生班级评议成绩提示:“Input class score:”
输入是否为学生干部提示:“Class cadre or not?(Y/N):”
输入是否为西部学生提示:“Students from the West or not?(Y/N):”
输入发表文章数量提示:“Input the number of published papers:”
输入格式:
输入学生人数:"%d"
输入学生姓名:"%s"
输入学生成绩:"%d"
输入是否为学生干部:" %c" (注意:%c前面有一个空格)
输入是否为西部学生:" %c" (注意:%c前面有一个空格)
输入发表文章数量: “%d”
输出格式:
输出学生获得的奖学金: “name:%s,scholarship:%d\n”
输出获得奖学金总数最高的学生:"%s get the highest scholarship %d\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include
int sum[10];
typedef struct winners
{
char name[20],work,west;
int finalScore,classScorepaper,scholarship;
} WIN;
void Addup(WIN stu[], int n)
{
if(stu[n].finalScore>80 && stu[n].paper>0)
{
sum[n]+=8000;
}
if(stu[n].finalScore>85 && stu[n].classScore>80)
{
sum[n]+=4000;
}
if(stu[n].finalScore>90)
{
sum[n]+=2000;
}
if(stu[n].finalScore>85 && stu[n].west=='Y')
{
sum[n]+=1000;
}
if(stu[n].finalScore>80 && stu[n].work=='Y')
{
sum[n]+=850;
}
printf( "name:%s,scholarship:%d\n",stu[n].name,sum[n]);
}
int FindMax(WIN student[], int n)
{
int i=0,max=sum[0],flag=0;
for(i=1;i<10;i++)
{
if(sum[i]>max)
{
max=sum[i];
flag=i;
}
}
printf("%s get the highest scholarship %d\n",student[flag].name,max);
}
int main()
{
int n,i,m=0;
WIN stu[10];
printf("Input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Input name:");
scanf("%s",stu[i].name);
printf("Input final score:");
scanf("%d",&stu[i].finalScore);
printf("Input class score:");
scanf("%d",&stu[i].classScore);
printf("Class cadre or not?(Y/N):");
scanf(" %c",&stu[i].work);
printf("Students from the West or not?(Y/N):");
scanf(" %c",&stu[i].west);
printf("Input the number of published papers:");
scanf("%d",&stu[i].paper);
Addup(stu,i);
}
FindMax(stu,n);
return 0;
}
3
评选最牛群主v1.0(4分)
题目内容:
现在要评选最牛群主,已知有3名最牛群主的候选人(分别是tom,jack和rose),有不超过1000人参与投票,最后要通过投票评选出一名最牛群主,从键盘输入每位参与投票的人的投票结果,即其投票的候选人的名字,请你编程统计并输出每位候选人的得票数,以及得票数最多的候选人的名字。候选人的名字中间不允许出现空格,并且必须小写。若候选人名字输入错误,则按废票处理。
程序运行结果示例1:
Input the number of electorates:8↙
Input vote 1:tom↙
Input vote 2:jack↙
Input vote 3:rose↙
Input vote 4:tom↙
Input vote 5:rose↙
Input vote 6:rose↙
Input vote 7:jack↙
Input vote 8:rose↙
Election results:
tom:2
jack:2
rose:4
rose wins
程序运行结果示例2:
Input the number of electorates:5↙
Input vote 1:tom↙
Input vote 2:mary↙
Input vote 3:rose↙
Input vote 4:jack↙
Input vote 5:tom↙
Election results:
tom:2
jack:1
rose:1
tom wins
提示输入候选人数量:“Input the number of electorates:”
提示输入候选人: “Input vote %d:”
输入格式:
输入候选人数量:"%d"
输入候选人姓名:"%s"
输出格式:
输出候选人得票数:"%s:%d\n"
输出票数最多的候选人姓名:"%s wins\n"
输出评选结果提示信息:“Election results:\n”
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include
int main()
{
int n, i, vote1 = 0, vote2 = 0, vote3 = 0;
char name[10];
printf("Input the number of electorates:");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Input vote %d:", i + 1);
scanf("%s", name);
if (strcmp("tom", name) == 0)
{
vote1++;
}
else if (strcmp("jack", name) == 0)
{
vote2++;
}
else if (strcmp("rose", name) == 0)
{
vote3++;
}
}
printf("Election results:\n");
printf("%s:%d\n", "tom", vote1);
printf("%s:%d\n", "jack", vote2);
printf("%s:%d\n", "rose", vote3);
if (vote1>vote2&&vote1>vote3)
{
printf("%s wins\n", "tom", vote1);
}
else if (vote2>vote1&&vote2>vote3)
{
printf("%s wins\n", "jack", vote2);
}
else
{
printf("%s wins\n", "rose", vote3);
}
return 0;
}
/*第三题贴的代码,转侵删,请多见谅。
4
星期判断(4分)
题目内容:请输入星期几的第一个字母(不区分大小写)来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母(小写),否则输出“data error”。
程序运行结果示例1:
please input the first letter of someday:
S↙
please input second letter:
u↙
sunday
程序运行结果示例2:
please input the first letter of someday:
F↙
friday
程序运行结果示例2:
please input the first letter of someday:
h↙
data error
第一个字母的输入提示信息:“please input the first letter of someday:\n”
第二个字母的输入提示信息:“please input second letter:\n”
用户输入错误提示信息:“data error\n”
输入格式: " %c" (注意:%c前面有一个空格)
输出格式:
星期一:“monday\n”
星期二:“tuesday\n”
星期三:“wednesday\n”
星期四:“thursday\n”
星期五:“friday\n”
星期六:“saturday\n”
星期日:“sunday\n”
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include
int main()
{
char n,m;
printf("please input the first letter of someday:\n");
scanf(" %c",&n);
getchar();
switch(n)
{
case 'm':case 'M':
printf("monday\n");
break;
case 'w':case 'W':
printf("wednesday\n");
break;
case 'f':case 'F':
printf("friday\n");
break;
case 't':case 'T':
printf("please input second letter:\n");
scanf(" %c",&m);
if (m=='u')
{
printf("tuesday\n");
break;
}
else if (m=='h')
{
printf("thursday\n");
break;
}
else
{
printf("data error\n");
break;
}
case 's':case 'S':
printf("please input second letter:\n");
scanf(" %c",&m);
if (m=='a')
{
printf("saturday\n");
break;
}
else if (m=='u')
{
printf("sunday\n");
break;
}
else
{
printf("data error\n");
break;
}
default :
printf("data error\n");
break;
}
return 0;
}