实验项目10 结构体简单应用
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验项目10 结构体简单应用
●实验目的
1.掌握结构体数据类型的基本概念。
2.掌握结构体类型变量的定义和使用方法。
3.掌握结构体类型数组与指针的关系及使用方法。
4.掌握结构体类型变量和数组作为函数参数的使用方法和技巧
5.掌握C语言中的存储分配函数使用方法。
6.理解链式存储结构与数组的区别。
7.掌握单链表中的建立、访问、插入和删除等最常见操作的C语言处理方法。
●实验内容
1.程序填空
(1)下面程序的功能是统计每个候选人的选票和无效的选票,请填空完成程序。
#include
#include
struct candidate
{
char name[20];
int count;
};
int main()
{
int i,n;
char temp[20];
struct candidate list[]={{"Invalid",0},{"Li",0},{"Wang",0},{"Sun",0}};
printf("请输入参加投票的人数:");
scanf("%d",&n);
while(n--)
{
printf("Enter the Name of Candidate:");
scanf("%s",temp);
for(i=1;i<4;i++)
if( )
{
list[i].count++;
;
}
if(i==4)
;
};
for (i=0; i<=3; i++)
printf("%s:%d\n",list[i].name,list[i].count);
return 0;
}
(2)下面程序的功能是:从键盘上输入3个学生的姓名、英语成绩、数学成绩,然后计算每个学生的总成绩并输出,请填空完成程序。
#include
#define Total 3
struct Student
{
char Name[20];
int English;
int Math;
int Sum;
}Person[Total];
void Input(struct Student *p,int N)
{
int i;
for (i=0; i { printf("\nInput Name and Score:"); scanf("%s%d%d", ); } } int main( ) { struct Student *p; int i; p=Person; Input( ); for (i=0; i p->Sum=p->English+p->Math; for (i=0; i printf("%s %d\n", ); return 0; } 2.程序改错 (1)下面程序中实现的功能是:用结构体变量表示复数,进行2个复数的加法和乘法运算,并输出结果。程序中标有星号行存在错误,请改正这些错误。 #include #include struct Complex { int r; int i; }; struct Complex Add(struct Complex x,struct Complex y) { struct Complex z; z.i=x.i+y.i; z.r=x.r+y.r; return z; }; struct Complex* Mulitply(struct Complex x,struct Complex y) { struct Complex z; //***** z.r=x.r*y.r-x.i*y.i; z.i=x.r*y.i+x.i*y.r; return z; //***** } void Input(struct Complex x) //***** { printf("\nInput the values of r and i:"); scanf("%d%d",x->r,x->i); //***** return; } int main( ) { struct Complex a,b=(1,1),z,*p; //***** Input(&a); printf("a=%d+%di\n",a.r,a.i); printf("b=%d+%di\n",b.r,b.i); p=Mulitply(a,b); z=Add(a,b); printf("a+b=%d+%di\n",z.r,z.i); printf("a*b=%d+%di\n",p->r,p->i); return 0; } (2)下面程序的功能是计算教师的年龄并输出。程序中存在若干错误,请改正这些错误。#include #include #define ThisYear 2016 struct Date { int Year; int Month; }; struct Teacher { char name[20]; char sex; struct Date birthday; int age; };