练习一:
/*请编写程序,其功能是:将s所指字符串中除了下标为偶数,同时ASCII值也为偶数的字符外,其余的全部删除,串中剩余的字符所形成的一个新串 放在t所指的数组中?
例如,若s所指字符串中的字符是ABCDEFG123456,则删除后剩余的部分应是246*/
#include "stdio.h"
#include "conio.h"
#include "stdlib.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';
}
void main()
{
char s[100],t[100];
system("CLS");
printf("/nPlease enter string S: ") ;
scanf("%s",s);
fun(s,t);
printf("The result is:%s/n",t);
getch();
}
练习2:
/*请编写程序,其功能是:删除一个字符串中指定下标的字符。其中,a指向原字符串,删除后的字符串存在b所指数组中,n存放指定的下标。例如:输入一个 字符串world,然后输入3,则运行后结果为word*/
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "string.h"
#define LEN 20
void fun(char a[],char b[],int n)
{
int i,j=0;
for(i=0;a[i]!='/0';i++)
{
if(i==n)
{
i++ ;
}
b[j++]=a[i];
}
b[j]='/0';
}
void main()
{
char str1[LEN],str2[LEN];
int n;
system("CLS");
printf("Enter the string:/n");
gets(str1);
printf("Enter the postion of the string deleted:") ;
scanf("%d",&n);
fun(str1,str2,n);
printf("The new string is:%s ",str2);
getch();
}
练习3
/*学生的记录有学号和成绩组成,N名学生的数据全在主函数中放入结构体数组s中,请编写函数fun,该函数的作用是:把高于等于平均分的学生数 据放入b所指数组中,高于等于平均分的学生人数通过形参n传回,,平均分通过函数值返回*/
#include "stdio.h"
#define N 12
typedef struct
{
char num[10];
double s;
}STREC;
double fun(STREC *a,STREC *b,int *n)
{
int i,j=0;
double ave;
double sum=0.0;
for(i=0;i<N;i++)
{
sum+=a[i].s ;
}
ave=sum/N;
for(i=0;i<N;i++)
{
if(a[i].s>=ave)
{
b[j++].s=a[i].s;
}
*n=j;
}
return ave;
}
void main()
{
STREC s[N]={{"05",58},{"03",76},{"02",69},{"04",85},{"01",91},{"07",72},{"08",64},{"06",87},{"09",60},{"11",79},{"12",73},{"10",90}};
STREC h[N];
int i,n;
double ave;
ave=fun(s,h,&n);
printf("The %d student data which is higer than %7.2f:/n",n,ave);
for(i=0;i<n;i++)
printf("%s %4.1f/n",h[i].num,h[i].s);
printf("/n");
getch();
}
练习4
/*请编写函数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;
for(i=0;i<=10;i++)
{b[i]=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);
}
void main()
{
int age[N],i,d[M];
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]); /*每行输出10个*/
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]);
getch();
}