100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 计算机二级C语言-A程序填空题

计算机二级C语言-A程序填空题

时间:2021-05-30 11:47:00

相关推荐

计算机二级C语言-A程序填空题

原文链接

/weixin_43323830/article/details/106313660

文章目录

前言1. 函数调用为指针型变量1-1 将行参a所指结构体变量中的数据1-2 将形参a所指结构体变量s中的数据进行修改1-3 将该学生的各科成绩都乘以一个系数2 矩阵贪吃蛇2. 矩阵左移2.1.矩阵外围元素顺时针旋转2.2 矩阵中元素的值按列向右移动一个位置3 文件读写3-1.按照学号从小到大排序3-2 将形参给定的字符串、整数、浮点数写到文本文件中3-4 将参数给定的字符串、整数、浮点数写到文本文件中3-5 将自然数1~10及其平方根3-6 调用函数fun将指定源文件中的内容3-7 从键盘输入若干行字符串4. 回文3-6 建立班级通讯录5.字符串按从长到短的顺序排序6.寻找与参数c相同的字符8.用-1作字符串输入结束的标志9.经过n次这样洗牌后的结果10.找出在行上最大,列上最小的元素11.统计形参 t所指的字符中数字字符出现的次数12.数组中前n个数据12.字符串中包含的单词个数13 链表13-1 找出链表各结点数据域中的最大值13-2 将不带头结点的单向链表逆置13-3 将带头结点的单向链表逆置13-4 将a、b、c三个结点链接成一个单向链表13-5 用函数指针指向要调用的函数13-6 在带头结点的单向链表中13-7 把形参x的值放入一个新结点13-8 每调用一次,输出链表结点中的数据21.所有整数中各位上数字之和为x14.函数...若x=2.5,函数值为12.18249415.用变量a统计大于0的个数16.存储学生的学号、姓名和三门课的成绩18.数字字符转换19.有一只狐狸和一只兔子22.偶数按原顺序依次存放23.数组中找出两科成绩最高的学生并返回其所在数组中的下标24.矩阵主对角线、负对角线对应元素交换25.下标为奇数的字符取出,并按ASCII码大小递增排序(选择排序法)26.其余字符串用字符*补齐27.计算前n项之和28.幻方29.调用随机函数产生20各互不相同的整数放在形参a所指数组中30.字符串转换成面值相同的整数31.每个数字字符之后插入一个*号32.按顺序打印出三个相邻的字母33 所指字符串数组中长度最长的字符串所在的行下标34 查找有不及格科目的学生35 每列元素的最大值36 字符串中最右边的n各字符串复制37 各数字出现的次数38 利用结构体变量存储了一名学生的信息39 数字字符移到所有非数字字符之后40 根据形参n计算并返回阶乘n!41 把形参a所知数组中的奇数按原顺序依次存放42 重写filename所指文件中最后一个学生的数据43 计算形参x所指数组中N个数的平均值44 调用随机函数产生20个互不相同的整数45 建立一个NxN的矩阵46 用函数指针指向要调用的函数52 将形参s所指字符串中所有数字字符顺序前移53 在形参ss所指字符串数组中54 逆置数组元素中的值55 参数xx的前10个元素已经按升序排列好

前言

难度等级:★☆☆☆☆~★★★★★★

1. 函数调用为指针型变量

1-1 将行参a所指结构体变量中的数据

修改前

#include <stdio.h>#include <string.h>struct student {long sno;char name[10];float score[3];};void fun( struct student *b){/**********found**********/b__1__ = 10004;/**********found**********/strcpy(b__2__, "LiJie");}void main(){struct student t={10002,"ZhangQi", 93, 85, 87};int i;printf("\n\nThe original data :\n");printf("\nNo: %ld Name: %s\nScores: ",t.sno, t.name);for (i=0; i<3; i++) printf("%6.2f ", t.score[i]);printf("\n");/**********found**********/fun(__3__);printf("\nThe data after modified :\n");printf("\nNo: %ld Name: %s\nScores: ",t.sno, t.name);for (i=0; i<3; i++) printf("%6.2f ", t.score[i]);printf("\n");}

修改后

#include <stdio.h>#include <string.h>struct student {long sno;char name[10];float score[3];};void fun( struct student *b){/**********found**********/b->sno = 10004;/**********found**********/strcpy(b->name, "LiJie");}void main(){struct student t={10002,"ZhangQi", 93, 85, 87};int i;printf("\n\nThe original data :\n");printf("\nNo: %ld Name: %s\nScores: ",t.sno, t.name);for (i=0; i<3; i++) printf("%6.2f ", t.score[i]);printf("\n");/**********found**********/fun(&t);printf("\nThe data after modified :\n");printf("\nNo: %ld Name: %s\nScores: ",t.sno, t.name);for (i=0; i<3; i++) printf("%6.2f ", t.score[i]);printf("\n");}

fun(&t);

函数fun的形参为指针型变量,故main中调用函数fun参数应为指针或者地址

1-2 将形参a所指结构体变量s中的数据进行修改

修改前

#include <stdio.h>#include <string.h>struct student {long sno;char name[10];float score[3];};/**********found**********/__1__ fun(struct student *a){int i;a->sno = 10002;strcpy(a->name, "LiSi");/**********found**********/for (i=0; i<3; i++) __2__ += 1;/**********found**********/return __3__ ;}void main(){struct student s={10001,"ZhangSan", 95, 80, 88}, *t;int i;printf("\n\nThe original data :\n");printf("\nNo: %ld Name: %s\nScores: ",s.sno, s.name);for (i=0; i<3; i++) printf("%6.2f ", s.score[i]);printf("\n");t = fun(&s);printf("\nThe data after modified :\n");printf("\nNo: %ld Name: %s\nScores: ",t->sno, t->name);for (i=0; i<3; i++) printf("%6.2f ", t->score[i]);printf("\n");}

修改后

#include <stdio.h>#include <string.h>struct student {long sno;char name[10];float score[3];};/**********found**********/struct student* fun(struct student *a){int i;a->sno = 10002;strcpy(a->name, "LiSi");/**********found**********/for (i=0; i<3; i++) a->score[i] += 1;/**********found**********/return a ;}void main(){struct student s={10001,"ZhangSan", 95, 80, 88}, *t;int i;printf("\n\nThe original data :\n");printf("\nNo: %ld Name: %s\nScores: ",s.sno, s.name);for (i=0; i<3; i++) printf("%6.2f ", s.score[i]);printf("\n");t = fun(&s);printf("\nThe data after modified :\n");printf("\nNo: %ld Name: %s\nScores: ",t->sno, t->name);for (i=0; i<3; i++) printf("%6.2f ", t->score[i]);printf("\n");}

注意到以下,故为结构指针变量

printf("\nNo: %ld Name: %s\nScores: ",t->sno, t->name);for (i=0; i<3; i++) printf("%6.2f ", t->score[i]);

1-3 将该学生的各科成绩都乘以一个系数

修改前

#include <stdio.h>typedef struct{int num;char name[9];float score[3];}STU;void show(STU tt){int i;printf("%d %s : ",tt.num,tt.name);for(i=0; i<3; i++)printf("%5.1f",tt.score[i]);printf("\n");}/**********found**********/void modify(___1___ *ss,float a){int i;for(i=0; i<3; i++)/**********found**********/ss->___2___ *=a;}void main( ){STU std={1,"Zhanghua",76.5,78.0,82.0 };float a;printf("\nThe original number and name and scores :\n");show(std);printf("\nInput a number : "); scanf("%f",&a);/**********found**********/modify(___3___,a);printf("\nA result of modifying :\n");show(std);}

修改后

#include <stdio.h>typedef struct{int num;char name[9];float score[3];}STU;void show(STU tt){int i;printf("%d %s : ",tt.num,tt.name);for(i=0; i<3; i++)printf("%5.1f",tt.score[i]);printf("\n");}/**********found**********/void modify(STU *ss,float a){int i;for(i=0; i<3; i++)/**********found**********/ss->score[i] *=a;}void main( ){STU std={1,"Zhanghua",76.5,78.0,82.0 };float a;printf("\nThe original number and name and scores :\n");show(std);printf("\nInput a number : "); scanf("%f",&a);/**********found**********/modify(&std,a);printf("\nA result of modifying :\n");show(std);}

2 矩阵贪吃蛇

2. 矩阵左移

修改前

#include <stdio.h>#define M 3#define N 5void fun(int (*a)[N],int k){int i,j,p,temp;/**********found**********/for(p=1; p<= __1__; p++)for(i=0; i<M; i++){temp=a[i][0];/**********found**********/for(j=0; j< __2__ ; j++) a[i][j]=a[i][j+1];/**********found**********/a[i][N-1]= __3__;}}void main( ){int x[M][N]={{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5} },i,j;printf("The array before moving:\n\n");for(i=0; i<M; i++){for(j=0; j<N; j++) printf("%3d",x[i][j]);printf("\n");}fun(x,2);printf("The array after moving:\n\n");for(i=0; i<M; i++){for(j=0; j<N; j++) printf("%3d",x[i][j]);printf("\n");}}

修改后

#include <stdio.h>#define M 3#define N 5void fun(int (*a)[N],int k){int i,j,p,temp;/**********found**********/for(p=1; p<= k; p++)for(i=0; i<M; i++){temp=a[i][0];/**********found**********/for(j=0; j< N-1 ; j++) a[i][j]=a[i][j+1]; /**********found**********/a[i][N-1]= temp;}}void main( ){int x[M][N]={{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5} },i,j;printf("The array before moving:\n\n");for(i=0; i<M; i++){for(j=0; j<N; j++) printf("%3d",x[i][j]);printf("\n");}fun(x,2);printf("The array after moving:\n\n");for(i=0; i<M; i++){for(j=0; j<N; j++) printf("%3d",x[i][j]);printf("\n");}}

外循环p数值为数组移动次数,故要求第k列左移,移动次数为k。注意矩阵N列,从0开始的。临时变量temp中存放数值为最左边数值,将其放置在最后。

2.1.矩阵外围元素顺时针旋转

修改前

#include <stdio.h>#define N 4void fun(int (*t)[N]){int j ,r[N];for(j=0; j<N; j++) r[j]=t[0][j];for(j=0; j<N; j++)/**********found**********/t[0][N-j-1]=t[j][___1___ ];for(j=0; j<N; j++)t[j][0]=t[N-1][j];/**********found**********/for(j=N-1; j>=0;___2___ )t[N-1][N-1-j]=t[j][N-1];for(j=N-1; j>=0; j--)/**********found**********/t[j][N-1]=r[___3___];}void main(){int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10}, i, j;printf("\nThe original array:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%2d ",t[i][j]);printf("\n");}fun(t);printf("\nThe result is:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%2d ",t[i][j]);printf("\n");}}

修改后

#include <stdio.h>#define N 4void fun(int (*t)[N]){int j ,r[N];for(j=0; j<N; j++) r[j]=t[0][j];for(j=0; j<N; j++)/**********found**********//*第一行元素t[0][N-j-1]第一列元素t[j][0]*/t[0][N-j-1]=t[j][0];for(j=0; j<N; j++)/*最后一行元素t[N-1][j]第一列元素t[j][0]*/t[j][0]=t[N-1][j];/**********found**********/for(j=N-1; j>=0;j-- )/*最后一列元素t[j][N-1]最后一行元素t[N-1][N-1-j]*/t[N-1][N-1-j]=t[j][N-1];for(j=N-1; j>=0; j--)/**********found**********/t[j][N-1]=r[j];}void main(){int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10}, i, j;printf("\nThe original array:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%2d ",t[i][j]);printf("\n");}fun(t);printf("\nThe result is:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%2d ",t[i][j]);printf("\n");}}

输出结果

**难度等级:**★★☆☆☆

学点啥

2.2 矩阵中元素的值按列向右移动一个位置

修改前

#include <stdio.h>#define N 4void fun(int (*t)[N]){int i, j, x;/**********found**********/for(i=0; i<___1___; i++){/**********found**********/x=t[i][___2___] ;for(j=N-1; j>=1; j--)t[i][j]=t[i][j-1];/**********found**********/t[i][___3___]=x;}}void main(){int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10}, i, j;printf("The original array:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%2d ",t[i][j]);printf("\n");}fun(t);printf("\nThe result is:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%2d ",t[i][j]);printf("\n");}}

修改后

#include <stdio.h>#define N 4void fun(int (*t)[N]){int i, j, x;/**********found**********/for(i=0; i<N; i++){/**********found**********/x=t[i][N-1] ;for(j=N-1; j>=1; j--)t[i][j]=t[i][j-1];/**********found**********/t[i][0]=x;}}void main(){int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10}, i, j;printf("The original array:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%2d ",t[i][j]);printf("\n");}fun(t);printf("\nThe result is:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%2d ",t[i][j]);printf("\n");}}

输出结果

**难度等级:**★★☆☆☆

学点啥

3 文件读写

3-1.按照学号从小到大排序

修改前

#include <stdio.h>#define N 5typedef struct student {long sno;char name[10];float score[3];} STU;void fun(char *filename){FILE *fp;int i, j;STU s[N], t;/**********found**********/fp = fopen(filename, __1__);fread(s, sizeof(STU), N, fp);fclose(fp);for (i=0; i<N-1; i++)for (j=i+1; j<N; j++)/**********found**********/if (s[i].sno __2__ s[j].sno){t = s[i]; s[i] = s[j]; s[j] = t; }fp = fopen(filename, "wb");/**********found**********/__3__(s, sizeof(STU), N, fp); fclose(fp);}void main(){STU t[N]={{10005,"ZhangSan", 95, 80, 88}, {10003,"LiSi", 85, 70, 78},{10002,"CaoKai", 75, 60, 88}, {10004,"FangFang", 90, 82, 87},{10001,"MaChao", 91, 92, 77}}, ss[N];int i,j;FILE *fp;fp = fopen("student.dat", "wb");fwrite(t, sizeof(STU), 5, fp);fclose(fp);printf("\n\nThe original data :\n\n");for (j=0; j<N; j++){printf("\nNo: %ld Name: %-8sScores: ",t[j].sno, t[j].name);for (i=0; i<3; i++) printf("%6.2f ", t[j].score[i]);printf("\n");}fun("student.dat");printf("\n\nThe data after sorting :\n\n");fp = fopen("student.dat", "rb");fread(ss, sizeof(STU), 5, fp);fclose(fp);for (j=0; j<N; j++){printf("\nNo: %ld Name: %-8sScores: ",ss[j].sno, ss[j].name);for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);printf("\n");}}

修改后

#include <stdio.h>#define N 5typedef struct student {long sno;char name[10];float score[3];} STU;void fun(char *filename){FILE *fp;int i, j;STU s[N], t;/**********found**********/fp = fopen(filename, "rb");fread(s, sizeof(STU), N, fp);fclose(fp);for (i=0; i<N-1; i++)for (j=i+1; j<N; j++)/**********found**********/if (s[i].sno > s[j].sno){t = s[i]; s[i] = s[j]; s[j] = t; }fp = fopen(filename, "wb");/**********found**********/fwrite(s, sizeof(STU), N, fp); fclose(fp);}void main(){STU t[N]={{10005,"ZhangSan", 95, 80, 88}, {10003,"LiSi", 85, 70, 78},{10002,"CaoKai", 75, 60, 88}, {10004,"FangFang", 90, 82, 87},{10001,"MaChao", 91, 92, 77}}, ss[N];int i,j;FILE *fp;fp = fopen("student.dat", "wb");fwrite(t, sizeof(STU), 5, fp);fclose(fp);printf("\n\nThe original data :\n\n");for (j=0; j<N; j++){printf("\nNo: %ld Name: %-8sScores: ",t[j].sno, t[j].name);for (i=0; i<3; i++) printf("%6.2f ", t[j].score[i]);printf("\n");}fun("student.dat");printf("\n\nThe data after sorting :\n\n");fp = fopen("student.dat", "rb");fread(ss, sizeof(STU), 5, fp);fclose(fp);for (j=0; j<N; j++){printf("\nNo: %ld Name: %-8sScores: ",ss[j].sno, ss[j].name);for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);printf("\n");}}

3-2 将形参给定的字符串、整数、浮点数写到文本文件中

修改前

#include <stdio.h>void fun(char *s, int a, double f){/**********found**********/__1__ fp;char ch;fp = fopen("file1.txt", "w");fprintf(fp, "%s %d %f\n", s, a, f);fclose(fp);fp = fopen("file1.txt", "r");printf("\nThe result :\n\n");ch = fgetc(fp);/**********found**********/while (!feof(__2__)) {/**********found**********/putchar(__3__); ch = fgetc(fp); }putchar('\n');fclose(fp);}void main(){char a[10]="Hello!"; int b=12345;double c= 98.76;fun(a,b,c);}

修改后

#include <stdio.h>void fun(char *s, int a, double f){/**********found**********/FILE* fp;char ch;fp = fopen("file1.txt", "w");fprintf(fp, "%s %d %f\n", s, a, f);fclose(fp);fp = fopen("file1.txt", "r");printf("\nThe result :\n\n");ch = fgetc(fp);/**********found**********/while (!feof(fp)) {/**********found**********/putchar(ch); ch = fgetc(fp); }putchar('\n');fclose(fp);}void main(){char a[10]="Hello!"; int b=12345;double c= 98.76;fun(a,b,c);}

3-4 将参数给定的字符串、整数、浮点数写到文本文件中

修改前

#include <stdio.h>#include <stdlib.h>void fun(char *s, int a, double f){/**********found**********/__1__ fp;char str[100], str1[100], str2[100];int a1;double f1;fp = fopen("file1.txt", "w");fprintf(fp, "%s %d %f\n", s, a, f);/**********found**********/__2__ ;fp = fopen("file1.txt", "r");/**********found**********/fscanf(__3__,"%s%s%s", str, str1, str2);fclose(fp);a1 = atoi(str1);f1 = atof(str2);printf("\nThe result :\n\n%s %d %f\n", str, a1, f1);}void main(){char a[10]="Hello!"; int b=12345;double c= 98.76;fun(a,b,c);}

修改后

#include <stdio.h>#include <stdlib.h>void fun(char *s, int a, double f){/**********found**********/FILE* fp;char str[100], str1[100], str2[100];int a1;double f1;fp = fopen("file1.txt", "w");fprintf(fp, "%s %d %f\n", s, a, f);/**********found**********/fclose(fp);fp = fopen("file1.txt", "r");/**********found**********/fscanf(fp,"%s%s%s", str, str1, str2);fclose(fp);a1 = atoi(str1);f1 = atof(str2);printf("\nThe result :\n\n%s %d %f\n", str, a1, f1);}void main(){char a[10]="Hello!"; int b=12345;double c= 98.76;fun(a,b,c);}

3-5 将自然数1~10及其平方根

修改前

#include <math.h>#include <stdio.h>int fun(char *fname ){FILE *fp;int i,n;float x;if((fp=fopen(fname, "w"))==NULL) return 0;for(i=1;i<=10;i++)/**********found**********/fprintf(___1___,"%d %f\n",i,sqrt((double)i));printf("\nSucceed!!\n");/**********found**********/___2___;printf("\nThe data in file :\n");/**********found**********/if((fp=fopen(___3___,"r"))==NULL)return 0;fscanf(fp,"%d%f",&n,&x);while(!feof(fp)){printf("%d %f\n",n,x); fscanf(fp,"%d%f",&n,&x); }fclose(fp);return 1;}void main(){char fname[]="myfile3.txt";fun(fname);}

修改后

#include <math.h>#include <stdio.h>int fun(char *fname ){FILE *fp;int i,n;float x;if((fp=fopen(fname, "w"))==NULL) return 0;for(i=1;i<=10;i++)/**********found**********/fprintf(fp,"%d %f\n",i,sqrt((double)i));printf("\nSucceed!!\n");/**********found**********/fclose(fp);printf("\nThe data in file :\n");/**********found**********/if((fp=fopen(fname,"r"))==NULL)return 0;fscanf(fp,"%d%f",&n,&x);while(!feof(fp)){printf("%d %f\n",n,x); fscanf(fp,"%d%f",&n,&x); }fclose(fp);return 1;}void main(){char fname[]="myfile3.txt";fun(fname);}

3-6 调用函数fun将指定源文件中的内容

修改前

#include <stdio.h>#include <stdlib.h>int fun(char *source, char *target){FILE *fs,*ft;char ch;/**********found**********/if((fs=fopen(source, ___1___))==NULL)return 0;if((ft=fopen(target, "w"))==NULL)return 0;printf("\nThe data in file :\n");ch=fgetc(fs);/**********found**********/while(!feof(___2___)){putchar( ch );/**********found**********/fputc(ch,___3___);ch=fgetc(fs);}fclose(fs); fclose(ft);printf("\n\n");return 1;}void main(){char sfname[20] ="myfile1",tfname[20]="myfile2";FILE *myf;int i;char c;myf=fopen(sfname,"w");printf("\nThe original data :\n");for(i=1; i<30; i++){c='A'+rand()%25;fprintf(myf,"%c",c); printf("%c",c); }fclose(myf);printf("\n\n");if (fun(sfname, tfname)) printf("Succeed!");else printf("Fail!");}

修改后

#include <stdio.h>#include <stdlib.h>int fun(char *source, char *target){FILE *fs,*ft;char ch;/**********found**********/if((fs=fopen(source, "r"))==NULL)return 0;if((ft=fopen(target, "w"))==NULL)return 0;printf("\nThe data in file :\n");ch=fgetc(fs);/**********found**********/while(!feof(fs)){putchar( ch );/**********found**********/fputc(ch,ft);ch=fgetc(fs);}fclose(fs); fclose(ft);printf("\n\n");return 1;}void main(){char sfname[20] ="myfile1",tfname[20]="myfile2";FILE *myf;int i;char c;myf=fopen(sfname,"w");printf("\nThe original data :\n");for(i=1; i<30; i++){c='A'+rand()%25;fprintf(myf,"%c",c); printf("%c",c); }fclose(myf);printf("\n\n");if (fun(sfname, tfname)) printf("Succeed!");else printf("Fail!");}

3-7 从键盘输入若干行字符串

修改前

#include <stdio.h>#include <string.h>#include <stdlib.h>void WriteText(FILE *);void ReadText(FILE *);void main(){FILE *fp;if((fp=fopen("myfile4.txt","w"))==NULL){printf(" open fail!!\n"); exit(0);}WriteText(fp);fclose(fp);if((fp=fopen("myfile4.txt","r"))==NULL){printf(" open fail!!\n"); exit(0);}ReadText(fp);fclose(fp);}/**********found**********/void WriteText(FILE ___1___){char str[81];printf("\nEnter string with -1 to end :\n");gets(str);while(strcmp(str,"-1")!=0) {/**********found**********/fputs(___2___,fw); fputs("\n",fw);gets(str);}}void ReadText(FILE *fr){char str[81];printf("\nRead file and output to screen :\n");fgets(str,81,fr);while( !feof(fr) ) {/**********found**********/printf("%s",___3___);fgets(str,81,fr);}}

修改后

#include <stdio.h>#include <string.h>#include <stdlib.h>void WriteText(FILE *);void ReadText(FILE *);void main(){FILE *fp;if((fp=fopen("myfile4.txt","w"))==NULL){printf(" open fail!!\n"); exit(0);}WriteText(fp);fclose(fp);if((fp=fopen("myfile4.txt","r"))==NULL){printf(" open fail!!\n"); exit(0);}ReadText(fp);fclose(fp);}/**********found**********/void WriteText(FILE *fw){char str[81];printf("\nEnter string with -1 to end :\n");gets(str);while(strcmp(str,"-1")!=0) {/**********found**********/fputs(str,fw); fputs("\n",fw);gets(str);}}void ReadText(FILE *fr){char str[81];printf("\nRead file and output to screen :\n");fgets(str,81,fr);while( !feof(fr) ) {/**********found**********/printf("%s",str);fgets(str,81,fr);}}

4. 回文

#include <stdio.h>#include <string.h>#include <ctype.h>int fun(char *s){char *lp,*rp;/**********found**********/lp= __1__ ;rp=s+strlen(s)-1;while((toupper(*lp)==toupper(*rp)) && (lp<rp) ) {/**********found**********/lp++; rp __2__ ; }/**********found**********/if(lp<rp) __3__ ;else return 1;}void main(){char s[81];printf("Enter a string: "); scanf("%s",s);if(fun(s)) printf("\n\"%s\" is a Palindrome.\n\n",s);else printf("\n\"%s\" isn't a Palindrome.\n\n",s);}

修改后

#include <stdio.h>#include <string.h>#include <ctype.h>int fun(char *s){char *lp,*rp;/**********found**********/lp= s ;rp=s+strlen(s)-1;while((toupper(*lp)==toupper(*rp)) && (lp<rp) ) {/**********found**********/lp++; rp--;};/**********found**********/if(lp<rp) return 0;else return 1;}void main(){char s[81];printf("Enter a string: "); scanf("%s",s);if(fun(s)) printf("\n\"%s\" is a Palindrome.\n\n",s);else printf("\n\"%s\" isn't a Palindrome.\n\n",s);}

考察知识点

(1)指针起始地址,函数fun中,对变量lp和rp可知,lp指向形参s起始地址,rp指向s终止地址,故填空1应为s;

(2)每循环一次头指针++,尾指针–。rp指向尾指针,故得

(3)逻辑判断

if(lp<rp) __3__ ;else return 1;

可得return 0;

实际上,lp与rp相等时,可推得回文

void main(){char s[81];printf("Enter a string: "); scanf("%s",s);if(fun(s)) printf("\n\"%s\" is a Palindrome.\n\n",s);else printf("\n\"%s\" isn't a Palindrome.\n\n",s);}

学习一下:

toupper()将小写字母转换为大写字母

返回值:

如果转换成功,那么返回与 c 对应的大写字母;如果转换失败,那么直接返回 c(值未变)。

3-6 建立班级通讯录

修改前

#include <stdio.h>#include <stdlib.h>#define N 5typedef struct{int num;char name[10];char tel[10];}STYPE;void check();/**********found**********/int fun(___1___ *std){/**********found**********/___2___ *fp;int i;if((fp=fopen("myfile5.dat","wb"))==NULL)return(0);printf("\nOutput data to file !\n");for(i=0; i<N; i++)/**********found**********/fwrite(&std[i], sizeof(STYPE), 1, ___3___);fclose(fp);return (1);}void main(){STYPE s[10]={{1,"aaaaa","111111"},{1,"bbbbb","222222"},{1,"ccccc","333333"},{1,"ddddd","444444"},{1,"eeeee","555555"}};int k;k=fun(s);if (k==1){printf("Succeed!"); check(); }elseprintf("Fail!");}void check(){FILE *fp;int i;STYPE s[10];if((fp=fopen("myfile5.dat","rb"))==NULL){printf("Fail !!\n"); exit(0); }printf("\nRead file and output to screen :\n");printf("\n num nametel\n");for(i=0; i<N; i++){fread(&s[i],sizeof(STYPE),1, fp);printf("%6d %s %s\n",s[i].num,s[i].name,s[i].tel);}fclose(fp);}

修改后

#include <stdio.h>#include <stdlib.h>#define N 5typedef struct{int num;char name[10];char tel[10];}STYPE;void check();/**********found**********/int fun(STYPE *std){/**********found**********/FILE *fp;int i;if((fp=fopen("myfile5.dat","wb"))==NULL)return(0);printf("\nOutput data to file !\n");for(i=0; i<N; i++)/**********found**********/fwrite(&std[i], sizeof(STYPE), 1, fp);fclose(fp);return (1);}void main(){STYPE s[10]={{1,"aaaaa","111111"},{1,"bbbbb","222222"},{1,"ccccc","333333"},{1,"ddddd","444444"},{1,"eeeee","555555"}};int k;k=fun(s);if (k==1){printf("Succeed!"); check(); }elseprintf("Fail!");}void check(){FILE *fp;int i;STYPE s[10];if((fp=fopen("myfile5.dat","rb"))==NULL){printf("Fail !!\n"); exit(0); }printf("\nRead file and output to screen :\n");printf("\n num nametel\n");for(i=0; i<N; i++){fread(&s[i],sizeof(STYPE),1, fp);printf("%6d %s %s\n",s[i].num,s[i].name,s[i].tel);}fclose(fp);}

5.字符串按从长到短的顺序排序

修改前

#include <stdio.h>#include <string.h>#define N 5#define M 8void fun(char (*ss)[M]){char *ps[N],*tp; int i,j,k;for(i=0; i<N; i++) ps[i]=ss[i];for(i=0; i<N-1; i++) {/**********found**********/k= __1__ ;for(j=i+1; j<N; j++)/**********found**********/if(strlen(ps[k]) < strlen(__2__) ) k=j;/**********found**********/tp=ps[i]; ps[i]=ps[k]; ps[k]= __3__ ;}printf("\nThe string after sorting by length:\n\n");for(i=0; i<N; i++) puts(ps[i]);}void main(){char ch[N][M]={"red","green","blue","yellow","black"};int i;printf("\nThe original string\n\n");for(i=0;i<N;i++)puts(ch[i]); printf("\n");fun(ch);}

修改后

#include <stdio.h>#include <string.h>#define N 5#define M 8void fun(char (*ss)[M]){char *ps[N],*tp; int i,j,k;for(i=0; i<N; i++) ps[i]=ss[i];for(i=0; i<N-1; i++) {/**********found**********/k= i ;for(j=i+1; j<N; j++)/**********found**********/if(strlen(ps[k]) < strlen(ps[j]) ) k=j;/**********found**********/tp=ps[i]; ps[i]=ps[k]; ps[k]= tp;}printf("\nThe string after sorting by length:\n\n");for(i=0; i<N; i++) puts(ps[i]);}void main(){char ch[N][M]={"red","green","blue","yellow","black"};int i;printf("\nThe original string\n\n");for(i=0;i<N;i++)puts(ch[i]); printf("\n");fun(ch);}

(1)外循环每循环一次,k保存当前i值;

(2)内循环比较i+1后面字符串长度;

(3)经典的交换两个变量。

temp = a; a = b; b = temp;

temp = a;由C语言赋值语句可得,把a的值赋给temp

6.寻找与参数c相同的字符

修改前

#include <stdio.h>void fun(char *s, char c){int i, j, n;/**********found**********/for(i=0; s[i]!=___1___ ; i++)if(s[i]==c){/**********found**********/n=___2___ ;while(s[i+1+n]!='\0') n++;for(j=i+n+1; j>i; j--) s[j+1]=s[j];/**********found**********/s[j+1]=___3___ ;i=i+1;}}void main(){char s[80]="baacda", c;printf("\nThe string: %s\n",s);printf("\nInput a character: "); scanf("%c",&c);fun(s,c);printf("\nThe result is: %s\n",s);}

修改后

#include <stdio.h>void fun(char *s, char c){int i, j, n;/**********found**********/for(i=0; s[i]!= '\0'; i++)if(s[i]==c){/**********found**********/n=0 ;while(s[i+1+n]!='\0') n++;for(j=i+n+1; j>i; j--) s[j+1]=s[j];/**********found**********/s[j+1]= c;i=i+1;}}void main(){char s[80]="baacda", c;printf("\nThe string: %s\n",s);printf("\nInput a character: "); scanf("%c",&c);fun(s,c);printf("\nThe result is: %s\n",s);}

考察知识点

字符串结束标识’\0’;变量赋初值;数组元素赋值

void fun(char *s, char c){int i, j, n;/**********found**********/for(i=0; s[i]!= '\0'; i++)/*判断是否到达字符串结尾,即是否'\0'*/if(s[i]==c){/**********found**********/n=0 ;/*while 语句确定字符串长度*/while(s[i+1+n]!='\0') n++;for(j=i+n+1; j>i; j--) s[j+1]=s[j];/**********found**********//*找到与参数c相同字符,在后面插入相同的字符*/s[j+1]= c;i=i+1;}}

下列代码图示为:

i=0if(s[i]==c)

for(j=i+n+1; j>i; j--) s[j+1]=s[j];

s[j+1]= c;

8.用-1作字符串输入结束的标志

修改前

#include <stdio.h>#include <string.h>#include <stdlib.h>void WriteText(FILE *);void ReadText(FILE *);void main(){FILE *fp;if((fp=fopen("myfile4.txt","w"))==NULL){printf(" open fail!!\n"); exit(0);}WriteText(fp);fclose(fp);if((fp=fopen("myfile4.txt","r"))==NULL){printf(" open fail!!\n"); exit(0);}ReadText(fp);fclose(fp);}/**********found**********/void WriteText(FILE ___1___){char str[81];printf("\nEnter string with -1 to end :\n");gets(str);while(strcmp(str,"-1")!=0) {/**********found**********/fputs(___2___,fw); fputs("\n",fw);gets(str);}}void ReadText(FILE *fr){char str[81];printf("\nRead file and output to screen :\n");fgets(str,81,fr);while( !feof(fr) ) {/**********found**********/printf("%s",___3___);fgets(str,81,fr);}}

修改后

#include <stdio.h>#include <string.h>#include <stdlib.h>void WriteText(FILE *);void ReadText(FILE *);void main(){FILE *fp;if((fp=fopen("myfile4.txt","w"))==NULL){printf(" open fail!!\n"); exit(0);}WriteText(fp);fclose(fp);if((fp=fopen("myfile4.txt","r"))==NULL){printf(" open fail!!\n"); exit(0);}ReadText(fp);fclose(fp);}/**********found**********/void WriteText(FILE *fw){char str[81];printf("\nEnter string with -1 to end :\n");gets(str);while(strcmp(str,"-1")!=0) {/**********found**********/fputs(str,fw); fputs("\n",fw);gets(str);}}void ReadText(FILE *fr){char str[81];printf("\nRead file and output to screen :\n");fgets(str,81,fr);while( !feof(fr) ) {/**********found**********/printf("%s",str);fgets(str,81,fr);}}

考察知识点

文件指针;fputs向指定文件写入字符串:fputs(字符串,文件指针)

9.经过n次这样洗牌后的结果

修改前

#include <stdio.h>void fun( int a[55], int n ){int i, k ;/**********found**********/int __(1)__[55];for (i=0; i<n; i++) {for (k=1; k<= 27; k++) {b[ 2*k-1 ] = a[k];/**********found**********/b[ __[2]___* k ] = a[k+27];}for (k=1; k<=54; k++)/**********found**********/a[k]=___(3)___;}}void main( ){int m, a[55],i;for (i=1; i<55; i++) a[i]= i;printf("请输入洗牌次数 : "); scanf("%d", &m);fun(a, m);for (i=1; i<55; i++) printf("%d,",a[i]);printf("\n");}

修改后

#include <stdio.h>void fun( int a[55], int n ){int i, k ;/**********found**********/int b[55];for (i=0; i<n; i++) {for (k=1; k<= 27; k++) {b[ 2*k-1 ] = a[k];/**********found**********/b[ 2* k ] = a[k+27];}for (k=1; k<=54; k++)/**********found**********/a[k]= b[k];}}void main( ){int m, a[55],i;for (i=1; i<55; i++) a[i]= i;printf("请输入洗牌次数 : "); scanf("%d", &m);fun(a, m);for (i=1; i<55; i++) printf("%d,",a[i]);printf("\n");}

10.找出在行上最大,列上最小的元素

修改前

#include <stdio.h>#define M 3#define N 4void fun(int (*a)[N]){int i=0,j,find=0,rmax,c,k;while( (i<M) && (!find)){rmax=a[i][0]; c=0;for(j=1; j<N; j++)if(rmax<a[i][j]) {/**********found**********/rmax=a[i][j]; c= __1__ ; }find=1; k=0;while(k<M && find) {/**********found**********/if (k!=i && a[k][c]<=rmax) find= __2__ ;k++;}if(find) printf("find: a[%d][%d]=%d\n",i,c,a[i][c]);/**********found**********/__3__ ;}if(!find) printf("not found!\n");}void main(){int x[M][N],i,j;printf("Enter number for array:\n");for(i=0; i<M; i++)for(j=0; j<N; j++) scanf("%d",&x[i][j]);printf("The array:\n");for(i=0; i<M; i++){for(j=0; j<N; j++) printf("%3d",x[i][j]);printf("\n\n");}fun(x);}

修改后

#include <stdio.h>#define M 3#define N 4void fun(int (*a)[N]){int i=0,j,find=0,rmax,c,k;while( (i<M) && (!find)){rmax=a[i][0]; c=0;for(j=1; j<N; j++)if(rmax<a[i][j]) {/**********found**********/rmax=a[i][j]; c= j; }find=1; k=0;while(k<M && find) {/**********found**********/if (k!=i && a[k][c]<=rmax) find= 0 ;k++;}if(find) printf("find: a[%d][%d]=%d\n",i,c,a[i][c]);/**********found**********/i++;}if(!find) printf("not found!\n");}void main(){int x[M][N],i,j;printf("Enter number for array:\n");for(i=0; i<M; i++)for(j=0; j<N; j++) scanf("%d",&x[i][j]);printf("The array:\n");for(i=0; i<M; i++){for(j=0; j<N; j++) printf("%3d",x[i][j]);printf("\n\n");}fun(x);}

(1)找出行上最大的数,并将列数j保存在c

(2)该数非列中最小find=0

(3)i为while控制变量,循环自加1

11.统计形参 t所指的字符中数字字符出现的次数

修改前

#include<stdio.h>void fun(char *s, int *t){int i, n;n=0;/**********found**********/for(i=0; ___1___ !=0; i++)/**********found**********/if(s[i]>='0'&&s[i]<= ___2___ ) n++;/**********found**********/___3___ ;}void main(){char s[80]="abcdef35adgh3kjsdf7";int t;printf("\nThe original string is : %s\n",s);fun(s,&t);printf("\nThe result is : %d\n",t);}

修改后

#include<stdio.h>void fun(char *s, int *t){int i, n;n=0;/**********found**********/for(i=0; s[i] !=0; i++)/**********found**********/if(s[i]>='0'&&s[i]<= '9' ) n++;/**********found**********/*t = n;}void main(){char s[80]="abcdef35adgh3kjsdf7";int t;printf("\nThe original string is : %s\n",s);fun(s,&t);printf("\nThe result is : %d\n",t);}

运行结果

函数fun类型为void,无法返回值;

#include<stdio.h>int fun(char *s, int *t){int i, n;n=0;/**********found**********/for(i=0; s[i] !=0; i++)/**********found**********/if(s[i]>='0'&&s[i]<= '9' ) n++;/**********found**********/return n;//函数fun类型为void,无法返回值;}void main(){char s[80]="abcdef35adgh3kjsdf7";int t;printf("\nThe original string is : %s\n",s);fun(s,&t);printf("\nThe result is : %d\n",t);}

运行结果(有误)

程序设计题 第7题 同样问题

可做如下修改,函数定义为int fun();即可使用return n;

#include<stdio.h>int fun(char *s, int *t){int i, n;n=0;/**********found**********/for(i=0; s[i] !=0; i++)/**********found**********/if(s[i]>='0'&&s[i]<= '9' ) n++;/**********found**********/*t = n;}int fun1(char *s){int i, n;n = 0;/**********found**********/for (i = 0; s[i] != 0; i++)/**********found**********/if (s[i] >= '0'&&s[i] <= '9') n++;/**********found**********/return n;}void main(){char s[80]="abcdef35adgh3kjsdf7";int t;printf("\nThe original string is : %s\n",s);//fun(s,&t);printf("\nThe result is : %d\n", fun1(s));}

运行结果

12.数组中前n个数据

修改前

#include <stdio.h>#include <stdlib.h>void fun( int *a, int n ){int i;for(i=0; i<n; i++){/**********found**********/if( ___1___==0 )/**********found**********/printf("___2___");/**********found**********/printf("%d ",___3___);}}void main(){int a[100]={0}, i,n;n=22;for(i=0; i<n;i++) a[i]=rand()%21;fun( a, n);printf("\n");}

修改后

#include <stdio.h>#include <stdlib.h>void fun( int *a, int n ){int i;for(i=0; i<n; i++){/**********found**********/if( (i%5)==0 )/**********found**********/printf("\n");/**********found**********/printf("%d ",a[i]);}}void main(){int a[100]={0}, i,n;n=22;for(i=0; i<n;i++) a[i]=rand()%21;fun( a, n);printf("\n");}

12.字符串中包含的单词个数

修改前

#include <stdio.h>int fun(char *s){int n=0, flag=0;while(*s!='\0'){if(*s!=' ' && flag==0) {/**********found**********/__1__ ; flag=1;}/**********found**********/if (*s==' ') flag= __2__ ;/**********found**********/__3__ ;}return n;}void main(){char str[81]; int n;printf("\nEnter a line text:\n"); gets(str);n=fun(str);printf("\nThere are %d words in this text.\n\n",n);}

修改后

#include <stdio.h>int fun(char *s){int n=0, flag=0;while(*s!='\0'){if(*s!=' ' && flag==0) {/**********found**********/n++ ; flag=1;}/**********found**********/if (*s==' ') flag= 0 ;/**********found**********/s++ ;}return n;}void main(){char str[81]; int n;printf("\nEnter a line text:\n"); gets(str);n=fun(str);printf("\nThere are %d words in this text.\n\n",n);}

单词非空格且flag为0(flag初始值为0)时断定发现新单词,计数+1,flag置1;

单词空格flag置1

13 链表

13-1 找出链表各结点数据域中的最大值

修改前

#include <stdio.h>#include <stdlib.h>#pragma warning (disable:4996)struct list{int data;struct list *next;};struct list *createlist(int data[], int n){struct list *head = 0, *p, *q;int i;head = (struct list *) malloc(sizeof(struct list));head->data = data[0];p = q = head;for(i=1; i<n; i++){p = (struct list *) malloc(sizeof(struct list));p->data = data[i]; q->next = p; q = p;}p->next = NULL;return head;}/**********found**********/int func(___1___ head){int pmax = head->data;struct list *p = head->next;while(p != NULL){if(p->data > pmax) pmax = p->data;/**********found**********/p = ___2___;}/**********found**********/___3___}void main(){int data[] = {123, 21, 65, 789, 32, 310, 671, 651, 81, 101}, pmax;struct list *head;head = createlist(data, 10);pmax = func(head);printf("Max=%d\n", pmax);}

修改后

#include <stdio.h>#include <stdlib.h>#pragma warning (disable:4996)struct list{int data;struct list *next;};struct list *createlist(int data[], int n){struct list *head = 0, *p, *q;int i;head = (struct list *) malloc(sizeof(struct list));head->data = data[0];p = q = head;for(i=1; i<n; i++){p = (struct list *) malloc(sizeof(struct list));p->data = data[i]; q->next = p; q = p;}p->next = NULL;return head;}/**********found**********/int func(struct list* head){int pmax = head->data;struct list *p = head->next;while(p != NULL){if(p->data > pmax) pmax = p->data;/**********found**********/p = p->next;}/**********found**********/return pmax;}void main(){int data[] = {123, 21, 65, 789, 32, 310, 671, 651, 81, 101}, pmax;struct list *head;head = createlist(data, 10);pmax = func(head);printf("Max=%d\n", pmax);}

程序定义了结构体类型list,用来作为链表的结点类型,它包含 两个成员:data数据成员,next指针成员,func〇函数参数为链 表的头结点指针,pmax用来存放最大值,通过while循环遍历 整个链表,在遍历的过程中,榕当前结点的data与pmax比较, 若pmax小于—前结点的data,则使用—前结点的data更新 pmax, 最 后 将pmax的值作为函数返回值返回。

13-2 将不带头结点的单向链表逆置

修改前

#include <stdio.h>#include <stdlib.h>#define N 5typedef struct node {int data;struct node *next;} NODE;/**********found**********/__1__ * fun(NODE *h){NODE *p, *q, *r;p = h;if (p == NULL)return NULL;q = p->next;p->next = NULL;while (q){/**********found**********/r = q->__2__;q->next = p;p = q;/**********found**********/q = __3__ ;}return p;}NODE *creatlist(int a[]){NODE *h,*p,*q; int i;h=NULL;for(i=0; i<N; i++){q=(NODE *)malloc(sizeof(NODE));q->data=a[i];q->next = NULL;if (h == NULL) h = p = q;else {p->next = q; p = q; }}return h;}void outlist(NODE *h){NODE *p;p=h;if (p==NULL) printf("The list is NULL!\n");else{printf("\nHead ");do{printf("->%d", p->data); p=p->next; }while(p!=NULL);printf("->End\n");}}void main(){NODE *head;int a[N]={2,4,6,8,10};head=creatlist(a);printf("\nThe original list:\n");outlist(head);head=fun(head);printf("\nThe list after inverting :\n");outlist(head);}

修改后

#include <stdio.h>#include <stdlib.h>#define N 5typedef struct node {int data;struct node *next;} NODE;/**********found**********/NODE * fun(NODE *h){NODE *p, *q, *r;p = h;if (p == NULL)return NULL;q = p->next;p->next = NULL;while (q){/**********found**********/r = q->next;q->next = p;p = q;/**********found**********/q = r;}return p;}NODE *creatlist(int a[]){NODE *h,*p,*q; int i;h=NULL;for(i=0; i<N; i++){q=(NODE *)malloc(sizeof(NODE));q->data=a[i];q->next = NULL;if (h == NULL) h = p = q;else {p->next = q; p = q; }}return h;}void outlist(NODE *h){NODE *p;p=h;if (p==NULL) printf("The list is NULL!\n");else{printf("\nHead ");do{printf("->%d", p->data); p=p->next; }while(p!=NULL);printf("->End\n");}}void main(){NODE *head;int a[N]={2,4,6,8,10};head=creatlist(a);printf("\nThe original list:\n");outlist(head);head=fun(head);printf("\nThe list after inverting :\n");outlist(head);}

(1)定义函数返回类型(3)q指针向后移

13-3 将带头结点的单向链表逆置

修改前

#include <stdio.h>#include <stdlib.h>#define N 5typedef struct node {int data;struct node *next;} NODE;void fun(NODE *h){NODE *p, *q, *r;/**********found**********/p = h->__1__;/**********found**********/if (p==__2__) return;q = p->next;p->next = NULL;while (q){r = q->next; q->next = p;/**********found**********/p = q;q = __3__;}h->next = p;}NODE *creatlist(int a[]){NODE *h,*p,*q; int i;h = (NODE *)malloc(sizeof(NODE));h->next = NULL;for(i=0; i<N; i++){q=(NODE *)malloc(sizeof(NODE));q->data=a[i];q->next = NULL;if (h->next == NULL) h->next = p = q;else {p->next = q; p = q; }}return h;}void outlist(NODE *h){NODE *p;p = h->next;if (p==NULL) printf("The list is NULL!\n");else{printf("\nHead ");do{printf("->%d", p->data); p=p->next; }while(p!=NULL);printf("->End\n");}}void main(){NODE *head;int a[N]={2,4,6,8,10};head=creatlist(a);printf("\nThe original list:\n");outlist(head);fun(head);printf("\nThe list after inverting :\n");outlist(head);}

修改后

#include <stdio.h>#include <stdlib.h>#define N 5typedef struct node {int data;struct node *next;} NODE;void fun(NODE *h){NODE *p, *q, *r;/**********found**********/p = h->next;/**********found**********/if (p==NULL) return;q = p->next;p->next = NULL;while (q){r = q->next; q->next = p;/**********found**********/p = q;q = r;}h->next = p;}NODE *creatlist(int a[]){NODE *h,*p,*q; int i;h = (NODE *)malloc(sizeof(NODE));h->next = NULL;for(i=0; i<N; i++){q=(NODE *)malloc(sizeof(NODE));q->data=a[i];q->next = NULL;if (h->next == NULL) h->next = p = q;else {p->next = q; p = q; }}return h;}void outlist(NODE *h){NODE *p;p = h->next;if (p==NULL) printf("The list is NULL!\n");else{printf("\nHead ");do{printf("->%d", p->data); p=p->next; }while(p!=NULL);printf("->End\n");}}void main(){NODE *head;int a[N]={2,4,6,8,10};head=creatlist(a);printf("\nThe original list:\n");outlist(head);fun(head);printf("\nThe list after inverting :\n");outlist(head);}

13-4 将a、b、c三个结点链接成一个单向链表

修改前

#include <stdio.h>typedef struct list{char data;struct list *next;} Q;void fun( Q *pa, Q *pb, Q *pc){Q *p;/**********found**********/pa->next=___1___;pb->next=pc;p=pa;while( p ){/**********found**********/printf(" %c",____2_____);/**********found**********/p=____3____;}printf("\n");}void main(){Q a, b, c;a.data='E'; b.data='F'; c.data='G'; c.next=NULL;fun( &a, &b, &c );}

修改后

#include <stdio.h>typedef struct list{char data;struct list *next;} Q;void fun( Q *pa, Q *pb, Q *pc){Q *p;/**********found**********/pa->next=pb;pb->next=pc;p=pa;while( p ){/**********found**********/printf(" %c", p->data);/**********found**********/p=p->next;}printf("\n");}void main(){Q a, b, c;a.data='E'; b.data='F'; c.data='G'; c.next=NULL;fun( &a, &b, &c );}

**难度等级:**★★☆☆☆

学点啥

链表中将三个结点连接在一起,将pa的指针域指向pb,pb的指针域指向pc;输出链表结点后的数据,表示为p->p.data;指针后移,指向下一个结点,也即p=p->next

13-5 用函数指针指向要调用的函数

修改前

#include <stdio.h>double f1(double x){return x*x; }double f2(double x, double y){return x*y; }double fun(double a, double b){/**********found**********/__1__ (*f)();double r1, r2;/**********found**********/f = __2__ ; r1 = f(a);/**********found**********/f = __3__ ; r2 = (*f)(a, b);return r1 + r2;}void main(){double x1=5, x2=3, r;r = fun(x1, x2);printf("\nx1=%f, x2=%f, x1*x1+x1*x2=%f\n",x1, x2, r);}

修改后

#include <stdio.h>double f1(double x){return x*x; }double f2(double x, double y){return x*y; }double fun(double a, double b){/**********found**********/double (*f)();double r1, r2;/**********found**********/f = f1 ; r1 = f(a);/**********found**********/f = f2;r2 = (*f)(a, b);return r1 + r2;}void main(){double x1=5, x2=3, r;r = fun(x1, x2);printf("\nx1=%f, x2=%f, x1*x1+x1*x2=%f\n",x1, x2, r);}

学点啥

13-6 在带头结点的单向链表中

修改前

#include <stdio.h>#include <stdlib.h>#define N 8typedef struct list{int data;struct list *next;} SLIST;SLIST *creatlist(char *);void outlist(SLIST *);int fun( SLIST *h, char ch){SLIST *p; int n=0;p=h->next;/**********found**********/while(p!=___1___){n++;/**********found**********/if (p->data==ch) return ___2___;else p=p->next;}return 0;}void main(){SLIST *head; int k;char ch;char a[N]={'m','p','g','a','w','x','r','d'};head=creatlist(a);outlist(head);printf("Enter a letter:");scanf("%c",&ch);/**********found**********/k=fun(___3___);if (k==0) printf("\nNot found!\n");else printf("The sequence number is : %d\n",k);}SLIST *creatlist(char *a){SLIST *h,*p,*q;int i;h=p=(SLIST *)malloc(sizeof(SLIST));for(i=0; i<N; i++){q=(SLIST *)malloc(sizeof(SLIST));q->data=a[i]; p->next=q; p=q;}p->next=0;return h;}void outlist(SLIST *h){SLIST *p;p=h->next;if (p==NULL) printf("\nThe list is NULL!\n");else{printf("\nHead");do{printf("->%c",p->data); p=p->next; }while(p!=NULL);printf("->End\n");}}

修改后

#include <stdio.h>#include <stdlib.h>#define N 8typedef struct list{int data;struct list *next;} SLIST;SLIST *creatlist(char *);void outlist(SLIST *);int fun( SLIST *h, char ch){SLIST *p; int n=0;p=h->next;/**********found**********/while(p!=NULL){n++;/**********found**********/if (p->data==ch) return n;else p=p->next;}return 0;}void main(){SLIST *head; int k;char ch;char a[N]={'m','p','g','a','w','x','r','d'};head=creatlist(a);outlist(head);printf("Enter a letter:");scanf("%c",&ch);/**********found**********/k=fun(head,ch);if (k==0) printf("\nNot found!\n");else printf("The sequence number is : %d\n",k);}SLIST *creatlist(char *a){SLIST *h,*p,*q;int i;h=p=(SLIST *)malloc(sizeof(SLIST));for(i=0; i<N; i++){q=(SLIST *)malloc(sizeof(SLIST));q->data=a[i]; p->next=q; p=q;}p->next=0;return h;}void outlist(SLIST *h){SLIST *p;p=h->next;if (p==NULL) printf("\nThe list is NULL!\n");else{printf("\nHead");do{printf("->%c",p->data); p=p->next; }while(p!=NULL);printf("->End\n");}}

13-7 把形参x的值放入一个新结点

#include <stdio.h>#include <stdlib.h>#define N 8typedef struct list{int data;struct list *next;} SLIST;void fun( SLIST *h, int x){SLIST *p, *q, *s;s=(SLIST *)malloc(sizeof(SLIST));/**********found**********/s->data=___1___;q=h;p=h->next;while(p!=NULL && x>p->data) {/**********found**********/q=___2___;p=p->next;}s->next=p;/**********found**********/q->next=___3___;}SLIST *creatlist(int *a){SLIST *h,*p,*q;int i;h=p=(SLIST *)malloc(sizeof(SLIST));for(i=0; i<N; i++){q=(SLIST *)malloc(sizeof(SLIST));q->data=a[i]; p->next=q; p=q;}p->next=0;return h;}void outlist(SLIST *h){SLIST *p;p=h->next;if (p==NULL) printf("\nThe list is NULL!\n");else{printf("\nHead");do {printf("->%d",p->data); p=p->next; } while(p!=NULL);printf("->End\n");}}void main(){SLIST *head;int x;int a[N]={11,12,15,18,19,22,25,29};head=creatlist(a);printf("\nThe list before inserting:\n"); outlist(head);printf("\nEnter a number : "); scanf("%d",&x);fun(head,x);printf("\nThe list after inserting:\n"); outlist(head);}

#include <stdio.h>#include <stdlib.h>#define N 8typedef struct list{int data;struct list *next;} SLIST;void fun( SLIST *h, int x){SLIST *p, *q, *s;s=(SLIST *)malloc(sizeof(SLIST));/**********found**********/s->data=x;q=h;p=h->next;while(p!=NULL && x>p->data) {/**********found**********/q=p;p=p->next;}s->next=p;/**********found**********/q->next=s;}SLIST *creatlist(int *a){SLIST *h,*p,*q;int i;h=p=(SLIST *)malloc(sizeof(SLIST));for(i=0; i<N; i++){q=(SLIST *)malloc(sizeof(SLIST));q->data=a[i]; p->next=q; p=q;}p->next=0;return h;}void outlist(SLIST *h){SLIST *p;p=h->next;if (p==NULL) printf("\nThe list is NULL!\n");else{printf("\nHead");do {printf("->%d",p->data); p=p->next; } while(p!=NULL);printf("->End\n");}}void main(){SLIST *head;int x;int a[N]={11,12,15,18,19,22,25,29};head=creatlist(a);printf("\nThe list before inserting:\n"); outlist(head);printf("\nEnter a number : "); scanf("%d",&x);fun(head,x);printf("\nThe list after inserting:\n"); outlist(head);}

13-8 每调用一次,输出链表结点中的数据

修改前

#include <stdio.h>#include <stdlib.h>#define N 8typedef struct list{int data;struct list *next;} SLIST;void fun( SLIST *p){SLIST *t, *s;t=p->next; s=p;while(t->next != NULL){s=t;/**********found**********/t=t->___1___;}/**********found**********/printf(" %d ",___2___);s->next=NULL;/**********found**********/free(___3___);}SLIST *creatlist(int *a){SLIST *h,*p,*q;int i;h=p=(SLIST *)malloc(sizeof(SLIST));for(i=0; i<N; i++){q=(SLIST *)malloc(sizeof(SLIST));q->data=a[i]; p->next=q; p=q;}p->next=0;return h;}void outlist(SLIST *h){SLIST *p;p=h->next;if (p==NULL) printf("\nThe list is NULL!\n");else{printf("\nHead");do {printf("->%d",p->data); p=p->next; } while(p!=NULL);printf("->End\n");}}void main(){SLIST *head;int a[N]={11,12,15,18,19,22,25,29};head=creatlist(a);printf("\nOutput from head:\n"); outlist(head);printf("\nOutput from tail: \n");while (head->next != NULL){fun(head);printf("\n\n");printf("\nOutput from head again :\n"); outlist(head);}}

修改后

#include <stdio.h>#include <stdlib.h>#define N 8typedef struct list{int data;struct list *next;} SLIST;void fun( SLIST *p){SLIST *t, *s;t=p->next; s=p;while(t->next != NULL){s=t;/**********found**********/t=t->next;}/**********found**********/printf(" %d ", t->data);s->next=NULL;/**********found**********/free(t);}SLIST *creatlist(int *a){SLIST *h,*p,*q;int i;h=p=(SLIST *)malloc(sizeof(SLIST));for(i=0; i<N; i++){q=(SLIST *)malloc(sizeof(SLIST));q->data=a[i]; p->next=q; p=q;}p->next=0;return h;}void outlist(SLIST *h){SLIST *p;p=h->next;if (p==NULL) printf("\nThe list is NULL!\n");else{printf("\nHead");do {printf("->%d",p->data); p=p->next; } while(p!=NULL);printf("->End\n");}}void main(){SLIST *head;int a[N]={11,12,15,18,19,22,25,29};head=creatlist(a);printf("\nOutput from head:\n"); outlist(head);printf("\nOutput from tail: \n");while (head->next != NULL){fun(head);printf("\n\n");printf("\nOutput from head again :\n"); outlist(head);}}

21.所有整数中各位上数字之和为x

修改前

#include <stdio.h>int fun(int x){int n, s1, s2, s3, t;n=0;t=100;/**********found**********/while(t<=__1__){/**********found**********/s1=t%10; s2=(__2__)%10; s3=t/100;/**********found**********/if(s1+s2+s3==__3__){printf("%d ",t);n++;}t++;}return n;}void main(){int x=-1;while(x<0){printf("Please input(x>0): "); scanf("%d",&x); }printf("\nThe result is: %d\n",fun(x));}

修改后

#include <stdio.h>int fun(int x){int n, s1, s2, s3, t;n=0;t=100;/**********found**********/while(t<=999){/**********found**********/s1=t%10; s2=(s1/10)%10; s3=t/100;/**********found**********/if(s1+s2+s3==x){printf("%d ",t);n++;}t++;}return n;}void main(){int x=-1;while(x<0){printf("Please input(x>0): "); scanf("%d",&x); }printf("\nThe result is: %d\n",fun(x));}

**难度等级:**★★☆☆☆

学点啥

14.函数…若x=2.5,函数值为12.182494

修改前

#include <stdio.h>#include <math.h>double fun(double x){double f, t; int n;/**********found**********/f = 1.0+___1___;t = x;n = 1;do {n++;/**********found**********/t *= x/___2___;/**********found**********/f += ___3___;} while (fabs(t) >= 1e-6);return f;}void main(){double x, y;x=2.5;y = fun(x);printf("\nThe result is :\n");printf("x=%-12.6f y=%-12.6f \n", x, y);}

修改后

#include <stdio.h>#include <math.h>double fun(double x){double f, t; int n;/**********found**********/f = 1.0+x;t = x;n = 1;do {n++;/**********found**********/t *= x/n;/**********found**********/f += t;} while (fabs(t) >= 1e-6);return f;}void main(){double x, y;x=2.5;y = fun(x);printf("\nThe result is :\n");printf("x=%-12.6f y=%-12.6f \n", x, y);}

15.用变量a统计大于0的个数

修改前

#include <stdio.h>void fun( int *px, int *py){/**********found**********/int __(1)__ ;scanf( "%d", &k );/**********found**********/while __(2)__{if (k>0 ) a++;if(k<0 ) b++;/**********found**********/__(3)__;}*px=a; *py=b;}void main(){int x, y;fun( &x, &y );printf("x=%d y=%d\n", x,y );}

修改后

#include <stdio.h>void fun( int *px, int *py){/**********found**********/int a = 0, b = 0,k;scanf( "%d", &k );/**********found**********/while (k!=0){if (k>0 ) a++;if(k<0 ) b++;/**********found**********/scanf("%d", &k);}*px=a; *py=b;}void main(){int x, y;fun( &x, &y );printf("x=%d y=%d\n", x,y );}

16.存储学生的学号、姓名和三门课的成绩

修改前

#include <stdio.h>#include <string.h>struct student {long sno;char name[10];float score[3];};/**********found**********/__1__ fun(struct student a){int i;a.sno = 10002;/**********found**********/strcpy(__2__, "LiSi");/**********found**********/for (i=0; i<3; i++) __3__+= 1;return a;}void main(){struct student s={10001,"ZhangSan", 95, 80, 88}, t;int i;printf("\n\nThe original data :\n");printf("\nNo: %ld Name: %s\nScores: ",s.sno, s.name);for (i=0; i<3; i++) printf("%6.2f ", s.score[i]);printf("\n");t = fun(s);printf("\nThe data after modified :\n");printf("\nNo: %ld Name: %s\nScores: ",t.sno, t.name);for (i=0; i<3; i++) printf("%6.2f ", t.score[i]);printf("\n");}

修改后

#include <stdio.h>#include <string.h>struct student {long sno;char name[10];float score[3];};/**********found**********/struct student fun(struct student a){int i;a.sno = 10002;/**********found**********/strcpy(a.name, "LiSi");/**********found**********/for (i=0; i<3; i++) a.score[i] += 1;return a;}void main(){struct student s={10001,"ZhangSan", 95, 80, 88}, t;int i;printf("\n\nThe original data :\n");printf("\nNo: %ld Name: %s\nScores: ",s.sno, s.name);for (i=0; i<3; i++) printf("%6.2f ", s.score[i]);printf("\n");t = fun(s);printf("\nThe data after modified :\n");printf("\nNo: %ld Name: %s\nScores: ",t.sno, t.name);for (i=0; i<3; i++) printf("%6.2f ", t.score[i]);printf("\n");}

函数类型定义,由下可得使用结构体,故应定义为结构体

18.数字字符转换

**难度等级:**★☆☆☆☆

修改前

#include <stdio.h>/**********found**********/___1___ fun(char ch){/**********found**********/if (ch>='0' && ___2___)/**********found**********/return '9'- (ch-___3___);return ch ;}void main(){char c1, c2;printf("\nThe result :\n");c1='2'; c2 = fun(c1);printf("c1=%c c2=%c\n", c1, c2);c1='8'; c2 = fun(c1);printf("c1=%c c2=%c\n", c1, c2);c1='a'; c2 = fun(c1);printf("c1=%c c2=%c\n", c1, c2);}

修改后

#include <stdio.h>/**********found**********/int fun(char ch){/**********found**********/if (ch>='0' && ch <= '9')/**********found**********/return '9'- (ch-'0');return ch ;}void main(){char c1, c2;printf("\nThe result :\n");c1='2'; c2 = fun(c1);printf("c1=%c c2=%c\n", c1, c2);c1='8'; c2 = fun(c1);printf("c1=%c c2=%c\n", c1, c2);c1='a'; c2 = fun(c1);printf("c1=%c c2=%c\n", c1, c2);}

注意上述并未满分,错误类型:

1、函数标识符指明了函数类型,也即返回类型,由printf("c1=%c c2=%c\n", c1, c2);%c可得返回char fun(char ch)

%d,用来输出du十进制整数。%f,用来输出实数(包括单,双精度),以小数形式输出,默认情况下保留小数点6位%c,用来输出一个字符。%s,用来输出一个字符串

2、return '9' - (ch - '0');用于实现字符转换,若无

#include <stdio.h>/**********found**********/char fun(char ch){/**********found**********/if (ch >= '0' && ch <= '9')/**********found**********/return '9' - (ch );return ch ;}void main(){char c1, c2;printf("\nThe result :\n");c1='2'; c2 = fun(c1);printf("c1=%c c2=%c\n", c1, c2);c1='8'; c2 = fun(c1);printf("c1=%c c2=%c\n", c1, c2);c1='a'; c2 = fun(c1);printf("c1=%c c2=%c\n", c1, c2);}

运行结果

正确

#include <stdio.h>/**********found**********/char fun(char ch){/**********found**********/if (ch >= '0' && ch <= '9')/**********found**********/return '9' - (ch - '0');return ch ;}void main(){char c1, c2;printf("\nThe result :\n");c1='2'; c2 = fun(c1);printf("c1=%c c2=%c\n", c1, c2);c1='8'; c2 = fun(c1);printf("c1=%c c2=%c\n", c1, c2);c1='a'; c2 = fun(c1);printf("c1=%c c2=%c\n", c1, c2);}

输出结果

19.有一只狐狸和一只兔子

**难度等级:**★★☆☆☆

修改前

#include <stdio.h>#define N 100 void fun( int *a , int n ){int i, t;for( i=0; i<n; i++ )/**********found**********/a[i]=___1___;i=0; /**********found**********/___2___=1;while( i<n ){a[i]= 1;t++;/**********found**********/i=___3___;}}void main(){int a[N], i, n=30;fun( a, n);for(i=0; i<n; i++)if( a[i]==1 ) printf("不安全的洞号是 : %d\n",i );}

修改后

#include <stdio.h>#define N 100 void fun( int *a , int n ){int i, t;for( i=0; i<n; i++ )/**********found**********/a[i]=i;i=0; /**********found**********/t=1;while( i<n ){a[i]= 1;t++;/**********found**********/i=i+t;}}void main(){int a[N], i, n=30;fun( a, n);for(i=0; i<n; i++)if( a[i]==1 ) printf("不安全的洞号是 : %d\n",i );}

22.偶数按原顺序依次存放

修改前

#include<stdio.h>#define N 9int fun(int a[], int n){int i,j;j = 0;for (i=0; i<n; i++)/**********found**********/if (___1___== 0) {/**********found**********/___2___ = a[i]; j++;}/**********found**********/return ___3___;}void main(){int b[N]={9,1,4,2,3,6,5,8,7}, i, n;printf("\nThe original data :\n");for (i=0; i<N; i++) printf("%4d ", b[i]);printf("\n");n = fun(b, N);printf("\nThe number of even :%d\n", n);printf("\nThe even :\n");for (i=0; i<n; i++) printf("%4d ", b[i]);printf("\n");}

修改后

#include<stdio.h>#define N 9int fun(int a[], int n){int i,j;j = 0;for (i=0; i<n; i++)/**********found**********/if (a[i]%2== 0) {/**********found**********/a[j] = a[i]; j++;}/**********found**********/return j;}void main(){int b[N]={9,1,4,2,3,6,5,8,7}, i, n;printf("\nThe original data :\n");for (i=0; i<N; i++) printf("%4d ", b[i]);printf("\n");n = fun(b, N);printf("\nThe number of even :%d\n", n);printf("\nThe even :\n");for (i=0; i<n; i++) printf("%4d ", b[i]);printf("\n");}

**难度等级:**★☆☆☆☆

学点啥

23.数组中找出两科成绩最高的学生并返回其所在数组中的下标

修改前

#include<stdio.h>typedef struct stu{char ID[30];char name[20];int score[2];} STU;int fun(STU *d,int n){int i,m;/******found******/______(1)______;for(i=1;i<n;i++)/******found******/if(d[i].score[0]+d[i].score[1]>________(2)________)m=i;/******found******/______(3)______;}void main(){STU a[10]={"500301","李清水",83,92,"500336","刘世才",85,94,"500371","王子晨",88,88};int i,n=3;i=fun(a,n);printf("%30s%20s%4d%4d",a[i].ID,a[i].name,a[i].score[0],a[i].score[1]);printf("\n");}

修改后

#include<stdio.h>typedef struct stu{char ID[30];char name[20];int score[2];} STU;int fun(STU *d,int n){int i,m;/******found******/m = 0;for(i=1;i<n;i++)/******found******/if(d[i].score[0]+d[i].score[1]> d[m].score[0] + d[m].score[1])m=i;/******found******/return m;}void main(){STU a[10]={"500301","李清水",83,92,"500336","刘世才",85,94,"500371","王子晨",88,88};int i,n=3;i=fun(a,n);printf("%30s%20s%4d%4d",a[i].ID,a[i].name,a[i].score[0],a[i].score[1]);printf("\n");}

**难度等级:**★★☆☆☆

学点啥

24.矩阵主对角线、负对角线对应元素交换

修改前

#include <stdio.h>#define N 4/**********found**********/void fun(int ___1___ , int n){int i,s;/**********found**********/for(___2___; i++){s=t[i][i];t[i][i]=t[i][n-i-1];/**********found**********/t[i][n-1-i]=___3___;}}void main(){int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10}, i, j;printf("\nThe original array:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%d ",t[i][j]);printf("\n");}fun(t,N);printf("\nThe result is:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%d ",t[i][j]);printf("\n");}}

修改后

#include <stdio.h>#define N 4/**********found**********/void fun(int t[][N] , int n){int i,s;/**********found**********/for(i=0;i<N; i++){s=t[i][i];t[i][i]=t[i][n-i-1];/**********found**********/t[i][n-1-i]=s;}}void main(){int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10}, i, j;printf("\nThe original array:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%d ",t[i][j]);printf("\n");}fun(t,N);printf("\nThe result is:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%d ",t[i][j]);printf("\n");}}

**难度等级:**★★☆☆☆

学点啥

25.下标为奇数的字符取出,并按ASCII码大小递增排序(选择排序法)

修改前

#include <stdio.h>void fun(char *s, char *p){int i, j, n, x, t;n=0;for(i=0; s[i]!='\0'; i++) n++;for(i=1; i<n-2; i=i+2) {/**********found**********/___1___;/**********found**********/for(j=___2___+2 ; j<n; j=j+2)if(s[t]>s[j]) t=j;if(t!=i){x=s[i]; s[i]=s[t]; s[t]=x; }}for(i=1,j=0; i<n; i=i+2, j++) p[j]=s[i];/**********found**********/p[j]=___3___;}void main(){char s[80]="baawrskjghzlicda", p[50];printf("\nThe original string is : %s\n",s);fun(s,p);printf("\nThe result is : %s\n",p);}

修改后

#include <stdio.h>void fun(char *s, char *p){int i, j, n, x, t;n=0;for(i=0; s[i]!='\0'; i++) n++;for(i=1; i<n-2; i=i+2) {/**********found**********/t=i;/**********found**********/for(j=i+2 ; j<n; j=j+2)if(s[t]>s[j]) t=j;if(t!=i){x=s[i]; s[i]=s[t]; s[t]=x; }}for(i=1,j=0; i<n; i=i+2, j++) p[j]=s[i];/**********found**********/p[j]=0;}void main(){char s[80]="baawrskjghzlicda", p[50];printf("\nThe original string is : %s\n",s);fun(s,p);printf("\nThe result is : %s\n",p);}

**难度等级:**★★★☆☆

学点啥

排序题的选择法,也就是遍历,通过比较在第一个位置放置最值;然后第二个位置,遍历余下,放置次最值…

选择排序法是一种不稳定的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到全部待排序的数据元素排完。

void swap(int *a,int *b) {int temp = *a;*a = *b;*b = temp;}void selection_sort(int arr[], int len) {int i,j;for (i = 0 ; i < len - 1 ; i++) {int min = i;for (j = i + 1; j < len; j++)if (arr[j] < arr[min]) min = j; swap(&arr[min], &arr[i]); }}

本题选择排序法放置在1、3、5而已

#include <stdio.h>void fun(char *s, char *p){int i, j, n, x, t;n=0;//for(i=0; s[i]!='\0'; i++) n++; /*求得字符串长度*/i = 0; while (s[i++]) n++; /*求得字符串长度 等效方式*/for(i=1; i<n-2; i=i+2) {/**********found**********/t = i;//循环变量幅初值/**********found**********//*第i个位置放置最值如初始在第一个位置放置最小值*/for(j=t+2 ; j<n; j=j+2)if(s[t]>s[j]) t=j;/*遍历找到的最值是否为初值指定位置i若非,交换之。如最值为s[3]而非初值s[1],交换两者,从而第一个位置仍为最值*/if(t!=i){x=s[i]; s[i]=s[t]; s[t]=x; }}for(i=1,j=0; i<n; i=i+2, j++) p[j]=s[i];/**********found**********/p[j]='\0';}void main(){char s[80]="baawrskjghzlicda", p[50];printf("\nThe original string is : %s\n",s);fun(s,p);printf("\nThe result is : %s\n",p);}

26.其余字符串用字符*补齐

修改前

#include <stdio.h>#include <string.h>#define M 5#define N 20void fun(char (*ss)[N]){int i, j, k=0, n, m, len;for(i=0; i<M; i++){len=strlen(ss[i]);if(i==0) n=len;if(len>n) {/**********found**********/n=len; ___1___=i;}}for(i=0; i<M; i++)if (i!=k){m=n;len=strlen(ss[i]);/**********found**********/for(j=___2___; j>=0; j--)ss[i][m--]=ss[i][j];for(j=0; j<n-len; j++)/**********found**********/___3___='*';}}void main(){char ss[M][N]={"shanghai","guangzhou","beijing","tianjing","cchongqing"};int i;printf("\nThe original strings are :\n");for(i=0; i<M; i++) printf("%s\n",ss[i]);printf("\n");fun(ss);printf("\nThe result:\n");for(i=0; i<M; i++) printf("%s\n",ss[i]);}

修改后

#include <stdio.h>#include <string.h>#define M 5#define N 20void fun(char (*ss)[N]){int i, j, k=0, n, m, len;for(i=0; i<M; i++){len=strlen(ss[i]);if(i==0) n=len;if(len>n) {/**********found**********/n=len; k=i;}}for(i=0; i<M; i++)if (i!=k){m=n;len=strlen(ss[i]);/**********found**********/for(j=len; j>=0; j--)ss[i][m--]=ss[i][j];for(j=0; j<n-len; j++)/**********found**********/ss[i][j] ='*';}}void main(){char ss[M][N]={"shanghai","guangzhou","beijing","tianjing","cchongqing"};int i;printf("\nThe original strings are :\n");for(i=0; i<M; i++) printf("%s\n",ss[i]);printf("\n");fun(ss);printf("\nThe result:\n");for(i=0; i<M; i++) printf("%s\n",ss[i]);}

程序解析

#include <stdio.h>#include <string.h>#define M 5#define N 20void fun(char (*ss)[N]){int i, j, k=0, n, m, len;/*寻找最长的字符串n-最长字符串长度k-第几个字符串最长*/for(i=0; i<M; i++){len=strlen(ss[i]);if(i==0) n=len;if(len>n) {/**********found**********/n=len; k=i;}}/*按行输出 */for(i=0; i<M; i++)if (i!=k)/*判断是否非最长的字符串*/{m=n;len=strlen(ss[i]);/**********found**********//*原字符串右移*/for(j=len; j>=0; j--)ss[i][m--]=ss[i][j];/*左边用*补齐*/for(j=0; j<n-len; j++)/**********found**********/ss[i][j] ='*';}}void main(){char ss[M][N]={"shanghai","guangzhou","beijing","tianjing","cchongqing"};int i;printf("\nThe original strings are :\n");for(i=0; i<M; i++) printf("%s\n",ss[i]);printf("\n");fun(ss);printf("\nThe result:\n");for(i=0; i<M; i++) printf("%s\n",ss[i]);}

变形右边补齐

#include <stdio.h>#include <string.h>#define M 5#define N 20void fun(char (*ss)[N]){int i, j, k=0, n, m=0, len;/*寻找最长的字符串n-最长字符串长度k-第几个字符串最长*/for(i=0; i<M; i++){len=strlen(ss[i]);if(i==0) n=len;if(len>n) {/**********found**********/n=len; k=i;}}/*按行输出 */for(i=0; i<M; i++)if (i!=k)/*判断是否非最长的字符串*/{len=strlen(ss[i]);/**********found**********/for (j = 0; j <len; j++){ss[i][m] = ss[i][j];m++;}/*右边用*补齐*/for(j=len; j<n; j++)/**********found**********/ss[i][j] ='*';ss[i][j] = '\0';}}void main(){char ss[M][N]={"abc","defg","hijkl","mnopqr","stuvwxy"};int i;printf("\nThe original strings are :\n");for(i=0; i<M; i++) printf("%s\n",ss[i]);fun(ss);printf("\nThe result:\n");for(i=0; i<M; i++) printf("%s\n",ss[i]);}

优化

void fun(char (*ss)[N]){int i, j, k=0, n, m=0, len;/*寻找最长的字符串n-最长字符串长度k-第几个字符串最长*/for(i=0; i<M; i++){len=strlen(ss[i]);if(i==0) n=len;if(len>n) {/**********found**********/n=len; k=i;}}/*按行输出 */for(i=0; i<M; i++)if (i!=k)/*判断是否非最长的字符串*/{len=strlen(ss[i]);/**********found**********///for (j = 0; j <len; j++)//{// ss[i][m] = ss[i][j];// m++;//}/*右边用*补齐*/for(j=len; j<n; j++)/**********found**********/ss[i][j] ='*';ss[i][j] = '\0';}}

**难度等级:**★★☆☆☆

学点啥

27.计算前n项之和

修改前

#include <stdio.h>#include <math.h>double fun(double x, int n){double f, t;int i;/**********found**********/f = ___1___;t = -1;for (i=1; i<n; i++){/**********found**********/t *= (___2___)*x/i;/**********found**********/f += ___3___;}return f;}void main(){double x, y;x=2.5;y = fun(x, 15);printf("\nThe result is :\n");printf("x=%-12.6f y=%-12.6f\n", x, y);}

修改后

#include <stdio.h>#include <math.h>double fun(double x, int n){double f, t;int i;/**********found**********/f = 1.0;t = -1;for (i=1; i<n; i++){/**********found**********/t *= (-1)*x/i;/**********found**********/f += t;}return f;}void main(){double x, y;x=2.5;y = fun(x, 15);printf("\nThe result is :\n");printf("x=%-12.6f y=%-12.6f\n", x, y);}

输出结果

**难度等级:**★☆☆☆☆

学点啥

28.幻方

修改前

#include <stdio.h>#define N 3int fun(int (*a)[N]){int i,j,m1,m2,row,colum;m1=m2=0;for(i=0; i<N; i++){j=N-i-1; m1+=a[i][i]; m2+=a[i][j]; }if(m1!=m2) return 0;for(i=0; i<N; i++) {/**********found**********/row=colum= __1__;for(j=0; j<N; j++){row+=a[i][j]; colum+=a[j][i]; }/**********found**********/if( (row!=colum) __2__ (row!=m1) ) return 0;}/**********found**********/return __3__;}void main(){int x[N][N],i,j;printf("Enter number for array:\n");for(i=0; i<N; i++)for(j=0; j<N; j++) scanf("%d",&x[i][j]);printf("Array:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%3d",x[i][j]);printf("\n");}if(fun(x)) printf("The Array is a magic square.\n");else printf("The Array isn't a magic square.\n");}

修改后

#include <stdio.h>#define N 3int fun(int (*a)[N]){int i,j,m1,m2,row,colum;m1=m2=0;for(i=0; i<N; i++){j=N-i-1; m1+=a[i][i]; m2+=a[i][j]; }if(m1!=m2) return 0;for(i=0; i<N; i++) {/**********found**********/row=colum= 0;for(j=0; j<N; j++){row+=a[i][j]; colum+=a[j][i]; }/**********found**********/if( (row!=colum)|| (row!=m1) ) return 0;}/**********found**********/return 1;}void main(){int x[N][N],i,j;printf("Enter number for array:\n");for(i=0; i<N; i++)for(j=0; j<N; j++) scanf("%d",&x[i][j]);printf("Array:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%3d",x[i][j]);printf("\n");}if(fun(x)) printf("The Array is a magic square.\n");else printf("The Array isn't a magic square.\n");}

输出结果

**难度等级:**★★☆☆☆

学点啥

29.调用随机函数产生20各互不相同的整数放在形参a所指数组中

修改前

#include <stdlib.h>#include <stdio.h>#define N 20void fun( int *a){int i, x, n=0;x=rand()%20;/**********found**********/while (n<__1__){for(i=0; i<n; i++ )/**********found**********/if( x==a[i] ) __2__;/**********found**********/if( i==__3__){a[n]=x; n++; }x=rand()%20;}}void main(){int x[N]={0} ,i;fun( x );printf("The result : \n");for( i=0; i<N; i++ ){printf("%4d",x[i]);if((i+1)%5==0)printf("\n");}printf("\n\n");}

修改后

#include <stdlib.h>#include <stdio.h>#define N 20void fun( int *a){int i, x, n=0;x=rand()%20;/**********found**********/while (n<20){for(i=0; i<n; i++ )/**********found**********/if( x==a[i] ) break;/**********found**********/if( i==n){a[n]=x; n++; }x=rand()%20;}}void main(){int x[N]={0} ,i;fun( x );printf("The result : \n");for( i=0; i<N; i++ ){printf("%4d",x[i]);if((i+1)%5==0)printf("\n");}printf("\n\n");}

输出结果

**难度等级:**★★☆☆☆

学点啥

使用for循环判断x是否与数组中已存的元素重复,若重复则跳出for循环结束;若for循环由break语句结束也即x与数组中元素重复,则i必然小于x;若遍历仍不重复,则必然i=n,不再满足循环,也即均不重复

30.字符串转换成面值相同的整数

修改前

#include <stdio.h>#include <string.h>#include <ctype.h>#define N 9long ctod( char *s ){long d=0;while(*s)if(isdigit( *s)) {/**********found**********/d=d*10+*s-__1__;/**********found**********/__2__; }return d;}long fun( char *a, char *b ){/**********found**********/return __3__;}void main(){char s1[N],s2[N];do{printf("Input string s1 : "); gets(s1); }while( strlen(s1)>N );do{printf("Input string s2 : "); gets(s2); }while( strlen(s2)>N );printf("The result is: %ld\n", fun(s1,s2) );}

修改后

#include <stdio.h>#include <string.h>#include <ctype.h>#define N 9long ctod( char *s ){long d=0;while(*s)if(isdigit( *s)) {/**********found**********/d=d*10+*s-'0';/**********found**********/s++; }return d;}long fun( char *a, char *b ){/**********found**********/return ctod(a) + ctod(b);}void main(){char s1[N],s2[N];do{printf("Input string s1 : "); gets(s1); }while( strlen(s1)>N );do{printf("Input string s2 : "); gets(s2); }while( strlen(s2)>N );printf("The result is: %ld\n", fun(s1,s2) );}

输出结果

**难度等级:**★★☆☆☆

学点啥

字符串转换成面值相同的整数,只需减去’0’即可

31.每个数字字符之后插入一个*号

修改前

#include<stdio.h>void fun(char *s){int i, j, n;for(i=0; s[i]!='\0'; i++)/**********found**********/if(s[i]>='0' ___1___ s[i]<='9'){n=0;/**********found**********/while(s[i+1+n]!= ___2___) n++;for(j=i+n+1; j>i; j--)/**********found**********/s[j+1]= ___3___;s[j+1]='*';i=i+1;}}void main(){char s[80]="def35adh3kjsdf7";printf("\nThe original string is : %s\n",s);fun(s);printf("\nThe result is : %s\n",s);}

修改后

#include<stdio.h>void fun(char *s){int i, j, n;for(i=0; s[i]!='\0'; i++)/**********found**********/if(s[i]>='0' && s[i]<='9'){n=0;/**********found**********/while(s[i+1+n]!= 0) n++;for(j=i+n+1; j>i; j--)/**********found**********/s[j+1]= s[j];s[j+1]='*';i=i+1;}}void main(){char s[80]="def35adh3kjsdf7";printf("\nThe original string is : %s\n",s);fun(s);printf("\nThe result is : %s\n",s);}

输出结果

**难度等级:**★★☆☆☆

学点啥

32.按顺序打印出三个相邻的字母

修改前

#include <stdio.h>#include <ctype.h>#pragma warning (disable:4996)void fun(char ch){char t[3];int i;t[1]=ch;/**********************found***********************/if (ch=='Z'){t[2]='A'; t[0]= ____(1)____ ; }/**********************found***********************/else if (ch=='A') {t[2]='B'; t[0]= ____(2)____; }/**********************found***********************/else {t[0]=ch-1; t[2]= ____(3)____;}for(i=0;i<3; i++) putchar(t[i]);putchar('\n');}main( ){char c;do{printf(" enter a upper letter:");c=getchar(); getchar(); }while(!isupper(c));fun(c);}

修改后

#include <stdio.h>#include <ctype.h>#pragma warning (disable:4996)void fun(char ch){char t[3];int i;t[1]=ch;/**********************found***********************/if (ch=='Z'){t[2]='A'; t[0]= 'Y'; }/**********************found***********************/else if (ch=='A') {t[2]='B'; t[0]= 'Z'; }/**********************found***********************/else {t[0]=ch-1; t[2]= ch + 1;}for(i=0;i<3; i++) putchar(t[i]);putchar('\n');}main( ){char c;do{printf(" enter a upper letter:");c=getchar(); getchar(); }while(!isupper(c));fun(c);}

输出结果

**难度等级:**☆☆☆☆☆

学点啥

33 所指字符串数组中长度最长的字符串所在的行下标

修改前

#include <stdio.h>#include <string.h>#define M 5#define N 20/**********found**********/int fun(char (*ss) ___1___, int *n){int i, k=0, len=0;for(i=0; i<M; i++){len=strlen(ss[i]);/**********found**********/if(i==0) *n=___2___;if(len>*n) {/**********found**********/___3___;k=i;}}return(k);}void main(){char ss[M][N]={"shanghai","guangzhou","beijing","tianjing","cchongqing"};int n,k,i;printf("\nThe original strings are :\n");for(i=0;i<M;i++)puts(ss[i]);k=fun(ss,&n);printf("\nThe length of longest string is : %d\n",n);printf("\nThe longest string is : %s\n",ss[k]);}

修改后

#include <stdio.h>#include <string.h>#define M 5#define N 20/**********found**********/int fun(char (*ss)[N], int *n){int i, k=0, len=0;for(i=0; i<M; i++){len=strlen(ss[i]);/**********found**********/if(i==0) *n=len;if(len>*n) {/**********found**********/*n = len;k=i;}}return(k);}void main(){char ss[M][N]={"shanghai","guangzhou","beijing","tianjing","cchongqing"};int n,k,i;printf("\nThe original strings are :\n");for(i=0;i<M;i++)puts(ss[i]);k=fun(ss,&n);printf("\nThe length of longest string is : %d\n",n);printf("\nThe longest string is : %s\n",ss[k]);}

输出结果

**难度等级:**★★☆☆☆

学点啥

形参ss定义,是字符串数组的定义,其宽度为N,故 [N]

34 查找有不及格科目的学生

修改前

#include <stdio.h>typedef struct{char num[8]; double score[2];/**********found**********/} __(1)__ ;int fun(STU std[ ], int n){int i, k=0;for(i=0; i<n; i++)/**********found**********/if( std[i].score[0]<60__(2)__std[i].score[1]<60 ) {k++;printf("学号:%s ",std[i].num); }/**********found**********/return __(3)__ ;}void main(){STU std[4]={"N1001", 76.5,82.0 ,"N1002", 53.5,73.0, "N1005", 80.5,66.0,"N1006", 81.0,56.0 };printf( "\n共有%d位学生有不及格科目\n" , fun(std,4) );}

修改后

#include <stdio.h>typedef struct{char num[8]; double score[2];/**********found**********/} STU;int fun(STU std[ ], int n){int i, k=0;for(i=0; i<n; i++)/**********found**********/if( std[i].score[0]<60||std[i].score[1]<60 ) {k++;printf("学号:%s ",std[i].num); }/**********found**********/return k ;}void main(){STU std[4]={"N1001", 76.5,82.0 ,"N1002", 53.5,73.0, "N1005", 80.5,66.0,"N1006", 81.0,56.0 };printf( "\n共有%d位学生有不及格科目\n" , fun(std,4) );}

输出结果

**难度等级:**★★☆☆☆

学点啥

35 每列元素的最大值

修改前

#include <stdio.h>#define N 4void fun(int (*a)[N], int *b){int i,j;for(i=0; i<N; i++) {/**********found**********/b[i]=__1__;for(j=1; j<N; j++)/**********found**********/if(b[i]__2__ a[j][i])b[i]=a[j][i];}}void main(){int x[N][N]={{12,5,8,7},{6,1,9,3},{1,2,3,4},{2,8,4,3} },y[N],i,j;printf("\nThe matrix :\n");for(i=0;i<N; i++){for(j=0;j<N; j++) printf("%4d",x[i][j]);printf("\n");}/**********found**********/fun(__3__);printf("\nThe result is:");for(i=0; i<N; i++) printf("%3d",y[i]);printf("\n");}

修改后

#include <stdio.h>#define N 4void fun(int (*a)[N], int *b){int i,j;for(i=0; i<N; i++) {/**********found**********/b[i]= a[0][i];for(j=1; j<N; j++)/**********found**********/if(b[i]< a[j][i])b[i]=a[j][i];}}void main(){int x[N][N]={{12,5,8,7},{6,1,9,3},{1,2,3,4},{2,8,4,3} },y[N],i,j;printf("\nThe matrix :\n");for(i=0;i<N; i++){for(j=0;j<N; j++) printf("%4d",x[i][j]);printf("\n");}/**********found**********/fun(x[N][N],y);//此处错误printf("\nThe result is:");for(i=0; i<N; i++) printf("%3d",y[i]);printf("\n");}

注意下列错误

/**********found**********/fun(x[N][N],y);//此处错误

C语言二维数组指针(指向二维数组的指针)详解

int a[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11} };int (*p)[4] = a;

括号中的*表明 p 是一个指针,它指向一个数组,数组的类型为int [4],这正是 a 所包含的每个一维数组的类型。

/**********found**********/fun(x,y);// fun(x[0],y);亦可

x,y均为地址,x或者x[0]均为二维数组初始地址

输出结果

**难度等级:**★★☆☆☆

学点啥

36 字符串中最右边的n各字符串复制

修改前

在这里插入代码片

修改后

#include <stdio.h>#include <string.h>#define N 80void fun(char *s, int n, char *t){int len,i,j=0;len=strlen(s);/**********found**********/if(n>=len) strcpy(t,s);else {/**********found**********/for(i=len-n; i<=len-1; i++) t[j++]= s[i] ;/**********found**********/t[j]= 0 ;}}void main(){char s[N],t[N]; int n;printf("Enter a string: ");gets(s);printf( "Enter n:"); scanf("%d",&n);fun(s,n,t);printf("The string t : "); puts(t);}

输出结果

**难度等级:**★★☆☆☆

学点啥

37 各数字出现的次数

修改前

#include <stdio.h>void fun( int m, int a[10]){int i;for (i=0; i<10; i++)/**********found**********/__1__ = 0;while (m > 0){/**********found**********/i = ___2___;a[i]++;/**********found**********/m = ___3___;}}void main(){int m, a[10],i;printf("请输入一个整数 : "); scanf("%d", &m);fun(m, a);for (i=0; i<10; i++) printf("%d,",a[i]); printf("\n");}

修改后

#include <stdio.h>void fun( int m, int a[10]){int i;for (i=0; i<10; i++)/**********found**********/a[i] = 0;while (m > 0){/**********found**********/i = m%10;a[i]++;/**********found**********/m = m/10;}}void main(){int m, a[10],i;printf("请输入一个整数 : "); scanf("%d", &m);fun(m, a);for (i=0; i<10; i++) printf("%d,",a[i]); printf("\n");}

输出结果

**难度等级:**★★☆☆☆

学点啥

38 利用结构体变量存储了一名学生的信息

修改前

#include<stdio.h>typedef struct{int num;char name[9];char sex;struct {int year,month,day ;} birthday;float score[3];}STU;/**********found**********/void show(STU ___1___){int i;printf("\n%d %s %c %d-%d-%d", tt.num, tt.name, tt.sex,tt.birthday.year, tt.birthday.month, tt.birthday.day);for(i=0; i<3; i++)/**********found**********/printf("%5.1f", ___2___);printf("\n");}void main( ){STU std={1,"Zhanghua",'M',1961,10,8,76.5,78.0,82.0 };printf("\nA student data:\n");/**********found**********/show(___3___);}

修改后

#include<stdio.h>typedef struct{int num;char name[9];char sex;struct {int year,month,day ;} birthday;float score[3];}STU;/**********found**********/void show(STU tt){int i;printf("\n%d %s %c %d-%d-%d", tt.num, tt.name, tt.sex,tt.birthday.year, tt.birthday.month, tt.birthday.day);for(i=0; i<3; i++)/**********found**********/printf("%5.1f", tt.score[i]);printf("\n");}void main( ){STU std={1,"Zhanghua",'M',1961,10,8,76.5,78.0,82.0 };printf("\nA student data:\n");/**********found**********/show(std);}

输出结果

**难度等级:**★★☆☆☆

学点啥

39 数字字符移到所有非数字字符之后

修改前

#include <stdio.h>void fun(char *s){int i, j=0, k=0; char t1[80], t2[80];for(i=0; s[i]!='\0'; i++)if(s[i]>='0' && s[i]<='9'){/**********found**********/t2[j]=s[i]; ___1___;}else t1[k++]=s[i];t2[j]=0; t1[k]=0;/**********found**********/for(i=0; i<k; i++) ___2___;/**********found**********/for(i=0; i<___3___; i++) s[k+i]=t2[i];}void main(){char s[80]="ba3a54j7sd567sdffs";printf("\nThe original string is : %s\n",s);fun(s);printf("\nThe result is : %s\n",s);}

修改后

#include <stdio.h>void fun(char *s){int i, j=0, k=0; char t1[80], t2[80];for(i=0; s[i]!='\0'; i++)if(s[i]>='0' && s[i]<='9'){/**********found**********/t2[j]=s[i]; j++;}else t1[k++]=s[i];t2[j]=0; t1[k]=0;/**********found**********/for(i=0; i<k; i++) s[i]=t1[i];/**********found**********/for(i=0; i<j; i++) s[j]=t2[i];}void main(){char s[80]="ba3a54j7sd567sdffs";printf("\nThe original string is : %s\n",s);fun(s);printf("\nThe result is : %s\n",s);}

输出结果

**难度等级:**★★☆☆☆

学点啥

40 根据形参n计算并返回阶乘n!

修改前

#include <stdio.h>#include <stdlib.h>#include <ctype.h>#pragma warning (disable:4996)struct Ord{int n;long ordor;};long fun(int n){static struct Ord old={0,1};int i;if(n==old.n)/**********************found***********************/return (____(1)____);if(n>old.n)for(i=old.n+1;i<=n;i++)old.ordor*=i;elsefor(i=old.n;i>n;i--)old.ordor/=i;/**********************found***********************/old.n=____(2)____;/**********************found***********************/return ( ____(3)____);}main( ){int i,n;for(i=0;i<5;i++){n=rand()%10;printf("%d %ld\n",n,fun(n));}}

修改后

#include <stdio.h>#include <stdlib.h>#include <ctype.h>#pragma warning (disable:4996)struct Ord{int n;long ordor;};long fun(int n){static struct Ord old={0,1};int i;if(n==old.n)/**********************found***********************/return (old.ordor);if(n>old.n)for(i=old.n+1;i<=n;i++)old.ordor*=i;elsefor(i=old.n;i>n;i--)old.ordor/=i;/**********************found***********************/old.n=n;/**********************found***********************/return (old.ordor);}main( ){int i,n;for(i=0;i<5;i++){n=rand()%10;printf("%d %ld\n",n,fun(n));}}

输出结果

**难度等级:**★★☆☆☆

学点啥

41 把形参a所知数组中的奇数按原顺序依次存放

修改前

#include <stdio.h>#define N 9int fun(int a[], int n){int i,j;j = 0;for (i=0; i<n; i++)/**********found**********/if (a[i]%2==___1___){/**********found**********/a[j] = a[i]; ___2___;}/**********found**********/return ___3___;}void main(){int b[N]={9,1,4,2,3,6,5,8,7}, i, n;printf("\nThe original data :\n");for (i=0; i<N; i++) printf("%4d ", b[i]);printf("\n");n = fun(b, N);printf("\nThe number of odd : %d \n", n);printf("\nThe odd number :\n");for (i=0; i<n; i++) printf("%4d ", b[i]);printf("\n");}

修改后

#include <stdio.h>#define N 9int fun(int a[], int n){int i,j;j = 0;for (i=0; i<n; i++)/**********found**********/if (a[i]%2==1){/**********found**********/a[j] = a[i]; j++;}/**********found**********/return j;}void main(){int b[N]={9,1,4,2,3,6,5,8,7}, i, n;printf("\nThe original data :\n");for (i=0; i<N; i++) printf("%4d ", b[i]);printf("\n");n = fun(b, N);printf("\nThe number of odd : %d \n", n);printf("\nThe odd number :\n");for (i=0; i<n; i++) printf("%4d ", b[i]);printf("\n");}

输出结果

**难度等级:**★★☆☆☆

学点啥

42 重写filename所指文件中最后一个学生的数据

修改前

#include <stdio.h>#define N 5typedef struct student {long sno;char name[10];float score[3];} STU;void fun(char *filename, STU n){FILE *fp;/**********found**********/fp = fopen(__1__, "rb+");/**********found**********/fseek(__2__, -(long)sizeof(STU), SEEK_END);/**********found**********/fwrite(&n, sizeof(STU), 1, __3__);fclose(fp);}void main(){STU t[N]={{10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},{10003,"LiSi", 85, 70, 78}, {10004,"FangFang", 90, 82, 87},{10005,"ZhangSan", 95, 80, 88}};STU n={10006,"ZhaoSi", 55, 70, 68}, ss[N];int i,j;FILE *fp;fp = fopen("student.dat", "wb");fwrite(t, sizeof(STU), N, fp);fclose(fp);fp = fopen("student.dat", "rb");fread(ss, sizeof(STU), N, fp);fclose(fp);printf("\nThe original data :\n\n");for (j=0; j<N; j++){printf("\nNo: %ld Name: %-8sScores: ",ss[j].sno, ss[j].name);for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);printf("\n");}fun("student.dat", n);printf("\nThe data after modifing :\n\n");fp = fopen("student.dat", "rb");fread(ss, sizeof(STU), N, fp);fclose(fp);for (j=0; j<N; j++){printf("\nNo: %ld Name: %-8sScores: ",ss[j].sno, ss[j].name);for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);printf("\n");}}

修改后

#include <stdio.h>#define N 5typedef struct student {long sno;char name[10];float score[3];} STU;void fun(char *filename, STU n){FILE *fp;/**********found**********/fp = fopen(filename, "rb+");/**********found**********/fseek(fp, -(long)sizeof(STU), SEEK_END);/**********found**********/fwrite(&n, sizeof(STU), 1, fp);fclose(fp);}void main(){STU t[N]={{10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},{10003,"LiSi", 85, 70, 78}, {10004,"FangFang", 90, 82, 87},{10005,"ZhangSan", 95, 80, 88}};STU n={10006,"ZhaoSi", 55, 70, 68}, ss[N];int i,j;FILE *fp;fp = fopen("student.dat", "wb");fwrite(t, sizeof(STU), N, fp);fclose(fp);fp = fopen("student.dat", "rb");fread(ss, sizeof(STU), N, fp);fclose(fp);printf("\nThe original data :\n\n");for (j=0; j<N; j++){printf("\nNo: %ld Name: %-8sScores: ",ss[j].sno, ss[j].name);for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);printf("\n");}fun("student.dat", n);printf("\nThe data after modifing :\n\n");fp = fopen("student.dat", "rb");fread(ss, sizeof(STU), N, fp);fclose(fp);for (j=0; j<N; j++){printf("\nNo: %ld Name: %-8sScores: ",ss[j].sno, ss[j].name);for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);printf("\n");}}

难度等级:★★★★★☆☆☆☆

学点啥

43 计算形参x所指数组中N个数的平均值

修改前

#include <stdlib.h>#include <stdio.h>#define N 10double fun(double x[],double *y){int i,j; double av;/**********found**********/av=__1__;/**********found**********/for(i=0; i<N; i++) av=av+__2__;for(i=j=0; i<N; i++)/**********found**********/if(x[i]>av) y[__3__]= x[i];y[j]=-1;return av;}void main(){int i; double x[N],y[N];for(i=0; i<N; i++){x[i]=rand()%50; printf("%4.0f ",x[i]);}printf("\n");printf("\nThe average is: %f\n",fun(x,y));for(i=0; y[i]>=0; i++) printf("%5.1f ",y[i]);printf("\n");}

修改后

#include <stdlib.h>#include <stdio.h>#define N 10double fun(double x[],double *y){int i,j; double av;/**********found**********/av=0.0;/**********found**********/for(i=0; i<N; i++) av=av+x[i]/N;for(i=j=0; i<N; i++)/**********found**********/if(x[i]>av) y[j++]= x[i];y[j]=-1;return av;}void main(){int i; double x[N],y[N];for(i=0; i<N; i++){x[i]=rand()%50; printf("%4.0f ",x[i]);}printf("\n");printf("\nThe average is: %f\n",fun(x,y));for(i=0; y[i]>=0; i++) printf("%5.1f ",y[i]);printf("\n");}

输出结果

**难度等级:**☆☆☆☆☆

学点啥

44 调用随机函数产生20个互不相同的整数

修改前

#include <stdlib.h>#include <stdio.h>#define N 20void fun( int *a){int i, x, n=0;x=rand()%20;/**********found**********/while (n<__1__){for(i=0; i<n; i++ )/**********found**********/if( x==a[i] ) __2__;/**********found**********/if( i==__3__){a[n]=x; n++; }x=rand()%20;}}void main(){int x[N]={0} ,i;fun( x );printf("The result : \n");for( i=0; i<N; i++ ){printf("%4d",x[i]);if((i+1)%5==0)printf("\n");}printf("\n\n");}

修改后

#include <stdlib.h>#include <stdio.h>#define N 20void fun( int *a){int i, x, n=0;x=rand()%20;/**********found**********/while (n<N){for(i=0; i<n; i++ )/**********found**********/if( x==a[i] ) break;/**********found**********/if( i==n){a[n]=x; n++; }x=rand()%20;}}void main(){int x[N]={0} ,i;fun( x );printf("The result : \n");for( i=0; i<N; i++ ){printf("%4d",x[i]);if((i+1)%5==0)printf("\n");}printf("\n\n");}

学点啥

C语言中的rand()函数

**难度等级:**★★☆☆☆

学点啥

45 建立一个NxN的矩阵

修改前

#include <stdio.h>#define N 7/**********found**********/void fun(int (*a)__1__){int i,j,k,m;if(N%2==0) m=N/2 ;else m=N/2+1;for(i=0; i<m; i++) {/**********found**********/for(j=__2__; j<N-i; j++)a[i][j]=a[N-i-1][j]=i+1;for(k=i+1; k<N-i; k++)/**********found**********/a[k][i]=a[k][N-i-1]=__3__;}}void main(){int x[N][N]={0},i,j;fun(x);printf("\nThe result is:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%3d",x[i][j]);printf("\n");}}

修改后

#include <stdio.h>#define N 7/**********found**********/void fun(int (*a)[N]){int i,j,k,m;if(N%2==0) m=N/2 ;else m=N/2+1;for(i=0; i<m; i++) {/**********found**********/for(j=i; j<N-i; j++)a[i][j]=a[N-i-1][j]=i+1;for(k=i+1; k<N-i; k++)/**********found**********/a[k][i]=a[k][N-i-1]=i+1;}}void main(){int x[N][N]={0},i,j;fun(x);printf("\nThe result is:\n");for(i=0; i<N; i++){for(j=0; j<N; j++) printf("%3d",x[i][j]);printf("\n");}}

学点啥

46 用函数指针指向要调用的函数

修改前

#include <stdio.h>double f1(double x){return x*x; }double f2(double x, double y){return x*y; }double fun(double a, double b){/**********found**********/__1__ (*f)();double r1, r2;/**********found**********/f = __2__ ; r1 = f(a);/**********found**********/f = __3__ ; r2 = (*f)(a, b);return r1 + r2;}void main(){double x1=5, x2=3, r;r = fun(x1, x2);printf("\nx1=%f, x2=%f, x1*x1+x1*x2=%f\n",x1, x2, r);}

修改后

#include <stdio.h>double f1(double x){return x*x; }double f2(double x, double y){return x*y; }double fun(double a, double b){/**********found**********/double (*f)();double r1, r2;/**********found**********/f = f1 ; r1 = f(a);/**********found**********/f = f2;r2 = (*f)(a, b);return r1 + r2;}void main(){double x1=5, x2=3, r;r = fun(x1, x2);printf("\nx1=%f, x2=%f, x1*x1+x1*x2=%f\n",x1, x2, r);}

学点啥

52 将形参s所指字符串中所有数字字符顺序前移

修改前

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <ctype.h>char *fun(char *s){int i, j, k, n; char *p, *t;n=strlen(s)+1;t=(char*)malloc(n*sizeof(char));p=(char*)malloc(n*sizeof(char));j=0; k=0;for(i=0; i<n; i++){if(isdigit(s[i])) {/**********found**********/p[__1__]=s[i]; j++;}else{t[k]=s[i]; k++; }}/**********found**********/for(i=0; i<__2__; i++) p[j+i]= t[i];p[j+k]=0;/**********found**********/return __3__;}void main(){char s[80];printf("Please input: "); scanf("%s",s);printf("\nThe result is: %s\n",fun(s));}

修改后

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <ctype.h>char *fun(char *s){int i, j, k, n; char *p, *t;n=strlen(s)+1;t=(char*)malloc(n*sizeof(char));p=(char*)malloc(n*sizeof(char));j=0; k=0;for(i=0; i<n; i++){if(isdigit(s[i])) {/**********found**********/p[j]=s[i]; j++;}else{t[k]=s[i]; k++; }}/**********found**********/for(i=0; i<k; i++) p[j+i]= t[i];p[j+k]=0;/**********found**********/return p;}void main(){char s[80];printf("Please input: "); scanf("%s",s);printf("\nThe result is: %s\n",fun(s));}

学点啥

53 在形参ss所指字符串数组中

修改前

#include <stdio.h>#include <string.h>#define N 5#define M 10/**********found**********/void fun(char (*ss) __1__, int k){int i=0 ;/**********found**********/while(i< __2__) {/**********found**********/ss[i][k]=__3__; i++; }}void main(){char x[N][M]={"Create","Modify","Sort","skip","Delete"};int i;printf("\nThe original string\n\n");for(i=0;i<N;i++)puts(x[i]); printf("\n");fun(x,4);printf("\nThe string after deleted :\n\n");for(i=0; i<N; i++) puts(x[i]); printf("\n");}

修改后

#include <stdio.h>#include <string.h>#define N 5#define M 10/**********found**********/void fun(char (*ss) [M], int k){int i=0 ;/**********found**********/while(i< N) {/**********found**********/ss[i][k]=0; i++; }}void main(){char x[N][M]={"Create","Modify","Sort","skip","Delete"};int i;printf("\nThe original string\n\n");for(i=0;i<N;i++)puts(x[i]); printf("\n");fun(x,4);printf("\nThe string after deleted :\n\n");for(i=0; i<N; i++) puts(x[i]); printf("\n");}

54 逆置数组元素中的值

修改前

#include <stdio.h>void fun(int a[], int n){int i,t;/**********found**********/for (i=0; i<___1___; i++){t=a[i];/**********found**********/a[i] = a[n-1-___2___];/**********found**********/___3___ = t;}}void main(){int b[9]={1,2,3,4,5,6,7,8,9}, i;printf("\nThe original data :\n");for (i=0; i<9; i++)printf("%4d ", b[i]);printf("\n");fun(b, 9);printf("\nThe data after invert :\n");for (i=0; i<9; i++)printf("%4d ", b[i]);printf("\n");}

修改后

#include <stdio.h>void fun(int a[], int n){int i,t;/**********found**********/for (i=0; i<n/2; i++){t=a[i];/**********found**********/a[i] = a[n-1-i];/**********found**********/a[n - 1 - i] = t;}}void main(){int b[9]={1,2,3,4,5,6,7,8,9}, i;printf("\nThe original data :\n");for (i=0; i<9; i++)printf("%4d ", b[i]);printf("\n");fun(b, 9);printf("\nThe data after invert :\n");for (i=0; i<9; i++)printf("%4d ", b[i]);printf("\n");}

55 参数xx的前10个元素已经按升序排列好

修改前

#include <stdio.h>void func(int xx[], int num){int n1,n2,pos,i,j;pos=xx[9];if (num > pos)/**********found**********/xx[10] = __(1)___;else {for(i=0;i<10;i++){if(xx[i]>num){n1=xx[i];xx[i]=num;for(j=i+1;j<11;j++){n2=xx[j];xx[j] = n1;/**********found**********/_____(2)____;}/**********found**********/_____(3)_____;}}}}int main(){int xx[11] = {2,5,7,10,17,51,63,73,85,99};int i,num;printf("original array is:\n");for(i=0;i<10;i++) printf("%5d",xx[i]);printf("\n");printf("insert a new number:");scanf("%d", &num);func(xx, num);for(i=0;i<11;i++) printf("%5d", xx[i]);printf("\n");return 0;}

修改后

#include <stdio.h>void func(int xx[], int num){int n1,n2,pos,i,j;pos=xx[9];if (num > pos)/**********found**********/xx[10] = num;else {for(i=0;i<10;i++){if(xx[i]>num){n1=xx[i];xx[i]=num;for(j=i+1;j<11;j++){n2=xx[j];xx[j] = n1;/**********found**********/n1=n2;}/**********found**********/break;}}}}int main(){int xx[11] = {2,5,7,10,17,51,63,73,85,99};int i,num;printf("original array is:\n");for(i=0;i<10;i++) printf("%5d",xx[i]);printf("\n");printf("insert a new number:");scanf("%d", &num);func(xx, num);for(i=0;i<11;i++) printf("%5d", xx[i]);printf("\n");return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。