60.请编写函数fun,该函数的功能是:删去一维数组中所有相同的数,使之只剩一个。数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数。
例如,若一维数组中的数据是: 2 2 2 3 4 4 5 6 6 6 6 7 7 8 9 9 10 10 10。
删除后,数组中的内容应该是: 2 3 4 5 6 7 8 9 10。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#define N 80
int fun(int a[], int n)
{
int i, j=1;
for(i=1;i<n;i )
if(a[j-1]!=a[i])
a[j ]=a[i];
return j;
}
main()
{
FILE *wf;
int a[N]={ 2,2,2,3,4,4,5,6,6,6,6,7,7,8,9,9,10,10,10,10}, i, n=20;
printf("The original data :\n");
for(i=0; i<n; i )
printf("%3d",a[i]);
n=fun(a,n);
printf("\n\nThe data after deleted :\n");
for(i=0; i<n; i )
printf("%3d",a[i]);
printf("\n\n");
wf=fopen("out.dat","w");
for(i=0; i<n; i )
fprintf(wf,"%3d",a[i]);
fclose(wf);
}
61.请编写函数fun,该函数的功能是:统计各年龄段的人数。N个年龄通过调用随机函数获得,并放在主函数的age数组中;要求函数把0至9岁年龄段的人数放在d[0]中,把10至19岁年龄段的人数放在d[1]中,把20至29岁年龄段的人数放在d[2]中,其余依此类推,把100岁(含100)以上年龄的人数都放在d[10]中。结果在主函数中输出。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
# define N 50
# define M 11
void fun(int *a, int *b)
{
int i,j;
for(j=0;j<M;j )
b[j]=0;
for(i=0;i<N;i )
if(a[i]>=100)
b[10] ;
else
b[a[i]/10] ;
}
double rnd()
{
static t=29,c=217,m=1024,r=0;
r=(r*t c)%m;
return((double)r/m);
}
main()
{
FILE *wf;
int age[N], i,d[M];
int b[N]={32,45,15,12,86,49,97,3,44,52,17,95,63};
for(i=0; i<N; i )
age[i]=(int)(115*rnd());
printf("The original data :\n");
for(i=0; i<N; i )
printf((i 1)%10==0? "%4d\n":"%4d",age[i]);
printf("\n\n");
fun(age,d);
for(i=0; i<10; i )
printf("%4d---%4d :%4d\n", i*10, i*10 9,d[i]);
printf("Over 100 : %4d\n",d[10]);
wf=fopen("out.dat","w");
fun(b,d);
for(i=0; i<10; i )
fprintf(wf,"%4d---%4d :%4d\n", i*10, i*10 9,d[i]);
fprintf(wf,"Over 100 : %4d",d[10]);
fclose(wf);
}
62.请编写函数fun,该函数的功能是:统计一行字符串中单词的个数,作为函数值返回。一行字符串在主函数中输入,规定所有单词由小写字母组成,单词之间由若干个空格隔开,一行的开始没有空格。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include<string.h>
#include<stdio.h>
#define N 80
int fun(char *s)
{
int i,j=0;
for(i=0;s[i]!='\0';i )
if(s[i]!=' '&&(s[i 1]==' '||s[i 1]=='\0'))
j ;
return j;
}
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("out.dat","w");
fprintf(wf,"%d",fun("a big car"));
fclose(wf);
}
63.请编写一个函数fun,它的功能是:计算并输出给定整数n的所有因子(不包括1与自身)之和。规定n的值不大于1000。
例如,若主函数从键盘给n输入的值为856,则输出为sum=763。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
int fun(int n)
{
int s=0,i;
for(i=2;i<=n-1;i )
if(n%i==0) s =i;
return s;
}
main()
{ int n,sum;
printf("Input n: "); scanf("%d",&n);
sum=fun(n);
printf("sum=%d\n",sum);
NONO();
}
NONO()
{/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */
FILE *rf, *wf ; int i, n, sum ;
rf = fopen("K:\\k1\\24000318\\in.dat","r") ;
wf = fopen("K:\\k1\\24000318\\out.dat","w") ;
for(i = 0 ; i < 10 ; i ) {
fscanf(rf, "%d", &n) ;
sum = fun(n) ;
fprintf(wf, "%d=%d\n", n, sum) ;
}
fclose(rf) ; fclose(wf) ;
}
64.请编写函数fun,其功能是:将s所指字符串中ASCII值为奇数的字符删除,串中剩余字符形成一个新串放在t所指的数组中。
例如,若s所指字符串中的内容为ABCDEFGl2345,其中字符A的ASCII码值为奇数、…字符1的ASCII码值也为奇数、…都应当删除,其他依次类推。最后t所指的数组中的内容应是BDF24。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#include<string.h>
#include<conio.h>
void fun ( char *s, char t[ ])
{ int i, j=0, n ;
n=strlen(s);
for(i=0; i<n; i )
if (s[i]%2==0)
{ t[j]=s[i] ;
j ; }
t[j]= '\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);
}
65.请编写函数fun,其功能是:将两个两位数的正整数a、b合并形成一个整数放在c中。合并的方式是:将a数的十位和个位数依次放在c数的百位和个位上,b数的十位和个位数依次放在c数的十位和千位上。
例如,当a=45,b=12,调用该函数后c=2415。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#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);
}
66.假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:删除字符串中所有的*号。在编写函数时,不得使用C语言提供的字符串函数。
例如,若字符串中的内容为****A*BC*DEF*G*******,删除后,字符串中的内容则应当是ABCDEFG。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include<conio.h>
#include<stdio.h>
void fun(char *a)
{
int i,j=0;
for(i=0;a[i]!='\0';i )
if(a[i]!='*')
a[j ]=a[i];
a[j]='\0';
}
main()
{
FILE *wf;
char s[81],*p="****A*BC*DEF*G*******";
printf("Enter a string:\n");
gets(s);
fun(s);
printf("The string after deleted:\n");
puts(s);
wf=fopen("out.dat","w");
fun(p);
fprintf(wf,"%s",p);
fclose(wf);
}
67.学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:函数返回指定学号的学生数据,指定的学号在主函数中输入。若没找到指定学号,在结构体变量中给学号置空串,给成绩置-1,作为函数值返回(用于字符串比较的函数是strcmp)。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 16
typedef struct
{ char num[10];
int s;
}STRUC;
STRUC fun(STRUC *a, char *b)
{
int i;
STRUC str={"\0",-1};
for(i=0;i<N;i )
if(strcmp(a[i].num,b)==0)
str=a[i];
return str;
}
main()
{
FILE *wf;
STRUC s[N]={{ "GA005",85},{"GA003",76},{"GA002",69},{"GA004",85},
{"GA001",91},{"GA007",72},{"GA008",64},{"GA006",87},
{"GA015",85},{"GA013",91},{"GA012",64},{"GA014",91},
{"GA011",77},{"GA017",64},{"GA018",64},{"GA016",72}};
STRUC h;
char m[10];
int i;
FILE *out;
printf("The original data:\n");
for(i=0;i<N;i )
{ if(i%4==0)
printf("\n");
printf("%s %3d",s[i].num,s[i].s);
}
printf("\n\nEnter the number: ");
gets(m);
h=fun(s,m);
printf("The data: ");
printf("\n%s %4d\n",h.num,h.s);
printf("\n");
out=fopen("out80.dat", "w");
h=fun(s, "GA013");
fprintf(out, "%s %4d\n",h.num,h.s);
fclose(out);
wf=fopen("out.dat","w");
fprintf(wf,"%s %4d",h.num,h.s);
fclose(wf);
}