一、实验目的
- 了解开发环境
- 学习如何编辑、编译、连接和运行c程序
- 了解并掌握结构体的定义、申明及调用规则
- 理解结构体的应用
二、实验内容
【项目1—读程序】阅读下面的程序,体会结构体的应用方法。
- 体会结构体类型变量的定义方法
#include <stdio.h>
struct Student
{
int num;
char name[20];
char sex;
int age;
float score;
char addr[30];
};
int main()
{
struct Student student1,student2;
printf("%d\n",sizeof(student1));
return 0;
}
实验结果截图:
- 结构体作函数参数
#include <stdio.h>
struct Student
{
int num;
char name[20];
char sex;
int age;
float score;
char addr[30];
};
void print(struct Student s)
{
printf("%d %s %c\n",s.num,s.name,s.sex);
//可再加。。。
return;
}
int main()
{
struct Student student1,student2;
struct Student *p_stu;
student1.num = 10001;
student2.age = 120;
p_stu = &student2;
print(student1);
print(*p_stu);
return 0;
}
实验结果截图:
代码:
#include <stdio.h>
#include <string.h>
struct Student
{
int num;
char name[20];
char sex[20];
int age;
double score;
char addr[30];
};
void print(struct Student s)
{
printf("%d %s %s\n",s.num,s.name,s.sex);
return;
}
int main()
{
struct Student student1,student2;
struct Student *p_stu;
student1.num=10001;
strcpy(student1.name,"张三");
strcpy(student1.sex,"male");
student1.age=15;
student2.num=20002;
strcpy(student2.name,"李四");
strcpy(student2.sex,"female");
student2.age=120;
p_stu=&student2;
print(student1);
print(*p_stu);
return 0;
}
- 当结构体成员是数组
#include <stdio.h>
#include <string.h>
struct Student
{
int num;
char name[10];
double score[3];
};
void print(struct Student);
int main()
{
struct Student stu;
stu.num = 12345;
strcpy(stu.name,"Li Fung");
stu.score[0] = 67.5;
stu.score[1] = 89;
stu.score[2] = 78.5;
print(stu);
printf("%d %s ",stu.num , stu.name);
printf("%.1f %.1f %.1f\n",stu.score[0],stu.score[1],stu.score[2]);
return 0;
}
实验结果截图:
- 结构体数组的应用。
#include <stdio.h>
#include <string.h>
typedef struct
{
char name[20];
int count;
}Person;
int main()
{
Person person[3] = {{"Li",0},{"Zhang",0},{"Fun",0}};
int i, j;
char name[20];
//输入10人投票情况,并确定给3人中哪个计票
for(i = 0;i < 10;i++)
{
scanf("%s",name);
for(j = 0,j < 3;j++)
if(strcmp(name,person[j].name) == 0)
person[j].count++;
}
printf("\nResult:\n");
//输出计票结果
for(i = 0;i < 3; i++)
{
printf("%s: %d\n",person[i].name,person[i].count);
}
return 0;
}
实验结果截图:
方便输入投票姓名结果,将投票数由10改成了5
- 指向结构体变量的指针的应用
#include <stdio.h>
#include <string.h>
typedef struct
{
int num;
char name[12];
char sex;
float score;
};
int main()
{
struct Student stu;
stu.num = 10301;
strcpy(stu.name,"Wang Fun");
stu.sex = 'f';
stu.score = 89.5;
struct Student *p=&stu;
printf("%d %s %c %.1f\n",stu.num,stu.name,stu.sex,stu.score);
printf("%d %s %c %.1f\n",(*p).num,(*p).name,(*p).sex,(*p).score);
printf("%d %s %c %.1f\n",p->num,p->name,p->sex,p->score);
return 0;
}
实验结果截图:
- 用指向结构体变量的指针做实参
实验结果截图:
【项目2–编程题】简单链表的实现
用链表来进行学生信息管理,要求定义出该学生信息的结点的结构体(结点信息包括学号No Name ,还有一个存放下一个结点的地址的指针next),能查询学生信息,添加、删除学生。
源代码:
#include <stdio.h>
#include <string.h>
struct Student
{
int no;
char name[20];
struct Student *next;
};
int main()
{
struct Student a,b,c,*head,*p;
a.no=10001;
strcpy(a.name,"张三");
b.no=10002;
strcpy(b.name,"李四");
c.no=10003;
strcpy(c.name,"王二");
head=&a;
a.next=&b;
b.next=&c;
c.next=NULL;
p=head;
do
{
printf("%d %s\n",p->no,p->name);
p=p->next;
}
while(p!=NULL);
//添加
printf("添加学生李华后输出全部学生\n");
struct Student d,*s;
s=&d;
d.no=10004;
strcpy(d.name,"李华");
p=&b;
s->next=p->next;
p->next=s;
p=head;
do
{
printf("%d %s\n",p->no,p->name);
p=p->next;
}
while(p!=NULL);
//删除
printf("删除学生李华后输出全部学生\n");
struct Student *q;
p=&b;
q=p->next;
p->next=q->next;
p=head;
do
{
printf("%d %s\n",p->no,p->name);
p=p->next;
}
while(p!=NULL);
printf("查询学生李四信息\n");
p=head;
while(strcmp("李四",p->name)!=0)
p=p->next;
printf("%d\n",p->no);
return 0;
}
运行截图: