计算两n个自然数的和n和m(m<10000)之间所有数(包括n和m)的和(n和m从键盘输入)。

#include&stdio.h&int main(){&&& int N,m,n,&&& scanf("%d",&N);&&& while(N--)&&& {&&&&&& sum = 0;&&&&&& scanf("%d%d",&m,&n);&&&&&& while(m)&&&&&& {&&&&&&&&&& sum = sum + m/n;&&&&&&&&&& m = m/n;&&&&&& }&&&&&& printf("%d\n",sum);&&& }&&&&&& return 0;}5
阅读(...) 评论()导读:就爱阅读网友为您分享以下“完整的100套C上机试题”的资讯,希望对您有所帮助,感谢您对92to.com的支持!/*************found**************/
else if(m&=a[mid])
low=mid+1;
return(mid);
return(-1); }
void main()
int i,a[N]={-3,4,7,9,13,24,67,89,100,180},k,m;
printf(&a数组中的数据如下:&);
for(i=0;i&N;i++) /*找到指定学号的学生数据*/
/*返回学生记录*/
程序填空题:
请补充函数fun,该函数的功能是:建立一个带头结点的单向链表并输出到文件&out70.dat&和屏注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式
幕上,各结点的值为对应的下标,链表的结点数及输出的文件名作为参数传入。
printf(&%d &,a[i]);
printf(&Enter m: &);
scanf(&%d&,&m);
k=fun(a,m);
printf(&m=%d,index=%d\n&,m,k);
printf(&Not be found!\n&); } 3. 程序设计题:
学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数
fun,它的功能是:函数返回指定学号的学生数据,指定的序号在主函数中输入。若没找到指定的学号,在结构体变量中给学号置空串,给成绩置-1,作为函数值返回(用于字符串比较的函数是strcmp)。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include&stdio.h& #include&stdlib.h& #include&string.h& #define
16 typedef struct { char num[10]; }STREC;
fun(STREC *a, char *b) {
void main() {
STREC s[N]={{ &GA005&,85},{&GA003&,76},{&GA002&,69},{&GA004&,85},
{&GA001&,91},{&GA007&,72},{&GA008&,64},{&GA006&,87},
{&GA015&,85},{&GA013&,91},{&GA012&,64},{&GA014&,91},
{&GA011&,77},{&GA017&,64},{&GA018&,64},{&GA016&,72}};
char m[10];
printf(&The original data:\n&);
for(i=0;i&N;i++)
{ if(i%4==0)
printf(&\n&);
/*每行输出4个学生记录*/
printf(&%s %3d&,s[i].num,s[i].s);
printf(&\n\nEnter the number: &);
h=fun(s,m);
printf(&The data: &);
printf(&\n%s %4d\n&,h.num,h.s);
printf(&\n&);
h=fun(s, &GA013&);
/******************************/
wf=fopen(&out.dat&,&w&);
fprintf(wf,&%s %4d&,h.num,h.s);
fclose(wf);
/*****************************/ }
【参考答案】:1. (1) &out29.dat&, &w&(2) fputc(ch,fp)(3) fclose(fp)
2. (1)错误:void fun(int a[],int m)
正确:int fun(int a[],int m)
(2)错误:else if(m&=a[mid])
正确:else if(m&a[mid])
3. STREC fun(STREC *a, char *b)
STREC str={& \o&,-1};
/*若没找到指定的学号,在结构体变量中给学号置空串,给成绩置-1*/
for(i=0;i&N;i++)
if(strcmp(a[i].num,b)==0)
#include &stdio.h& #include &conio.h& #include &stdlib.h& typedef struct ss {
struct ss * }NODE;
void fun(int n,char*filename) {
NODE *h,*p,*s;
h=p=(NODE *)malloc(sizeof(NODE));
h-&data=0;
for(I=1;I&n;I++)
s=(NODE *)malloc (sizeof (NODE));
s-&data=【1】;
p-&next=NULL;
if((pf=fopen(filename,&w&))==NULL)
printf(&Can not open out98.dat!&);
fprintf(pf,&\n***THE LIST***\n&);
printf(&\n***THE LIST***\n&);
fprintf(pf,&%3d&,p-&data);
printf(&%3d&,p-&data);
if(p-&next!=NULL)
fprintf(pf,&-&&);
printf(&-&&);
fprintf(pf,&\n&);
printf(&\n&);
fclose(pf);
void main() {
char *filename=&out98.dat&;
system(&CLS&);
printf(&\nInput n:&);
scanf(&%d&,&n);
fun(n,filename); } 2. 程序改错题:
下列给定程序中的函数Creatlink的功能是:创建带头节点的单向链表,并为各节点数据域赋0到m-1的值。
请改正函数Creatlink中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include &stdio.h& #include &conio.h& #include &stdlib.h& typedef struct aa {
struct aa * } NODE;
NODE *Creatlink(int n, int m) { NODE *h=NULL,*p,*s;
s=(NODE *)malloc(sizeof(NODE));
/*************found**************/
p-&next=NULL;
for(i=1;i&n;i++)
{ s=(NODE *) malloc(sizeof(NODE)); /*************found**************/
s-&data=rand()%m;
s-&next=p-&p-&next=s;
s-&next=NULL;
/*************found**************/ }
outlink(NODE *h) { NODE
printf(&\n The LIST :\n\n HEAD&);
{ printf(&-&%d&,p-&data);
printf(&\n&); }
system(&CLS&);
head=Creatlink(8,22);
outlink(head); } 3.
程序设计题:
已知学生的记录由学号和学习成绩构成,N名学生的数据已存入a结构体数组中。请编写函数
fun,该函数的功能是:找出成绩最低的学生记录,通过形参返回主函数(规定只有一个最低分)。已给出函数的首部,请完成该函数。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include&stdio.h& #include&string.h& #include&conio.h& #include&stdlib.h& #define N 10
typedef struct ss { char num[10]; } STU;
fun(STU a[], STU *s) {
void main() {
STU a[N]={{ &A01&,81},{ &A02&,89},{ &A03&,66},{ &A04&,87},{ &A05&,77},
{ &A06&,90},{ &A07&,79},{ &A08&,61},{ &A09&,80},{ &A10&,71}},m;
system(&CLS&);
printf(&*****The original data*****\n&);
for(i=0;i&N;i++)
printf(&No=%s Mark=%d\n&, a[i].num,a[i].s);
fun(a,&m);
printf(&*****THE RESULT*****\n&);
printf(&The lowest :%s, %d\n&,m.num,m.s); /******************************/
wf=fopen(&out.dat&,&w&);
fprintf(wf,&%s, %d&,m.num,m.s);
fclose(wf);
/*****************************/
【参考答案】:1. (1) i
(2) p-&next=s
(3) p-&next 2. (1)错误:p-&next=NULL
(2)错误:s-&date=rand( )%
正确:s-&data=rand( )%(m - 1);
3. fun(STU a[], STU *s)
*s=a[0]; /*先认为第1个值最小*/
for(i=0;i&N;i++) /*如果在循环的过程中,发现比第1个值更小的则赋给*s*/
if(s-&s&a[i].s)
第25套 1. 程序填空题:
请补充main函数,该函数的功能是:求n!。
例如,7!=5040。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在main函数的横线上填入所编写的若干表达
式或语句。
#include &stdlib.h& #include &stdio.h& #include &conio.h& void main() {
system(&CLS&);
printf(&Input n: &);
scanf(&%d&,【1】);
for(【2】;I&=n;I++)
printf(&%d!=%ld\n&,n,f);
2. 程序改错题:
下列给定程序中,函数fun的功能是:求表达式s=aa…aa-…-aa-aa-a(此处aa…aa表示n个a,a和n的
值在1至9之间)的值。例如a=3,n=6,则以上表达式为:
其值是296298。a和n是函数fun的形参,表达式的值作为函数值传回main函数。
请改正程序中的错误,使它能计算出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include &conio.h& #include &stdio.h& long fun(int a,int n) {
/*************found**************/
long s=0,t=1;
/*************found**************/
for(j=0;j&=n;j++)
for(j=1;j&n;j++)
/*************found**************/
return(s); }
printf(&\nPlease enter a and n: &);
scanf(&%d%d&,&a,&n);
printf(&\nThe value of function is %ld\n&, fun(a,n)); } 3. 程序设计题:
请编写函数fun,其功能是计算并输出如下多项式的值:
Sn=l+1/1!+1/2!+1/3!+1/4!+…+1/n!
例如,若主函数从键盘给n输入15,则输出为S=2.718282。?
注意:n的值要求大于1但不大于100。部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &stdio.h& double fun(int n) {
void main() {
printf(&\nInput n: &);
scanf(&%d&,&n);
printf(&s=%f\n&,s);
/******************************/
out=fopen(&out.dat&,&w&);
fprintf(out,&%f\n&,fun(n));
fclose(out);
/******************************/
【参考答案】:1. (1) &n
(2) i=1或i=2
(3) f*=i 2. (1)错误:long s=0,t=1;
正确:long s=0, t=0;
(2)错误:for (j=0;j&=n;j++)
正确:for (j=0;j&n;j++)
(3)错误:t=t%10;
正确:t=t/10; 3. double fun(int n)
double t,sn=1.0;
for(i=1;i&=n;i++)
for(j=1;j&=i;j++)
sn+=1.0/t;
1. 程序填空题:
给定程序中,函数fun的功能是:将形参std所指结构体数组中年龄最大者的数据作为函数值返回,并在main函数中输出。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式
&stdio.h& typedef struct {
char name[10]; }STD;
STD fun(STD std[],int n) {
max=【1】;
for(i=1;i&n;i++)
if(max.age&【2】)
max=std[i]; }
STD std[5]={&aaa&,17,&bbb&,16,&ccc&,18,&eee&,15};
max=fun(std,5);
printf(&\nThe result is: \n &);
printf(&\nName :%s,Age :%d\n&,【3】,max.age); } 2. 程序改错题:
下列给定程序中,函数fun的功能是:对N名学生的学习成绩,按从高到低的顺序找出前
名学生来,并将这些学生数据存放在一个动态分配的连续存储区中,此存储区的首
地址作为函数值返回。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include &stdlib.h& #include
&conio.h& #include
&string.h& #include
&malloc.h& #define N 10
typedef struct ss
{ char num[10];
STU *fun(STU a[], int m) { STU b[N],*t;
int i, j,k;
/*************found**************/
*t=calloc(m,sizeof(STU));
for(i=0;i&N;i++) b[i]=a[i];
for(k=0;k&m;k++)
{ for (i=j=0;i&N;i++)
if(b[i].s&b[j].s) j=i; /*************found**************/
t[k].num=b[j].
t[k].s=b[j].s;
outresult(STU a[],FILE *pf) {
for(i=0;i&N;i++)
fprintf(pf, &No=%s Mark=%d\n &, a[i].num, a[i].s);
fprintf(pf, &\n\n &); }
void main()
{ STU a[N]={{ &A01 &,81},{ &A02 &,89},{ &A03 &,66},{ &A04 &,87},{ &A05 &,77},
{ &A06 &,90},{ &A07 &,79},{ &A08 &,61},{ &A09 &,80},{ &A10
system(&CLS&);
printf(&*****THE RESULT*****\n&);
outresult(a,stdout);
printf(&\nGive the number of the students who have better score: &);
scanf(&%d&,&m);
while(m&10)
{ printf(&\nGive the number of the students who have better score: &);
scanf(&%d&,&m); }
pOrder=fun(a,m);
printf(&***** THE RESULT*****\n&);
printf(&The top :\n&);
for(i=0;i&m;i++)
printf(&%s
%d\n&,pOrder[i].num, pOrder[i].s);
free(pOrder); } 3. 程序设计题:
学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:把分数最高的学生数据放在h所指的数组中。注意:分数高的学生可能不只一个,函数返回分数最高学生的人数。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &stdio.h& #define
16 typedef
struct { char num[10]; }STREC;
fun (STREC *a, STREC *b) {
void main () {
s[N]={{&GA005&,85},{&GA003&,76},{&GA002&,69},{&GA004&,85},
{&GA001&,91},{&GA007&,72},{&GA008&,64},{&GA006&,87},
{&GA015&,85},{&GA013&,91},{&GA012&,64},{&GA014&,91},
{&GA011&,66},{&GA017&,64},{&GA018&,64},{&GA016&,72}};
STREC h[N];
n=fun(s,h);
printf(&The %d highest score :\n&,n);
for (i=0; i&n; i++)
printf(&%s %4d\n &,h[i].num,h[i].s);
/*输出最高分学生的学号和成绩*/
printf(&\n&);
/******************************/
wf=fopen(&out.dat&,&w&);
fprintf(wf, &%d\n&,n);
for (i=0; i&n; i++)
fprintf(wf,&%s %4d
&,h[i].num,h[i].s);
fclose(wf);
/*****************************/
void main() { int n=5;
system(&CLS&);
printf(&\nThe result is %1f\n&,fun(n)); } 3.
程序设计题:
请编写函数fun,该函数的功能是:计算并输出
S=1+(1+20.5)+(1+20.5+30.5)+…+(1+20.5+30.5+…+n0.5)
例如,若主函数从键盘给n输入20后,则输出为S=534.188884。?
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
【参考答案】:1. (1) *std或std[0]
(2) std[i].age
(3) max.name 2. (1)错误:*t=calloc(m,sizeof(STU));
正确:t=calloc(m,sizeof(STU));
(2)错误:t[k].num=b[j].
正确:t[k]=b[j] ; 3. int fun (STREC *a, STREC *b)
int i,j=0,max=a[0].s;
for(i=0;i&N;i++)
if(max&a[i].s)
max=a[i].s;/*找出最大值*/
for(i=0;i&N;i++)
if(max==a[i].s) b[j++]=a[i];
/*找出成绩与max相等的学生的记录,存入结构体b中*/
/*返回最高成绩的学生人数*/
程序填空题:
请补允函数fun,该函数的功能是:计算并输出下列多项式的值。
1+2+3+??50
例如,若主函数从键盘给n输入50后,则输出为S=1.960784。?
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填若干表达式或语句。试题程序:
#include &stdio.h& 【1】 fun(int n) {
double sum=0.0,t;
for(i=1;i&=n;i++)
for(j=1;j&=i;j++)
sum+=【3】;
void main() {
printf(&\nInput n: &);
scanf(&%d&,&n);
printf(&\n\ns=%f\n\n&,s); } 2. 程序改错题:
下列给定程序中,函数fun的功能是:根据整型形参m,计算如下公式的值。
例如,若m的值为5,则应输出:1.463611。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
&conio.h& #include
&stdio.h& double fun (int m) { double y=1.0;
/*************found**************/
for(i=2;i&m;i++)
/*************found**************/
y+=1/(i*i);
return (y);
#include&math.h& #include&stdio.h& double fun(int n) {
void main() {
printf(&\n\nInput n: &);
scanf(&%d&,&n);
printf(&\n\ns=%f\n\n&,s);
/******************************/
wf=fopen(&out.dat&,&w&);
fprintf(wf,&%f&,fun(20));
fclose(wf);
/*****************************/ }
【参考答案】:1. (1) double
(3)1.0/t或(double)1/t
2. (1)错误:for (i=2;i&m;i++)
正确:for(i=2;i&=m;i++)
(2)错误:y+=1/(i*i) ;
正确:y+=1.0/(i*i) ;
3. double fun(int n)
double s=0.0,s1=0.0;
for(i=1;i&=n;i++)
s1=s1+pow(i,0.5);
/*求每一项*/
/*按公式求出s*/
1. 程序填空题:
人员的记录由编号和出生年、月、日组成,N名人员的数据已在主函数中存入结构体数组std
中,且编号唯一。函数fun的功能是:找出指定编号人员的数据,作为函数值返回,由主函数输出,若指定编号不存在,返回数据中的编号为空串。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式
#include &stdio.h& #include &string.h& #define N 8 typedef struct {
char num[10];
int year,month, }STU;
【1】 fun(STU *std,char *num) {
STU a={&&,};
for(i=0;i&=N;i++)
if(strcmp(【2】,num)==0)
return (【3】); }
1.程序填空题
给定程序的功能是:求二分之一的圆面积,函数通过形参得到圆的半径,函数返回二分之一的
圆面积(注:圆面积公式为:S?
r2,在程序中定义的变量名要与公式的变量相同)。
例如,输入圆的半径值:19.527,输出为:s=598.949991。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在横线上填入所编写的若干表达式或语句。
#include &stdio.h& double fun( double r) {
return 3.14159*【1】/2.0; }
void main() {
printf(&Enter x: &);
scanf(&%lf&,【2】);
printf(&s=%lf\n&,fun(【3】)); }
2.程序改错题
下列给定程序中,函数fun的功能是:计算S=f(-n)+f(-n+1)+…+f(0)+f(1)+f(2)+…f(n)的值。
例如,当n为5时,函数值应为10.407143。f(x)函数定义如下:
?(x?1)?(x?2)x?0??0
x?0或x?2?(x?1)?(x?2)x?0
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
&stdlib.h& #include
&conio.h& #include
&stdio.h& #include
/*************found**************/ f(double x) {
if(x==0.0||x==2.0)
return 0.0;
else if(x&0.0)
return(x-1)/(x-2);
return(x+1)/(x-2); }
double fun(int n) {
double s=0.0,y;
for(i=-n; i&=n;i++)
{ y=f(1.0*i); s+=y;}
/*************found**************/
return s }
void main()
{ system(&CLS&);
printf(&%f\n&, fun(5)); }
3.程序设计题
编写函数fun,函数的功能是:根据以下公式计算s,计算结果作为函数值返回;n通过形参传入。
S=1+1/(1+2)+1/(1+2+3)+…+1/(1+2+3+…+n)
例如,若n的值为11时,函数的值为1.833333。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &conio.h& #include &stdio.h& #include &string.h& #include &stdlib.h& float fun(int
void main() {
system(&CLS&);
printf(&\nPlease enter N: &);
scanf(&%d&,&n);
printf(&The result is:%f\n & , s); /******************************/
wf=fopen(&out.dat&,&w&);
fprintf (wf,&%f&,fun(11));
fclose(wf);
/*****************************/ }
【参考答案】:1.(1)r*r
.2. (1)错误:f(double x)
正确:double f(double x)
2)错误:return s
3. float fun(int n)
int i,s1=0;
/*定义整型变量s1,表示分母*/
float s=0.0;
/*定义单精度变量s,表示每一项*/
for(i=1;i&=n;i++)
/*求每一项的分母*/
s=s+1.0/s1;
/*求多项式的值*/
1.程序填空题
请补充main函数,该函数的功能是:计算两个自然数n和m(m&10000)之间所有数的和(n和m从键盘输入)。
例如,当n=1,m=100时,sum=5050;当n=100,m=1000时,sum=495550。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在main函数的横线上填入所编写的若干表达
式或语句。
#include &stdlib.h& #include &stdio.h& #include &conio.h& void main() {
system(&CLS&);
printf(&\nInput n,m\n&);
scanf(&%d,%d&,&n,&m);
while(n&=m)
printf(&sum=%【3】\n&,sum); }
2.程序改错题
下列给定程序中,fun函数的功能是:根据形参m,计算下列公式的值:
t=1+1/2+1/3+1/4+…+1/m
例如,若输入5,则应输出2.283333。
请改正程序中的错误,使它能计算出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include &stdlib.h& #include &conio.h& #include &stdio.h& double fun(int m) {
double t=1.0;
for(I=2;I&=m;I++)
/*************found**************/
/*************found**************/
return I; }
void main() {
system(&CLS&);
printf(&\nPlease enter 1integer number: &);
scanf(&%d&,&m);
printf(&\nThe result is %1f\n&, fun(m)); }
3.程序设计题
请编写一个函数 void fun(char *tt,int pp[]),统计在tt字符串中&a&到&z&26个字母各自出现的次
数,并依次放在pp所指数组中。
例如,当输入字符串abcdefghabcdeabc后,程序的输出结果应该是:3 3 3 2 2 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &conio.h& #include &stdio.h& #include &stdlib.h&
void fun(char *tt,int pp[] ) {
void main( ) {
char aa[1000];
system(&CLS&);
printf(&\nPlease enter a char string : &) ;
scanf(&%s&,aa);
fun(aa,bb);
for (k=0 ;k&26 ; k++)
printf(&%d &,bb[k]) ;
printf(&\n &);
/******************************/
wf=fopen(&out.dat&,&w&);
fun (&abcdefgabcdeabc&,bb) ;
for (k=0 ;k&26 ; k++)
fprintf(wf,&%d &,bb[k]) ;
fclose(wf);
/*****************************/ }
【参考答案】:1. (1) sum=0
(2) sum+=n
(3) ld 2. (1)错误:t+=1.0/k;
正确:t+=1.0/I;
(2)错误:return I;
3. void fun(char*tt,int pp[])
for(i=0;i&26;i++)
/*初始化pp数组各元素为0*/
for(;*tt!=' \0';tt++)
if(*tt&='a'&&*tt&='z')
pp[*tt-'a']++;
/*将tt字符串中'a'到'z'26个字母各自出现的次数,依次放在pp数组中,任一个小写字母减去'a'后所得结果正好是它对应的下标*/
1. 程序填空题
给定程序的功能是:判断字符ch是否与串str中的某个字符相同,若相同什么也不做,若不同则
插在串的最后。
注意:部分源程序给出如下。
请勿改动函数main和其他函数中的任何内容,仅在横线上填入所编写的若干表达式或语句。
&stdio.h& #include
&string.h&
void fun (char *str,char ch) {
while (*str &&*str !=ch) str++;
if(*str【1】ch)
void main() {char s[81],c;
system(&CLS&);
printf(&\nPlease enter a string : &); gets(s);
printf(&\n Please enter the character to search : &);
c=getchar();
fun(【3】);
printf(&\nThe result is %s\n&,s); }
2.程序改错题
下列给定程序中,函数fun的功能是:分别统计字符串中大写字母和小写字母的个数。例如,给字
符串s输入:AaaaBBb123CCccccd,则应输出结果:upper=5,lower=9。
请改正程序中的错误,使它能计算出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include&conio.h& #include&stdio.h&
/*********found***********/ void fun(char *s,int a,int b) {
/*********found***********/
if(*s&='A' && *s&='Z')
/*********found***********/
if(*s&='a' && *s&='z')
char s[100];
int upper=0,lower=0;
printf(&\nplease a string :&);
fun(s,&upper,&lower);
printf(&\n upper=%d lower=%d\n&,upper,lower); }
3.程序设计题
请编写函数fun,它的功能是:求出ss所指字符串中指定字符的个数,并返回此值。
例如,若输入字符串,输入字符1,则输出3。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &conio.h& #include &stdio.h& #include &stdlib.h& #define M 81
int fun(char *ss, char c) {
void main() {
char a[M],
system(&CLS&);
printf(&\nPlease enter a string: &);
printf(&\nPlease enter a char: &);
ch=getchar();
printf(&\nThe number of the char is: %d\n&, fun(a,ch)); /******************************/
wf=fopen(&out.dat&,&w&);
fprintf(wf,&%d&,fun(&&,'1'));
fclose(wf);
/*****************************/ }
【参考答案】:1. (1) !=
(2) str[1]或*(str+1)
2. (1)错误:void fun(char *s, int a, int b)
正确:void fun(char *s, int *a, int *b)
(2)错误:a++;
正确:(*a)++;
(3)错误:b++; 正确:(*b)++;
3. int fun(char *ss, char c)
for(;*ss!='\0';ss++)
if(*ss==c)
/*求出ss所指字符串中指定字符的个数*/
1. 程序填空题
请补充函数fun,该函数的功能是:把ASCII码为偶数的字符从字符串str中删除,结果仍然保存在字符串str中,字符串str从键盘输入,其长度作为参数传入函数fun。
例如,输入&abcdef&,输出&ace&。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式
#include &stdlib.h& #include &stdio.h& #define N 80 【1】 {
for (i=0;i&n;i++)
if(s[i]%2!=0)
s[j++]=s[i];
void main() {
int i=0, strlen=0;
char str[N];
system(&CLS&);
printf(&\nInput a string:\n&);
gets(str);
while(str[i]!='\0')
fun(str, strlen);
printf(&\n*** display string ***\n&);
puts (str); }
2.程序改错题
下列给定程序中函数fun的功能是:计算正整数num各位上的数字之积。例如,若输入252,则输
出应该是20。若输入202,则输出应该是0。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include &stdio.h& #include &conio.h& long fun(long num) {
/*************found**************/
k *=num%10;
/*************found**************/
}while(num);
return(k); }
printf(&\Please enter a number: &);
scanf(&%ld&,&n);
printf(&\n%ld\n&,fun(n)); }
2. 程序设计题
请编写一个函数fun,它的功能是:求出1到m(含m)之内能被7或11整除的所有整数放在数组a中,
通过n返回这些数的个数。
例如,若传给m的值为50,则程序输出:
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include&conio.h& #include&stdio.h& #include&stdlib.h& #define M 100
void fun(int m, int *a, int *n) {
void main() {
int aa[M],n,k;
system(&CLS&);
fun(50,aa,&n);
for(k=0;k&n;k++)
if((k+1)%20==0)
/*每行输出20个数*/
{printf(&%4d&,aa[k]);
printf(&\n&);
printf(&%4d&,aa[k]);
printf(&\n&);
/******************************/
wf=fopen(&out.dat&,&w&);
for(k=0;k&n;k++)
fprintf(wf,&%4d&,aa[k]);
fclose(wf);
/*****************************/ }
【参考答案】:1.(1) void fun(char s[],int n)
(3) s[j]='\0'
2. (1)错误:
正确:long k=1;
(2)错误:num\=10;
正确:num/ =10;
3. void fun(int m, int *a, int *n)
int i,j=0;
for(i=1;i&=m;i++)
if(i%7==0||i%11==0)
/*求出1到m(含m)之内能被7或11整除的所有整数放在数组a中*/
/*返回这些数的个数*/
1. 程序填空题
请补充函数fun,该函数的功能是:判断某一个年份是否为闰年。
例如,1900年不是闰年,2004是闰年。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式
#include &stdlib.h& #include &stdio.h& #include &conio.h& int fun(int n) {
int flag=0;
if(n%4==0)
if (【1】)
if (【2】)
return【3】; }
void main() {
system(&CLS&);
printf(&Input the year:&);
scanf(&%d&,&year);
if (fun(year))
printf(&%d is a leap year. \n&,year);
printf(&%d is not a leap year.\n&,year); }
2. 程序改错题
下列给定程序中函数fun的功能是:计算n!。例如,给n输入5,则输出120.000000。
请改正程序中的错误,使程序能输出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
&stdio.h& #include
&conio.h& double fun(int
n) { double result =1.0;
/*************found**************/
return 1.0;
while(n&1&&n&170)
/*************found**************/
result=n--; }
void main() {
printf(&Input
scanf(&%d&,&n);
printf(&\n\n%d!=%lf\n&,n,fun(n)); }
3. 程序设计题
请编写函数fun,其功能是:将两个两位数的正整数a、b合并成一个整数放在c中。合并的方式
是:将a数的十位和个位数依次放在c数的十位和千位上,b数的十位和个位数依次放在c数的个位和百位上。
例如,当a=45,b=12,调用该函数后,c=5241。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &stdio.h& #include &conio.h&
void fun(int a ,int b,long *c) {
void main() {
printf(&Input a ,b: &);
scanf(&%d%d&,&a,&b);
fun(a,b,&c);
printf(&The result is :%ld\n&,c);
/******************************/
out=fopen(&out.dat&,&w&);
fun(a+11,b+22,&c);
fprintf(out,&%ld\n&,c);
fclose(out);
/******************************/ }
【参考答案】:1. 程序填空题(1) n%100!=0或n%100
(2) n%400==0
2. 程序改错题
(1)错误:if n==0
正确:if(n==0)
(2)错误:result =n--;
正确:result *=n--;
3. 程序设计题
void fun(int a,int b,long *c)
*c=(a%10)*1000+(b%10)*100+(a/10)*10+b/10;
1. 程序填空题:
请补充函数fun,函数fun的功能是求n的阶乘。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式
#include &stdio.h& long fun(int
if (【1】)
return(n*fun(【2】));
return【3】; }
void main() {
printf(&10!=%ld\n&,fun(10)); } 2. 程序改错题 下列给定的程序中,函数fun的功能是:计算并输出k以内最大的10个能被13或17整除的自然数之和。k的值由主函数传入,若k的值为500,则函数的值为4622。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include &stdio.h& #include &conio.h& #include &stdlib.h&
int fun(int k)
{ int m=0,mc=0,
while((k&=2)&&(mc&10))
/*************found**************/
if((k%13=0)||(k%17=0))
{ m=m+k;mc++;}
/*************found**************/
void main() {system(&CLS&);
printf(&%d\n &,fun(500)); }
程序设计题
请编写函数fun,它的功能是计算:
s=(ln(1)+ln(2)+ln(3)+…+ln(m))??0.5?
在C语言中可调用log(n)函数求ln(n)。
例如,若m的值为20,则fun函数值为6.506583。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &conio.h& #include &stdio.h& #include &math.h& #include &stdlib.h& double fun(int m) {
void main() {
system(&CLS&);
printf(&%f\n &,fun(20));
/******************************/
wf=fopen(&out.dat&,&w&);
fprintf(wf,&%f&,fun(20));
fclose(wf);
/*****************************/ }
【参考答案】:1.程序填空题:(1)n&1或1&n
2.程序改错题:(1)错误:if((k%13=0)||(k%17=0))
正确:if((k%13==0)||(k%17==0))
(2)错误:缺少大括号 正确:加 } 3.程序设计题:double fun(int m)
double s=0.0;
for(i=1;i&=m;i++)
s=s+log(i);
return sqrt(s);
/*计算s=ln(1)+ln(2)+ln(3)+…+ln(m)*/
/*对s求平方根并返回*/
1.程序填空题
请补充函数fun,该函数的功能是:整除x且是偶数的数,把这些数保存在数组bb中,并按从大到
小的顺序输出。
例如当x=20时,依次输出:20 10 4 2。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式
#include &stdlib.h& #include &conio.h& #include &stdio.h&
void fun(int k,int bb[]) {
for(【1】;I&=k;I++)
bb[j++]=I;
}printf(&\n\n&);
for(I=【3】;I&=0;I--)
printf(&%d &,bb[I]); }
void main() {
int bb[100];
system(&CLS&);
printf(&\nPlease input X=&);
s=sqrt(s);
/*对s求平方根*/
1.程序填空题:
请补充函数fun,它的功能是:计算并输出n(包括n)以内所有能被3或7整除的自然数的倒数之
例如,在主函数中从键盘给n输入30后,输出为:s=1.226323。?
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式
scanf(&%d&,&k);
fun(k,bb); }
2程序改错题
下列给定程序中,函数fun()的功能是:根据整型形参m的值,计算如下公式的值:
t=1-1/(2?2)?1/(3?3)?...?1/(m?m)
请改正函数fun()中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include &stdlib.h& #include &conio.h& #include &stdio.h& double fun(int m) {double y=1.0;
/*************found**************/
for(I=2;I&m;I++)
/*************found**************/
y-=1/(I*I);
return(y); }
void main() {int n=5;
system(&CLS&);
printf(&\nThe result is %1f\n&, fun(n)); }
3.程序设计题
编写函数fun,它的功能是:求n以内(不包括n)同时能被5与11整除的所有自然数之和的平方根s,
并作为函数值返回。
例如,n为1000时,函数值应为s=96.979379。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
&conio.h& #include
&math.h& #include
&stdio.h& #include &stdlib.h& double
fun(int n) {
void main() {
system(&CLS&);
printf(&s=%f\n&, fun(1000)); /******************************/
wf=fopen(&out.dat&,&w&);
fprintf(wf,&%f&,fun(1000));
fclose(wf);
/*****************************/ }
【参考答案】:
1.程序填空题:(1)i=1
(2)k%i==0&&i%2==0
(3)--j或j-1
2.22.程序改错题:(1)错误:for(i=2;i&m;i++)
正确:for(i=2;i&=m;i++)
(2)错误:y-=1/(i*i);
正确:y-=1.0/(i*i);
3.程序设计题:double fun(int n)
{double s=0.0;
for(i=0;i&n;i++)
/*从0 n中找到既能被5整除同时又能被11整除的数,并将这些数求和*/
if(i%5==0&&i%11==0)
#include &stdio.h& double fun(int n) {
double sum=0.0;
for(I=1;【1】;I++)
if(I%3==0【2】I%7==0)
sum+=【3】/I; }
void main() {
printf(&\nInput n:&);
scanf(&%d&,&n);
printf(&\n\ns=%f\n&,s); }
2.程序改错题:在主函数中从键盘输入若干个数放入数组中,用0结束输入并放在最后一个元
素中。下列给定程序中,函数fun的功能是:计算数组元素中值为正数的平均值(不包括0)。例如,数组中元素的值依次为39,-47,21,2,-8,15,0,则程序的运行结果为19.250000。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include &conio.h& #include &stdio.h& double
fun(int x[]) {
/*************found**************/
int sum=0.0;
while(x[I]!=0)
if(x[I]&0)
sum+=x[I];
/*************found**************/
sum=sum\c; }
void main() {
system(&CLS&);
printf(&\nPlease enter some data(end with 0) :&);
scanf(&%d&,&x[i]);
}while(x[i++]!=0);
printf(&%f\n&,fun(x)); }
3 程序设计题:
请编写函数fun,其功能是:将所有大于1小于整数m的非素数存入xx所指数组中,非素数的个数通过k传回。
例如,输入17,则应输出4 6 8 9 10 12 14 15 16。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include&conio.h& #include&stdio.h&
#include&stdlib.h&
void fun(int m, int *k, int xx[]) {
void main() {
int m, n, zz[100];
system(&CLS&);
printf(&\nPlease enter an integer number between 10 and 100: &);
scanf(&%d&,&n);
fun(n,&m,zz);
printf(&\n\nThere are %d non-prime numbers less than %d: &,m,n);
for(n=0;n&m;n++)
printf(&\n %4d&,zz[n]);
/******************************/
wf=fopen(&out.dat&,&w&);
fun(17,&m,zz);
for(n=0;n&m;n++)
fprintf(wf,&%4d&,zz[n]);
fclose(wf);
/*****************************/ }
【参考答案】:1.程序填空题:(1)i&=n
(3)1.0或(double)1
2.程序改错题:(1)错误:int sum=0.0;
正确:double sum=0.0;
(2)错误:sum\=c;
正确: sum/=c; 3.程序设计题:void fun(int m, int *k, int xx[])
int i,j,n=0;
for(i=4;i&m;i++)
/*找出大于1小于整数m的非素数*/
{for(j=2;j&i;j++)
if(i%j==0)
if(j&i)xx[n++]=i;
/*返回非素数的个数*/
1.程序填空题:
请补充main函数,该函数的功能是:计算每个学生成绩的平均分,并把结果保存在数组bb中。
例如,当score[N][M]={{83.5,82,86,65,67},{80,?91.5?,84,99,95},{90.5,95,86,95,97}}时,三个
学生的平均分为76.7,89.9,92.7。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在main函数的横线上填入所编写的若干表达
式或语句。
#include &stdio.h& #define N 3 #define M 5 void main() {
static float
score[N][M]={{83.5,82,86,65,67},{80,91.5,84,99,95},{90.5,95,86,95,97}};
float bb[N];
system(&CLS&);
for(I=0;I&N;I++)
for(I=0;I&N;I++)
for(j=0;j&M;j++)
for(I=0;I&N;I++)
printf(&\nstudent%d\taverage=%5.1f&,I+1,bb[I]); }
2.程序改错题:
下列给定程序中,函数fun的功能是:判断一个整数m是否是素数,若是返回1,否则返回0。在main
函数中,若fun()返回1则输出&YES&,若fun()返回0则输出&NO!&。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
&stdlib.h& #include
&conio.h& #include
&stdio.h& int fun(int m)
{ int k=2;
while (k&=m&&(m%k))
/*************found**************/
/*************found**************/
else return 0; }
void main() {
system(&CLS&);
printf(&\nPlease enter n: &); scanf(&%d&,&n);
if(fun(n)) printf(&YES\n&);
else printf(&NO!\n&); }
3.程序设计题:
编写函数fun,它的功能是:根据以下公式求p的值,结果由函数值带回。m与n为两个正数且要求
m&n。P=m!/(n!(m-n)!),例如,m=12,n=8时,运行结果为495.000000。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &conio.h& #include &stdio.h& #include &stdlib.h&
float fun (int m, int n) {
void main () {
system(&CLS&);
printf (&p=%f\n&,fun (12,8) ) ; /******************************/
wf=fopen(&out.dat&,&w&);
fprintf (wf,&%f&,fun(12,8));
fclose(wf);
/*****************************/ }
【参考答案】:1.程序填空题:(1) bb[i]=0
(2) bb[i]+=score[i][j] 2.程序改错题:(1)错误:k++
(2)错误:if(m=k)
正确:if(m==k)
3.程序设计题:float fun (int m, int n)
double p=1.0;
for(i=1;i&=m;i++)
p=p*i; /*求m!*/
for(i=1;i&=n;i++)
p=p/i; /*求m!/n!*/
for(i=1;i&=m-n;i++)
p=p/i;/*求m!/(n!(m-n)!)*/
1.程序填空题:
请补充函数fun(char*s),该函数的功能是:把字符串中的内容逆置。
例如,字符串中原有的字符串为abcde,则调用该函数后,串中的内容为edcba。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式
#include &string.h& #include &conio.h& #include &stdio.h& #define N 81
void fun(char *s) {
int i,n=strlen(s)-1;
for(i=0;i&n;i++,【1】)
char a[N];
system(&CLS&);
printf(&Enter a string:&);
printf(&The
original string is:&);
printf(&\n&);
printf(&The string after modified:&);
puts(a); }
2.程序改错题
2.程序改错题:(1)错误:int fun(n) (2)错误:if (n=1)
3.程序设计题:float fun (float h)
/*单精度数h乘以1000后再加5,相当于对h中的第三位小数进行四舍五入,除以10后将其赋给一个长整型数时,把第三位小数后的数全部截去*/
return (float)t/100;
/*除以100,保留2位小数*/
1.程序填空题:
正确:int fun (int n)
正确:if (n==1)
请补充main函数,该函数的功能是:输出一个N?N矩阵,要求非周边元素赋值0,周边元素
下列给定程序中,函数fun的功能是:按如下递归公式求函数值。
fun(n)=?10
?fun(n?1)?2
例如,当给n输入5时,函数值为18;当给n输入3时,函数值为14。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
/*************found**************/ int fun(n) {
/*************found**************/
c=fun(n-1)+2;
return(c); }
void main() {
printf(&Enter n: &);
scanf(&%d&,&n);
printf(&The result: %d\n\n& ,fun(n));
3.程序设计题
请编一个函数 float fun(double h),函数的功能是:对变量h中的值保留两位小数,并对第三位进
行四舍五入(规定h中的值为正数)。
例如,若h 值为8.32433,则函数返回8.32;若h值为8.32533,?则函数返回8.33。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &stdio.h& #include &conio.h& #include &stdlib.h& float fun (float h ) {
void main() {
system(&CLS&);
printf(&Enter a: &);
scanf (&%f&,&a);
printf(&The original data is :
printf(&%f\n\n&, a);
printf(&The
result : %f\n&, fun(a)); /******************************/
wf=fopen(&out.dat&,&w&);
fprintf(wf,&%f&,fun(8.32533));
fclose(wf);
/*****************************/ }
【参考答案】:1.程序填空题:(1) n--或--n或n-=1或n=n-1
(2) s[i]=s[N]或*(s+i)=*(s+n)
(3) s[N]=t或*(s+n)=t
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在main函数的横线上填入所编写的若干表达
式或语句。
#include &stdlib.h& #include &stdio.h& #define N 10 void main() {
int bb[N][N];
int I,j,n;
system(&CLS&);
printf(& \nInput n:\n&);
scanf(&%d&,&n);
for(I=0;I&n;I++)
for(j=0;j&n;j++)
bb[I][j]=1;
printf(& \n*** the result *** \n&);
for(I=0;I&n;I++)
printf(& \n\n&);
for(j=0;j&n;j++)
printf(&%4d&,bb[I][j]);
2.程序改错题:
下列给定程序中,函数fun的功能是:用递归算法计算斐波拉契级数列中第n项的值。从第一项起,斐波拉契级数序列为1,1,2,3,5,8,13,21,…例如,若给n输入7,该项的斐波拉契级数值为13。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include &stdio.h& long fun(int g) {
/*************found**************/
switch(g);
{case 0:return 0;
/*************found**************/
case 1; case 2:return 1;
return (fun(g-1)+fun(g-2)); }
void main() {
printf(&Input n:&);scanf(&%d&,&n);
printf(&n=%d\n&,n);
fib=fun(n);
printf(&fib=%d\n\n&,fib); }
3.程序设计题:
请编写函数fun,函数的功能是:求出二维数组周边元素之和,作为函数值返回。二维数组中的值
在主函数中赋予。
例如,若二维数组中的值为
则函数值为61。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
puts(str);
2.程序改错题:
下列给定程序中,函数fun的功能是:求出数组中最大数和次最大数,并把最大数和a[0]中的数
#include&conio.h& #include&stdio.h& #include&stdlib.h& #define
int fun( int a [M][N]) {
void main() {
int aa[M][N]={{1,3,5,7,9},{2,9,9,9,4},{6,9,9,9,8},{1,3,5,7,0}};
system(&CLS&);
printf (&The original data is :\n &);
for(i=0; i&M;i++)
{for (j=0; j&N;j++)
printf(&%6d &,aa[i][j]);
printf(&\n &);
y=fun(aa);
printf(&\nThe sun: %d\n &,y);
printf(&\n &);
/******************************/
wf=fopen(&out.dat&,&w&);
fprintf (wf,&%d&,y);
fclose(wf);
/*****************************/ }
【参考答案】:1.程序填空题:(1)i==0||i==n-1||j==0||j==n-1
(2) bb[i][j]=0 2.程序改错题:(1)错误:switch(g);
正确:去掉分号
(2)错误:case 1;case 2: return 1;
正确:case 1:case 2:return 1;
3.程序设计题:int fun(int a[M][N])
int i,j,sum=0;
for(i=0;i&M;i++)
for(j=0;j&N;j++)
if(i==0||i==M-1||j==0||j==N-1) /*只要下标中有一个为0或M-1或N-1,则它一定是周边元素*/
sum=sum+a[i][j]; /*将周边元素相加*/
:1.程序填空题:
请补充main函数,该函数的功能是:从键盘输入一个字符串及一个指定字符,然后把这个字符
及其后面的所有字符全部删除。结果仍然保存在原串中。
例如,输入&abcdef&,指定字符为&e&,则输出&abcd&。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在main函数的横线上填入所编写的若干表达
式或语句。
#include &stdlib.h& #include &stdio.h& #define N 80 void main() {
char str[N];
system(&CLS&);
printf(&\n Input a string:\n&);
gets(str);
printf(&\n I\n&);
scanf(&%c&, &ch);
while(str[I]!='\0')
if(str[I]==ch)
str[I]=【3】;
printf(&\n*** display string ***\n&);
对调、次最大数和 a[1] 中的数对调。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
&conio.h& #include
&stdio.h& #define
/*************found**************/ void fun(int *a,int n); {
for(I=0; I&n;I++)
/*************found**************/
for(k=I+1; k&n; k++)
if(a[k]&a[m])
a[I]=a[m];
int b[N]={11,5,12,0,3,6,9,7,10,8},n=10,i;
system(&CLS&);
for(i=0; i&n; i++)
printf(&%d &,b[i]);
printf(&\n&);
for(i=0; i&n; i++)
printf(&%d &, b[i]);
printf(&\n&); }
3.程序设计题:
编写函数fun,它的功能是:利用以下所示的简单迭代方法求方程cos(x)-x=0 的一个实根。
xn+1=cos(xn)
迭代步骤如下:
(1)取x1初值为0.0;
(2)x0=x1,把x1的值赋给x0;
(3)x1=cos(x0),求出一个新的
(4)若x0-x1,的绝对值小于0.000001,则执行步骤 (5),否则执行步骤(2);
(5)所求x1就是方程cos(x)-x=0的一个实根,作为函数值返回。
程序将输出结果Root=0.739085。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &stdio.h& #include &conio.h& #include &math.h& float fun() {
float f=fun();
printf(&Root=%f\n&,f);
/******************************/
out =fopen(&out.dat&,&w&);
fprintf(out,&%f&,f);
fclose(out);
/******************************/ }
【参考答案】:1.程序填空题:(1)
(2) i++或++i或i+=1或i=i+1
(3)'\0'
2.程序改错题:(1)错误:void fun(int *a, int n);
正确:void fun(int *a, int n)
(2)错误:m=0;
正确:m=i; 3.程序设计题:float fun( )
float x1=0.0,x0;do
x1=cos(x0);
}while (fabs(x0-x1)&=1e-6);
return x1;
1.程序填空题:
请补充main函数,该函数的功能是:从键盘输入3个整数,然后找出最大的数并输出。
例如,输入12,45,43,最大值为45。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在main函数的横线上填入所编写的若干表达
式或语句。
#include &stdlib.h& #include &stdio.h& #include &conio.h& void main() {
int a, b, c,
system(&CLS&);
printf(&\nInput three numbers:\n&);
scanf(&%d,%d,%d&,&a,&b,&c);
printf(&The three numbers are:%d,%d,%d\n&,a,b,c);
printf(&max=%d\n&,max); }
2.程序改错题:下列给定程序中,函数fun的功能是:将十进制正整数m转换成
进制数,并按位输出。例如,若输入8和2,则应输出1000(即十进制数8转换成二进
制表示是1000)。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include &stdio.h& #include &conio.h&
/*************found**************/ void fun(int m,int k); { int aa[20],
for(i=0;m;i++)
/*************found**************/
aa[i]=m/k;
for(;i;i--)
/*************found**************/
printf(&%d&,aa[i]); }
printf(&\nPlease enter a number and a base:\n&);
scanf(&%d%d&,&n,&b);
printf(&\n &); }
3.程序设计题:
假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:将字符串中的前导*号全
部删除,中间和后面的*号不删除。
例如,若字符串中的内容为****A*BC*DEF*G*******,删除后,字符串中的内容则应当是
A*BC*DEF*G*******。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &stdio.h& #include &conio.h& void
(char *a) {
void main() {
char s[81],t[81]=&****A*BC*DEF*G*******&;
printf(&Enter a string :\n&);
printf(&The string after deleted:\n&);puts(s); /******************************/
wf=fopen(&out.dat&,&w&);
fprintf(wf,&%s&,t);
fclose(wf);
/*****************************/ }
【参考答案】:1.程序填空题:(1) max=a
2.程序改错题:(1)错误:void fun(int m, int k);
正确:void fun(int m, int k)
(2) 错误:aa[i]=m/ 正确:aa[i]=m%k;
错误:printf(&%d&, aa[i]) ;
正确:printf(&%d&, aa[i-1]);
3.程序设计题:void fun (char *a)
char *p=a;
while(*p=='*')
/*指针p指向字符串第一个字母*/
for(;*p!='\0';p++,a++)
/*从第一个字母开始,其后的字符都放入指针a所指的字符串中*/
*a='\0';
/*在字符串最后加上结束标记符*/
1.程序填空题:
函数fun的功能是:从三个形参a、b、c中找出中间的数,作为函数值返回。
例如,当a=3,b=5,c=4时,中间的数为4。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式
int fun(int a,int b,int c) {
t=(a&b)?(b&c?b:(a&c?c:【1】))
:((a&c)?【2】
:((b&c)?c:【3】));
int a1=3,a2=5,a3=4,r;
r=fun(a1,a2,a3);
printf(&\nThe
middle number is: %d\n &,r); }
2.程序改错题: 下列给定程序中,函数fun的功能是:计算函数F(x,y,z)=(x+y)/(x-y)+(z+y)/(z-y)的值。其中x和y
不相等,z和y不等。例如,当x的值为9,y的值为11,z的值为15时,函数值为-3.50。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
&stdlib.h& #include
&stdio.h& #include
/*************found**************/ #define FU(m,n) (m/n)
float fun(float a,float b,float c) {
value=FU(a+b,a-b)+FU(c+b,c-b); /*************found**************/
return(Value); }
void main()
{ float x,y,z,
printf(&Input x y z:: &);
scanf(&%f%f%f&,&x,&y,&z);
printf(&x=%f,y=%f,z=%f\n &,x,y,z);
if(x==y||y==z)
{ printf(&Data error! \n &);
sum=fun(x,y,z);
printf(&The result is:%5.2f\n &,sum); }
3.程序设计题:
请编写函数fun,该函数的功能是:判断字符串是否为回文,若是则函数返回1,主函数中输出
&YES&,否则返回0,主函数中输出&NO&。回文是指顺读和倒读都一样的字符串。
例如,字符串LEVEL是回文,而字符串123312就不是回文。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &stdio.h& #define N 80
int fun(char *str) { }
char s[N];
char *test[]={&1234321&,&123421&,&123321&,&abcdCBA&};
printf(&Enter a string : &);
printf(&\n\n&);
if(fun(s))
printf(&YES\n&);
printf(&NO\n&);
/************************************/
out=fopen(&out.dat&,&w&);
for(i=0;i&4;i++)
if(fun(test[i]))
fprintf(out,&YES\n&);
fprintf(out,&NO\n&);
fclose(out);
/************************************/ }
【参考答案】:1、程序填空题:(1) a
(3) b 2.程序改错题:(1)错误:#define FU(m,n) (m/n)
正确:#define FU(m,n) (m)/(n)
(2) 错误:return(Value);
正确:return (value);
3. 程序设计题:int fun(char *str)
int i,n=0,fg=1;
while (*p)
/*将指针p置位到字符串末尾,并统计字符数*/
for(i=0;i&n/2;i++)
/*循环比较字符*/
if(str[i]==str[n-1-i]);
/*相同,什么都不作*/
/*不同,直接跳出循环*/
1、 程序填空题
函数fun的功能是:统计长整数n的各位上出现数字1、2、3的次数,并用外部(全局)变量c1、c2、
c3返回主函数。
例如,当n=时,结果应该为:c1=3 c2=1 c3=2。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式
#include &stdio.h&
int c1,c2,c3; void fun(long n) {
c1=c2=c3=0;
switch(【1】)
c1++;【2】;
c2++;【3】;
printf(&\nThe result: \n&);
printf(&n=%ld c1=%d c2=%d c3=%d\n&,n,c1,c2,c3);
2、 程序改错题
下列给定程序中函数fun的功能是:先将在字符串s中的字符按逆序存放到t串中,然后把s中的
字符按正序连接到t串的后面。例如,当s中的字符串为ABCDE时,则t中的字符串应为EDCBAABCDE。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
&stdlib.h& #include
&conio.h& #include
&stdio.h& #include
&string.h&
void fun (char *s, char *t ) {
s1=strlen(s);
for (I=0;I&s1;I++)
/*************found**************/
t[I]=s[s1-1];
for (I=0;I&s1;I++)
t[s1+I]=s[I];
t[2*s1]='\0'; }
void main()
{char s[100], t[100];
system(&CLS&);
printf(&\nPlease enter string s: &);
scanf(&%s&,s);
printf (&The result is: %s\n&,t); }
3.程序设计题
请编一个函数fun(char *s),该函数的功能是把字符串中的内容逆置。
例如,字符串中原有的字符串为abcdefg,则调用该函数后,串中的内容为gfedcba。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &string.h& #include &conio.h& #include &stdio.h& #define N 81 void fun(char*s) { }
char a[N];
printf(&Enter a string:&);
printf(&The
original string is:&);
printf(&\n&);
printf(&The string after modified:&);
strcpy(a,&Hello World!&);
/******************************/
out=fopen(&out.dat&,&w&);
fprintf(out,&%s&,a);
j=fun(x1,x2,x3);
printf(&The minimal common multiple is: %d\n&,j); }
3. 程序设计题:
请编写一个函数fun,它的功能是:将一个数字字符串转换为一个整数(不得调用C语言提供的将字符串转为整数的函数)。
例如,若输入字符串&-1234&,则函数把它转换为整数值-1234。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
fclose(out);
/******************************/ }
【参考答案】:1. (1) n%10
(3) break 2. (1)错误:t[i]=s[s1-1]; 正确:t[i]=s[s1-1-i]; 3. void fun(char *s)
int i,m,n;
m=n=strlen(s)-1;
while(i&(n+1)/2)
s[i]=s[m];
1. 程序填空题:
请补充main函数,该函数的功能是:打印出1 1000中满足个位数字的立方等于其本身所有数。 本题的结果为:1 64 125 216 729
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数main的横线上填入所编写的若干表达
式或语句。
#include &stdlib.h& #include &stdio.h& void main() {
system(&CLS&);
for(I=1;I&1000;I++)
printf(&%4d&,I);
2. 程序改错题:
下列给定程序中,函数fun的功能是:求三个数的最小公倍数。例如,给变量x1、x2、x3分别输入
15、11、2,则输出结果应当是330。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include &stdio.h&
int fun(int x,int y,int z) { int j,t,n,m;
/*************found**************/
/*************found**************/
while (t!=0 && m!=0 && n!=0)
void main() {
int x1,x2,x3,j;
printf(&Input x1 x2 x3: &);
scanf(&%d%d%d&,&x1,&x2,&x3);
printf(&x1=%d,x2=%d,x3=%d \n&,x1,x2,x3);
#include &stdio.h& #include &string.h& long fun(char *p) {
void main() {
char s[6];
printf(&Enter a string:\n&);
printf(&%ld\n&,n);
/******************************/
wf=fopen(&out.dat&,&w&);
fprintf (wf,&%ld&,fun(&-1234&));
fclose(wf);
/*****************************/ }
【参考答案】:1. (1) i%10
(2) g*g*g==i 2. (1)错误:j=1;
(2)错误:while (t!=0 && m!=0 && n!=0)
正确:while (t!=0 || m!=0 || n!=0) 3. long fun(char *p)
int flag=1;
if(*p=='-')
/*负数时置flag为-1*/
{p++;flag= -1;}
else if(*p=='+')
/*正数时置flag为1*/
while(*p!='\0'
n=n*10+*p-'0';/*将字符串转成相应的整数*/
1. 程序填空题:
请补充main函数,该函数的功能是:打印出满足个位的数字、十位上的数字和百位上的数字都
相等的所有三位数。
本题的结果为:
111 222 333 444 555 666 777 888 999。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在main函数的横线上填入所编写的若干表达
式或语句。
#include &stdlib.h& #include &stdio.h& void main() {
system(&CLS&);
for(g=1; g&10; g++)
for (s=1; s&10; s++)
for(b=1; b&10; b++)
printf(&%5d&,【2】);
2. 程序改错题:
下列给定程序中,函数fun的功能是:从n个学生的成绩中统计出低于平均分的学生人数,此人数
由函数值返回,平均分存放在形参aver所指的存储单元中。例如输入8名学生的成绩:
80.5 60 72 90.5 98 51.5 88 64
则低于平均分的学生人数为4(平均分为75.5625)。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include &stdlib.h& #include &stdio.h& #include &conio.h& #define N 20
int fun(float *s, int n,float *aver) /*将下标为奇数位置的字母转换成大写*/
ss[i]=ss[i]-32;
ss[i]='\0'; }
1. 程序填空题:
请补充main函数。该函数的功能是:求1 100(不包括100)以内所有素数的平均值。
程序运行后的结果为42.40。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在main函数的横线上填入所编写的若干表达
{ float ave ,t=0.0;
int count=0,k,i;
for(k=0;k&n;k++)
/*************found**************/
for(i=0;i&n;i++)
if(s[i]&ave) count++;
/*************found**************/
void main()
{ float s[30],
system(&CLS&);
printf(&\nPlease enter m: &);
scanf(&%d&,&m);
printf(&\nPlease enter %d mark :\n&,m);
for(i=0;i&m;i++) scanf(&%f&,s+i);
printf(&\nThe number of students :%d\n&,fun(s,m,&aver));
printf(&Ave=%f\n&,aver); }
3. 程序设计题:
请编写一个函数void fun(char *ss),其功能是:将字符串ss中所有下标为奇数位置上的字母转换为大写(若该位置上不是字母,则不转换)。
例如,若输入abc4EFg,则应输出aBc4EFg。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &conio.h& #include &stdio.h& #include &string.h& void fun ( char *ss) {
void main() {
char tt[51]; FILE *
printf(&Please enter an character string within 50 characters:\n&); gets(tt);
printf(&\n\nAfter changing,the string\n%s&,tt); fun(tt);
printf(&\nbecomes\n %s&,tt);
/******************************/ out=fopen(&out.dat&,&w&);
strcpy(tt,&After changing the string&); fun(tt);
fprintf(out,&%s&,tt); fclose(out);
/******************************/ }
【参考答案】:1. (1) g==s&&s==b或者g==b&&s==b或者g==s&&g==b
(2) g+s*10+b*100
.2. (1)错误:t=s[k];
正确:t+=s[k];
(2)错误:*aver=& 正确:*aver= 3. void fun(char *ss)
int i,n=0;
for(i=0;i&n;i++)
if((ss[i]&='a'&&ss[i]&='z')&&i%2!=0)
式或语句。
#include &stdlib.h& #include &stdio.h& void main() {
int I, j, n=0,
float aver=0;
system(&CLS&);
for(j=2; j&100; j++)
for(I=2; I&j; I++)
printf(&\n\n average=%4.2f\n&,【3】); }
2. 程序改错题:
下列给定程序中,函数fun的功能是:在字符串的最前端加入n个*号,形成新串,并且覆盖原串。 注意:字符串的长度最长允许为79。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
&stdio.h& #include
&string.h& #include
void fun(char s[],int n)
char a[80],*p;
/*************found**************/
for(I=0;I&n;I++)
a[I]= '*';
/*************found**************/
___填空___
}while(*p);
/*************found**************/
a[I]='0';
strcpy(s,a); }
void main()
{char s[80];
system(&CLS&);
printf(&\nEnter a string: &);gets(s);
printf(&\nThe string:%s\n&,s);
printf(&\nEnter n (number of*):&);
scanf(&%d&,&n);
printf(&\nThe string after inster:%s\n&,s); }
3. 程序设计题:
编写一个函数,该函数可以统计一个长度为2的字符串在另一个字符串中出现的次数。例如,
假定输入的字符串为asd asasdfg asd as zx67 asd mklo,子字符串为as,则应当输出6。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &conio.h& #include &stdio.h& #include &string.h&#include &stdlib.h&
int fun(char *str, char *substr) {
void main() {
char str[81],substr[3];
system(&CLS&);
printf(&输入主字符串: &);
gets(str);
printf(&输入子字符串: &);
gets(substr);
puts(str);
puts(substr);
n=fun(str,substr);
printf(&n=%d\n &,n);
/******************************/
wf=fopen(&out.dat&,&w&);
n=fun(&asd asasdfg asd as zx67 asd mklo&,&as&);
fprintf(wf,&%d&,n);
fclose(wf);
/*****************************/ }
【参考答案】:1. (1) j%i==0
(2) flag==1
(3) aver/n 2. (1)错误:s=p;
(2)应填p++;或++p;或p+=1;或p=p+1;
(3)错误:a[i]='0';正确:a[i]='\0';或a[i]=0
3. int fun(char *str, char *substr)
int i,j=0;
for(i=0;str[i+1]!='\0';i++)
/*如果一个长度为2的子字符串在主字符串中出现一次,则j+1,如此循环*/
if(str[i]==substr[0]&&str[i+1]==substr[1])
/*返回子字符串在主字符串中出现的次数*/
1. 程序填空题:
请补充main函数,该函数的功能是:把文本文件B中的内容追加到文本文件A的内容之后。
例如,文件B的内容为&I?m ten.&,文件A的内容为&I?m a student!&,追加之后文件A的内容为
&I?m a student ! I?m ten.&。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数main的横线上填入所编写的若干表达
式或语句。
#include &stdlib.h& #include &stdio.h& #include &conio.h& #define N 80 void main() {
FILE *fp,*fp1,*fp2;
char c[N],
system(&CLS&);
if((fp=fopen(&A.dat&,&r&))==NULL)
printf(&file A cannot be opened\n&);
printf(&\n A contents are : \n\n&);
for(i=0;(ch=fgetc(fp))!=EOF;i++)
putchar(c[i]);
fclose(fp);
if((fp=fopen(&B.dat&,&r&))==NULL)
printf(&file B cannot be opened\n&);
printf(&\n\n\nB contents are : \n\n&);
for(i=0;(ch=fgetc(fp))!=EOF;i++)
putchar(c[i]);
fclose(fp);
if((fp1=fopen(&A.dat&,&a&)) 【1】 (fp2=fopen(&B.dat&,&r&)))
while((ch=fgetc(fp2))!=EOF)
printf(&Can not open A B !\n&);
fclose(fp2);
fclose(fp1);
printf(&\n***new A contents***\n\n&);
if((fp=fopen(&A.dat&,&r&))==NULL)
printf(&file A cannot be opened\n&);
for(i=0;(ch=fgetc(fp))!=EOF;i++)
putchar(c[i]);
【3】; } 2. 程序改错题:
下列给定程序是建立一个带头结点的单向链表,并用随机函数为各结点赋值。函数fun的功能
是将单向链表结点(不包括头结点)数据域为偶数的值累加起来,并且作为函数值返回。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include &stdio.h& #include &conio.h& #include &stdlib.h& typedef struct aa {
struct aa * } NODE;
int fun (NODE *h) { int sum=0;
/*************found**************/
while(p-&next)
{ if(p-&data%2==0)
/*************found**************/
NODE *creatlink(int n) {
NODE *h,*p,*s;
h=p=(NODE*)malloc(sizeof(NODE));
for(i=1;i&n;i++)
s=(NODE*)malloc(sizeof(NODE));
s-&data=rand()%16;
s-&next=p-&
p-&next=s;
p-&next=NULL; }
outlink(NODE *h) { NODE
printf(&\n\n The LIST :\n\n HEAD&);
{ printf(&-&%d&,p-&data);
printf(&\n&); }
void main()
system(&CLS&);
head=creatlink(10);
outlink(head);
sum=fun(head);
printf(&\nSUM=%d&,sum);
3. 程序设计题
学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数
fun,它的功能是:按分数的高低排列学生的记录,高分在前。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &stdio.h& #define
16 typedef
struct { char num[10];
char s[100];
int upper=0,lower=0;
printf(&\nPlease a string: &);
fun(s,&upper,&lower);
printf(&\n upper=%d lower=%d\n&,【3】); }
程序改错题:
下列给定的程序中,函数fun的功能是:用选择法对数组中的n个元素按从小到大的顺序进行排
2. }STREC;
fun (STREC
void main () {
s[N]={{ &GA005&,85},{&GA003&,76},{&GA002&,69},{&GA004&,85},
{&GA001&,91},{&GA007&,72},{&GA008&,64},{&GA006&,87},
{&GA015&,85},{&GA013&,91},{&GA012&,64},{&GA014&,91},
{&GA011&,66},{&GA017&,64},{&GA018&,64},{&GA016&,72}};
printf(&The data after sorted :\n&);
for (i=0; i&N; i++)
{if((i)%4==0)
/*每行输出4个学生记录*/
printf(&\n&);
printf(&%s %4d&,s[i].num,s[i].s);
printf(&\n&);
/******************************/
wf=fopen(&out.dat&,&w&);
for (i=0; i&N; i++)
{if((i)%4==0&&i)
fprintf(wf,&\n&);
fprintf(wf,&%s %4d&,s[i].num,s[i].s);
fclose(wf);
/*****************************/ }
【参考答案】:1. (1) &&
(2) fputc(ch,fp1)
(3) fclose(fp) 2. (1)错误:while (p-&next)
正确:while (p!=NULL)
(2)错误:p=h-&
正确:p=p-&
3. int fun (STREC a[])
for(i=1;i&N;i++)
/*用冒泡法进行排序,进行N-1次比较*/
for(j=0;j&N-1;j++)
/*在每一次比较中要进行N-1次两两比较*/
if(a[j].s&a[j+1].s)
{t=a[j];a[j]=a[j+1];a[j+1]=t;}
/*按分数的高低排列学生的记录,高分在前*/
1. 程序填空题:
给定程序的功能是:分别统计字符串中大写字母和小写字母的个数。
例如,给字符串ss输入:AaaaBBbl23CCccccd,则输出结果应为:upper=5,lower=9。
注意:部分源程序给出如下。
请勿改动函数中的其他内容,仅在横线上填入所编写的若干表达式或语句。
#include &stdlib.h& #include &stdio.h&
void fun(char *s,int *a,int *b) {
if(*s&='A' && *s&='Z')
if(*s&='a' && *s&='z')
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include &stdio.h& #define N 20
void fun(int a[],int n) {int I,j,t,p;
for(j=0;j&n-1;j++)
/*************found**************/
for(I=j;I&n;I++)
if(a[I]&a[p])
/*************found**************/
t=a[p]; a[p]=a[j]; a[j]=t;
void main()
{int a[N]={9,6,8,3,-1},i,m=5;
printf(&排序前的数据:&);
for(i=0;i&m;i++)
printf(&%d&,a[i]); printf(&\n&);
printf(&排序后的顺序:&);
for(i=0;i&m;i++) printf(&%d&,a[i]);
printf(&\n&); }
3. 程序设计题:
N名学生的成绩已在主函数中放入一个带头节点的链表结构中,h指向链表的头节点。请编写
函数fun,它的功能是:找出学生的最高分,由函数值返回。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &stdio.h& #include &stdlib.h& #define
struct slist
typedef struct slist
STREC; double fun(STREC *h) {
STREC * creat (double *s) {
STREC *h,*p,*q;
h=p=(STREC*)malloc(sizeof(STREC));
while(i&N)
/*产生8个节点的链表,各分数存入链表中*/
{q=(STREC*) malloc(sizeof(STREC));
p-&s=s[i]; i++; p-&next=q; p=q;
p-&next=NULL;
/*返回链表的首地址*/ }
outlist(STREC *h) {
printf(&head&);
{printf(&-&%2.0f &,p-&s);p=p-&}
/*输出各分数*/
while(p-&next!=NULL);
printf(&\n\n &); }
void main() {
double s[N]={85,76,69,85,91,72,64,87},
h=creat(s);
outlist(h);
max=fun(h);
printf(&max=%6.1f\n &,max); /******************************/
wf=fopen(&out.dat&,&w&);
fprintf (wf,&%6.1f&,max);
fclose(wf);
/*****************************/ }
【参考答案】:1. (1) (*a)++或++(*a)或*a+=1或*a=*a+1
(2) (*b)++或++(*b)或*b+=1或*b=*b+1
(3) upper,lower
2. (1)错误:p=j 正确:p=j;
(2)错误:p=j; 正确:p=i; 3. double fun(STREC *h)
double max=h-&s;
while(h!=NULL)
/*通过循环找到最高分数*/
{if(max&h-&s) max=h-&s;
程序填空题:
给定程序中,函数fun的功能是:把形参s所指字符串中下标为奇数的字符右移到下一个奇数位
置,最右边被移出字符串的字符绕回放到第一个奇数位置,下标为偶数的字符不动 (注:字符串的长度不小于2)。
例如,形参s所指的字符串为:abcdefgh,执行结果为:ahcbedgf。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式
#include &stdio.h& void fun(char *s) {
int I,n,k;
for(I=0;s[I]!='\0';I++)
if(n%2==0)
k=n-【1】;
for(I=k-2;I&=1;I=I-2)
{s[I+2]=s[I];
s[I]=【3】;} }
char s[80]=&abcdefgh&;
printf(&\nThe original string is:%s\n&,s);
printf(&\nThe result is:%s\n&,s); } 2. 程序改错题:
下列给定程序中,函数fun的功能是:用冒泡法对6个字符串按由小到大的顺序进行排序。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
&stdlib.h& #include
&string.h& #include
&conio.h& #include
&stdio.h& #define MAXLINE 20
/*************found**************/ fun(char *pstr[6]) { int I,
for(I=0;I&5;I++)
{for(j=I+1;j&6;j++)
/*************found**************/
{ if(strcmp(*(pstr+I),pstr+j)&0)
p=*(pstr+I);
/*************found**************/
*(pstr+I)=pstr+j;
*(pstr+j)=p;
void main() {
char *pstr[6],str[6][MAXLINE];
system(&CLS&);
for(i=0;i&6;i++)
pstr[i]=str[i];
printf(&\nEnter 6 string(1 string at each line):\n &);
for(i=0;i&6;i++)
scanf(&%s&,pstr[i]);
fun(pstr);
printf(&The strings after sorting:\n &);
for(i=0;i&6;i++)
printf(&%s\n &,pstr[i]); } 3. 程序设计题:
已知学生的记录由学号和学习成绩构成,N名学生的数据已存入a结构体数组中。请编写函数
fun,该函数的功能是:找出成绩最高的学生记录,通过形参返回主函数(规定只有一个最高分)。已给出函数的首部,请完成该函数。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include&stdio.h& #include&string.h& #include&conio.h& #include&stdlib.h& #define N 10
typedef struct ss
/*定义结构体*/ { char num[10]; } STU;
fun(STU a[], STU *s) {
void main() {
STU a[N]={{ &A01&,81},{ &A02&,89},{ &A03&,66},{ &A04&,87},{ &A05&,77},
{ &A06&,90},{ &A07&,79},{ &A08&,61},{ &A09&,80},{ &A10&,71}},m;
system(&CLS&);
printf(&*****The original data*****&);
for(i=0;i&N;i++)
printf(&No=%s Mark=%d\n&, a[i].num,a[i].s);
fun(a,&m);
printf(&*****THE RESULT*****\n&);
printf(&The top :%s, %d\n&,m.num,m.s); /******************************/
wf=fopen(&out.dat&,&w&);
fprintf(wf,&%s,%d&,m.num,m.s);
fclose(wf);
/*****************************/ }
【参考答案】:1.(1) 1
(2) s[k]或*(s+k)
2. (1)错误:fun(char *pstr[6])
正确:void fun(char *pstr[6])
( 2)错误:if(strcmp(*(pstr+i),pstr+j)&0)
正确:if(strcmp(*(pstr+i),*(pstr+j))&0)
( 3 )错误:*(pstr+i)=pstr+j;
正确:*(pstr+i)= *(pstr+j);
3. fun(STU a[], STU *s)
for(i=0;i&N;i++)
/*找出成绩最高的学生记录*/
if(s-&s&a[i].s)
程序填空题:
请补充main函数,该函数的功能是求方程ax2+bx+c=0的两个实数根。方程的系数a、b、c从键
盘输入,如果判别式(disc=b2-4ac)小于0,则要求重新输入a、b、c的值。
例如,当a=1,b=2,c=1时,方程的两个根分别是x1=-1.00,x2=-1.00。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式
#include &math.h& #include &stdio.h& #include &stdlib.h& void main() {
float a,b,c, disc,x1,x2;
system(&CLS&);
printf(&Input a,b,c:&);
scanf(&%f,%f,%f&,&a,&b,&c);
disc=b*b-4*a*c;
if(disc&0)
printf(&disc=%f\n Input again!&,disc);
}while(【1】);
printf(&*******the result*******\n&);
printf(&\nx1=%6.2f\nx2=%6.2f\n&,x1,x2); } 2. 程序改错题: 下列给定程序中函数fun的功能是:将长整型数中每一位上为奇数的数依次取出,构成一个新数放在t中。高位仍在高位,低位仍在低位。例如当s中的数为时,t中的数为7531。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
&stdlib.h& #include
&stdio.h& #include
void fun(long s,long *t) {
long s1=1;
/*************found**************/
while(s&0)
/*************found**************/
if(d%2==0)
{*t=d*s1+*t;
void main() {long s,
system(&CLS&);
printf(&\nPlease enter s: &); scanf (&%ld&,&s);
fun(s,&t);
printf(&The result is :%ld\n&,t); } 3. 程序设计题: 请编写一个函数fun,它的功能是:求出一个2?M整型二维数组中最大元素的值,并将此值返回调用函数。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#define M 4
#include &stdio.h& fun (int a[][M]) {
void main() {
int arr[2][M]={5,8,3,45,76,-4,12,82};
printf(&max=%d\n&,fun(arr)); /******************************/
wf=fopen(&out.dat&,&w&);
fprintf (wf,&%d&,fun(arr));
fclose(wf);
/*****************************/ }
【参考答案】:
1. (1) disc&0
(2) (-b+sqrt(disc))/(2*a)
(3) (-b-sqrt(disc))/(2*a)
2. (1)错误:t=0;
正确:*t=0;
(2)错误:if(d%2==0) 正确:if(d%2!=0) 3. fun (int a[][M])
int i,j,max=a[0][0];
for(i=0;i&2;i++)
for(j=0;j&M;j++)
if(max&a[i][j])
max=a[i][j];/*求出二维数组的最大值*/
程序填空题:
请补充main函数,该函数的功能是:先以只写方式打开文件&out29.dat&,再把字符串str中的字符
保存到这个磁盘文件中。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在main函数的横线上填入所编写的若干表达
式或语句。
#include &stdlib.h& #include &stdio.h& #include &conio.h& #define N 80 void main() {
char str[N]=&I'm a student!&;
system(&CLS&);
if((fp=fopen(【1】))==NULL)
printf(&cannot open out11.dat\n&);
while (str[i])
ch=str[i];
putchar(ch);
【3】; } 2. 程序改错题:
N个有序整数数列已放在一维数组中,给定下列程序中,函数fun 的功能是:利用折半查找算法
查找整数m在数组中的位置。若找到,则返回其下标值;反之,则返回-1。
折半查找的基本算法是:每次查找前先确定数组中待查的范围:low和high(low&high),然后把m
与中间位置(mid)中元素的值进行比较。如果m的值大于中间位置元素中的值,则下一次的查找范围放在中间位置之后的元素中;反之,下次查找范围落在中间位置之前的元素中。直到low&high,查找结束。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
&stdio.h& #define
/*************found**************/ void fun(int a[],int m) { int low=0,high=N-1,
while(low&=high)
mid=(low+high)/2;
if(m&a[mid])
high=mid-1;STU
std[N]={{&1,2,15},{&2,9,21},{&3,9,1},{&4,7,15},{&5,9,28},{&83,11,14},{&7,6,22},{&8,8,18}};
char n[10]=&666666&;
p=fun(std,n);
if(p.num[0]==0)
printf(&\nNot found!\n&);
scanf(&%d%d&,&low,&heigh);
if(heigh&low)
{t=heigh= low=t;}
n=fun(s,h,low, heigh);
printf(&The student 's data between %d--%d:\n &,low, heigh);
for(i=0;i&n;i++)
printf(&%s %4d\n &,h[i].num, h[i].s);
/*输出指定分数范围内的学生记录*/
printf(&\n &);
/******************************/
n=fun(s,h,80,98);
for(i=0;i&n-1;i++)
/*分数在80~98之间的学生记录按分数从低到高排列*/
printf(&\nSucceed!\n&);
printf(&%s
%d-%d-%d\n&,p.num,p.year,p.month,p.day);
} } 2. 程序改错题:
下列给定程序中,函数fun的功能是:利用插入排序法对字符串中的字符按从小到大的顺序进行排序。插入法的基本方法是:先对字符串中的头两个元素进行排序,然后把第3个字符插入到前两个字符中,插入后前3个字符依然有序;再把第4个字符插入到前3个字符中,待排序的字符串已在主函数中赋予。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
&string.h& #include
&stdio.h& #define
void insert(char *aa) { int i, j,
n=strlen(aa);
for(i=1;i&n;i++)
/*************found**************/
{ c=aa[i];
while((j&=0)&&(ch&aa[j]))
{ aa[j+1]=aa[j];
void main()
{ char a[N]= &QWERTYUIOPASDFGHJKLMNBVCXZ&;
printf(&The original string:
insert(a);
printf(&The string after sorting: %s\n\n&,a); }
3. 程序设计题:
学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数
fun,它的功能是:把指定分数范围内的学生数据放在b所指的数组中,分数范围内的学生人数由函数值返回。
例如,输入的分数是60和69,则应当把分数在60到69的学生数据进行输出,包含60分和69分的
学生数据。主函数中把60放在low中,把69放在heigh中。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &stdio.h& #define
16 typedef
struct { char num[10]; }STREC;
fun (STREC *a, STREC *b, int l, int h ) {
void main () {
s[N]={{ &GA005&,85},{&GA003&,76},{&GA002&,69},{&GA004&,85},
{&GA001&,96},{&GA007&,72},{&GA008&,64},{&GA006&,87},
{&GA015&,85},{&GA013&,94},{&GA012&,64},{&GA014&,91},
{&GA011&,90},{&GA017&,64},{&GA018&,64},{&GA016&,72}};
STREC h[N],
int i, j,n, low, heigh,
printf(&Enter 2 integer number low & heigh: &);
for(j=i+1;j&n;j++)
if(h[i].s&h[j].s)
{tt=h[i];h[i]=h[j];h[j]=}
wf=fopen(&out.dat&,&w&);
for(i=0;i&n;i++)
fprintf(wf, &%s %4d\n&,h[i].num, h[i].s);
fclose(wf);
/*****************************/ }
【参考答案】:1. (1) STU
(2) std[i].num
(3) std[i]
2. (1)错误:c=aa[i];
正确:ch=aa[i];
3. int fun (STREC *a, STREC *b, int l, int h )
int i,j=0;
for(i=0;i&N;i++)
if(a[i].s&=l&&a[i].s&=h)
/*将分数高于l,低于h的学生记录存于结构体数组b中*/
b[j++]=a[i];
/*返回分数范围内的学生人数*/
1. 程序填空题:
请补充main函数,该函数的功能是:求1+2!+3!+…+N!的和。
例如,1+2!+3!+…+5!+6!的和为873。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在main函数的横线上填入所编写的若干表达
式或语句。
#include &stdlib.h& #include &stdio.h& void main() {
long s=0, t=1;
system(&CLS&);
printf(&\nInput n:\n&);
scanf(&%d&,【1】);
for(I=1;I&=n;I++)
printf(&1!+2!+3!...+%d!=%ld\n&,n,s); } 2. 程序改错题:
下列给定程序中,函数fun的功能是:求S的值。设
S=(22/(1*3))*(42/(3*5))*(62/(5*7))*…*(2k)2/((2k-1)*(2k+1))
例如,当k为10时,函数的值应为1.533852。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
&stdlib.h& #include
&conio.h& #include
&stdio.h& #include
/*************found**************/ fun(int k)
{ float s, w, p,
while(n&=k)
{ w=2.0*n;
s=s*w*w/p/q;
/*************found**************/
void main()
{ system(&CLS&);
printf(&%f\n &,fun(10)); }
请编写函数fun,它的功能是:计算并输出n(包括n)以内所有能被5或9整除的自然数的倒数之
例如,若主函数从键盘给n输入20后,则输出为s= 0.583333。 注意:n的值要求不大于100。部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &stdio.h& double fun(int n) {
printf(&\nInput n:&);
scanf(&%d&,&n);
printf(&\n\ns=%f\n&,s);
/******************************/
out=fopen(&out.dat&,&w&);
fprintf(out,&%f\n&,fun(30));
fclose(out);
/******************************/ }
3. 请编写函数fun,它的功能是:计算并输出n(包括n)以内所有能被5或9整除的自然数的倒数
例如,若主函数从键盘给n输入20后,则输出为s= 0.583333。 注意:n的值要求不大于100。部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语
#include &stdio.h& double fun(int n) {
printf(&\nInput n:&);
scanf(&%d&,&n);
printf(&\n\ns=%f\n&,s);
/******************************/
out=fopen(&out.dat&,&w&);
fprintf(out,&%f\n&,fun(30));
fclose(out);
/******************************/ }
}

我要回帖

更多关于 自然数n次方求和公式 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信