37.某学生的记录由学号、8门课程成绩和平均分组成,学号和8门课程的成绩已在主函数中给出。请编写函数fun,它的功能是:求出该学生的平均分放在记录的ave成员中。请自己定义正确的形参。
例如,若学生的成绩是85.5,76,69.5,85,91,72,64.5,87.5,则他的平均分应当是78.875。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#define N 8
typedef struct
{ char num[10];
double s[N];
double ave;
} STRUC;
void fun( )
{
int i;
p->ave=0.0 ;
for(i=0;i<N;i )
p->ave=p->ave p->s[i];
p->ave= p->ave/N ;
}
main()
{
FILE *wf;
STRUC s={ "GA005 ",85.5,76,69.5,85,91,72,64.5,87.5};
int i;
fun(&s);
printf("The %s's student data:\n", s.num);
for(i=0;i<N;i )
printf("%4.1f\n",s.s[i]);
printf("\nave=%7.3f\n", s.ave);
wf=fopen("out.dat","w");
fprintf(wf,"ave=%7.3f", s.ave);
fclose(wf);
}
38.请编写函数fun,它的功能是:求出ss所指字符串中指定字符的个数,并返回此值。
例如,若输入字符串123412132,输入字符1,则输出3。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include<conio.h>
#include<stdio.h>
#define M 81
/* 注:本题用while()循环来控制字符的移动,每移动一个字符都要进行判断(if(*ss==c))是否为指定的字母,若是则个数加1。这里要注意如何让ss指针向下走动(ss )。*/
int fun(char *ss,char c)
{int num=0;
while(*ss!='\0')
{if(*ss==c) num ;
ss ;
}
return(num);
}
main()
{char a[M],ch;
clrscr();
printf("\nPlease enter a strint:");gets(a);
printf("\nPlease enter a char:");ch=getchar();
printf("\nThe number of the char is:%d\n",fun(a,ch));
}
39.请编写函数fun,该函数的功能是:移动一维数组中的内容;若数组中有n个整数,要求把下标从0到p(p小于等于n-1)的数组元素平移到数组的最后。
例如,一维数组中的原始内容为:1,2,3,4,5,6,7,8,9,10;p的值为3。移动后,一维数组中的内容应为:5,6,7,8,9,10,1,2,3,4。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#define N 80
void fun(int *w, int p, int n)
{
int i,k=0,b[N];
for(i=p 1; i < n; i ) b[k ]=w[i];
for(i=0; i <= p; i ) b[k ]=w[i];
for(i=0; i < n; i ) w[i]=b[i];
}
main()
{ int a[N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int i,p,n=15;
printf("The original data:\n");
for(i=0; i
printf("\n\nEnter p: ");scanf("%d",&p);
fun(a,p,n);
printf("\nThe data after moving:\n");
for(i=0; i
printf("\n\n");
NONO();
}
NONO()
{/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */
FILE *rf,*wf int a[N], i, j, p, n
rf = fopen("bc.in", "r")
wf = fopen("bc.out", "w")
for(i = 0 i < 5 i ) {
fscanf(rf, "%d %d", &n, &p)
for(j = 0 j < n j ) fscanf(rf, "%d", &a[j])
fun(a, p, n)
for(j = 0 j < n j ) fprintf(wf, "%3d", a[j]) fprintf(wf, "\n")
}
fclose(rf) fclose(wf)
}
40.请编写函数fun,该函数的功能是:移动字符串中的内容,移动的规则如下:把第1到第m个字符,平移到字符串的最后,把第m 1到最后的字符移到字符串的前部。
例如,字符串中原有的内容为ABCDEFGHIJK,m的值为3,移动后,字符串中的内容应该是DEFGHIJKABC。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#include<string.h>
#define N 80
void fun (char *w,int m)
{
int i,j;
char t;
for(i=1;i<=m;i )
{t=w[0];
for(j=1;w[j]!='\0';j )
w[j-1]= w[j] ;
w[j-1] =t;
}
}
main()
{
FILE *wf;
char a[N]= "ABCDEFGHIJK";
int m;
printf("The origina string :\n");
puts(a);
printf("\n\nEnter m: ");
scanf("%d",&m);
fun(a,m);
printf("\nThe string after moving :\n");
puts(a);
printf("\n\n");
wf=fopen("out.dat","w");
fun(b,3);
fprintf(wf,"%s",b);
fclose(wf);
}
41.请编写函数fun,该函数的功能是:将M行N列的二维数组中的字符数据,按列的顺序依次放到一个字符串中。
例如,若二维数组中的数据为:W W W W 则字符串中的内容应是WSHWSHWSH。
S S S S
H H H H
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include<stdio.h>
#define M 3
#define N 4
void fun(char (*s)[N],char *b )
{
int i,j,k=0;
for(i=0;i<N;i )
for(j=0;j<M;j )
b[k ]= s[j][i] ;
b[k]='\0';
}
main()
{
FILE *wf;
char a[100],w[M][N]={{ 'W', 'W', 'W', 'W'},{'S', 'S', 'S', 'S'},{'H', 'H', 'H', 'H'}};
int i,j;
printf("The matrix:\n");
for(i=0;i<M;i )
{ for(j=0;j<N;j )
printf("%3c",w[i][j]);
printf("\n");
}
fun(w,a);
printf("The A string:\n");
puts(a);
printf("\n\n");
wf=fopen("out.dat","w");
fprintf(wf,"%s",a);
fclose(wf);
}
42.下列程序定义了NXN的二维数组,并在主函数中自动赋值。请编写函数fun(int a[][N],int n),该函数的功能是:使数组右上半三角元素中的值乘以m。
例如:若m的值为2,a数组中的值为,则返回主程序后a数组中的值应为。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define N 5
/*该题的关键也是在如何表示右上半角,本题采用的是在一行内从最后一列的元素开始往前乘,只有j>=i时才改变。*/
int fun( int a[][N], int m)
{ int i,j;
for(i=0; i<N; i )
for(j=N-1; j>=i; j--)
a[i][j]*=m;
}
main()
{ int a[N][N],m,i,j;
printf("**** the array ****\n");
for( i=0; i<N;i )
{ for( j=0; j<N; j )
{a[i][j]=rand()%20; printf("%4d", a[i][j]); }
printf("\n");
}
do m=rand()%10; while( m>=3);
printf("m=%4d\n", m);
fun( a,m);
printf(" the result \n");
for(i=0; i<N; i )
{for(j=0; j<N; j ) printf("%4d", a[i][j]);
printf("\n");
}
}
43.编写一个函数,从传入的num个字符串中找出最长的一个字符串,并通过形参指针max传回该串地址(用****作为结束输入的标志)。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include "stdio.h"
#include "string.h"
#include "conio.h"
/* 函数strlen()用于求出字符串的长度,这个题中主要是*max=p;不能换成max=&p;如果用maz=&p;则只改变了max的指向,它不能传回给实参。因此我们要改变max指向地址中的内容,这才能使得实参ps有正确的值。*/
fun(char (*a)[81], int num,char **max)
{ char *p=a[0];int i;
for(i=1;i<num;i )
if(strlen(a[i])>strlen(p))
p=a[i];
*max=p;
}
main()
{ char ss[10][81],*ps;
int n,i=0;
clrscr();
printf("enter string:\n");
gets(ss[i]);
puts(ss[i]);
while(!strcmp(ss[i],"****")==0)
{
i ;
gets(ss[i]);
puts(ss[i]);
}
n=i;
fun(ss,n,&ps);
printf("\nmax=%s\n",ps);
}
44.编写一个函数,该函数可以统计一个长度为2的字符串在另一个字符串中出现的次数。例如,假定输入的字符串为:asd asasdfg asd as zx67 asdmklo,子字符串为as,则应输出6。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include "stdio.h"
#include "string.h"
#include "conio.h"
/*注:由于小串中只有2个字符所以可用str[i]==sbustr[0]&&str[i 1]==substr[1]来判断小串是否与长串当前位置(str[i])相同(即出现一次)。因而只要让长串当前位置逐一向后移即可(用for()循环来完成)。*/
int fun(char *str,char *substr)
{ int i,n=0,s=strlen(str);
for(i=0;i<s;i )
if((str[i]==substr[0])&&(str[i 1]==substr[1]))
n ;
return n;
}
main()
{
char str[81],substr[3];
int n; clrscr();
printf("enter 1:");
gets(str);
printf("enter 2:");
gets(substr);
puts (str);
puts(substr);
n=fun(str,substr);
printf("n=%d\n",n);
}