C语言编程问题

程序改错(在FOUND下的程序中找到错误并改正):
1题目:将一个字符串中的大写字母转换成小写字母。例如:输入aSdFG输出为asdfg。
#include<stdio.h>
/********FOUND*******/
bool fun(char *c)
{if(*c<='Z'&&*c>='A')*c-='A'-'a';
/*******FOUND********/
fun= c;
}
main()
/******FOUND********/
char s[81],*p=s;
gets(s);
while(*p)
{*p=fun(p);
/******FOUND*********/
puts(*p);
p++;
}
putchar('\n');
}
2题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数)
#include "stdio.h"
main()
{float peven(),podd(),dcall();
float sum;
int n;
while (1)
{/*******FOUND*******/
scanf("%d",n);
if(n>1)
break;
}
if(n%2==0)
{printf("Even=");
sum=dcall(peven,n);
}
else
{printf("Odd=");
sum=dcall(podd,n);
}
printf("%f",sum);
getch();
}
float peven(int n)
{float s;
int i;
s=0;
for(i=2;i<=n;i+=2)
/*******FOUND********/
s+=1%(float)i;
return(s);
}
float podd(n)
int n;
{float s;
int i;
/*******FOUND********/
s=1;
for(i=1;i<=n;i+=2)
s+=1/(float)i;
return(s);
}
float dcall(fp,n)
float (*fp)();
int n;
{float s;
s=(*fp)(n);
return(s);
}
3.题目:给定程序MODI1.C中函数 fun 的功能是:输入两个双精度数,函数返回它们的平方和的平方根值。例如输入:22.936 和 14.121,输出为:y = 26.934415。
#include <stdio.h>
#include <conio.h>
#include <math.h>
/*******FOUND********/
double fun (double *a, *b)
{ double c;
/*******FOUND********/
c = sqr(a*a + b*b);
/******FOUND********/
return *c;
}
main ( )
{ double a, b, y;
clrscr( );
printf ( "Enter a, b : ");
scanf ("%lf%lf", &a, &b );
y = fun (&a, &b);
printf ("y = %f \n", y );
}
4题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数)
#include "stdio.h"
main()
{float peven(),podd(),dcall();
float sum;
int n;
while (1)
{/******FOUND********/
scanf("%d",n);
if(n>1)
break;
}
if(n%2==0)
{printf("Even=");
sum=dcall(peven,n);
}
else
{printf("Odd=");
sum=dcall(podd,n);
}
printf("%f",sum);
getch();
}
float peven(int n)
{float s;
int i;
s=0;
for(i=2;i<=n;i+=2)
/******FOUND********/
s+=1%(float)i;
return(s);
}
float podd(n)
int n;
{float s;
int i;
/*******FOUND********/
s=1;
for(i=1;i<=n;i+=2)
s+=1/(float)i;
return(s);
}
float dcall(fp,n)
float (*fp)();
int n;
{float s;
s=(*fp)(n);
return(s);
}
5题目:给定程序MODI1.C中函数 fun 的功能是:根据整型形参 n,计算某一数据项的值。A[1]=1, A[2]=1/(1 + A[1]), A[3]=1/(1 + A[2]), …,A[n]=1/(1 + A[n-1])例如,若 n=10,则应输出:a10=0.617977。
#include <conio.h>
#include <stdio.h>
/*******FOUND********/
int fun ( int n )
{ float A=1; int i;
/*******FOUND*******/
for (i=2; i<n; i++)
/*******FOUND*******/
A = 1.0\(1+A);
return A ;
}
main( )
{ int n ;
clrscr( ) ;
printf("\nPlease enter n: ") ;
scanf("%d", &n ) ;
printf("A%d=%f\n",n, fun(n) ) ;
}

第1个回答  2019-12-10
#include<stdio.h>
main()
{
int
i,
j,s;
//定义3个变量
for
(
i=1,s=0;i<=10;i++)
//用i做第一层循环变量,从1到10共10此

for
(j=1;j<=i;j++)
//用j做第二层循环变量,从1到i工i次。次数更加第一层循环而递增

s=s+j;
//s用于存放计算的值,把每次j的值进行累计
printf("s=%d\n",s);
//输出s的值
}
也就是说,s的值是
1
1
2
1
2
3
1
2
3
4
.....
1
2......10
这些数字的和
第2个回答  2019-01-18
你应该学会自己检错,学会利用工具调试程序
。第一个程序是函数strupr未定义、少写语句分号、"\n"、puts错写成"\"、put。另外和第二个程序一样,正确主函数的框架至简应该是:int
main()
{...}。第二个程序"\0"错写成"\\0"、第一个循环括号不匹配。
加上ctype.h。
void
strupr(char
*str)
{
while(*str
!=
'\0')
{
*str
=
toupper(*str);
++str;
}
}
第3个回答  2019-12-10
你好:
首先说一下这个是用来干什么的:是计算自然数的和。
第二个for语句,总共输出10次。
第一次:S=1;
第二次:S=3;
第三次:S=6;
第四次:S=10;
第五次:S=15;
第六次:S=21;
第七次:S=28;
第八次:S=36;
第九次:S=45;
第十次:S=55;
第一个for语句输出一次,它将第二个for语句包括在内,也就是说计算刚才的10个数的和:
即,1+3+6+10+15+21+28+36+45+55=220
所以,最后结果是220
希望能够帮助到您。
第4个回答  2019-03-28
*
P83-4-17
*/
#include
"stdio.h"
#include
"string.h"
main()
{char
a[40],b[80];
int
c;
gets(a);
strcpy(b,a);
strupr(b);
strcat(b,a);
c=strcmp(a,b);
if(c<0)
printf("a<b
\
");//注意少个;
else
if(c>0)
printf("a>b
\
");//同上.
else
printf("a=b
\
");
printf("length
of
a(%s)
=
%d
\
",a,strlen(a));
printf("length
of
b(%s)
=
%d
\
",b,strlen(b));
puts(a);//注意不是put.
puts(b);//同上.
}
第二个问题:也是一样不知那里有问题。
/*P85-4-19*/
#include
"stdio.h"
main()
{char
a[80],b[80],c[80];
int
i;
gets(a);
i=0;
while(a[i]!='\\0')//a前多一个(.
{b[i]=a[i]+3;
/*当前字符加3后存入数组b*/
i++;
}
b[i]=a[i];
i=0;
while(b[i]!='\\0')//不用(b[i]).
{c[i]=b[i]-3;
i++;
}
c[i]=b[i];
puts(b);
puts(c);
}
第5个回答  2019-05-11
这个可以使用
#include<stdio.h>
#include<stdlib.h>
int
main()
{

int
i=0,mu=0,ch=0,sp=0;

char
*str;

str=(char
*)calloc(20,sizeof(char));

printf("请您输入15任意的字符\n");

do{

str[i]=getchar();

i++;

}while(str[i-1]!='\n');

str[i+1]='\0';

i=0;

while(str[i]!='\0'){

if(str[i]=='
')
sp++;

if((str[i]>=62&&str[i<=90])||(str[i]>=97&&str[i]<=122))
ch++;

if(str[i]>=48&&str[i]<=57)
mu++;

i++;

}

printf("这个字符串里面有%d个空格\n",sp);

printf("这个字符串里面有%d个字母\n",ch);

printf("这个字符串里面有%d个数字\n",mu);

free(str);

return
0;
}
相似回答