算法思路:建立关于课程的结构体,建立关于成绩的共同体。
算法代码:
#include<stdio.h>
#include<string.h>
union method{
int hundred;
char five[10];
};
struct course
{
char name[30];
char mode;
union method grade;
};
int main()
{
struct course report[4];
int i;
strcpy(report[0].name,"C Language Programing");
strcpy(report[1].name,"College Chinese");
strcpy(report[2].name,"Advanced Mathematics");
strcpy(report[3].name,"Data Structure");
for(i=0;i<4;i++)
{
printf("Input the mode(h/f) of course \"%s\":",report[i].name);
scanf("%c",&report[i].mode);
printf("Examination Result:");
if(report[i].mode=='h')
scanf("%d",&report[i].grade.hundred);
else
scanf("%s",report[i].grade.five);
getchar();//因为连续输入,下一个的开头不能时是回车符,因此添加一条语句,读入并丢弃“回车符”
}
puts("\nname mode grade");
for(i=0;i<4;i++)
{
printf("%-30s%-16c%",report[i].name,report[i].mode);
if(report[i].mode=='h')
printf("%d\n",report[i].grade.hundred);
else
printf("%s\n",report[i].grade.five);
}
return 0;
}
运行结果: