问答题给定程序MODI1.C中函数 fun 的功能是: 读入一个字符串(长度<20 ),将该字符串中的所有字符按ASCII码升序排序后输出。
例如, 若输入: edcba, 则应输出: abcde。
请改正程序中的错误,使它能统计出正确的结果。
注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
void fun( char t[] )
{
char c;
int i, j;
/**********found***********/
for( i = strlen( t ); i; i-- )
for( j = 0; j < i; j++ )
/**********found***********/
if( t[j] < t[ j + 1 ] )
{
c = t[j];
t[j] = t[ j + 1 ];
t[j + 1 ] = c;
}
}
main()
{
char s[81];
printf( "/nPlease enter a character string: " );
gets( s );
printf( "/n/nBefore sorting:/n /"%s/"", s );
fun( s );
printf( "/nAfter sorting decendingly:/n /"%s/"", s );
}
问答题请编写一个函数fun,它的功能是:比较两个字符串的长度,(不得调用C语言提供的求字符串长度的函数),函数返回较长的字符串。若两个字符串长度相同,则返回第一个字符串。 例如,输入beijing<CR>shanghai<CR>(<CR>为回车键),函数将返回shanghai。 注意:部分源程序存在文件PROG1.C中。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。1 #include<stdio.h>2 char *fun(char *s,char *t)3 {4 }5 main()6 { char a[20],b[20];7 void NONO();8 printf(''Input:1th string:'');9 gets(a);10 printf(''Input 2th string :'');11 gets(b);12 printf(''%s\n'',fun(a,b));13 NONO();14 }15 void NONO()16 {/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */17 FILE *fp,*wf;18 int i;19 char a[20],b[20];20 fp=fopen(''in.dat'',''r'');21 wf=fopen(''out''.dat'',''w'');22 for(i=0;i<10;i++){23 fscanf(fp,''%s%s'',a,b);24 fprintf(wf,''%s\n'',fun(a,b));25 }26 fclose(fp);28 fclose(wf); }
问答题给定程序MODI1.C中函数fun的功能是:将s所指字符串中出现的与t1所指字符串相同的子串全部替换成t2所指字符串,所形成的新串放在w所指的数组中。在此处,要求t1和t2所指字符串的长度相同。
例如,当s所指字符串中的内容为:"abcdabfab",t1所指子串中的内容为:"ab",t2所指子串中的内容为:"99"时,结果在w所指的数组中的内容应为:"99cd99f99"。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
#include<stdio.h>
#include<string.h>
void fun(char*s,char*t1,char*t2,char*w)
{
char*p,*r,*a;
strcpy(w,s);
while(*w)
{p=w;r=t1;
/**********found**********/
while(r)
if(*r==*p){r++;p++;}
else break;
if(*r=="/0")
{a=w;r=t2;
while(*r){
/**********found**********/
*a=*r;a++;r++
}
w+=strlen(t2);
}
else w++:
}
}
main()
{
chars[100],t1[100],t2[1003,w[100];
printf("/nPlease enter string S:");scanf("%s",s);
printf("/nPlease enter substring t1:");scanf("%s",t1);
printf("/nPlease enter substring t2:");scanf("%s",t2);
if(strlen(t1)==strlen(t2)){
fun(s,t1,t2,w);
printf("/nThe result is:%s/n",w);
}
else printf("Error:strlen(t1)!=strlen(t2)/n");
}
问答题请编写函数fun,函数的功能是:将放在字符串数组中的M个字符串(每串的长度不超过N),按顺序合并组成一个新的字符串。 例如,字符串数组中的M个字符串为 AAAA BBBBBBB CC 则合并后的字符串的内容应是:AAAABBBBBBBCC。 提示:strcat(a,b)的功能是将字符串b复制到字符串a的串尾上,成为一个新串。 注意:部分源程序在文件PROG1.C中。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。1 #include<stdio.h>2 #include<string.h>3 #define M 34 #define N 205 void fun(char a[M][N],char *b)6 {78 }9 main()10 { char w[M][N]={''AAAA'',''BBBBBBB'',''CC''},a[100];11 int i;void NONO();12 printf(''The string:\n'');13 for(i=0 ; i<M;i++)puts(w[i]);14 printf(''\n'');15 fun(w,a);16 printf(''The A string:\n'');17 printf(''%s'',a);printf(''\n\n'');18 NONO();19 }20 void NONO()21 {/*请在此函数内打开文件,输入测试数据,调用fun函数,输出数据,关闭文件。*/22 FILE *rf,*wf ;23 char w[M][N],a[100];int i;24 rf=fopen(''in.dat'',''r'');25 wf=fopen(''out.dat'',''w'');26 for(i=0;i<10;i++) {27 fscanf(rf,''%s%s%s'',w [0],w[1], w [2]);28 fun(w,a);29 fprintf(wf,''%s\n'',a);30 }31 fclose(rf);fclose(wf);32 }
问答题改错题
下列给定程序中,函数fun()的功能是逐个比较a,b两个字符串对应位置中的字符,把ASCII值小或相等的字符依次存放到c数组中,形成一个新的字符串。
例如:a中的字符串为fshADfg,b中的字符串为sdAEdi,则c中的字符串应为fdAADf。
请改正程序中的错误,使它能得到正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
void fun(char *p,char *q,char *c)
{ int k=0;
while(*p||*q)
/**********************found***********************/
{ if (*p<=*q)
c[k]=*q;
else c[k]=*p;
if(*p) p++;
if(*q) q++ ;
/**********************found***********************/
k++
}
}
main()
{ char a[10]="fshADfg",b[10]="sdAEdi",c[80]={''/0''};
fun(a,b,c);
printf("The string a:"); puts(a);
printf("The string b:"); puts(b);
printf("The result :"); puts(c);
}
问答题请编写一个函数fun, 其功能是: 将ss所指字符串中所有下标为奇数位置上的字母转换为大写(若该位置上不是字母, 则不转换)。
例如, 若输入"abc4EFg",则应输出"aBc4EFg"。
注意: 部分源程序在文件PROG1.C文件中。
请勿改动主函数main和其它函数中的任何内容, 仅在函数fun的花括号中填入你编写的若干语句。
给定源程序:
#include
#include
void fun( char *ss )
{
}
void main( void )
{
char tt[51];
printf( "/nPlease 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 );
NONO();
}
问答题下列给定程序中,fun()函数的功能是:求3个数的最小公倍数,例如,给变量a、b、c、分别输入15、11、2,则输出结果应当是330。
请修改函数中的错误,得出正确的结果。
注意:不要改动main()函数,不能增行或减行,也不能更改程序的结构。
试题程序:
#include<stdio.h>
int fun(int x, int y, int z)
{
int i, temp, n, m;
//****found****
i=1;
temp=m=n=1;
//****found****
while(temp!=0
temp=i%x;
m=i%y;
n=i%z;
}
return i;
}
void main()
{
int a, b, c, i;
printf("Input a b c: ");
scanf("%d%d%d",
printf("a=%d, b=%d, c=%d/n", a, b, c);
i=fun(a, b, c);
printf("The minimal common multiple is: %d/n", i);
}
问答题给定程序MODI1.C中函数fun的功能是:将tt所指字符串中的小写字母都改为对应的大写字母,其他字符不变。 例如,若输入''Ab,cD'',则输出''AB,CD''。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!1 #include<stdio.h>2 #include<string.h>3 char* fun(char tt[])4 {5 int i;6 for(i=0;tt[i];i++)7 /**********found**********/8 if(('a'<=tt[i])||(tt[i]<='z'))9 /**********found**********/10 tt[i]+=32;11 return(tt);12 }13 main()14 {15 char tt[8 1];16 printf(''\nPlease enter a string:'');17 gets(tt);18 printf(''\nThe result string is:\n%s'',fun(tt));19 }
问答题编写程序,实现矩阵(3行、3列)的转置(即行列互换)。例如,若输入下面的矩阵:100 200 300400 500 600700 800 900则程序输出:100 400 700200 500 800300 600 900注意:部分源程序在文件PROG1.C中。请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。试题程序:1 #include<stdio.h>2 #include<conio.h>3 #include<stdlib.h>4 void fun(int array[3][3])5 {67 }8 void main()9 {10 FILE,lc Wf;11 int i,j;12 int array[3][3]={{100,200,300),{400,500,600),{700,800,900});13 system("CLS");14 for(i=0;i<3;i++)15 { for(j=0;j<3;J++)16 printf("%7d",array[i][j]);17 printf("\n");18 }19 fun(array);20 printf("Converted array:\n");21 for(i=0;i<3;i++)22 { for(j=0;j<3;j++)23 printf("%7d",array[i][j]);24 printf("\n");25 }26 /*********found*********/27 wf=fopen(”out.dat”,”w”);28 for(i=0;i<3;i++)29 { for(j=0;j<3;j++)30 fprintf(wf,"%7d",array[i][j]);31 fprintf(wf,"\n");32 }33 fclose(wf);34 /*********found*********/35 }
问答题假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:除了尾部的*号之外,将字符串中其他木号全部删除。形参p已指向字符串中最后的一个字母。在编写函数时,不得使用c语言提供的字符串函数。 例如, 字符串中的内容为:****A*BC*DEF*G*******,删除后,字符串中的内容应当是:ABCDEFG*******。 注意:部分源程序在文件PROG1.C中。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。1 #include<stdio.h>2 void fun(char *a,char *p)3 {4 }5 main()6 { char s[81],*t ;7 void NONO();8 printf(''Enter a string:\n'');gets(s);9 t=s;10 while( *t)t++;11 t--;12 while(*t=='*')t--;13 fun(s,t);14 printf(''The string after deleted:\n'');puts(s);15 NONO();16 }17 void NONO()18 {/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */19 FTLE *in,*out;20 int i;char s[81],*t;21 in=fopen(''in.dat'',''r'');22 out=fopen(''out.dat'',''w'');23 for(i=0;i<10;i++) {24 fscanf(in,''%s'',s);25 t=s;26 while(*t)t++;27 t--;28 while(*t=='*')t--;29 fun(s,t);30 fprintf(out,''%s\n'',s);31 }32 fclose(in);33 fclose(out);}
问答题请编写函数fun,函数的功能是: 移动字符串中的内容,移动的规则如下: 把第1到第m个字符, 平移到字符串的最后, 把第m+1到最后的字符移到字符串的前部。
例如, 字符串中原有的内容为: ABCDEFGHIJK, m的值为3, 则移动后, 字符串中的内容应该是: DEFGHIJKABC。
注意:部分源程序在文件PROG1.C中。
请勿改动主函数main和其它函数中的任何内容, 仅在函数fun的花括号中填入你编写的若干语句。
给定源程序:
#include
#include
#define N 80
void fun1(char *w) /* 本函数的功能是将字符串中字符循环左移一个位置 */
{
int i; char t;
t=w[0];
for(i=0;i
w[i]=w[i+1];
w[strlen(w)-1]=t;
}
void fun(char *w, int m) /* 可调用fun1函数左移字符 */
{
}
main()
{ char a[N]= "ABCDEFGHIJK";
int m;
printf("The original string:/n");puts(a);
printf("/n/nEnter m: ");scanf("%d",
fun(a,m);
printf("/nThe string after moving:/n");puts(a);
printf("/n/n");
NONO();
}
问答题程序通过定义学生结构体变量,存储学生的学号、姓名和三门课的成绩。函数fun的功能是:将形参a中的数据进行修改,把修改后的数据作为函数值返回主函数进行输出。
例如,若传给形参a的数据中学号、姓名和三门课的成绩依次是:10001、“ZhangSan”、95、80、88,修改后的数据应为:10002、“LiSi”、96、81、89。
请在程序的下画线处填入正确的内容并将下画线删除,使程序得出正确的结果。
注意:部分源程序给出如下。
不得增行或删行,也不得更改程序的结构!
试题程序:
#include<stdio.h>
#include<string.h>
struct Student{
long sno;
char Flame[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;
}
main()
{ struct stuclent s={10001,"ZhangSan",95,80,88},t;
int i;
printf ("\n\nThe original data:\n");
printf("\nNo:%ld Name:%s\n Scores:",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\n Scores:",t.sno,t.name);
for(i=0;i<3;i++)
printf("%6.2f",t.score[i]);
printf("\n");
}
问答题程序定义了N×N的二维数组,并在主函数中自动赋值。请编写函数fun(inta[][N],intn),该函数的功能是:使数组左下半三角元素中的值乘以n。例如,若n的值为3,a数组中的值为:则返回主程序后a数组中的值应为:注意:部分源程序给出如下。请勿改动函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。试题程序:#include<stdio.h>#include<conio.h>#include<stdlib.h>#defineN5voidfun(inta[][N],intn){}main(){inta[N][N],n,i,j;printf("*****Thearray*****/n");for(i=0;i<N;i++){for(j=0;j<N;j++){a[i][j]=rand()%10;printf("%4d",a[i][j]);}printf("/n");}n=rand()%4;printf("n=%4d/n",n);fun(a,n);printf("*****THERESULT*****/n");for(i=0;i<N;i++){for(j=0;j<N;j++)printf("%4d",a[i][j]);printf("/n");}}
问答题请编写一个函数,用来删除字符串中的所有空格。例如,输入asd af aa z67,则输出为asdafaaz67。注意:部分源程序在文件PROG1.C中。请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。试题程序:1 #include<stdio.h>2 #include<ctype.h>3 #include<conio.h>4 #include<stdlib.h>5 void fun(char*str)6 {78 }9 main()10 {11 char str[81];12 char Nsg[]="Input a string.";13 int n;14 FILE*out;15 printf(Nsg);16 gets(str);17 puts(str);18 fun(str);19 printf("*** str:%s\n",str);20 /*****************/21 out=fopen("out.dat","w");22 fun(Nsg);23 fprintf(out,"%s",Hsg);24 fclose(out);25 /*****************/26 }
问答题请编写一个函数,用来删除字符串中的所有空格。
例如,输入abc de f gh,则输出为abcdefgh。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的花括号中填入所编写的若干语句。
试题程序:
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<stdiib.h>
void proc(char*str)
{
}
void main()
{
char str[81];
system("CLS");
printf("Input a string: ");
gets(str);
puts(str);
proc(str);
printf("***str: %s/n", str);
}
问答题编程题
程序定义了N×N的二维数组,并在主函数中自动赋值。请编写函数fun(int a[ ][ N ],int n),该函数的功能是使数组左下半三角元素中的值加上n。
例如:若n的值为3,a数组中的值为
a=2 5 4
1 6 9
5 3 7
则返回主程序后a数组中的值应为
5 5 4
4 9 9
8 6 10
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include
#include
#include
#define N 5
fun(int a[ ][N], int n)
{
}
main()
{
int a[N][N],n, i,j;
clrscr();
printf("***** The array *****/n");
for(i=0; i=5);
printf("n=%4d/n",n);
fun(a, n);
printf("*****THE RESULT*****/n");
for(i=0; i
问答题请编写一个函数proc(),它的功能是:找出一维数组元素中最大的值和它所在的下标,最大值和它所在的下标通过形参传同。数组元素中的值已在主函数中赋予。
主函数中arr是数组名,n是arr中的数据个数,max存放最大值,index存放最大值所在元素的下标。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的花括号中填入所编写的若干语句。
试题程序:
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
void proc(int arr[],int n,int*max,int*d)
{
}
void main()
int i,arr[20],max,index,n=10;
srand((unsigned)time(NULL));
for(i=0;i<=n;i++)
{
arr[i]=rand()%50;
printf("%4d",arr[i]);
//输出一个随机数组
}
printf("/n");
proc(arr,n,
printf("Max=%5d,Index=%4d/n",max,index);
}
问答题请编写函数fun,其功能是:计算并输出下列多项式的值:例如,在主函数中从键盘给n输入15,则输出为:s=2.718282。注意:要求n的值大于1但不大于100。部分源程序在文件PROG1.C中。请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。#inolude<stdio.h>doublefun(intn){}main(){intn;doubles;printf("Inputn:");scanf("%d",s=fun(n);printf("s=%f/n",s);NONO();}NONO(){/*请在此函数内打开文件,输入测试数据,调用fun函数,输出数据,关闭文件。*/FILE*rf,*wf;intn,i;doubles;rf=fopen("in.dat","r");wf=fopen("out.dat","w");for(i=0;i<10;i++){fscanf(rf,"%d",s=fun(n);fprintf(wf,"%lf/n",s);}fclose(rf);fclose(wf);}
问答题请编写函数fun,其功能是分别统计形参t所指二维数组中字母A和C的个数。注意:部分源程序存在PROG1.C中,请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语匈.试题程序:#include<stdio.h>#include<stdlib.h>#define M 14 void NONO():void fun(char(*t)[M],int*a,int*c)}void get(char(*s)[M]){int i,j; for(i=0;i<M;j++) { for(j=0;j<M;j++) { s[i][j]=65+rand()%12;printf(“%c”,s[i 3[j]);} printf(“\n”); }}main(){char a[M][M]; int x,y; get(a); fun(a,&x,&y); printf(“A=%d C=%d\n”,x,y); NONO();}void NONO(){/*本函数用于打开文件,输人数据,调用函数,输出数据,关闭文件。*/ FILE*rp,*wf: int i,j,x,y: char a[M][M]; fp=fopen(“c:\test\in.dat”,“r”); wf=fopen(“c:\test\out.dat”,“W”); for(i=0;i<M;i++) {for(j=0;j<M;j++) {fscanf(rp,“%c”,&a[i][j]);} } fun(a,&x,&y); fprintf(wf,“A=%d\n”,x); fprintf(wf,“C=%d\n”,y); fclose(fp); fclose(wf);
问答题请编写函数void fun(int x,int PP[],int*n),它的功能是:求出能整除x且不是偶数的各整数,并按从小到大的顺序放在pp所指的数组中,这些除数的个数通过形参n返回。 例如,若x中的值为30,则有4个数符合要求,它们是1、3、5、15。 注意:请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 试题程序:#include<conio.h>#include<stdio.h>#include<stdlib.h>void fun(int X,int PP[],int*n){}void main(){ FILE*wf; int x,aa[1000],n,i; system(“CLS”); printf(“\nPlease enter an integer number:\n”) ; scanf(“%d”,&x); fun(x,aa,&n); for(i=0;i<n;i++) prinff(“%d”,aa[i]);prinff(“\n”);/*****************/ wf=fopen(“out.dat”,“w”);fun(30,aa,&n);for(i=0;i<n;i++) fprintf(wf,“%d”,88.[i]); fclose(wf);/*****************/}
