30.请编写一个函数fun,它的功能是:求出—个2×M整型二维数组中最大元素的值,并将此值返回调用函数。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#define M 4
#include <stdio.h>
fun (int a[][M] )
{
int i,j,max= a[0][0] ;
for(i=0;i<2;i )
for(j=0;j<M;j )
if(max < a[i][j])
max=a[i][j];
return max;
}
main()
{
FILE *wf;
int arr[2][M]={5,8,3,45,76,-4,12,82};
printf("max=%d\n",fun(arr));
wf=fopen("out.dat","w");
fprintf (wf,"%d",fun(arr));
fclose(wf);
}
31.请编写函数fun,其功能是:将s所指字符串中除了下标为偶数、同时ASCII值也为偶数的字符外,其余的全都删除;串中剩余字符所形成的一个新串放在t所指的数组中。
例如,若s所指字符串中的内容为ABCDEFGl23456,其中字符A的ASCII码值为奇数,因此应当删除;其中字符B的ASCII码值为偶数,但在数组中的下标为奇数,因此也应当删除;而字符2的ASCII码值为偶数,所在数组中的下标也为偶数,因此不应当删除,其他依此类推。最后t所指的数组中的内容应是246。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <conio.h>
#include <stdio.h>
#include<string.h>
void fun(char*s, char t[])
{
int i,j=0;
for(i=0;s[i]!= '\0' ;i )
if(i%2==0 && s[i]%2==0)
t[j ]=s[i];
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("ABCDEFG123456",t);
fprintf(wf,"%s",t);
fclose(wf);
}
32.请编写函数fun,其功能是:将s所指字符串中除了下标为奇数、同时ASCII值也为奇数的字符之外,其余的所有字符都删除,串中剩余字符所形成的一个新串放在t所指的数组中。
例如,若s所指字符串中的内容为ABCDEFG12345,其中字符A的ASCII码值虽为奇数,但所在元素的下标为偶数,因此必需删除;而字符1的ASCII码值为奇数,所在数组中的下标也为奇数,因此不应当删除,其他依此类推。最后t所指的数组中的内容应是135。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <conio.h>
#include <stdio.h>
#include <string.h>
void fun (char *s, char t[ ])
{ int i, j=0, n;
n=strlen(s);
for( i=0; i<n; i )
if(i%2!=0&&s[i]%2!=0)
{ t[j]=s[i];
j ;}
t[j]= '\0' ;
}
main()
{ char s[100], t[100];
printf("\nplease enter string S:");
scanf("%s",s);
fun(s,t);
printf("\nthe result is :%s\n", t);
}
33.假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:使字符串中尾部的*号不得多于n个;若多于n个,则删除多余的*号;若少于或等于n个,则什么也不做,字符串中间和前面的*号不删除。
例如,字符串中的内容为****A*BC*DEF*C*******,若n的值为4,删除后,字符串中的内容则应当是****A*BC*DEF*G****,若n的值7,则字符串中的内容仍为****A*BC*DEF*C*******。n的值在主函数中输入。在编写函数时,不得使用C语言提供的字符串函数。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#include <conio.h>
void fun ( char *a , int n )
{ int i=0, k=0; char *p, *t;
p=t=a;
while ( *t) t ;
t--;
while (*t==’*’) {k ; t-- ;}
t ;
if (k>n)
{ while(*p&&p<t n)
{ a[i] = *p;
i ;
p ; }
a[i] = '\0';}
}
main()
{
FILE *wf;
char s[81],*t="****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(t);
fprintf(wf,"%s",t);
fclose(wf);
}
34.学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:把分数最高的学生数据放在h所指的数组中,注意:分数最高的学生可能不只一个,函数返回分数最高的学生的人数。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#define N 16
typedef struct
{ char num[10];
int s ;
}STRUC;
int fun (STRUC *a, STRUC *b)
{
int i,j=0,max=a[0].s;
for(i=0;i<N;i )
if(max<a[i].s) max=a[i].s;
for(i=0;i<N;i )
if(max==a[i].s) b[j ]=a[i];
return j;
}
main ()
{
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",66},{"GA017",64},{"GA018",64},{"GA016",72}};
STRUC h[N];
int i, n;
FILE *out;
n=fun(s,h);
printf("The %d highest score :\n",n);
for (i=0; i<n; i )
printf("%s %4d\n ",h[i].num,h[i].s);
printf("\n");
out=fopen("out45.dat", "w");
fprintf(out, "%d\n",n);
for(i=0; i<n; i )
fprintf(out, "%4d\n ",h[i].s);
fclose(out);
}
35.请编写一个函数,用来删除字符串中的所有空格。
例如,输入asd af aa z67,则输出为asdafaaz67。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
int fun(char *str)
{char *p=str;
for(; *str; str )
if(*str!=' ') *p =*str;
*p='\0';
}
main()
{
char str[81];
int n;
clrscr()
printf("Input a string:")
gets(str);
puts(str);
fun(str);
printf("*** str: %s\n",str);
NONO();
}
NONO()
{
/* 请在此函数内打开文件,输入调试数据,调用 fun 函数,输出数据,关闭文件。 */
char str[81];
int n = 0;
FILE *rf, *wf
rf = fopen("b0803.in", "r")
wf = fopen("b0803.out", "w")
while(n < 10) {
fgets(str, 80, rf);
fun(str);
fprintf(wf, "%s", str)
n
}
fclose(rf)
fclose(wf)
}
36.假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:将字符串中的前导*号全部移到字符串的尾部。
例如,若字符串中的内容为*******A*BC*DEF*G****,移动后,字符串中的内容应当是A*BC*DEF*G**********。在编写函数时,不得使用C语言提供的字符串函数。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#include <conio.h>
void fun ( char *a)
{ int i=0,n=0 ; char *p ;
p=a;
while (*p=='*')
{n ; p ; }
while (*p)
{ a[i]= *p ;
i ;
p ; }
while(n!=0)
{ a[i]= '*' ;
i ;
n-- ; }
a[i]= '\0' ;
}
main()
{
FILE *wf;
char s[81],*t="****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(t);
fprintf(wf,"%s",t);
fclose(wf);
}