41、采用下面的公式编程求π,要求计算到最后一项的值小于10-6为止。
#include <stdio.h>
#include <math.h>
void main()
{double n=1,t=0,pi=0.0,s=1.0,epselon;
do
{t=s/n;
pi+=t;
____________________________________________
n+=2;
epselon=fabs(t);
}while(epselon>=1e-6);
printf("pi=%8.6f\n",4*pi);
}
42、从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件“test.txt”中保存。输入的字符串以“!”结束。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{FILE *fp;
char str[100];
int i=0;
if((fp=fopen("test.txt","w"))==NULL)
{printf("Can't open this file.\n");
exit(0);
}
printf("Input a string:\n");
gets(str);
while(str[i]!='!')
{if(str[i]>='a'&&str[i]<='z')
str[i]=str[i]-32;
fputc(str[i],fp);
i++;
}
fclose(fp);
_______________________________________________________________
fgets(str,strlen(str)+1,fp);
printf("%s\n",str);
fclose(fp);
}
43、将一个10×10的矩阵对角线元素置0,其余元素置1。
#include <stdio.h>
#define N 10
void main()
{int a[N][N],i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
_______________________________________________________
for(i=0;i<N;i++)
{a[i][i]=0;
a[i][N-i-1]=0;
}
for(i=0;i<N;i++)
{for(j=0;j<N;j++)
printf("%2d",a[i][j]);
printf("\n");
}
}