最新C语言实验源代码.doc

上传人:1595****071 文档编号:33836152 上传时间:2022-08-12 格式:DOC 页数:17 大小:985.50KB
返回 下载 相关 举报
最新C语言实验源代码.doc_第1页
第1页 / 共17页
最新C语言实验源代码.doc_第2页
第2页 / 共17页
点击查看更多>>
资源描述

《最新C语言实验源代码.doc》由会员分享,可在线阅读,更多相关《最新C语言实验源代码.doc(17页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、精品资料C语言实验源代码.C语言实验源代码C语言实验源代码大一下c语言实验源代码 021112122-白娟娟实验一 1.输入三个整数,使用指针按由小到大的顺序排序并输出。编程思路:先定义三个整型变量和三个指向整型数据的指针变量pi(i=1,2,3),然后用这三个指针分别指向三个整型变量,输入三个整型变量的值,采用*pi去比较大小和输出。 #include stdafx.h int _tmain(int argc, _TCHAR* argv)int a,b,c,*p1,*p2,*p3,max,min; scanf(%d,%d,%d,&a,&b,&c); p1=&a;p2=&b;p3=&c; if

2、(*p1*p2) min=*p2; *p2=*p1; *p1=min; if(*p1*p3)if(*p2*p2)temp=*p2;*p2=*p1;*p1=temp;实验二1.输入十个整数,放在数组list中,然后用指针法从后向前输出该数组中的整数。编程思路:定义int *p,list10;令p指向数组的最后一个元素:p=list+9; 或者p=&list0+9; 采用循环10次,每次输出*p,然后p-,即前移一个元素。#include main() int list10,*p,i; for(i=0;i=0;p-) printf(%3d,*p);2查找整数。定义一个函数 search(int *

3、p,int n,int x),在指针p所指向的整型数组中查找元素x,若找到则返回相应下标,否则返回-1。在main()函数中调用search()。要求实参用指向整型数组的指针变量,数组长度是10,数据从键盘上输入。#include stdafx.hint _tmain(int argc, _TCHAR* argv) int search(int *p,int n,int x); int a10,i,x,n,m; scanf(%dn,&x); for(i=0;i10;i+) scanf(%d,&ai); n=10; m=search(a,10,x); printf(%10d,m);int sea

4、rch(int *p,int n,int x)int i;for(i=0;is2,则函数返回1;若s1*p2返回1, *p1*p2) return 1; else return -1; return 0;实验四1.定义一个结构体类型表示每个学生的数据,包括学号、姓名、3门课的成绩,定义并输入1个学生的数据到结构体变量中,然后输出该学生的姓名、成绩和平均成绩(保留2位小数)。编程指导:定义结构体含No、name、score1、score2、score3、average共6个成员,其中No、name为字符数组,score1、score2、score3为整型变量,average为浮点型变量。#inc

5、lude stdafx.hint _tmain(int argc, _TCHAR* argv) struct student int num; char name20; int score1; int score2; int score3; float average; boy; scanf(%d%s%d%d%d,&boy.num,boy.name,&boy.score1,&boy.score2,&boy.score3); boy.average=(boy.score1+boy.score2+boy.score3)/3.0; printf(平均成绩为%3.2f,boy.average); re

6、turn 0;2.定义一个结构体类型,包括年月日三个成员变量,输入一个日期,计算是这一年的第几天。编程指导:定义一个整型数组存储各个月份的天数,注意闰年问题。#include main() struct date int year; int month; int day; d; int a12=31,29,31,30,31,30,31,31,30,31,30,31,b=0,c=0,i;if (d.year%4=0&d.year%100!=0|d.year%400=0) b=29;else b=28;a1=b;scanf(%d%d%d,&d.year,&d.month,&d.day);if(d.

7、year%4=0&d.year%100!=0|d.year%400=0) for(i=0;i=d.month-2;i+) c=c+ai;else for(i=0;i=d.month-2;i+) c=c+ai; c=c+d.day; printf(第%d天,c); return 0;3. 第2题用结构体指针变量来实现。#include main() struct date int year; int month; int day; *d,b; d=&b;int a12=31,29,31,30,31,30,31,31,30,31,30,31,e=0,c=0,i;if (*d).year%4=0&(

8、*d).year%100!=0|(*d).year%400=0) e=29;else e=28;a1=e;scanf(%d%d%d,&d-year,&d-month,&d-day);if (*d).year%4=0&(*d).year%100!=0|(*d).year%400=0) for(i=0;i=(*d).month-2;i+) c=c+ai;else for(i=0;i=(*d).month-2;i+) c=c+ai; c=c+(*d).day; printf(第%d天,c); return 0;实验五1有5个学生,每个学生的数据包括学号、姓名、3门课的成绩,用赋初值的方法输入5个学生

9、的数据到结构体数组中,输出每个学生3门课的平均成绩(保留2位小数)。输出格式为:No name score1 score2 score3 average 101 Zhou 93 89 87 -102 Yang 85 80 78 -103 Chen 77 70 83 -104 Qian 70 67 60 -105 Li 72 70 69 -编程思路:定义结构体含No.、name、score1、score2、score3、average共6个成员,其中No、name为字符数组,score1、score2、score3为整型变量,average为浮点型变量。然后再定义5个元素的结构体数组,并赋初值。

10、用一重循环计算average并输出结果。#include main() struct student char num3; char name20; int score1; int score2; int score3; float average; boy5; int i; for(i=0;i5;i+)scanf(%s%s%3d%3d%3d,boyi.num,boyi.name,&boyi.score1,&boyi.score2,&boyi.score3); for(i=0;i5;i+) boyi.average=(boyi.score1+boyi.score2+boyi.score3)/3

11、.0; printf(No. name score1 score2 score3 averagen);for(i=0;i5;i+)printf(%3s%4s%7d%8d%6d%6.2fn,boyi.num,boyi.name,boyi.score1,boyi.score2,boyi.score3,boyi.average);return 0;2,在上题中,按平均成绩由高到低排序后,输出每个学生的成绩,输出格式与上题相同。注意:在排序中交换average成员的数据时,其他成员的数据也要作对应的交换。 方法一:#include main() struct student char num4; ch

12、ar name20; int score1; int score2; int score3; float average; boy5; int i; int k,j; struct student temp; for(i=0;i5;i+) scanf(%s%s%d%d%d,boyi.num,boyi.name,&boyi.score1,&boyi.score2,&boyi.score3);for(i=0;i5;i+)boyi.average=(float)(boyi.score1+boyi.score2+boyi.score3)/3.0;for(i=0;i5;i+) k=i;for(j=i+1

13、;j=5;j+) if(boyk.averageboyj.average) k=j; temp=boyk; boyk=boyi; boyi=temp; printf(No. name score1 score2 score3 averagen);for(i=0;i5;i+)printf(%s%s%d%d%d%6.2fn,boyi.num,boyi.name,boyi.score1,boyi.score2,boyi.score3,boyi.average);return 0;方法二:#include stdafx.hint _tmain(int argc, _TCHAR* argv) struc

14、t student char num4; char name20; int score1; int score2; int score3; float average; boy5; int i; int j; struct student temp;for(i=0;i5;i+)scanf(%s%s%d%d%d,boyi.num,boyi.name,&boyi.score1,&boyi.score2,&boyi.score3);for(i=0;i5;i+)boyi.average=(float)(boyi.score1+boyi.score2+boyi.score3)/3.0;for(i=0;i

15、5;i+) for(j=i+1;j=5;j+) if(boyi.averageboyj.average) temp=boyj; boyj=boyi; boyi=temp; printf(No. name score1 score2 score3 averagen);for(i=0;i5;i+)printf(%s%s%d%d%d%6.2fn,boyi.num,boyi.name,boyi.score1,boyi.score2,boyi.score3,boyi.average);return 0;实验六1定义一个结构体类型表示每个学生的数据,包括学号、姓名、3门课的成绩,定义并输入1个学生的数据到

16、结构体指针变量中,然后输出该学生的姓名、成绩和平均成绩(保留2位小数)。编程指导:定义结构体含No、name、score1、score2、score3、average共6个成员,其中No、name为字符数组,score1、score2、score3为整型变量,average为浮点型变量,要求定义结构体指针变量来处理数据。#include main() struct student char num10; char name20; int score1; int score2; int score3; float average; ; struct student *p; struct stud

17、ent s1; p=&s1; scanf(%s%s%d%d%d,p-num,p-name,&p-score1,&p-score2,&p-score3); p-average=(p-score1+p-score2+p-score3)/3.0;printf(%3s%3s%3d%3d%3d%6.2f,p-num,p-name,p-score1,p-score2,p-score3,p-average);return 0;2用结构体类型指针求两个复数之积。编程指导:定义一个结构体类型struct fushu float x; float y,包括两个浮点数成员。如(a1+b1i)*(a2+b2i)=(a

18、1*b1-a2*b2)+(a1*b2+a2*b1)i。#include main()struct fushu float x; float y; ; struct fushu *p1,*p2; struct fushu s1,s2; float a,b; p1=&s1;p2=&s2; printf(分别输入两个复数的实部和虚部n); scanf(%f%f%f%f,&p1-x,&p1-y,&p2-x,&p2-y); a=(p1-x*p1-y)-(p2-x*p2-y); b=(p1-x*p2-y)+(p1-y+p2-x); printf(答案为%1.2f+%1.2fi,a,b); return 0

19、;3. 重新实现第1题,其中有关成绩的成员变量改为数组int score3。#include main() struct student char num10; char name20; int score3; float average; ; struct student *p; struct student s1; p=&s1; int i; float sum=0; printf(请输入该生的学号和姓名n); scanf(%s,p-num); scanf(%s,p-name); printf(请输入该生的成绩n); for(i=0;iscorei); sum=sum+p-scorei;

20、p-average=sum/3.0; printf(%3s%3s,p-num,p-name); for(i=0;iscorei); printf(%6.2f,p-average);return 0;实验七1.建立一个3个节点的静态链表,每个节点包括学号、姓名、3门课的成绩,从键盘输入学生的数据,按照链表顺序依次进行输出。编程指导:定义结构体类型以及3个变量、3个指针,分别输入数据,建立链表,采用循环输出数据。注意如何判断链表结束。#include stdafx.h#define NULL 0struct student char num10; char name20; int score1;

21、int score2; int score3; struct student *next; ;int _tmain(int argc, _TCHAR* argv) struct student a, b, c, *head, *p; p=head=&a; a.next=&b; b.next=&c; c.next=NULL; for(;p!=NULL;p=p-next) scanf(%s%s%d%d%d,p-num,p-name,&p-score1,&p-score2,&p-score3); p=head=&a; for(;p!=NULL;p=p-next)printf(%3s%3s%3d%3d

22、%3dn,p-num,p-name,p-score1,p-score2,p-score3);2.先建立一个具有4个节点的动态链表,每个节点包括学号、姓名、2门课的成绩,从键盘输入学生的数据;然后输入一个学号,在链表中进行查找,找到则输出该节点中的所有信息;否则输出未找到。编程指导:注意动态内存分配函数的使用。#include stdafx.h#define NULL 0struct student char num10; char name20; int score1; int score2; struct student *next; ;int _tmain(int argc, _TCHAR

23、* argv) struct student a, b, c,d, *head, *p; char r10; int e; p=head=&a; a.next=&b; b.next=&c; c.next=&d; d.next=NULL;for(;p!=NULL;p=p-next) scanf(%s%s%d%d,p-num,p-name,&p-score1,&p-score2);printf(请输入您要查找的学生学号n);scanf(%s,r);p=head;for(;p!=NULL;p=p-next)e=strcmp(p-num,r);if(e=0) printf(%3s%3s%3d%3dn,

24、p-num,p-name,p-score1,p-score2); break; if(p=NULL) printf(未找到);实验81.有5个学生,每个学生有3门课的成绩,从键盘上输入上述数据(包括学号,姓名,成绩),计算出每个学生的平均成绩,连同学号、姓名、成绩一起存入文本文件。要求学号为整型,成绩为浮点型,其它为字符型)。编程思路:每个学生的数据是一个节点,用一个结构体变量来表示,或者采用具有3个元素的结构体数组来表示。每个结构体还要包括平均成绩的成员变量。以打开方式建立一个文本文件。 Struct student int num; char name10; float score3; f

25、loat average; ;#include stdafx.hstruct student int num;char name20;float s1;float s2;float s3; float average;int _tmain(int argc, _TCHAR* argv) struct student b5; int i; FILE *ff; ff=fopen(d:/bjj.txt,wb); for(i=0;i5;i+) scanf(%d%s%f%f%f,&bi.num,bi.name,&bi.s1,&bi.s2,&bi.s3);bi.average =(bi.s1+bi.s2+

26、bi.s3)/3; fwrite(b,38,5,ff); fclose(ff);return 0;2,读出1题文件中的内容,按照:学号、姓名、成绩、三门课平均成绩的格式打印出数据。编程思路:以读方式打开上面实验所存的文本文件,使用格式化读命令,按照定义的结构体变量形式读出文件中每个学生的数据,用循环读5次。#include stdafx.hstruct student int num;char name20;float s1;float s2;float s3; float average;int _tmain(int argc, _TCHAR* argv) struct student b2

27、=1,w,22,33,44,33,2,e,33,55,66,45; int i; FILE *ff; ff=fopen(d:bjj.txt,w+); for(i=0;i2;i+)fprintf(ff,%10d%10s%10f%10f%10f%10fn,bi.num,bi.name,bi.s1,bi.s2,bi.s3,bi.average); for(i=0;i2;i+)fscanf(ff,%10d%10s%10f%10f%10f%10fn,bi.num,bi.name,bi.s1,bi.s2,bi.s3,bi.average); fclose(ff); for(i=0;i2;i+)printf(%10d%10s%10f%10f%10f%10fn,bi.num,bi.name,bi.s1,bi.s2,bi.s3,bi.average);return 0;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com