当前位置:文档之家› C语言实验八结构体上机报告

C语言实验八结构体上机报告

《标准C语言程序设计》上机报告实验八结构体程序设计
专业:电子信息工程
班级:电信1301
学号:U201313480
姓名:秦行
完成日期:2014/6/9
一、实验目的:
1、掌握结构体类型的说明和结构体变量的定义;
2、掌握结构体变量成员的引用和对结构体变量的初始化;
3、掌握结构体数组及结构体指针变量的定义及使用。

4、理解并掌握结构体在函数间的传递;
5、进一步掌握复杂程序编译、连接和调试的技巧。

二、实验内容及要求(鼓励一题多解)
——以下均要求不得使用全局变量:
1
(1)、正确定义该表格内容要求的数据类型;
(2)、分别输入各成员项数据,并打印输出(为简便起见,假设只有3名考生)。

#include<stdio.h>
#include<string.h>
#define N 3
struct date
{
int year;
int month;
int day;
};
struct student
{
unsigned int num;
char name[20];
char sex;
struct date birth;
};
void main()
{
struct student tester[N];
int i;
for(i=0;i<N;i++)
{
printf("请输入考生学生证号:");
scanf("%d",&tester[i].num);
printf("请输入姓名:");
scanf("%s",tester[i].name);
printf("请输入M或W,M表示男,W表示女");
scanf("%s",&tester[i].sex);
printf("请输入考生生日,(按年月日敲如2014 enter 06 enter 09 enter)");
scanf("%d%d%d",&tester[i].birth.year,&tester[i].birth.month,&tester[i].birth. day);
}
printf("学生证号\t姓名\t性别\t考生生日\n");
printf("______________________________\n");
for(i=0;i<N;i++)
{
printf("%d\t%s\t%c\t%d%d%d\n",tester[i].num,tester[i].name,tester[i].sex,tester[I ].birth.year,tester[i].birth.month,tester[i].birth.day);
}
}
2、编程实现:(1)输入10个职工姓名和工资,(2)按职工姓名由小到大排序,
(3)在主函数中输出排序后的职工姓名与工资、调用查找函数(需要编程)查找输入姓名的职工工资并输出职工姓名和工资。

要求:定义结构体数组(由结构体构成的链表——可选)保存10个职工姓名和工资信息,各模块均用函数实现
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 10
struct worker
{
char name[20];
int salary;
};
int input(struct worker *pt); //录入函数
void display(struct worker *p,int N); //输出函数
void compare(struct worker *pdata,int N); //排序函数
void main()
{
struct worker man[N];
int i;
char search[20];
for(i=0;i<N;i++)
if(input(&man[i])==0)break;
compare(man[N].name,N);
display(man,N);
printf("键入查找的职工的名字");
scanf("%s",search);
int j,flag=0;
for(j=0;j<N;j++)
{
if(strcmp(man[j].name,search)==0)
{
flag=1;
k=j+1;
printf("find the worker");
break;
}
}
if(!flag)
printf("can not find the worker");
printf("the worker's name :%s,He's salary is %d",man[k].name,man[k].salary); }
int input(struct worker *pt)
{
printf("Name?");
gets(pt->name);
if(pt->name=='/0')return 0;
printf("Salary?");
gets(pt->salary);
return 1;
}
void compare(struct worker *pdata,int n)
{
struct worker temp;
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<N;j++)
if(strcmp(pdata[i].name,pdata[j].name)>0
{
temp=pdata[i];
pdata[i]=pdata[j];
pdata[j]=temp;
}
}
}
void display(struct worker *p,int n)
{
int i;
printf(" name\tsalary\n");
for(i=0;i<N;i++)
printf("%15s\t%d\n",p->name,p->salary);
}
3、调试下面程序修改错误
下面程序实现建立一个单项链表,链表中这个节点有一个int类型的数据域,输入0时标志链表建立过程结束。

如输入123456,应该输出1,2,3,4,5,6.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
#define LEN sizeof(NODE) //多了“;”
NODE *setup()
{
NODE *head=NULL,*p1,*p2;
int n=0;
p2=(NODE*)malloc(LEN);
p2=p1; //原来是p1=p2;
scanf("%d",&p1->data);
while(p1->data)
{
n++;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;//p2=p1;
p1=(NODE*)malloc(LEN);
scanf("%d",&p1->data);
}
p2=NULL;
return head;
}
void main()
{
NODE*p;
p=setup();
while(p)
{
printf("%d",p->data);
p=p->next;
}
}
4、理解书上P.238-241例8.3、例8.4、例8.5有关结构体指针的使用
5、复习书上8.6节typedef的用法和例8.12
三、上机中的感受、困惑及教学建议
1、对报告中的某些概念的理解
2、课堂某个知识点的困惑
3、上机调试中问题
4、…。

相关主题