实验报告
专业软件工程班级X 班学号_ XXXXXXXXX_ 姓名
实验日期:201X年X月X日报告退发(订正、重做)
课程C程序设计实验实验名称结构体
一、实验目的
二、实验环境(描述实验的软件、硬件环境)
①软件环境:windows xp/win7等操作系统,Microsoft Visual C++ 6.0编译器;
②硬件环境:PC机一台
三、实验内容、步骤和结果分析
题目一:
#include
#include
struct Stu
{
int num;
char name[20];
char sex;
int age;
float score[3];
};
void fun(struct Stu *stu, int i)
{
float sum = stu->score[0] + stu->score[1] + stu->score[2], aeg = sum / 3.0;
printf("\n第%d位学生的基本信息如下:\n\n", i);
printf("学号:(%d)\t姓名:(%s)\t性别:(%c)\t年龄:(%d)\n\n", stu->num, stu->name, stu->sex, stu->age);
printf("三科成绩:(%.2f,%.2f,%.2f)\n\n", stu->score[0], stu->score[1], stu->score[2]);
printf("总成绩:(%.2f) 平均成绩:(%.2f)\n", sum, aeg);
printf("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"); }
int main()
{
int i, n;
printf("The number of students is:");
scanf("%d", &n);
struct Stu *stu = (struct Stu *)calloc(n, sizeof(struct Stu));
for (i = 0; i { printf("请输入第%d位学生的基本信息:\n", i + 1); printf("学号:"); scanf("%d", &(stu + i)->num); printf("姓名:"); scanf("%s", (stu + i)->name); printf("性别:"); scanf(" %c", &(stu + i)->sex); printf("年龄:"); scanf("%d", &(stu + i)->age); printf("三科成绩:"); scanf("%f%f%f", &(stu + i)->score[0], &(stu + i)->score[1], &(stu + i)->score[2]); putchar('\n'); } printf("===================================================================\n"); for (i = 0; i { fun(stu + i, i + 1); } return 0; } 题目二:计算每一个结构体的两个变量所表示的长度之和(单位:厘米) (1)分别定义公制METRIC(成员包括:米、厘米)和英制BRITISH(成员包括:英尺、英寸)两个结构体; (2)为每一个结构定义两个变量。 #include int main() { struct METRIC { float m; float cm; } m1, m2; struct BRITISH{ float foot; float inches; } b1, b2; printf("Enter the info of m1(米,厘米):"); scanf("%f%f", &m1.m, &m1.cm); printf("Enter the info of m2(米,厘米):"); scanf("%f%f", &m2.m, &m2.cm); printf("\nEnter the info of m2(英尺,英寸):"); scanf("%f%f", &b1.foot, &b1.inches); printf("Enter the info of m2(英尺,英寸):"); scanf("%f%f", &b2.foot, &b2.inches); printf("\nSum of m1 and m2 is:%.2f(厘米)\n", (m1.m + m2.m) * 100 + m1.cm + m2.cm); printf("Sum of b1 and b2 is:%.2f(厘米)\n\n", (b1.inches + b2.inches)*30.48 + (b1.foot + b2.foot)*2.54); return 0; } 题目三:编写一个函数用于计算某个日期是相应年份的第几天 声明一个描述日期(年、月、日)的结构体类型。 #include struct time { int year; int month; int day; }; void fun(struct time time) { int a,b,sum=0; for(a=1;a { if(a==4||a==6||a==9||a==11) sum+=30; else if(a==2) { if((time.year%4==0)&&(time.year%100!=0||time.year%400==0)) sum+=29; else sum+=28; } else sum+=31; } sum+=time.day; printf("%d-%d-%d 是该年的第%d 天!\n\n",time.year,time.month,time.day,sum); } int main() { struct time time; printf("Enter the date(Example:xxxx xx xx)(End with 0000 00 00):\n\n"); while(scanf("%d%d%d",&time.year,&time.month,&time.day)!=EOF) { if(time.year==0&&time.month==0&&time.day==0) break; fun(time); } return 0; } 题目四:定义两个函数分别用于输入和输出N个STUDENT结构体变量的信息 要求: 定义两个结构体STUDENT和BIRTHDAY。 其中结构体STUDENT包含成员有:姓名(char name[20]),学号(int number),出生日期(BIRTHDAY birth);结构体BIRTHDAY包含的成员有:年份(int year),月份(int month)和日期(int day)。 #include #include struct BIRTHDAY { int year; int month; int day; }; struct STUDENT { char name[20]; int number; struct BIRTHDAY birth; }; void input(struct STUDENT *stu,int n) { int i; putchar('\n'); for (i = 0; i < n; i++) { printf("第%d 位学生的信息:\n姓名:", i + 1); scanf("%s", stu[i].name); printf("学号:"); scanf("%d", &stu[i].number); printf("生日:"); scanf("%d-%d-%d", &stu[i].birth.year, &stu[i].birth.month, &stu[i].birth.day); if (i != n - 1) putchar('\n'); else printf("输入完毕!\n"); } } void output(struct STUDENT *stu, int n) { int i; for (i = 0; i < n; i++) { printf("\n第%d 位学生的信息如下:\n", i + 1); printf("姓名:(%s)\t学号:(%d)\t生日:(%d-%d-%d)\n", stu[i].name, stu[i].number, stu[i].birth.year, stu[i].birth.month, stu[i].birth.day); if (i != n - 1) putchar('\n'); else printf("输出完毕!\n"); } } int main() { int n; printf("请输入待输入资料的学生的数目N:"); scanf("%d", &n); struct STUDENT *stu = (struct STUDENT *)calloc(n, sizeof(struct STUDENT)); input(stu, n); printf("====================================================================\n"); output(stu, n); printf("====================================================================\n"); return 0; } 题目五:函数sortInfo(……)是按照总成绩从高到低的顺序进行排序的。如果两位学生的总成绩相等, 则按照学号从小到大排序。 要求: ①声明一个STUDENT结构体,成员有:姓名(char name[20]),学号(int number),总成绩(float totalScore) ②在main函数中定义一个长度为N的结构体数组arr[],调用函数sortInfo(struct STUDENT *arr, int n)对N位学生的信息进行排序 #include #include struct STUDENT { char name[20]; int number; float totalScore; }; void input(struct STUDENT *stu,int n) { int i; putchar('\n'); for (i = 0; i < n; i++) { printf("第%d 位学生的基本信息:\n姓名:", i + 1); scanf("%s", stu[i].name); printf("学号:"); scanf("%d", &stu[i].number); printf("总成绩:"); scanf("%f", &stu[i].totalScore); putchar('\n'); } } void sortInfo(struct STUDENT *stu, int n) { int i,j; struct STUDENT temp; for (i = 0; i < n; i++) { for (j = 1; j < n - i; j++) { if ((stu[i].totalScore < stu[i + j].totalScore) || (stu[i].totalScore == stu[i + j].totalScore && (stu[i].number)>(stu[i + j].number))) { temp = stu[i]; stu[i] = stu[i + j]; stu[i + j] = temp; } } } } void output(struct STUDENT *stu, int n) { int i; for (i = 0; i < n; i++) { printf("姓名:(%s)\t学号:(%d)\t总成绩:(%.2f)\n", stu[i].name, stu[i].number, stu[i].totalScore); } } int main() { int n; printf("请输入待输入资料的学生的数目N:"); scanf("%d", &n); struct STUDENT *stu = (struct STUDENT *)calloc(n, sizeof(struct STUDENT)); input(stu, n); printf("====================================================================\n"); printf("排序之前的初始状态:\n"); output(stu, n); printf("====================================================================\n"); sortInfo(stu, n); printf("排序之后的最终状态:\n"); output(stu, n); printf("====================================================================\n"); return 0; } 题目六:编程模拟一个时钟(时间实时更新) 要求: ①声明一个时间结构体TIME,包含成员:时(int hour),分(int minute),秒(int second)。定义函数updata(……)用于更新时间。 ②假设当前时刻为23:59:59,则调用函数updata将得到的下一刻时间为00:00:00 假设当前时刻为23:45:56,则调用函数updata将得到的下一刻时间为23:45:57 #include #include struct TIME { int hour; int minute; int second; }; updata(struct TIME *t) { if (t->second == 59) { t->second = 0; if (t->minute == 59) { t->minute = 0; if (t->hour == 23) { t->hour = 0; }else (t->hour)++; }else (t->minute)++; }else (t->second)++; } int main() { struct TIME t; printf("请设定此刻时钟的时间(XX:XX:XX):"); scanf("%d:%d:%d",&t.hour,&t.minute,&t.second); putchar('\n'); while (1) { printf("%02d:%02d:%02d\r", t.hour, t.minute, t.second); updata(&t); Sleep(1000); } return 0; } 四、讨论 c 语言程序设计教程第二版课后习题 答 案 篇一: c 语言程序设计教程_李含光_ 郑关胜_ 清华大学 出版社习题答案习题答案[完美打印版]】1.单项选择题 (1)a (2)c(3)d (4)c (5)b 2.填空题(1)函数 (2)主函数(main )(3)printf(),scanf()第 2 章习题参考答案 1.单项选择题 1-5 cbccc 6-10 cdcdc 11-13 dbb 2 .填空题 (1)1(2)26 (3)6 , 4 , 2 (4)10 , 6 (5)3.000000 (6)双精度(double )(7)9 (8)字母,数字,下划线(9)13.700000 (10)11(11)((m/10 )%10 )*100+ (m/100 )*10+m%10 (12)0 (13)10 ,9 ,11(15) (x0y0)||(x0z0)||(y0||z0)(16 )double (17)x==0 (18)sqrt(fabs(a-b))/(3*(a+b))(19 )sqrt((x*x+y*y)/ (a+b)) 第 3 章习题参考答案 1.单项选择题 1-5 cccdd 6-10 bcdbc11-15 bcbbb16 a 2 .填空题 (1)用;表示结束(2){ } (3 )y=x0?1:x==0?0:-1 (4)y%4==0y%100!=0||y%400==0 (5)上面未配对(6)default 标号(7)while ,do while ,for (8)do while (9)本次(10)本层3.阅读程序,指出结果 (1)yes (2)*(3)abother (4)28 70 (5)2,0(6)8(7)36 (8)1(9)3,1,-1,3,1,-1(10)a=12 ,y=12 (11) i=6 ,k=4 (12)1,-2 4 .程序填空 (1)x:y ,u :z (2)m=n ,m !=0 ,m=m/10 (3)teps , t*n/(2*n+1) , printf( n”“,%2*lsf )(4)m%5==0 , 《C程序设计》实验教学大纲 欧阳家百(2021.03.07) 一、适用范围 大纲适用信息管理专业本科教学使用。 二、课程名称 C程序设计 三、学时数与学分 总学时:90 总学分: 4 实验学时:28 实验学分:1 四、教学目的和基本要求 目的:通过C程序设计实验,培养学生对学习程序设计的兴趣,加深对讲授内容的理解,尤其是通过上机来掌握语法规则,使学生全面了解C 语言的特点,熟练掌握C 语言程序设计的基本方法和编程技巧。 基本要求:了解和熟悉C语言程序开发的环境;学会上机调试程序,善于发现程序中的错误,并且能很快地排除这些错误,使程序能正确运行,达到实验知识和理论知识的融会贯通。上机实验前,学生必须事先根据题目的内容编好程序,然后在实验时输入程序、调试程序、直至运行结果正确为止,上机结束后,应整理出实验报告。 五、实验项目与学时分配 注:带*的实验项目为选做实验项目 六、教材、讲义及参考书 《C程序设计题解与上机指导》谭浩强主编清华大学出版社 七、实验成绩评定办法 实验成绩=平时实验表现+实验报告。实验成绩占总成绩的20%。 实验成绩以等级形式给出,评定等级分优、良、中、及格、不及格五类。 1、平时考核:上机实验前,学生必须事先根据题目的内容编好程序,然后在实验时输入程序、调试程序、直至运行结果正确为止。在实验中,教师可根据学生编程操作能力、观察和分析及运用知识能力、程序编制正确性以及学生的课堂纪律、实验态度、保持实验室卫生等方面的表现进行综合考核。 2、实验报告:学生实验后应按时完成实验报告。 八、实验教学大纲说明 本大纲共安排28学时的实验,其中带*号实验项目为选做实验项目,实际课时为18学时。实验项目多为设计性实验项目,每 验证性实验:数组指针结构体实验报告 1.实验目的 (1)复习c++语言数组的用法。 (2)复习c++语言指针的用法。 (3)复习c++语言结构体的用法。 (4)理解算法时间复杂度分析的基本方法。 (5)通过实验程序,分析它们的时间复杂度。 2.实验内容 (1)将1~10存入数组a[10],并将其逆序输出。 (2)用指针方式编写程序:从键盘输入10个整数数据,并存入数组,要求将10个数中最大的数与第1个输入的输交换:将10个数中最小的数和最后一个输入的数交换。(3)有5个学生,每个学生的数据包括学号、姓名3门课的成绩、平均分。 要求:从键盘依次输入5个学生的学号、姓名3门课的成绩,自动计算3门课的平均分,将5个学生的数据在屏幕上输出。 3.实验结果 (1)#includec语言程序设计教程第二版课后习题答案
C语言程序设计实验报告(实验大纲+过程)之欧阳家百创编
数组指针结构体实验报告