手机版

C语言程序设计实验报告——实验

时间:2025-05-10   来源:未知    
字号:

c语言的设计

C语言程序设计

c语言的设计

printf("score=%f\n",info.score); } 2.建立 输入这 5 名学生的信息表,每个学生的数据包括学号、姓名及

一门课的成绩。要求从键盘 5 名学生的信息,并按照每一行显示一名学生信息的形 式将 5 名学生的信息显示出 来。上机运行以下程序。 分析:每个学生的数据学号、姓名及一门课的成绩用结构体表示, 5 名学生的信息表 用结构体数组表示,体会结构体数组元素的引用方法。 #include"stdio.h" #define N 5 struct stud { int num; char name [20]; 实 验 内 float score; }; struct stud s[N]; void main() { int i;

容 for (i=0;i<N;i++) { 与 printf("input number:"); scanf("%d",&s[i].num); 步 printf("input name:"); scanf("%s",s[i].name); 骤 printf("input score:"); scanf("%f",&s[i].score); } for (i=0;i<N;i++) { printf("%d ",s[i].num); printf("%s ",s[i].name); printf("%f\n",s[i].score); } } 3.显示某人工资信息的程序如下,分析显示结果。上机运行以下程 序。 分析:某人工资信息可以由结构体来定义,表中的内容可以通过 结构体中的成员来表 示。体会结构体成员的指针运算符引用方法。

c语言的设计

#include"stdio.h" #include "string.h" void main() { struct staff { char name[20]; 实 char department[20]; 验 内 容 与 步 骤 int salary; }; struct staff w1,*p; p=&w1; strcpy(http://www.77cn.com.cn,"Li-Li"); /*个人信息*/ strcpy((*p).department,"part1"); p->salary=1000; printf("%s %s %d\n",

http://www.77cn.com.cn,w1.department,w1.salary); printf("%s %s %d\n",(*p).name,(*p).department,(*p).salary); printf("%s %s %d\n",p>name,p->department,p->salary); } 4.编写 input()和 output()函数,输入、输出 录。上机运行以下程序。 分析:体会结构化程序设计思想的应用。 #include "stdio.h" #define N 2 struct student { char num[6]; char name[8]; int score[4]; }stu[N]; void input() { int i,j; for(i=0;i<N;i++) { printf("\n please input %d of %d\n",i+1,N); printf("num: "); scanf("%s",stu[i].num); printf("name: "); scanf("%s",stu[i].name); for(j=0;j<3;j++) { printf("score %d.",j+1); 2 个学生的数据记

c语言的设计

scanf("%d",&stu[i].score[j]); } printf("\n"); } } void print(struct student stu[N]) { int i,j; printf("\nNo. Name Sco1 Sco2 Sco3\n"); for(i=0;i<N;i++) { printf("%-6s%-10s",stu[i].num,stu[i].name); for(j=0;j<3;j++) printf("%-8d",stu[i].score[j]); printf("\n"); } } void main() { input(); print(stu); } 5.利用结构体类型,编程计算一名同学 5 门课的平均分。上机运 行以下程序。 分析:计算一名同学 算术运算。 #include "stdio.h" void main() { struct stuscore { char float float }; struct int i; float sum=0; for(i=0; i<5; i++) sum+=x.score[i]; x.average=sum/5; printf("The average score of %s is %4.1f\n", http://www.77cn.com.cn,x.average); } stuscore x={"WangWei", 90.5,85, 70, 90, 98.5}; name[20]; score[5]; average; 5 门课的平均分,是对结构体成员变量的

c语言的设计

6.用结构体型数组初始化建立一工资登记表。 然后键入其中一人的 姓名, 查询其工资情况。 上机运行以下程序。 分析:查询工资情况,是对结构体成员的查询操作。 #include "stdio.h" #include "string.h" void main() { struct staff { char name[20]; char department[20]; int salary; int cost; } worker[3]={{"XuGuo", "part1",800,200}, {"Wu-Xia","part2",1000,300},{"Li-Jun","part3",1200,350},}; int i; char xname[20]; printf("\nInput the worker\'s name: "); scanf("%s", xname); for(i=0; i<3; i++) if(strcmp(xname,worker[i].name)==0) { printf("\n printf("\n printf("\n st); } } 7.用子函数求出 以下程序。 分析:求出每个工人的实发工资,是对结构体成员变量的算术运 算。 #include"stdio.h" #define NUM 3 struct staff { char name[20]; char department[20]; int salary; int cost; int realsum; }; worker 数组中每个工人的实发工资。上机运行 salary:%6d", worker[i].salary); cost:%6d", worker[i].cost); payed:%6d\n",worker[i].salaryworker[i].co

c语言的设计

void main() { void getreal(struct staff *p,int n); struct staff worker[NUM],*pm; int i; printf("Input %d worker\'s name department salary cost:\n",NUM); for(i=0,pm=worker; i<NUM; i++,pm++) scanf("%s%s%d%d",pm>name,pm>department,&pm>salary,&pm>cost); pm=worker; getreal(pm,NUM); for(pm=worker; pm<worker+NUM; pm++) printf("%s of %s should be payed %d yuan\n",pm->name,pm->department,pm->realsum); } void getreal(struct staff *p,int n) { int i; for(i=0; i<n; i++,p++) p->realsum=p->salary-p->cost; } 8.输入并运行下面程序,

观察结果,分析联合变量的存储特点。上 机运行以下程序。 #include"stdio.h" void main() { union { int i[2]; long k; char c[4]; }t,*s=&t; s->i[0]=0x39; /* 按照整型成员的类型赋值 */ s->i[1]=0x38; printf("%x\n ",s->k); /* 按照长整型成员的类型使用储存内容 */ printf("%c\n ",s->c[0]); /* 按照字符型成员的类型使用储存内容 */ } 9.今天星期三,再过 10 天是星期几?上机运行以下程序。 分析:枚举类型变量与整型变量是两种不同数据类型,注意它们 之间的数据类型转换。 #include&q …… 此处隐藏:1954字,全部文档内容请下载后查看。喜欢就下载吧 ……

C语言程序设计实验报告——实验.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
    ×
    二维码
    × 游客快捷下载通道(下载后可以自由复制和排版)
    VIP包月下载
    特价:29 元/月 原价:99元
    低至 0.3 元/份 每月下载150
    全站内容免费自由复制
    VIP包月下载
    特价:29 元/月 原价:99元
    低至 0.3 元/份 每月下载150
    全站内容免费自由复制
    注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
    × 常见问题(客服时间:周一到周五 9:30-18:00)