文档详情

计算机c语言例题

huo****ian
实名认证
店铺
DOC
139.51KB
约13页
文档ID:157902997
计算机c语言例题_第1页
1/13

插入排序引例:写一个函数,将一个整型数x插入到由小到大排列的整型数组a[0]~a[N-1]中,使得插入元素后的数组a[0]~a[N]保持升序void insert(int a[N+1],int x) {   int i = N - 1;    while (i >= 0 && a[i] > x) {       a[i+1] = a[i];      i--;    }   a[i+1] = x;}算法要点:将升序数组中大于x的所有元素向后挪动一个下标位置;循环退出时,下标i+1位置为一空位置,正好是正确插入元素x的位置.插入排序算法:N元数组a[0]~a[N-1]由小到大排序:第1步:将a[1]插入a[0]~a[1]中,使得a[0]~a[1]升序;第2步:将a[2]插入a[0]~a[2]中,使得a[0]~a[2]升序;第3步:将a[3]插入a[0]~a[3]中,使得a[0]~a[3]升序; …第i步:将a[i]插入a[0]~a[i]中,使得a[0]~a[i]升序;…第N-1步:将a[N-1]插入a[0]~a[N-1]中,使得a[0]~a[N-1]升序;算法停止思考:由大到小排序算法如何改动?#include "stdio.h"#define N 10void InsSort(int a[N]) { /*N元数组插入排序*/   int i,j,x;   for(i = 1;i < N;i++){      x = a[i];      j = i - 1;      while(j >= 0 && a[j] > x) {          a[j+1] = a[j];         j--;       }      a[j+1] = x;   }}void main() {   int a[N],i;   for (i = 0;i < N;i++)      scanf("%d",&a[i]);   InsSort(a);   for (i = 0;i < N;i++)       printf("%6d",a[i]);}11、编写子函数:(1)用冒泡法将一个数组排成升序的函数---SUB1;(2)在升序数组中插入一个数,并且保持该数组仍为升序数组的函数---SUB2。

主函数:①输入任意10个正整数给数组;②调用SUB1对数组进行排序;③从键盘输入一个正整数,调用SUB2将其插入该数组include #include void main() { int i,k,a[12]={0}; //a[0] 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("%d",&a[i]); getchar(); sub1(a); for(i=1;i<=10;i++) printf("\na[%d]=%d\n",i,a[i]); printf("\n\nplease input a number to be inserted into the array:"); scanf("%d",&k); sub2(a,k); for(i=1;i<=11;i++) printf("\na[%d]=%d\n",i,a[i]); puts("\nAny key to exit!"); getch(); } void sub1(b) int b[]; { int i,j,t; for (i=1;i<10;i++) //the first one is always the smallest for(j=i;j<=10;j++) if (b[i]>b[j]) { t=b[i]; b[i]=b[j]; b[j]=t; } } void sub2(int b[],int k) { int i; for(i=10;i>=1;i--) { if(k

分别编写3个函数实现以下要求: (1) 求各门课的平均分; (2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩; (3) 找出三门课平均成绩在85-90分的学生,并输出其学号和姓名 主程序输入5个学生的成绩,然后调用上述函数输出结果define SNUM 5 /*student number*/ #define CNUM 3 /*course number*/ #include #include /*disp student info*/ void DispScore(char num[][6],char name[][20],float score[][CNUM]) { int i,j; printf("\n\nStudent Info and Score:\n"); for(i=0;i=2) { printf("%s ",num[i]); for(j=0;j=85&&score[i][j]<=90) n++; if(n==3) printf("%s %s\n",num[i],name[i]); } } /*input student info*/ void main() { char num[SNUM][6],name[SNUM][20]; //array num refers to student number float score[SNUM][CNUM]; //and its length is 6 int i,j; clrscr();printf("\nPlease input student num and score:\n"); for(i=0;ilong fac( int n ){ if ( n==1 ) return 1; return n*fac(n-1);}main(){ printf( "5!=%ld\n", 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、建立一个链表,每个结点包括:学号、姓名、性别、年龄,输入一个学号,如果链表中的结点包括该学号,则输出该结点内容后,并将其结点删去 #define LEN sizeof(struct stud_node) #include #include #include struct stud_record { char StudNo[6]; char StudName[10]; 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() { struct stud_node *head,*p,*q; char vno[6],vname[10],vsex; int vage; head=NULL; while(1) { printf("\nPlease input a student record\n\nNo\tName\tSex\tAge\n\n"); scanf("%s",vno); getchar(); if(strcmp(vno,"0")==0) //when vno=="0" to exit break; scanf("%s",vname); getchar(); scanf("%c%d",&vsex,&vage); 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 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 no[6]) { 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->next=p->next; else //delete the last node q->next=NULL; } printf("\n\t\t%s\t%s\t%c\t%d\n",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; printf("\nRecord\tNo\tName\tSex\tAge\n"); while(p) { printf("%3d\t%s\t%s\t%c\t%d\n",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 no[6]; clrscr(); head=create(); prn(head); getch(); printf("\nPlease input a studno to Find:"); gets(no); head=delete(head,no); prn(head); getch(); } 3、以下程序从文件“student.txt”读取学生的学号、姓名、平时成绩和考试成绩,从键盘上输入平时成绩在总成绩中所占比重,计算每个学生的总成绩(四舍五入为整数)后输出到屏幕上。

文件的最后一行为0表示学生数据结束设文件student.txt的内容为101 Zhao 95 58103 Qian 75 81105 Sun 99 91107 Li 80 670运行时键盘输入:0.1则屏幕输出:101 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 name[20]; while ( !feof(fp) ) { /* 文件还有未读数据时 */ num = 0; fscanf( fp, "%d%s%d%d", &num, name, &score1, &score2 ); if ( num > 0 ) { /* 学生数据有效时 */ score3 = score1 * x + score2 * (1-x); /* 计算总成绩 */ printf( "%3d %-7s %3d %3d %3d\n", 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(x>250)putchar('X');if(x<250)putchar('Y');else putchar('X');}有几种输出结果?X XX YStrlen 。

下载提示
相关文档
正为您匹配相似的精品文档