编程题:
94.规定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:将字符串中的前导*号全部删除,中间和尾部的*号不删除。
例如,若字符串中的内容为*******A*BC*DEF*G****,删除后,字符串中的内容则应当是 A*BC*DEF*G****。在编写函数时,不得使用C语言提供的字符串函数。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#include
void fun(char *a)
{
}
main()
{
char s[81];
printf(“Enter a string :\n”);
gets(s);
fun(s);
printf(“The string after deleted :\n”);
puts(s);
}
96.请编写函数fun,其功能是:计算并输出给定数组(长度为9)中每相邻两个元素之平均值的平方根之和。
例如,若给定数组中的9个元素依次为12.0、34.0、4.0、23.0、34.0、45.0、18.0、3.0、11.0,则输出应为s=35.951014。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#include
double fun(double x[9])
{
}
main()
{
double s,a[9]={12.0,34.0,4.0,23.0,34.0,45.0,18.0,3.0,11.0};
int i;
printf(“\nThe original data is :\n”);
for(i=0;i<9;i++)
printf(“%6.1f”,a[i]);
printf(“\n\n”);
s=fun(a);
printf(“s=%f\n\n”,s);
}
改错题:
57.下列给定程序中,函数fun的功能是:首先把b所指字符串中的字符按逆序存放,然后将a所指字符串中的字符和b所指字符串中的字符,按排列的顺序交叉合并到c所指数组中,过长的剩余字符接在c所指数组的尾部。例如,当a所指字符串中的内容为abcdefg,b所指字符串中的内容为1234时,c所指数组中的内容应该为a4b3c2d1efg;而当a所指字符串中的内容为1234,b所指字符串中的内容为abcdefg时,c所指数组中的内容应该为1g2f3e4dcba。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include
#include
#include
void fun( char *a, char *b, char *c )
{
int i, j;
char ch;
i = 0;
j = strlen(b)-1;
/********found********/
while ( i > j )
{
ch = b[i];
b[i] = b[j];
b[j] = ch;
i++;
j–;
}
while ( *a || *b )
{
if ( *a )
{
*c = *a;
c++;
a++;
}
if ( *b )
{
*c = *b;
c++;
b++;
}
}
/********found********/
c= 0;
}
main()
{
char s1[100],s2[100],t[200];
clrscr();
printf(“\nEnter s1 string : “);
scanf(“%s”,s1);
printf(“\nEnter s2 steing : “);
scanf(“%s”,s2);
fun( s1, s2, t );
printf(“\nThe result is : %s\n”, t );
}
58.下列给定程序中函数fun的功能是:先将在字符串s中的字符按正序存放到t串中,然后把s中的字符按逆序连接到t串的历面。例如:当s中的字符串为ABCDE时,则t中的字符串应为ABCDEEDCBA。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include
#include
#include
void fun(char *s, char *t)
{
int i, s1;
s1 = strlen(s);
/********found********/
for( i=0; i<=s1; i ++)
t[i] = s[i];
for (i=0; i
t[s1+i] = s[s1-i-1];
/********found********/
t[s1]=’\0′;
}
main()
{
char s[100], t[100];
clrscr();
printf(“\nPlease enter string s:”);
scanf(“%s”, s);
fun(s, t);
printf(“The result is: %s\n”, t);
}