76.请编写函数fun,其功能是:计算并输出当x<0.97时下列多项式的值,直到|Sn-S(n-1)|<0.000001为止。
Sn=1 0.5x 0.5(0.5-1)/2!x(2) ... 0.5(0.5-1)(0.5-2).....(0.5-n 1)/n!x(n)
例如,若主函数从键盘给x输入0.21后,则输出为s=1.100000。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#include <math.h>
double fun(double x)
{
double s1=1.0,p=1.0,sum=0.0,s0,t=1.0;
int n=1;
do
{s0=s1;
sum =s0;
t*=n;
p*=(0.5-n 1)*x;
s1=p/t;
n ;}while(fabs(s1-s0)>1e-6);
return sum;
}
main()
{ double x,s;
printf("Input x: "); scanf("%lf",&x);
s=fun(x);
printf("s=%f\n",s);
NONO();
}
NONO()
{/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */
FILE *rf, *wf ; int i ; double s, x ;
rf = fopen("K:\\k1\\24000317\\in.dat","r") ;
wf = fopen("K:\\k1\\24000317\\out.dat","w") ;
for(i = 0 ; i < 10 ; i ) {
fscanf(rf, "%lf", &x) ;
s = fun(x) ;
fprintf(wf, "%lf\n", s) ;
}
fclose(rf) ; fclose(wf) ;
}
77.请编写函数fun,其功能是:将两个两位数的正整数a、b合并形成一个整数放在c中。合并的方式是:将a数的十位和个位数依次放在c数的个位和百位上,b数的十位和个位数依次放在c数的十位和千位上。
例如,当a=45,b=12,调用该函数后c=2514。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <conio.h>
#include <stdio.h>
/*本题的关键在于如何表示出个、十、百、千位数。对于一个两位的整数,用10对它求余得到个位数上的数,将它除10得到十位数上的数。*/
void fun ( int a, int b , long *c)
{ *c=(b%10)*1000 (a%10)*100 (b/10)*10 a/10 ; }
main()
{ int a,b; long c;
printf(" input a, b: ");
scanf("%d%d", &a,&b);
fun(a,b,&c);
printf(" the result is :%ld\n", c);
}
78.请编写函数fun,其功能是:将s所指字符串中ASCII值为偶数的字符删除,串中剩余字符形成一个新串放在t所指的数组中。
例如,若s所指字符串中的内容为ABCDEFGl2345,其中字符B的ASCII码值为偶数、…、字符2的ASCII码值为偶数、…都应当删除,其他依此类推。最后t所指的数组中的内容应是ACEGl35。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#include<string.h>
#include<conio.h>
void fun(char *s, char t[])
{
int i=0 ;
for(;*s!='\0';s )
if(*s%2==1)
t[i ]=*s;
t[i]='\0';
}
main()
{
FILE *wf;
char s[100],t[100];
printf("\nPlease enter string S: ");
scanf("%s",s);
fun(s,t);
printf("\nThe result is :%s\n",t);
wf=fopen("out.dat","w");
fun("ABCDEFG12345",t);
fprintf(wf,"%s",t);
fclose(wf);
}
79.已知学生的记录由学号和学习成绩构成,N名学生的数据已存入a结构体数组中。请编写函数fun,该函数的功能是:找出成绩最低的学生记录,通过形参返回主函数(规定只有一个最低分)。已给出函数的首部,请完成该函数。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define N 10
typedef struct ss
{ char num[10]; int s;} STU;
/*这个题目中有一结构组数,要找的是数组元素中整型成员的值最小的元素。要注意本题中的赋值方式(分两部分,对于字符型成员只能用strcpy()函数,而不能用等号“=”进行赋(即不能用(*s).num=a[k].num),这与字符串的赋值相同(切记)。*/
fun( STU a[], STU *s)
{ int k,i;
(*s).s=a[0].s;
for( i=0;i<N; i )
if (a[i].s<(*s).s)
{(*s).s=a[i].s; k=i; }
strcpy ( (*s).num, a[k].num);
}
main()
{ STU a[N]={ {"A01",81},{"A02",89},{"A03",66}
,{"A04",87},{"A05",77},
{"A06",90},{"A07",79},{"A08",61},{"
;A09",80},{"A10",71} }, m;
int i;
printf("**** the original data ****\n");
for(i=0;i<N; i ) printf("N0=%s Mark=%d\n", a[i].num,a[i].s
);
fun( a,&m);
printf("**** the result****\n");
printf(" the lowest: %s ,%d\n", m.num,m.s);
}
80.程序定义了N×N的二维数组,并在主函数中自动赋值。请编写函数fun(inta[][N],int n),该函数的功能是:使数组左下半三角元素中的值乘以n。例如:若n的值为3,a数组中的值为
,则返回主程序后a数组中的值应为。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define N 5
/*只要能表示出左下半角的元素即可。*/
int fun( int a[][N], int n)
{ int i,j;
for(i=0; i<N; i )
for(j=0; j<=i; j )
a[i][j]*=n;
}
main()
{ int a[N][N],n,i,j;
printf("**** the array ****\n");
for( i=0; i<N; i )
{for( j=0; j<N; j )
{a[i][j]=rand()%10; printf("%4d", a[i][j]); }
printf("\n");
}
do n=rand()%10 ; while(n>=3);
printf("n=%4d \n", n);
fun( a,n);
printf( "**** the result ****\n");
for(i=0;i<N; i )
{for(j=0; j<N; j ) printf("%4d", a[i][j]);
printf("\n");
}
}
81.请编写函数fun,其功能是:将两个两位数的正整数a、b合并形成一个整数放在c中。合并的方式是:将a数的十位和个位数依次放在c数的百位和个位上,b数的十位和个位数依次放在c数的千位和十位上。
例如,当a=45,b=12,调用该函数后c=1425。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <conio.h>
#include <stdio.h>
/*本题的关键在于如何表示出个、十、百、千位数。对于一个两位的整数,用10对它求余得到个位数上的数,将它除10得到十位数上的数。*/
void fun ( int a, int b , long *c)
{ *c=(b/10)*1000 (a/10)*100 (b%10)*10 a%10 ; }
main()
{ int a,b; long c;
printf(" input a, b: ");
scanf("%d%d", &a,&b);
fun(a,b,&c);
printf(" the result is :%ld\n", c);
}