7、通过键盘输入3名学生4门课程的成绩,分别求每个学生
的平均成绩和每门课程的平均成绩。要求所有成绩均放入一
个4行5列的数组中,输入时间一人数据间用空格,不同人用
回车,其中最后一列和最后一行分别放每个学生的平均成绩
和每门课程的平均成绩及班级总平均成绩
**************************************************/
#include<stdio.h>
#include<stdlib.h>
void main()
{
float a[4][5],sum1,sum2;
int i,j;
printf("now enter 12 number:\n");
for(i=0;i<3;i++)
for(j=0;j<4;j++)
scanf("%f",&a[i][j]);
for(i=0;i<3;i++)
{
sum1=0;
for(j=0;j<4;j++)
sum1+=a[i][j];
a[i][4]=sum1/4;
}
for(j=0;j<5;j++)
{
sum2=0;
for(i=0;i<3;i++)
sum2+=a[i][j];
a[3][j]=sum2/3;
}
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
printf("%6.2f",a[i][j]);
printf("\n");
}
//getchar();
system("pause");
}
/**************************************************
8、将输入的字符串反序输出
**************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char c[200],c1;
int i,j,k;
printf("enter a string:\n");
scanf("%s",c);
k=strlen(c);
for(i=0,j=k-1;i<k/2;i++,j--)
{
c1=c[i];
c[i]=c[j];
c[j]=c1;
}
printf("%s\n",c);
system("pause");
return 0;
}
/**************************************************
9、从字符数组s中删除存放在c中的字符
**************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char s[80],c;
int i,j,k=0;
printf("enter a string:\n");
gets(s);
printf("enter a char:\n");
c=getchar();
j=strlen(s);
printf("j=%d\n",j);
for(i=0;i<j;i++)
if(c!=s[i])
s[k++]=s[i];
s[j-1]='\0';
printf("%s\n",s);
system("pause");
}
/*************************************************************
10、编写一个void sort(int *x,int n)实现将x数组中的n个
数据从大到小排序。n及数组元素在主函数中输入。将结果显
示在屏幕上并输出到文件test10.out中
*************************************************************/
#include<stdio.h>
#include<stdlib.h>
void sort(int *x,int n)
{
int i,j,k,t;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(x[j]>x[i])
{
t=x[i];
x[i]=x[j];
x[j]=t;
}
}
}
}
void main()
{
FILE *fp;
int *p,i,a[10];
if((fp=fopen("test10.out","w+"))==NULL)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>printf("cannot open the file\n");
<span style="white-space:pre"> </span>exit(0);
<span style="white-space:pre"> </span>}
printf("Input 10 numbers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
p=a;
sort(p,10);
for(i=0;i<10;i++)
{
printf("%d ",*p);
fprintf(fp,"%d ",*p);
p++;
}
system("pause");
fclose(fp);
}
/**************************************************
11、已知数组a中的元素已按由小到大顺序排列,以下程序
的功能是将输入的一个数插入数组a中,插入后,数组a中
的元素仍然由小到大顺序排列
**************************************************/
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a[10]={12,17,20,25,28,30};
int x , i, j;
printf("Enter a number: \n");
scanf("%d",&x);
j=6; //数组中不为0的元素个数
i=j-1;
while(a[i]>x) //从最后一个单元开始
{
a[i+1]=a[i]; //将比x大的数往后移动一个位置
i--;
}
a[++i]=x;
j++; //插入x后数组不为0的元素总个数增加
for(i=0;i<j;i++)
printf("%6d",a[i]);
printf("\n");
system("pause");
}
/**************************************************
12、编写函数replace_s(char *s,char c1,char c2)实现
将s所指向的字符串中所有字符c1用c2替换,字符串、
字符c1和c2均在主函数中输入,将原始字符串和替换
后的字符串显示在屏幕上,并输出到文件test12.out中
**************************************************/
#include<stdio.h>
#include<stdlib.h>
void replace_s(char *s,char c1,char c2)
{
while(*s!='\0')
{
if (*s==c1)
*s=c2;
s++;
}
}
void main()
{
FILE *fp;
char str[100],a,b;
if((fp=fopen("test12.out","w+"))==NULL)
{
printf("cannot open the file\n");
exit(0);
}
printf("Enter a string:\n");
gets(str);
printf("Enter a&&b:\n");
scanf("%c%c",&a,&b);//连着输入两个字符即可
printf("The old string is: %s\n",str);
fprintf(fp,"The old string is: %s\n",str);
replace_s(str,a,b);
printf("The new string is:%s\n",str);
fprintf(fp,"The new string is: %s\n",str);
fclose(fp);
system("pause");
}