45.假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:只删除字符串前导和尾部的*号,串中字母之间的*号都不删除。形参n给出了字符串的长度,形参h给出了字符串中前导*号的个数,形参e给出了字符串中最后*号的个数。在编写函数时,不得使用C语言提供的字符串函数。
例如,若字符串中的内容为****A*BC*DEF*G*******,删除后,字符串中的内容则应当是A*BC*DEF*G。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#include <conio.h>
void fun (char *a,int n ,int h , int e)
{
int i,j=0;
for(i=h;i< n-e ;i )
a[j ]=a[i];
a[j]= ‘\0’ ;
}
main()
{
FILE *wf;
char s[81],*t,*f;
char *p="****A*BC*DEF*G*******";
int m=0,tn=0, fn=0;
printf("Enter a string :\n");
gets(s);
t=f=s;
while(*t)
{t ;m ;}
t-- ;
while(*t=='*')
{t--;tn ;}
while(*f=='*')
{f ;fn ;}
fun( s, m, fn, tn);
printf("The string after deleted:\n");
puts(s);
wf=fopen("out.dat","w");
fun(p,21,4,7);
fprintf(wf,"%s",p);
fclose(wf);
}
46.学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:按分数的高低排列学生的记录,高分在前。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <stdio.h>
#define N 16
typedef struct
{ char num[10];
int s ;
}STRUC;
void fun (STRUC a[])
{
int i,j;
STRUC t;
for(i=1 ;i<N;i )
for(j=0;j< N-1;j )
if(a[j].s < a[j 1].s)
{t=a[j];a[j]=a[j 1];a[j 1]=t;}
}
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",66},{"GA017",64},{"GA018",64},{"GA016",72}};
int i;
FILE *out;
fun(s);
printf("The data after sorted :\n");
for (i=0; i<N; i )
{if((i)%4==0)
printf("\n");
printf("%s %4d",s[i].num,s[i].s);
}
printf("\n");
out=fopen("out65.dat", "w");
for(i=0; i<N; i )
{if((i)%4==0&&i)
fprintf(out, "\n");
fprintf(out, "%4d",s[i].s);
}
fprintf(out, "\n");
fclose(out);
wf=fopen("out.dat","w");
for (i=0; i<N; i )
{if((i)%4==0&&i)
fprintf(wf,"\n");
fprintf(wf,"%s %4d",s[i].num,s[i].s);
}
fclose(wf);
}
47.请编写—个函数void fun(char *ss),其功能是:将字符串ss中所有下标为奇数位置上的字母转换为大写(若该位置上不是字母,则不转换)。
例如,若输入abc4EFg,则应输出aBc4EFg。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include<conio.h>
#include<stdio.h>
void fun ( char *ss)
{ int i;
for(i=0;ss[i]!='\0';i )
{
if(i%2 ==1&&ss[i]>='a' && ss[i]<='z')
ss[i]= ss[i] -32;
}
}
main()
{
FILE *wf;
char tt[51],*s="abc4Efg";
printf("Please enter an character string within 50 characters:\n");
gets(tt);
printf("\n\nAfter changing,the string\n %s",tt);
fun(tt);
printf("\nbecomes\n %s",tt);
wf=fopen("out.dat","w");
fun(s);
fprintf(wf,"%s",s);
fclose(wf);
}
48.请编写函数fun,其功能是:将两个两位数的正整数a、b合并形成一个整数放在c中。合并的方式是:将a数的十位和个位数依次放在c数的千位和十位上,b数的十位和个位数依次放在c数的百位和个位上。
例如,当a=45,b=12,调用该函数后c=4152。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <conio.h>
#include <stdio.h>
/*本题的关键在于如何表示出个、十、百、千位数。对于一个两位的整数,用10对它求余得到个位数上的数,将它除10得到十位数上的数。*/
void fun ( int a, int b , long *c)
{ *c=(a/10)*1000 (b/10)*100 (a%10)*10 b%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);
}
49.请编写函数fun,其功能是:将s所指字符串中下标为偶数同时ASCII值为奇数的字符删除,s中剩余的字符形成的新串放在t所指的数组中。
例如,若s所指字符串中的内容为ABCDEFGl2345,其中字符C的ASCII码值为奇数,在数组中的下标为偶数,因此必须删除;而字符1的ASCII码值为奇数,在数组中的下标也为奇数,因此不应当删除,其他依此类推。最后t所指的数组中的内容应是BDFl2345。
♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣♣
#include <conio.h>
#include <stdio.h>
#include <string.h>
void fun ( char *s, char t[ ])
{ int i, j=0, n=strlen(s);
for(i=0; i<n; i )
if (i%2==0&&s[i]%2!=0)
;
else
{ 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);
}