计算机c语言例题

上传人:jiups****uk12 文档编号:39440826 上传时间:2018-05-15 格式:DOC 页数:13 大小:163.50KB
返回 下载 相关 举报
计算机c语言例题_第1页
第1页 / 共13页
计算机c语言例题_第2页
第2页 / 共13页
计算机c语言例题_第3页
第3页 / 共13页
计算机c语言例题_第4页
第4页 / 共13页
计算机c语言例题_第5页
第5页 / 共13页
点击查看更多>>
资源描述

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

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

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

3、o use void sub1(int b),sub2(int b,int k); clrscr(); printf(“Please input 10 numbers:“); for(i=1;ibj) t=bi; bi=bj; bj=t; void sub2(int b,int k) int i; for(i=10;i=1;i-) if(k #include /*disp student info*/ void DispScore(char num6,char name20,float scoreCNUM) int i,j; printf(“nnStudent Info and Score:n

4、“); for(i=0;i=2) printf(“%s “,numi); for(j=0;j=85return 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=t 22、建立一个链表,每个结点包括:学号、姓名、性别、年龄,输入一个学号,如果链表中的结点包括 该学号,则输出该结点内容后,并将其结点删

5、去。 #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 table*/ struct stud_node *create() str

6、uct 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“, getchar(); p=(struct stud_node *)ma

7、lloc(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 node has no child return head; /*Find

8、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-next!=NULL) /delete the middle node q-

9、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) struct stud_node *p; int i=1; p=head

10、; 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(head); getch(); printf(“nPlease inp

11、ut a studno to Find:“); gets(no); head=delete(head,no); prn(head); getch(); 3、 以下程序从文件“student.txt”读取学生的学号、姓名、平时成绩和考试成绩,从键盘上输入平时成绩 在总成绩中所占比重,计算每个学生的总成绩(四舍五入为整数)后输出到屏幕上。文件的最后一行为 0表示学生数据结束。设文件student.txt的内容为 101 Zhao 95 58 103 Qian 75 81 105 Sun99 91 107 Li 80 67 0 运行时键盘输入:0.1 则屏幕输出: 101 Zhao 95 58 62

12、 103 Qian 75 81 80 105 Sun 99 91 92 107 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“, if ( num 0 ) /* 学生数据有效时 */ score3 = score1 * x + score2 * (1-x); /* 计算总成绩 */ print

13、f( “%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“, 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#include int main(void) int x; scanf(“%d“, if(x250)putchar(X); if(x250)putchar(Y); else putchar(X); 有几种输出结果? X XX YStrlen

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号