资源描述
程序设计
/*请编写函数fun,该函数的功能是:判断字符串是否为回文,若是则函数返回1,主函数中输出"YES",否则返回0,主函数中输出"NO"。回文是指顺读和倒读都一样的字符串。例如,字符串LEVEL是回文,而字符串123312就不是回文。
试题程序:*/
#include <stdio.h>
#define N 80
int fun(char str[])
{
/***************Begin************/
/*************** End ************/
}
void main()
{
char s[N];
FILE *out;
char test[][80]={"1234321","123421","123321","abcdCBA"};
int i;
printf("Enter a string : ");
gets(s);
printf("\n\n");
puts(s);
if(fun(s))
printf("YES\n");
else
printf("NO\n");
/************************************/
out=fopen("debug\\out12.dat","w");
for(i=0;i<4;i++)
if(fun(test[i]))
fprintf(out,"YES\n");
else
fprintf(out,"NO\n");
fclose(out);
/************************************/
}
【参考代码】
int fun(char str[])
{
int i,n=0,fg=1,j=0;
while (str[j])
{
n++;
j++;
}
for(i=0;i<n/2;i++)
/*循环比较字符*/
if(str[i]==str[n-1-i]);
/*相同,什么都不作*/
else
/*不同,直接跳出循环*/
{
fg=0;
break;
}
return fg;
}
/*请编写函数fun,该函数的功能是:统计一行字符串中单词的个数,作为函数值返回。
一行字符串在主函数中输入,规定所有单词由小写字母组成,单词之间有若干个空格隔开,一行的开始没有空格。
试题程序:*/
#include<string.h>
#include<stdio.h>
#define N 80
int fun(char *s)
{
/************Begin*************/
/*************End*************/
}
void main()
{
FILE *wf;
char line[N];
int num=0;
printf("Enter a string:\n ");
gets(line);
num=fun(line);
printf("The number of word is:%d\n\n ",num);
/******************************/
wf=fopen("d:\\out19.dat","w");
fprintf(wf,"%d",fun("This is a big car"));
fclose(wf);
/*****************************/
}
【参考代码】
int i,j=0;
for(i=0;s[i]!='\0';i++)
if(s[i]!=' '&&(s[i+1]==' '||s[i+1]== '\0'))
/*如果一个字母的下一个字符为空格或者结束标记,则表示一个单词结束*/
j++;
return j;/*返回单词个数*/
/*假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:除了字符串前导的*号之外,将串中其他*号全部删除。
在编写函数时,不得使用C语言提供的字符串函数。
例如,若字符串中的内容为****A*BC*DEF*G*******,删除后,字符串中的内容则应当是****ABCDEFG。
试题程序: */
#include <string.h>
#include <stdio.h>
void fun (char *a)
{
/************Begin*************/
/************End**************/
}
void main()
{
char s[81];
FILE *in,*out;
int i;
printf("Enter a string :\n");
gets(s);
fun(s);
printf("The string after deleted:\n");
puts(s);
/******************************/
in=fopen("in21.dat","r");
out=fopen("debug\\out21.dat","w");
for(i=0;i<8;i++)
{
fscanf(in,"%s",s);
fun(s);
fprintf(out,"%s\n",s);
}
fclose(in);
fclose(out);
/******************************/
}
【参考代码】
int i=0;
char *p=a;
while(*p&&*p=='*')
{
a[i]=*p;i++;p++;
}
while(*p)
{
if(*p!='*')
{
a[i]=*p;i++;
}
p++;
}
a[i]='\0';
/*假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:将字符串中的前导*号全部删除,中间和后面的*号不删除。
例如,若字符串中的内容为****A*BC*DEF*G*******,删除后,字符串中的内容则应当是A*BC*DEF*G*******。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序: */
#include <stdio.h>
void fun (char *a)
{
/************Begin**************/
/************End*************/
}
void main()
{
FILE *wf,*in;
int i;
char s[81],t[81]="****A*BC*DEF*G*******";
printf("Enter a string :\n");
gets(s);
fun(s);
printf("The string after deleted:\n");puts(s);
/******************************/
in=fopen("in18.dat","r");
wf=fopen("debug\\out18.dat","w");
for(i=0;i<8;i++)
{
fscanf(in,"%s",t);
fun(t);
fprintf(wf,"%s\n",t);
}
fclose(in);
fclose(wf);
/*****************************/
}
【参考代码】
char *p=a;
while(*p=='*')
p++;
/*指针p指向字符串第一个字母*/
for(;*p!='\0';p++,a++)
*a=*p;
*a='\0';
/*请编一个函数fun(char *s),该函数的功能是把字符串中的内容逆置。
例如,字符串中原有的字符串为abcdefg,则调用该函数后,串中的内容为gfedcba。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:*/
#include <string.h>
#include <stdio.h>
#define N 81
void fun(char*s)
{
/************Begin*************/
/************End**************/
}
void main()
{
char a[N];
FILE *out;
printf("Enter a string:");
gets(a);
printf("The original string is:");
puts(a);
fun(a);
printf("\n");
printf("The string after modified:");
puts(a);
strcpy(a,"Hello World! This is a first C program!");
fun(a);
/******************************/
out=fopen("d:\\out17.dat","w");
fprintf(out,"%s",a);
fclose(out);
/******************************/
}
【参考代码】
char ch;
int i,m,n;
i=0;
m=n=strlen(s)-1;
while(i<(n+1)/2)
{
ch=s[i];
s[i]=s[m];
s[m]=ch;
i++;
m--;
}
/*请编写函数fun,其功能是:将两个两位数的正整数a、b合并形成一个整数放在c中。
合并的方式是:将a数的十位和个位依次放在c数的千位和十位上,b数的十位和个位依次放在c数的百位和个位上。
例如,当a=45,b=12,调用该项函数后,c=4152。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序: */
#include <stdio.h>
#include <conio.h>
void fun(int a ,int b,long *c)
{
/************Begin************/
/************End***************/
}
void main()
{
int a,b;
long c;
FILE *out;
printf("Input a ,b: ");
scanf("%d%d",&a,&b);
fun(a,b,&c);
printf("The result is :%ld\n",c);
/******************************/
out=fopen("debug\\out20.dat","w");
for(a=10;a<20;a++)
{
fun(a,109-a,&c);
fprintf(out,"%d\n",c);
}
fclose(out);
/******************************/
}
【参考代码】
*c=(a/10)*1000+(b/10)*100+(a%10)*10+b%10;
程序改错
/*下列给定程序中函数fun的功能是:先将在字符串s中的字符按逆序存放到t串中,然后把s中的字符按正序连接到t串的后面。
例如,当s中的字符串为ABCDE时,则t中的字符串应为EDCBAABCDE。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序: */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void fun (char *s, char *t )
{
int i,s1;
s1=strlen(s);
for (i=0;i<s1;i++)
/**********ERROR**********/
t[i]=s[s1-1];
for (i=0;i<s1;i++)
t[s1+i]=s[i];
/**********ERROR**********/
t[2*s1]="\0";
}
void main()
{char s[100], t[100];
printf("\nPlease enter string s: ");
scanf("%s",s);
fun(s,t);
printf ("The result is: %s\n",t);
}
【参考答案】
t[i]=s[s1-1-i];
t[i]=s[s1-i-1i];
【参考答案】
t[2*s1]='\0';
t[2*s1]=0;
/*下列给定程序中,函数fun的功能是:在字符串str中找出ASCII码值最大的字符,将其放在第一个位置上,
并将该字符前的原字符向后顺序移动。例如,调用fun函数之前给字符串输入ABCDeFGH,调用后字符串中的内容为eABCDFGH。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序: */
#include <stdio.h>
/**********ERROR**********/
fun(char *p)
{ char max, *q;int i=0;
max=p[i];
while (p[i]!=0)
{if (max<p[i])
{
/**********ERROR**********/
p = q +i;max=p[i];
}
i++;
}
/**********ERROR**********/
while(q<p)
{*q=*(q-1);
q--;
}
p[0]=max;
}
void main()
{char str[80];
printf("Enter a string: "); gets(str);
printf("\nThe original string: ");
puts(str);
fun(str);
printf("\nThe string after moving: ");
puts(str); printf("\n\n");
}
【参考答案】
void fun(char *p)
【参考答案】
q=p+i;
q=i+p;
【参考答案】
while(q>p)
while(p<q)
/*下列给定程序中,函数fun的功能是:计算s所指字符串中含有t所指字符串的数目,并作为函数值返回。
请改正函数fun中的错误或在横线处填上适当的内容并把横线删除,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:*/
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#define N 80
int fun(char *s,char *t)
{ int n;
char *p, *r;
n=0;
p=&s[0];
/**********ERROR**********/
*r=t;
while(*p)
{
if(*r==*p)
{
r++;
if(*r=='\0')
{
n++;
/**********ERROR**********/
【1】;
}
}
p++;
}
return n;
}
void main()
{char a[N],b[N]; int m;
system("CLS");
printf("\nPlease enter string a: ");
gets(a);
printf("\nPlease enter substring b: ");
gets(b);
m=fun(a,b);
m=printf("\nThe result is :m=%d\n",m);
}
【参考答案】
r=t;
【参考答案】
r=t;
r=&t[0];
/*下列给定程序中,函数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>
#define N 20
int fun(float *s, int n,float *aver)
{ float ave ,t=0.0;
int count=0,k,i;
for(k=0;k<n;k++)
/**********ERROR**********/
t=s[k];
ave=t/n;
for(i=0;i<n;i++)
if(s[i]<ave) count++;
/**********ERROR**********/
*aver=&ave;
return count;
}
void main()
{ float s[30],aver;
int m,i;
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);
}
【参考答案】
t+=s[k];
t=t+s[k];
【参考答案】
*aver=ave;
/*下列给定程序中,函数fun的功能是:实现两个整数的交换。例如,给a和b分别输入60和65,输出为:a=65 b=60
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:*/
#include <stdio.h>
#include <stdlib.h>
/**********ERROR**********/
void fun(int a,b)
{ int t;
/**********ERROR**********/
t=b;b=a;a=t;
}
void main()
{int a,b;
printf("Enter a, b: ");
scanf("%d%d",&a,&b);
fun(&a, &b);
printf("a=%d b=%d\n ", a,b);
}
【参考答案】
void fun(int *a,int *b)
【参考答案】
t=*b;*b=*a;*a=t;
t=*b,*b=*a,*a=t;
t=*a;*a=*b;*b=t;
t=*a,*a=*b,*b=t;
/*给定程序中函数fun的功能是:把主函数中输入的3个数,最大的放在 a中,最小的放在c中,中间的放在b中。
例如,输入的数为:55 12 34,
输出结果应当是:a=55.0 , b=34.0 , c=12.0。
请改正程序中的错误,使它能得出正确结果。
给定源程序:*/
#include <stdio.h>
void fun(float *a,float *b,float *c)
{
/**********ERROR**********/
float *k;
if( *a<*b )
{ k=*a; *a=*b; *b=k; }
/**********ERROR**********/
if( *a>*c )
{ k=*c; *c=*a; *a=k; }
if( *b<*c )
{ k=*b; *b=*c; *c=k; }
}
int main()
{ float a,b,c;
printf("Input a b c: "); scanf("%f%f%f",&a,&b,&c);
printf("a = %4.1f, b = %4.1f, c = %4.1f\n\n",a,b,c);
fun(&a,&b,&c);
printf("a = %4.1f, b = %4.1f, c = %4.1f\n\n",a,b,c);
return 0;
}
【参考答案】
float k;
【参考答案】
*a<*c
*c>*a
/*给定程序中函数fun的功能是:把主函数中输入的3个数,最大的放在 a中,最小的放在c中,中间的放在b中。
例如,输入的数为:55 12 34,
输出结果应当是:a=55.0 , b=34.0 , c=12.0。
请改正程序中的错误,使它能得出正确结果。
给定源程序:*/
#include <stdio.h>
void fun(float *a,float *b,float *c)
{
/**********ERROR**********/
float *k;
if( *a<*b )
{ k=*a; *a=*b; *b=k; }
/**********ERROR**********/
if( *a>*c )
{ k=*c; *c=*a; *a=k; }
if( *b<*c )
{ k=*b; *b=*c; *c=k; }
}
int main()
{ float a,b,c;
printf("Input a b c: "); scanf("%f%f%f",&a,&b,&c);
printf("a = %4.1f, b = %4.1f, c = %4.1f\n\n",a,b,c);
fun(&a,&b,&c);
printf("a = %4.1f, b = %4.1f, c = %4.1f\n\n",a,b,c);
return 0;
}
【参考答案】
p=j;
【参考答案】
p=i;
程序填空
/*给定程序中,函数fun的功能是:在形参s所指字符串中的每个数字字符之后插入一个*号。
例如,形参s所指的字符串为:def35adh3kjsdf7。执行结果为:def3*5*adh3*kjsdf7*。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。
试题程序:*/
#include <stdio.h>
void fun(char *s )
{
int i,j,n;
for(i=0;s[i]!='\0';i++)
/**********FILL**********/
if(s[i]>='0' [1] s[i]<='9')
{
n=0;
/**********FILL**********/
while(s[i+1+n]!= [2] )
n++;
for(j=i+n+1;j>i;j--)
/**********FILL**********/
s[j+1]= [3] ;
s[j+1]='*';
i=i+1;
}
}
void main()
{
char s[60]="ba3a54cd23a";
printf("\n the original string is: %s\n",s);
fun(s);
printf("\nthe result is: %s\n",s);
}
【参考答案】
&&
【参考答案】
0
'\0'
【参考答案】
s[j]
*(s+j)
请勿改动main函数和其他函数中的任何内容,仅在main函数的横线上填入所编写的若干表达式或语句。
试题程序:*/
#include <stdlib.h>
#include <string.h>
void main()
{
char str1[81],str2[81];
char *p1=str1,*p2=str2;
do
{
printf(" Input str1 \n");
gets(str1);
printf(" Input str2 \n");
gets(str2);
/**********FILL**********/
}while( 【1】 );
/**********FILL**********/
while( 【2】 )
*p1++=*p2++;
printf(" Display str1 \n");
/**********FILL**********/
puts( 【3】 );
}
【参考答案】
strlen(str1)<strlen(str2)
strlen(str2)>strlen(str1)
【参考答案】
*p2
*p2!=0
【参考答案】
str1
/*给定程序的功能是:分别统计字符串中大写字母和小写字母的个数。
例如,给字符串ss输入:AaaaBBbl23CCccccd,则输出结果应为:upper=5,lower=9。
注意:部分源程序给出如下。
请勿改动函数中的其他内容,仅在横线上填入所编写的若干表达式或语句。
试题程序: */
#include <stdlib.h>
#include <stdio.h>
void fun(char *s,int *a,int *b)
{
while(*s)
{
if(*s>='A' && *s<='Z')
/**********FILL**********/
[1] ;
if(*s>='a' && *s<='z')
/**********FILL**********/
【2】 ;
s++;
}
}
void main()
{
char s[100];
int upper=0,lower=0;
printf("\nPlease a string: ");
gets(s);
fun(s,&upper,&lower);
/**********FILL**********/
printf("\n upper=%d lower=%d\n",【3】 );
}
【参考答案】
(*a)++
++(*a)
*a+=1
*a=*a+1
【参考答案】
(*b)++
++(*b)
*b+=1
*b=*b+1
【参考答案】
upper,lower
/*请补充main函数,该函数的功能是:从键盘输入若干字符放到一个字符数组中,当按回车键时结束输入,
最后输出这个字符数组中的所有字符。
注意:部分源程序给出如下。
请勿改动main函数和其他函数中的任何内容,仅在main函数的横线上填入所编写的若干表达式或语句。
试题程序:*/
#include <stdlib.h>
#include <stdio.h>
void main()
{
int i=0;
char s[81];
char *p=s;
printf(" Input a string \n");
for(i=0;i<80;i++)
{
s[i]=getchar();
if(s[i]=='\n')
/**********FILL**********/
【1】 ;
}
/**********FILL**********/
s[i]= 【2】 ;
printf(" display the string \n");
while(*p)
/**********FILL**********/
putchar( 【3】 );
}
【参考答案】
Break
【参考答案】
0
'\0'
【参考答案】
*p++
/*下面程序是实现两数的交换,如输入5 9,输出9 5。*/
#include<stdio.h>
void swap(int *p1, int *p2);
int main()
{
int a,b;
int *point1,*point2;
/**********FILL**********/
scanf("%d %d", [1] );
point1=&a; point2=&b;
/**********FILL**********/
swap( [2] );
printf("\n%d,%d\n",a,b);
return 0 ;
}
void swap(int *p1, int *p2)
{ int p;
p=*p1;
/**********FILL**********/
[3] ;
*p2=p;
}
【参考答案】
&a,&b
【参考答案】
point1,point2
&a,&b
【参考答案】
*p1=*p2
展开阅读全文