1.定义结构体类型,包括候选人名和选票两个成员,编程实现对候选人得票的统计。
1.Write a C program that implements the statistics of the candidate votes using the definition of structures including two members ( candidate name and vote count)
#include<stdio.h>
#include<string.h>
int main()
{
struct person
{
char name[7];
int count;
};
struct person leader[2]={"Biden",0,"Trump",0};
int i,j;
char leadername[7];
for(i=0;i<5;i++)
{
scanf("%s",leadername);
for(j=0;j<2;j++)
if(strcmp(leadername,leader[j].name)==0)
leader[j].count++;
}
for(i=0;i<2;i++)
printf("%7s: %d\n",leader[i].name,leader[i].count);
return 0;
}
2.定义结构体类型变量(包括年、月、日),编写程序实现输入一个日期显示它是该年的第几天。
2.Write a C program that Enter a certain day a year to determine the day of the year the first few days. Define a structure variable including year, month and day.
#include<stdio.h>
int main()
{
int order=0,a,i;
struct time
{
int year,month,day;
}date;
scanf("%d.%d.%d",&date.year,&date.month,&date.day);
if(date.year%400==0)
a=1;
else if(date.year%4==0&&date.year%100!=0)
a=1;
else
a=0;
for(i=1;i<date.month;i++)
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
order+=31;
else if(i==2&&a==0)
order+=28;
else if(i==2&&a!=0)
order+=29;
else
order+=30;
}
order+=date.day;
printf("%d",order);
return 0;
}
3.编写程序,使用结构体数组存储2个学生的学号、姓名、2门课成绩和平均成绩,要求定义输入函数实现学生的学号、姓名、2门课成绩的键盘输入以及平均成绩的计算,定义输出函数实现以上信息的输出。
3.Write a C program that stores the number, name, two scores and average score of two students using structure array. Input function is required to realize input of student number, name, scores by keyboard and the calculation of average score. Output function is defined to realize the output of the above information.
#include<stdio.h>
#include<string.h>
#define N 2
int main()
{
int i;
struct student
{
char name[10];
int num,g1,g2;
float gave;
}stu[N];
for(i=0;i<N;i++)
{
scanf("%d\n",&stu[i].num);
gets(stu[i].name);
if(i!=N-1)
scanf("\n%d\n%d\n",&stu[i].g1,&stu[i].g2);
else
scanf("\n%d\n%d",&stu[i].g1,&stu[i].g2);
}
printf("\n");
for(i=0;i<N;i++)
{
stu[i].gave=0.5*(stu[i].g1+stu[i].g2);
printf("%d\n",stu[i].num);
puts(stu[i].name);
printf("%d\n%d\n%f\n",stu[i].g1,stu[i].g2,stu[i].gave);
}
return 0;
}
4.定义共用体,实现一个整型数据按字节输出(以8进制数和字符两种形式输出)。
4.Write a C program that outputs the results of an integer convert to Bytes by defining Union.
#include<stdio.h>
#include<string.h>
int main()
{
int i;
union
{
char ch[4];
int numino;
}num;
scanf("%d",&num.numino);
printf("八进制:\n%o\n字符:\n",num.numino);
for(i=0;i<4;i++)
{
printf("\t%d\n",num.ch[i]);
}
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)