计算机c语言例题

上传人:wuli****0220 文档编号:140536790 上传时间:2022-08-23 格式:DOC 页数:13 大小:139.50KB
收藏 版权申诉 举报 下载
计算机c语言例题_第1页
第1页 / 共13页
计算机c语言例题_第2页
第2页 / 共13页
计算机c语言例题_第3页
第3页 / 共13页
资源描述:

《计算机c语言例题》由会员分享,可在线阅读,更多相关《计算机c语言例题(13页珍藏版)》请在装配图网上搜索。

1、插入排序引例:写一个函数,将一个整型数x插入到由小到大排列的整型数组a0aN-1中,使得插入元素后的数组a0aN保持升序。void insert(int aN+1,int x) int i = N - 1; while (i = 0 & ai x) ai+1 = ai; i-; ai+1 = x;算法要点:将升序数组中大于x的所有元素向后挪动一个下标位置;循环退出时,下标i1位置为一空位置,正好是正确插入元素x的位置.插入排序算法:N元数组a0aN-1由小到大排序:第1步:将a1插入a0a1中,使得a0a1升序;第2步:将a2插入a0a2中,使得a0a2升序;第3步:将a3插入a0a3中,使得

2、a0a3升序;第i步:将ai插入a0ai中,使得a0ai升序;第N-1步:将aN-1插入a0aN-1中,使得a0aN-1升序;算法停止。思考:由大到小排序算法如何改动?#include stdio.h#define N 10void InsSort(int aN) /*N元数组插入排序*/ int i,j,x; for(i = 1;i = 0 & aj x) aj+1 = aj; j-; aj+1 = x; void main() int aN,i; for (i = 0;i N;i+) scanf(%d,&ai); InsSort(a); for (i = 0;i N;i+) printf(

3、%6d,ai);11、编写子函数:(1)用冒泡法将一个数组排成升序的函数-SUB1;(2)在升序数组中插入一个数,并且保持该数组仍为升序数组的函数-SUB2。 主函数:输入任意10个正整数给数组;调用SUB1对数组进行排序;从键盘输入一个正整数,调用SUB2将其插入该数组。#include #include void main() int i,k,a12=0; /a0 for no use void sub1(int b),sub2(int b,int k); clrscr(); printf(Please input 10 numbers:); for(i=1;i=10;i+) scanf(

4、%d,&ai); getchar(); sub1(a); for(i=1;i=10;i+) printf(na%d=%dn,i,ai); printf(nnplease input a number to be inserted into the array:); scanf(%d,&k); sub2(a,k); for(i=1;i=11;i+) printf(na%d=%dn,i,ai); puts(nAny key to exit!); getch(); void sub1(b) int b; int i,j,t; for (i=1;i10;i+) /the first one is al

5、ways the smallest for(j=i;jbj) t=bi; bi=bj; bj=t; void sub2(int b,int k) int i; for(i=10;i=1;i-) if(kbi) bi+1=bi; else bi+1=k; break; 16、某班有5个学生,三门课。分别编写3个函数实现以下要求: (1) 求各门课的平均分; (2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩; (3) 找出三门课平均成绩在85-90分的学生,并输出其学号和姓名 主程序输入5个学生的成绩,然后调用上述函数输出结果。#define SNUM 5 /*student n

6、umber*/ #define CNUM 3 /*course number*/ #include #include /*disp student info*/ void DispScore(char num6,char name20,float scoreCNUM) int i,j; printf(nnStudent Info and Score:n); for(i=0;iSNUM;i+) printf(%s ,numi); printf(%s ,namei); for(j=0;jCNUM;j+) printf(%8.2f,scoreij); printf(nn); /*calculate

7、all student average score*/ void CalAver(float scoreCNUM) float sum,aver; int i,j; for(i=0;iCNUM;i+) sum=0; for(j=0;jSNUM;j+) sum=sum+scoreji; aver=sum/SNUM; printf(Average score of course %d is %8.2fn,i+1,aver); /*Find student: two courses no pass*/ void FindNoPass(char num6,float scoreCNUM) int i,

8、j,n; printf(nTwo Course No Pass Students:n); for(i=0;iSNUM;i+) n=0; for(j=0;jCNUM;j+) if(scoreij=2) printf(%s ,numi); for(j=0;jCNUM;j+) if(scoreij60) printf(%8.2f,scoreij); printf(n); /*Find student: three courses 85-90*/ void FindGoodStud(char num6,char name20,float scoreCNUM) int i,j,n; printf(nSc

9、ore of three courses between 85 and 90:n); for(i=0;iSNUM;i+) n=0; for(j=0;j=85&scoreij=90) n+; if(n=3) printf(%s %sn,numi,namei); /*input student info*/ void main() char numSNUM6,nameSNUM20; /array num refers to student number float scoreSNUMCNUM; /and its length is 6 int i,j; clrscr();printf(nPleas

10、e input student num and score:n); for(i=0;iSNUM;i+) printf(nnStudent%d number: ,i+1); scanf(%s,numi); printf(nStudent%d name: ,i+1); scanf(%s,namei); printf(nStudent%d three scores: ,i+1); for(j=0;jCNUM;j+) scanf(%f,&scoreij); DispScore(num,name,score); CalAver(score); FindNoPass(num,score); FindGoo

11、dStud(num,name,score); getch(); 6、写出下列程序的输出结果_ #include long fac( int n ) if ( n=1 ) return 1; return n*fac(n-1);main() printf( 5!=%ldn, fac(5) );Answer: 【5!=120】7、假定建立了以下链表结构,如图所示。指针p与q指向2个不同的结点,t为与data同类型的数据变量,则交换2结点数据的语句为:t=p-data; _;和_;7. 1)【p-data=q-data】2)【q-data=t22、建立一个链表,每个结点包括:学号、姓名、性别、年龄,

12、输入一个学号,如果链表中的结点包括该学号,则输出该结点内容后,并将其结点删去。 #define LEN sizeof(struct stud_node) #include #include #include struct stud_record char StudNo6; char StudName10; char StudSex; /*M-Male F-Female*/ int StudAge; ; struct stud_node struct stud_record stud_mem; struct stud_node *next; ; /*Create Student Linear t

13、able*/ struct stud_node *create() struct stud_node *head,*p,*q; char vno6,vname10,vsex; int vage; head=NULL; while(1) printf(nPlease input a student recordnnNotNametSextAgenn); scanf(%s,vno); getchar(); if(strcmp(vno,0)=0) /when vno=0 to exit break; scanf(%s,vname); getchar(); scanf(%c%d,&vsex,&vage

14、); getchar(); p=(struct stud_node *)malloc(LEN); /allocate space to node p strcpy(p-stud_mem.StudNo,vno); strcpy(p-stud_mem.StudName,vname); p-stud_mem.StudSex=vsex; p-stud_mem.StudAge=vage; if(head=NULL) head=p; else q-next=p; /q is the previous node of p q=p; if(head!=NULL) q-next=NULL; /the last

15、node has no child return head; /*Find a student and If Found then Delete the node*/ struct stud_node *delete(struct stud_node *head,char no6) struct stud_node *p,*q; p=head; q=p; while(p) if(strcmp(p-stud_mem.StudNo,no)=0) /*Delete the node*/ if(p=head) /delete the first node head=p-next; else if(p-

16、next!=NULL) /delete the middle node q-next=p-next; else /delete the last node q-next=NULL; printf(ntt%st%st%ct%dn,p-stud_mem.StudNo,p-stud_mem.StudName, p-stud_mem.StudSex,p-stud_mem.StudAge); free(p); break; q=p; p=p-next; return head; /*Disp linear table content*/ void prn(struct stud_node *head)

17、struct stud_node *p; int i=1; p=head; printf(nRecordtNotNametSextAgen); while(p) printf(%3dt%st%st%ct%dn,i,p-stud_mem.StudNo,p-stud_mem.StudName, p-stud_mem.StudSex,p-stud_mem.StudAge); p=p-next; i+; /*main program here*/ void main() struct stud_node *head; char no6; clrscr(); head=create(); prn(hea

18、d); getch(); printf(nPlease input a studno to Find:); gets(no); head=delete(head,no); prn(head); getch(); 3、以下程序从文件“student.txt”读取学生的学号、姓名、平时成绩和考试成绩,从键盘上输入平时成绩在总成绩中所占比重,计算每个学生的总成绩(四舍五入为整数)后输出到屏幕上。文件的最后一行为0表示学生数据结束。设文件student.txt的内容为101 Zhao9558103 Qian7581105 Sun9991107 Li 80670运行时键盘输入:0.1则屏幕输出:101

19、Zhao 95 58 62103 Qian 75 81 80105 Sun 99 91 92107 Li 80 67 68源程序:#include void calc( FILE *fp, float x )int num, score1, score2;float score3;char name20;while ( !feof(fp) ) /* 文件还有未读数据时 */num = 0;fscanf( fp, %d%s%d%d, &num, name, &score1, &score2 );if ( num 0 ) /* 学生数据有效时 */score3 = score1 * x + sco

20、re2 * (1-x);/* 计算总成绩 */printf( %3d %-7s %3d %3d %3dn, num, name, score1, score2, _(1)_ ); /* 总成绩四舍五入为整数 */void main()FILE *fp;float x;fp = fopen( student.txt, r );if ( _(2)_ )/* 如果文件打开失败 */printf( File Open Error!n );return;scanf( %f, &x );calc( _(3)_ );/* 调用calc函数 */fclose( _(4)_ );/* 关闭文件 */3. 1)【(int)(score3+0.5)】【(int)(10*score3+5)/10】2)【fp=NULL】3)【fp,x】4)【fp】BC#includeint main(void)int x;scanf(%d,&x);if(x250)putchar(X);if(x250)putchar(Y);else putchar(X);有几种输出结果?X XX YStrlen

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


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