资源描述
二、改错题
Cmody191.c
修改程序,用以计算1~100之间所有偶数之和。
#include <stdio.h>
#include <math.h>
void main( )
{
int i=1;
/**/ int sum=2 /**/;
while(i++,i<=100)
{ if(/**/ i==(i/2)*2 /**/)
continue;
sum+=i;
}
printf("Sum is %d\n",sum);
getch();
}
Cmody192.c
修改程序,将二维数组表达方阵左下半三角(不含对角线)各元素加4,右上半三角(含对角线)各元素乘2。
#include <stdio.h>
#define N 5
void main()
{
int a[N][N],i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
a[i][j]=i*5+j+11;
printf("\nArray a is:\n");
for(i=0;i<N;i++)
{for(j=0;j<N;j++)
printf("%3d ",a[i][j]);
printf("\n");
}
for(i=0;i<N;i++)
/**/ for(j=0;j<=i;j++)/**/
a[i][j]+=4;
for(i=0;i<N;i++)
/**/ for(j=i+1 ;j<N;j++) /**/
a[i][j]*=2;
printf("\nArray a is turned:\n");
for(i=0;i<N;i++)
{for(j=0;j<N;j++)
printf("%3d ",a[i][j]);
printf("\n");
}
getch();
}
Cmody201.c
修改程序,计算满足如下条件整数n最大值。
22+42+62+82+……+n2<1000
#include <stdio.h>
#include <math.h>
void main()
{
int n=0,sum=0;
/**/ while(sum<1000);/**/
{
/**/ sum+=n^2 /**/;
n++,n++;
}
printf("n=%d\n",n-2);
getch();
}
Cmody202.c
修改程序,实现从键盘输入一串字符,并在字符串中从第m个字符串开始截取n个字符。
例如:输入字符串为”welcome”,m=2,n=3,则截取字串为”elc”
#include <stdio.h>
#include <string.h>
void main()
{
char str[100],sub[100];
int m,n,len,i;
printf("Enter string:");
gets(str);
printf("\nEnter m n:");
scanf("%d%d",&m,&n);
len=strlen(str);
if( m-1+n>len )
printf("Can't run with %d and %d!\n",m,n);
else
{ i=0;
/**/ while(i<=n) /**/
{
/**/ sub[i]=str[m+i];/**/
i++;
}
/**/ sub[i]='\n';/**/
printf("sub string is:%s\n",sub);
}
getch();
}
Cmody211.c
修改程序,实现输入一整型二维数组,计算其中数组元素最大值与最小值差。
#include <stdio.h>
#include <math.h>
#define ROW 3
#define COL 4
void main()
{
int a[ROW][COL],max,min,i,j,result;
clrscr();
printf("Enter array a:\n");
for(i=0;i<ROW;i++)
for(j=0;j<COL;j++)
scanf("%d",&a[i][j]);
/**/ max=min=0;/**/
for(i=0;i<ROW;i++)
for(j=0;j<COL;j++)
{if(/**/ min<a[i][j] /**/)
min=a[i][j];
if(a[i][j]>max)
/**/ max=a[i][j] /**/
}
printf("Result= %d\n",max-min);
getch();
}
Cmody212.c
修改程序cmody212.c,实现从键盘输入一串字符和一种字串,输出该子串在原字符串中浮现次数。
#include <stdio.h>
#include <string.h>
void main()
{
int i,j,k,count;
char s1[100],s2[100];
printf("Enter main String:");
gets(s1);
printf("Enter Sub String:");
gets(s2);
count=0;
/**/ for(i=0;*s1!='\0';i++) /**/
{
/**/ for(k=0;(s1[j]==s2[k])&&(s1[j]!='\0');j++,k++ );/**/
if(s2[k]=='\0') count++;
}
printf("\nCount=%d\n",count);
getch();
}
Cmody221.c
修改程序,输出存储在构造体数组中8位学生成绩平均分以及最高学生姓名
#include <stdio.h>
/**/ structure student /**/
{
char name[10];
float score;
};
void main( )
{
struct student stu[8]={{"Mary",76},{"John",85},
{"Tom",81},{"Susa",87},{"Wilu",79},{"Yili",65},{"Sonmu",73},{"Lichar",70}};
int i=0,mrk;
float total=0,aver,max;
max=stu[0].score;
mrk=0;
while(i<8)
{
/**/ total=total+stu[i]; /**/
if(stu[i].score>max)
{max=stu[i].score;
mrk=i;
}
/**/ i++ /**/
}
aver=total/8;
printf("\naver=%.2f \n Best is %s\n",aver,stu[mrk].name);
getch();
}
Cmody222.c
修改程序,完毕其中n个字符串升序排列
#include <stdio.h>
#include <string.h>
#define ROW 5
#define COL 30
void fun(char s[][COL],int n)
{
char temp[COL];
int i,j;
for(i=0;i<n-1;i++)
/**/ for (j=i+1;j<n-1;j++) /**/
if(strcmp(s[i],s[j])>0)
{
strcpy(temp,s[i]);
strcpy(s[i],s[j]);
strcpy(s[j],temp);
}
}
void main()
{
int i;
char str[][COL]={"DEF","BAEELCS","FHIAME","CBADF","APMAE"};
/**/ fun(str,COL);/**/
clrscr();
for(i=0;i<ROW;i++)
printf("%s\n",str[i]);
getch();
}
Cmody231.c
修改程序cmody231.c,实现将输入十进制正整数转换成十六进制数,且用字符串存储该十六进制数
#include <math.h>
#include <string.h>
#include <stdio.h>
void main()
{
int x,b,i,j;
char s[5];
printf("Input a number(Dec):");
scanf("%d",&x);
/**/ i=1;/**/
while(x>0)
{b=x%16;
if(b>9)
s[i]=b-10+'A';
else
/**/ s[i]=b /**/;
x=x/16;
i++;
}
printf("\nHex number is:");
for(j=i-1;j>=0;j--)
putchar(/**/ s[i] /**/);
getch();
}
Cmody232.c
修改程序cmody232.c,输出下列9行数字金字塔
1
121
12321
1234321
………………………
54321
#include<stdio.h>
void main()
{
int i,j,k;
for(k=1;k<=9;k++)
{
/**/ for(i=0;i<=k;i++) /**/
printf(" ");
for(i=1;i<=k;i++)
printf("%d",i);
/**/ for(j=k-1;j>1;j--) /**/
printf("%d",j);
printf("\n");
}
getch( );
}
三、填空题
Ccon191.c
程序ccon191.c,从键盘输入3个实型值,若它们能构成一种三角形(即任意两边之和不不大于第三边),则依照如下公式求该三角形面积;否则,提示相应信息。
#include <stdio.h>
#include <math.h>
void main()
{
float a,b,c,d,area;
printf("Please input 3 numbers:\n");
scanf("%f,%f,%f",&a,&b,&c);
if( a+b>c /**/ /**/)
{d = (a+b+c)/2;
area = /**/ /**/;
printf("area is %.2f\n",area);
}
else
printf("Not Triangle!\n");
getch();
}
Ccon192.c
程序ccon192.c,计算:已知大货车限载8吨、中型货车限载3吨、微型货车限载1吨,需50辆车运送100吨货品时,应配备大、中、微型车分别多少辆?共有多少种解决方案?
#include <stdio.h>
#include <conio.h>
int /**/ /**/( )
{
int big,median,small,n=0;
printf(" big median small \n");
for(big=0;big<=13;/**/ /**/)
for(median=0;median<=33;median++)
{
small=100-8*big-3*median;
if(/**/ /**/==50)
{
n++;
printf("%d-->%2d %2d %2d\n",
n,big,median,small);
}
}
return n;
}
void main()
{
int num;
num = fun();
printf("\n There are %d solutions.\n",num);
getch();
}
Ccon201.c
程序ccon201.c,按照如下袋装状况求一堆苹果总数,总数在100~300之间。若按每袋装8个苹果则多余5个,若按每袋装7个苹果,则多余4个,若按每袋装5个苹果,则多余2个。
#include <stdio.h>
void main()
{
int n;
for( n=300;/**/ /**/;n-- )
{if( n%8 == 5)
if( !((n-4)%7) /**/ /**/)
printf("n=%d\n",n);
}
getch();
}
Ccon202.c
程序ccon202.c中swap函数,通过指针变量实现两个变量值互换。
#include <stdio.h>
void swap(/**/ /**/)
{ int sum;
sum = *p1+*p2;
*p1 =/**/ /**/;
*p2 = sum-*p1;
}
void main()
{
int x,y;
printf("please input 2 numbers:\n");
scanf("%d,%d",&x,&y);
printf("\nOriginal:x=%d y=%d\n",x,y);
swap(&x,&y);
printf("\nNow:x=%d y=%d\n",x,y);
getch();
}
Ccon211.c
程序ccon211.c,按每行最多5个数方式输出100~400之间能同步被7和4整除所有数及它们之和。
#include <stdio.h>
void main()
{
int i,n;
int sum=0;
/**/ /**/
for(i=101;i<=399;i++)
{
if(/**/ /**/ && !(i%4) )
{printf("%d ",i);
sum+=/**/ /**/;
n++;
if(n%5==0)
printf("\n");
}
}
printf("\n sum=%d\n",sum);
getch();
}
Ccon212.c
程序ccon212.c中cmob函数用以输出等式a*b*c+a*c*c=560所有a,b,c组合(其中a,b,c只取1~9之间一位整数)。如a=7,b=2,c=8时,7*2*8+7*8*8=560
#include <stdio.h>
void comb(/**/ /**/)
{int a,b,c;
for( a=1;a<10;a++ )
for( b=1;b<10;b++ )
for( c=1;c<10;c++ )
{
if(/**/ /**/ ==m)
printf("a=%d,b=%d,c=%d\n",a,b,c);
}
}
void main()
{
int result=560;
comb(result);
getch();
}
Ccon221.c
程序ccon221.c,输出如下图形
% % % % % % %
& & & & & &
% % % % %
& & & &
% % %
& &
%
#include <stdio.h>
#include <conio.h>
void main()
{
int row,col;
for (row=1;row<=7;row++)
{
printf("\t");
for(col=1;col<=/**/ /**/ ;col++)
if(row%2)
printf("%c",/**/ /**/);
else
printf("%c",/**/ /**/);
printf("\n");
}
getch();
}
Ccon222.c
程序ccon222.c中pnt函数,依照参数n(2≤n≤10),输出类似如下所示方针(n=5).
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25
#include <stdio.h>
#include <conio.h>
#define M 10
void pnt(int a[M][M],int n)
{int i,/**/ /**/;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{if(i%2==0)
a[i][j]=/**/ /**/;
else
a[i][j]=i*n+n-j;
}
printf("array a(n=%d) is:\n",n);
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
printf("%3d",a[i][j]);
printf("\n");
}
}
void main()
{
int a[M][M];
int n;
printf("Input n(2<=n And n<=10):");
scanf("%d",&n);
pnt(/**/ /**/,n);
getch();
}
Ccon231.c
程序ccon231.c,计算如下分数序列前18项之和
#include <stdio.h>
void main()
{
int i;
float sum,a=2,b=1,t;
/**/ /**/;
for (i=1;i<=18;i++)
{
sum+=/**/ /**/;
t=a;
a=a+b;
b=/**/ /**/;
}
printf("sum=%f\n",sum);
getch();
}
Ccon232.c
程序ccon232.c中inv函数,通过指针变量将字符串str内容”asdfjk”,调用inv函数后,变成:”kjfdsa”。
#include <stdio.h>
#include <string.h>
void inv(char *str)
{int i,j,k;
j=/**/ /**/;
for(i=0;i<strlen(str)/2;i++,j--)
{k=*(str+i);
*(str+i)=*(str+j);
*(str+j)=/**/ /**/;
}
}
void main( )
{
char str[]="asdfjk";
printf("\n Original is:%s\n",str);
inv(str);
printf("\n Now is:%s\n",str);
getch();
}
四、编程题
cprog191.c
打开程序cprog191.c,完毕其中fun函数:依照输入汇款数额,求应交汇费。设应交汇费计算公式如下:
#include <stdio.h>
#include <math.h>
double fun(float x)
{
/**/
/**/
}
void main()
{
float x;
double y;
clrscr();
printf("Please input a number:\n");
scanf("%f",&x);
y = fun(x);
printf("f(%.2f)=%.2f\n",x,y);
getch();
}
cprog192.c
打开程序cprog192.c,完毕其中fun函数:对4×4矩阵从顶行开始各行按从左到右顺序查找,找出各行中0元素之后所有负数,并按元素浮现先后顺序存储到数组b中,并返回这些负数之和。如矩阵为:
则调用函数fun( )后,数组b各元素依次为-3,-1,-4,-3,-12,-13,并返回-36.
#include <stdio.h>
#include <math.h>
#define ROW 4
#define COL 4
#define LEN 12
int fun(int a[][COL],int b[])
{
/**/
/**/
}
void main( )
{
int arra[ROW][COL]={{-2,0,-3,-1},{-8,2,0,-4},
{0,3,-3,-12},{21,0,-13,3}};
int arrb[LEN],i,result;
for(i=0;i<LEN;i++)
arrb[i]=0;
clrscr();
result=fun(arra,arrb);
printf("array arrb is:\n");
for(i=0;i<LEN;i++)
if(arrb[i]==0) printf(" ");
else printf("%4d",arrb[i]);
printf("\nresult=%d",result);
getch( );
}
cprog201.c
打开cprog201.c,完毕其中函数fun,该函数数学表达式是
#include <stdio.h>
#include <math.h>
double fun(float x)
{
/**/
/**/
}
void main( )
{
float x;
double y;
printf("Please input a number:\n");
scanf("%f",&x);
y = fun(x);
printf("f(%.3f)=%.3f\n",x,y);
getch();
}
cprog202.c
打开程序cprog202.c,完毕其中fun函数:
函数fun(double x,int n),用如下公式近似计算cos(x)值:
#include <stdio.h>
void main()
{
double x;
int n;
double fun(double x,int n);
printf("Please enter x,n:");
scanf("%lf%d",&x,&n);
printf("cos(%lf)=%lf\n",x,fun(x,n));
getch();
}
double fun(double x,int n)
{
/**/
/**/
}
cprog211.c
打开cprog211.c,完毕其中函数fun,该函数数学表达式是
#include <stdio.h>
#include <math.h>
double fun(float x)
{
/**/
/**/
}
void main( )
{
float x;
double y;
clrscr();
printf("Please input a number:\n");
scanf("%f",&x);
y = fun(x);
printf("f(%.2f)=%.2f\n",x,y);
getch( );
}
cprog212.c
打开程序cprog212.c,完毕其中fun函数:对两个字符串中小写英文字母个数进行比较,并返回比较成果。(注:第1个字符串比第2个字符串小写英文字母个数多,则返回正数,相等返回0,个数少则返回负数)
例如:字符串str1为“abc123”,字符串str2为”abcd”,则函数fun返回值应不大于0。
#include <stdio.h>
int fun(char *str1,char *str2)
{
/**/
/**/
}
void main( )
{
int rela;
char *str1,*str2;
clrscr();
printf("Please input string NO.1:");
gets(str1);
printf("Please input string NO.2:");
gets(str2);
rela=fun(str1,str2);
if(rela>0)
printf("Numbers of Lower char in %s > Numbers of Lower char in %s",str1,str2);
if(rela==0)
printf("Numbers of Lower char in %s = Numbers of Lower char in %s",str1,str2);
if(rela<0)
printf("Numbers of Lower char in %s < Numbers of Lower char in %s",str1,str2);
getch();
}
cprog221.c
打开程序cporg221.c,完毕其中fun函数:依照输入x和y值,按如下公式计算:
将计算成果整数某些存入数组元素arr[0],并将该计算成果小数某些前两位存入arr[1]中。
#include <stdio.h>
#include <math.h>
void fun(int arr[],double x,int y)
{
/**/
/**/
}
void main()
{
int y;
double x;
int arra[2];
printf("Please enter x(1<x<2),y(0<y<=6):\n");
scanf("%lf%d",&x,&y);
if((x>1.0 &&x<2.0)&&(y>0 && y<=6))
{
fun(arra,x,y);
printf("\npart1=%d,part2=%d",arra[0],arra[1]);
}
else
printf("x or y is out of range!");
getch();
}
cprog222.c
打开程序cprog222.c,完毕其中fun函数:将一种升序数组a和一种降序数组b中所有元素按降序存入数组c中。
例如:数组a为{5,10,15,27,46},数组b为{50,45,42,29,15,8,5,2},则数组c就应为{50,46,45,42,29,27,15,15,10,8,5,5,2}。
#include <stdio.h>
#define N 5
#define M 8
void fun(int a[],int b[],int c[])
{
/**/
/**/
}
void main()
{
int arra[N]={5,10,15,27,46},arrb[M]={50,45,42,29,15,8,5,2};
int arrc[N+M],k;
fun(arra,arrb,arrc);
printf("array arra is:");
for(k=0;k<N;k++)
printf("%d ",arra[k]);
printf("\narray arrb is:");
for(k=0;k<M;k++)
printf("%d ",arrb[k]);
printf("\nresult arrc is:");
for(k=0;k<N+M;k++)
printf("%d ",arrc[k]);
getch();
}
cprog231.c
打开程序cprog231.c,完毕其中函数fun,该函数递归公式是:
#include <stdio.h>
#include <math.h>
void main()
{
double x;
int n;
double fun(double x,int n);
printf("Please enter x,n:\n");
scanf("%lf%d",&x,&n);
printf("fun=%lf\n",fun(x,n));
getch();
}
double fun(double x,int n)
{
/**/
/**/
}
cprog232.c
打开程序cprog232.c,完毕其中fun函数:返回一维数组中次大数(即仅不大于最大数数),设数组a中没有重复元素。
例如:数组元素为{45,58,33,24,40,20,30,28,31},函数fun返回值为45.
#include <stdio.h>
#define ROW 9
int fun(int a[ ])
{
/**/
/**/
}
void main()
{
int arra[ROW]={45,58,33,24,40,20,30,28,31};
int max_pre,i;
max_pre=fun(arra);
printf("array arra is:");
for(i=0;i<ROW;i++)
printf("%d ",arra[i]);
printf("\nmax_pre is %d",max_pre);
getch();
}
参照答案
二、改错题
cmody191.c
/**/ int sum=2 /**/;→int sum=0;
/**/ int sum=2 /**/;→if(i!=(i/2)*2)
cmody192.c
/**/ for(j=0;j<=i;j++)/**/→for(j=0;j<i;j++)
/**/ for(j=i+1 ;j<N;j++) /**/→for(j=i;j<N;j++)
cmody201.c
/**/ while(sum<1000);/**/→while(sum<1000)
/**/ sum+=n^2 /**/;/**/ →sum+=n*n
cmody202.c
/**/ while(i<=n) /**/→while(i<n)
/**/ sub[i]=str[m+i];/**/→sub[i]=str[m-1+i];
/**/ sub[i]='\n';/**/→sub[i]= '\0'
cmody211.c
/**/ max=min=0;/**/→max=min=a[0][0];
/**/ min<a[i][j] /**/→min>a[i][j]
/**/ max=a[i][j] /**/→max=a[i][j];
cmody212.c
/**/ for(i=0;*s1!='\0';i++) /**/
→for(i=0;*(s1+i)!=’\0’;i++)
/**/ for(k=0;(s1[j]==s2[k])&&(s1[j]!='\0');j++,k++ );/**/→
for(k=0,j=i;(s1[j]==s2[k]&&(s2[k]!=’\0’);j++,k++);
cmody221.c
/**/ structure student /**/→struct student
/**/ total=total+stu[i]; /**/→
total=total+stu[i].score
/**/ i++ /**/→i++;
cmody222.c
/**/ for (j=i+1;j<n-1;j++) /**/→for(j=i+1;j<n;j++)
/**/ fun(str,COL);/**/→fun(str,RO
展开阅读全文