《高级语言程序设计》
学
生
实
验
报
告
专业
学号姓名
实验一 C程序的运行环境和使用方法
1. 实验目的
1) 了解所用的计算机系统的基本操作方法,学会独立使用该系统。
2) 了解在该系统上如何编辑、编译、连接和运行一个C程序。
3) 通过运行简单的C程序,初步了解C程序的特点。
2. 实验程序清单及运行结果
课后习题1.5
编写一个C程序,输出以下信息:
**************************
Very Good!
**************************
Source
#include<stdio.h>
int main()
{
printf("**************************\n");
printf(" Very Good!\n");
printf("**************************\n");
}
课后习题1.6
编写一个程序,输入a、b、c三个值,输出其中最大值。 Source
#include<stdio.h>
int main
{ int max,a,b,c;
scanf(“%d %d %d”,&a,&b,&c);
max=a>=b?a:b;
printf(“%d”,max>=c?max:c);
}
1
实验二 顺序结构程序设计
1. 实验目的
1) 掌握C语言的数据类型,熟悉如何定义整型、字符型和实型的变量,以及对它们赋值的方法。
2) 掌握不同类型的数据之间赋值的规律。
3) 学会用C的有关算术运算符,以及包含这些运算符的表达式。
4) 掌握C语言中使用最多的一种语句——赋值语句的使用方法。
5) 掌握scanf()函数、printf()函数的调用方法,掌握各种类型数据的输入输出的方法,能正确使用各种格式转换符。
6) 掌握字符输入输出函数getchar()、putchar()函数的调用方法。
2. 运行程序及填空
(一)输入并运行下面的程序,写出运行结果。
#include <stdio.h>
int main( )
{
char c1 , c2;
c1=97; c2=98;
printf(“%c,%c\n”, c1, c2 );
printf(“%d,%d\n”, c1, c2 );
return 0;
}
运行结果:a,b
97,98
【思考题】若将第四行改为:int c1, c2; 再使之运行,输出结果一样吗?为什么? 答:一样,因为第六行的printf输出的是%c即字符,任然将c1,c2按照ASCII码字以符形式输出。
(二)程序填空:输入一个十进制整数,输出与之相应的八进制数与十六进制数。 2
例如:输入31,输出37(八进制)和1F(十六进制)。源程序如下: #include <stdio.h>
int main()
{
long n;
printf("输入一个十进制整数:");
scanf("%d",&n); printf("对应的八进制整数是 %\n", n); printf("对应的十六进制整数是 %\n", n);
return 0;
}
运行程序,输入,显示结果如何?
答:输入一个十进制整数:1234567
对应的八进制整数是 4553207
对应的十六进制整数是 12D687
3. 实验程序清单及运行结果
课后习题3.7
要将"China"译成密码。
Source
#include<stdio.h>
int main()
{
char c1='C',c2='h',c3='i',c4='n',c5='a';
printf("%c%c%c%c%c\n",c1+4,c2+4,c3+4,c4+4,c5+4);
}
课后习题4.8
求圆周长C1、圆面积Sa、圆球表面积Sb、圆球体积Va、圆柱体积Vb。 3
Source
#include <stdio.h>
#define PI 3.14
#include<math.h>
int main()
{
float r,h,C1,Sa,Sb,Va,Vb;
scanf("%f%f",&r,&h);
C1=2*PI*r;
Sa=PI*r*r;
Sb=4*PI*r*r;
Va=4.0/3*r*r*r*PI;
Vb=PI*r*r*h;
printf("C1=%.2f\nSa=%.2f\nSb=%.2f\nVa=%.2f%\nVb=%.2f\n",C1,Sa,Sb,Va,Vb); }
课后习题4.9
输入一个华氏温度,要求输出摄氏温度。公式为c=5(F-32)/9。取位2小数。 Source
#include<stdio.h>
int main()
{
float F,c;
scanf("%f",&F);
printf("c=%.2f\n",c=5*(F-32)/9;);
return 0;
}
4
实验三 选择结构程序设计
1. 实验目的
1)了解C语言表示逻辑量的方法(以0代表“假”,以非0代表“真”)。
2)学会正确使用关系运算符、逻辑运算符和相关表达式。
3)熟练掌握if语句和switch语句。
4)结合程序掌握一些简单的的算法。
5)学习调试程序。
2. 程序填空
请将下列源程序填写完整。程序说明:输入实数x, 计算并输出下列分段函数f(x)的值,结果保留2位小数。
?x?1x??1?f(x)??ex?1?x?1
x?1#include <stdio.h>
int main ( )
{ double x,y;
printf("Input x:");
scanf(“%lf”, if (x<-1) y= x+1;
else if ( ) y= exp(x) ;
else ;
printf("f(%.2f)=%.2f\n", x, y);
return 0;
}
运行程序,输入 -2,显示结果是:-1
输入 0.5,显示结果是:1.65
5
输入 3,显示结果是:3.16
3. 实验程序清单及运行结果
课后习题5.5
有一个函数
/ x (x<1)
y={ 2x-1 (1<=x<10)
\ 3x-11 (x>=10)
写一段程序,输入x,输出y
Source
#include<stdio.h>
int main()
{
int x,y;
scanf("%d",&x);
if(x<10)
{if(x<1)y=x;
else y=2*x-1;}
else y=3*x-11;
printf("%d\n",y);
return 0;
}
课后习题5.6
给出一百分制成绩,要求输出成绩等级?A?、?B?、?C?、?D?、?E?。 Source
#include<stdio.h>
int main()
{
int x;
char A,B,C,D,E;
scanf("%d",&x);
if(x>=90&&x<=100) printf("%c\n",'A');
else if(x>=80&&x<=89) printf("%c\n",'B');
6
}
else if(x>=70&&x<=79) printf("%c\n",'C'); else if(x>=60&&x<=69) printf("%c\n",'D'); else if(x<=59&&x>=0) printf("%c\n",'E');
课后习题5.7
给出一个不多于5位的非负整数,要求
1、求出它是几位数
2、分别输出每一位数字
3、按逆序输出各位数字,例如原数为321,应输出123
Source
#include<stdio.h>
int main()
{
int x,a[6],i,z=0;
scanf("%d",&x);
for(i=10;x;)
{
a[++z]=x%i;
x=x/10;
}
if(z==0) printf("1\n0\n0\n");
else printf("%d\n",z);
for(i=z;i>=1;i--)
{
if(i!=1)printf("%d ",a[i]);
else printf("%d\n",a[i]);
}
for(i=1;i<=z;i++)
{
printf("%d",a[i]);
if(i==z) printf("\n");
}
}
7
课后习题5.8
企业发放的奖金根据利润提成。
Source
#include<stdio.h>
int main()
{
int I;
scanf("%d",&I);
if(I<=100000) printf("%d\n",I*1/10);
else
if(I<=200000&&I>100000) printf("%d\n",10000+(I-100000)*75/1000); else
if(I<=400000&&I>200000) printf("%d\n",17500+(I-200000)*5/100); else
if(I<=600000&&I>400000) printf("%d\n",27500+(I-400000)*3/100); else
if(I<=1000000&&I>600000) printf("%d\n",33500+(I-600000)*15/1000); else
if(I>1000000) printf("%d\n",39500+(I-1000000)*1/100); return 0;
}
8
实验四 循环结构程序设计
1. 实验目的
1) 熟悉掌握用while语句、do-while语句和for语句实现循环的方法。
2) 掌握循环次数不确定的解题技巧,掌握多项式求和问题的解题技巧。
3) 掌握多重循环的编程技巧。
4) 掌握在程序设计中用穷举法、迭代法、递推法求解问题的编程技巧。
5) 进一步学习调试程序。
2. 程序填空
输出菲波那契数列的前20项,要求每行输出10项。
#include <stdio.h>
int main()
{ int i,x1,x2,x;
x1=x2=1;
printf("%6d%6d",x1,x2); /*输出前2项*/
for(i=; i<=20; i++)
{ x=x1+x2; printf("%6d",x);
/*每行输出10项*/ if() printf("\n"); x1= ; x2=;
}
return 0;
}
3. 实验程序清单及运行结果
课后习题6.1
输入两个正整数m和n,求其最大公约数和最小公倍数。
9
Source
#include<stdio.h>
int main()
{
int a,b,t;
scanf("%d%d",&a,&b);
if(b>a){t=a;a=b;b=t;};
int k=a*b;
while(t){t=a%b;a=b;b=t;}
printf("%d %d",a,k/a);
}
课后习题6.2
输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。
Source
#include<stdio.h>
int main()
{
char x;
int a1=0,a2=0,a3=0,a4=0;
for(x=getchar();x!='\n';x=getchar())
{
if(x>='a'&&x<='z')a1=a1+1;
else
if(x>='A'&&x<='Z') a1=a1+1; else
if(x>='0'&&x<='9') a2=a2+1; else
if(x==' ') a3=a3+1;
else a4=a4+1;
}
printf("%d ",a1);
10
} printf("%d ",a2); printf("%d ",a3); printf("%d\n",a4); return 0;
课后习题6.3
Sn=2+22+222+2222+22222+22...2222(最后一项n个2)
Source
#include<stdio.h>
int main()
{
int n,a=2,i,t=0,sum=0;
scanf("%d",&n);
while(n--)
{
t+=a;sum+=t;a*=10;
}
printf("%d",sum);
return 0;
}
课后习题6.10
猴子吃桃问题。求第一天共摘多少桃子。
Source
#include<stdio.h>
int main()
{
int s=1,N,i;
scanf("%d",&N);
for(i=1;i<N;i++)
{
s=(s+1)*2;
}
printf("%d\n",s);
}
11
实验五 数组
1. 实验目的
1) 掌握一维数组和二维数组的定义、赋值和输入输出的方法。
2) 掌握字符数组和字符串函数的使用。
3) 掌握与数组有关的算法(特别是排序算法)。
2. 程序填空
(一)程序填空:输入一个整数n (1≤n≤10),再输入n个整数,将它们按升序排列后
输出。
选择排序基本思想:
(1)由于n最大是10,需要定义一个长度为10的整型数组a;
(2)整个排序只需要确定前n-1个元素(a[0] ~a[n-2]),最后一个元素a[n-1]无需
另外处理;
(3)在确定a[i](i=0,1,…,n-2)时,先将a[i]本身看成最小,即令k=i,并将a[k]与
后面的元素a[j](j=i+1, i+2, …, n-1)一一比较,如果a[j]< a[k],则更新k的值:k =j。 找出对应于下标i的最小元素a[k]后,交换a[i]与a[k]。
#include <stdio.h>
int main()
{
int i, j, k,n, temp, a[10];
printf("Input n (1~10):");
while ( scanf("%d",&n),); //输入正确的n:1≤n≤10 printf("Input %d numbers:\n", n );
for(i=0; i<n; i++)
scanf("%d",&a[i]); //输入n个整型元素
//选择法排序
for(i=0; i<; i++)
{ 12
k= ; //设a[i]为剩余部分的最小元素
for(j=; j<; j++) if( ) k=j; //Line 12 a[k]=temp; //a[i]??a[k] temp=a[i]; a[i]=a[k];
}
for(i=0; i<n; i++)
printf("%d ",a[i]); //输出排序后的数组
printf("\n");
return 0;
}
【思考题】运行程序,输入n:5个整数:,显示结
果是什么?
如果是按从大到小的降序排列,语句行Line 12 应怎么修改?
答; ⑴显示结果Input n (1~10):5 ⑵Line 12 应改为if( a[j]>a[k] )
k=j;
Input 5 numbers:
23 -9 14 0 3
-9 0 3 14 23
(修改上面的程序,从在线评测系统中提交调试好的程序,题号1032。)
(二)程序填空:输入5个整数, 将这5个数按逆时针顺序转动一次后再输出,如输入
1 2 3 4 5,逆时针转动一次后,输出2 3 4 5 1。
提示:设存放整数的数组为a。要实现数组逆时针转动,只要先将首元素a[0]“搬”至一个变量t中;然后从第1个元素到最后1个元素,依次向前移一位,即a[i-1]= a[i] ( i= 1, … , 4);最后将存放在t中的首元素放入最后的元素。
#include<stdio.h>
int main()
{
int i, t, a[5];
13
printf("Input 5 integers:\n");
for(i=0; i<5; i++)
scanf("%d", );
t= ;
for(i=; i<; i++)
a[i-1]=a[i];
= t;
printf("After rotation:\n");
for(i=0;i<5;i++)
printf("%5d",);
printf("\n");
return 0;
}
3. 实验程序清单及运行结果 课后习题7.1
用筛法求之N内的素数。
Source
#include<stdio.h>
#include<math.h>
int main()
{
int N,n,i,k;
scanf("%d",&N);
for(n=2;n<=N;n++)
{
k=sqrt(n);
for(i=2;i<=k;i++)
if(n%i==0) break; if(i>=k+1){ printf("%d\n",n); } }
}
14
课后习题7.2
用选择法对10个整数从小到大排序。
Source
#include<stdio.h>
int main()
{
int a[10],i,j,min,t,k=0;
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<9;i++)
{
min=a[i];k=i;
for(j=i+1;j<10;j++)
{
if(min>a[j]) min=a[j],k=j;
}
if(k!=i) {t=a[k];a[k]=a[i];a[i]=t;}
}
for(i=0;i<10;i++)
{
printf("%d\n",a[i]);
}
}
课后习题7.4
有一个已排好的9个元素的数组,输入一个数要求按原来排序的规律将它插入数组中。
Source
#include<stdio.h>
int main()
{
int a[11],i,t,k;
for(i=0;i<10;i++)
{
15
scanf("%d",&a[i]); } for(i=8,k=9;i>=0;i--) { if(a[k]<a[i]) t=a[i],a[i]=a[k],a[k]=t,k=i; }
for(i=0;i<10;i++)
{
printf("%d\n",a[i]);
}
}
课后习题7.5
输入10个数字,然后逆序输出。
Source
#include<stdio.h>
int main()
{
int a[10],i;
for(i=0;i<=9;i++)
scanf("%d",&a[i]);
for(i=9;i>=0;i--)
printf("%d ",a[i]);
return 0;
}
16
实验六 函数
1. 实验目的
1) 掌握定义函数的方法。
2) 掌握函数实参与形参的对应关系以及“值传递”的方式。
3) 掌握函数的嵌套调用和递归调用的方法。
4) 掌握全局变量和局部变量、动态变量和静态变量的概念和使用方法。
5) 学习对多文件程序的编译和运行。
2. 程序填空
验证哥德巴赫猜想——任何一个大于6的偶数均可表示为两个素数之和。例如:6=3+3, 8=3+5,10=5+5,…,18=7+11。要求将6-100之间的偶数均表示为素数之和,一行输出5组。
#include<stdio.h>
#include<math.h> int main()
{ int a,b,k ;
for( ) //逐个处理6~100之间的偶数k {
for() //针对偶数k, 寻找第一个素数加数项a { b=k-a; if( ) break; //如果a,b同为素数, 跳出内层循环 } //内层循环 printf("%d=%d+%d ",k,a,b); //输出k=a+b if(k%5==0) printf("\n");
//每行输出5组,注意第一行只有3组 } //外层循环
return 0;
}
//自定义函数prime(n),判断整数n是否是素数,是则返回1,不是则返回0 17
int prime(int n)
{ int i ;
for( i=2; ; i++)
if ( n%i==0 ) ;
if () return 1; //n是素数,返回1
else return 0; // n不是素数,返回0
}
3. 实验程序清单及运行结果
课后习题8.1
写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数
Source
#include<stdio.h>
int main()
{
int gcd(int x,int y);
int lcm(int x,int y);
int a,b;
scanf("%d %d",&a,&b);
printf("%d %d\n",gcd(a,b),lcm(a,b));
}
int gcd(int x,int y)
{
int t;
if(y>x) {t=x;x=y;y=t;}
while(t)
{
t=x%y;
x=y;
y=t;
}
return x;
}
int lcm(int x,int y)
{
18
}
return x*y/gcd(x,y);
课后习题8.3
写一个判断素数的函数,在主函数输入一个正整数,输出是否是素数的消息。
Source
#include<stdio.h>
int main()
{
int a;
int prime(int x);
scanf("%d",&a);
if(prime(a)) printf("prime");
else printf("not prime");
}
int prime(int x)
{
int i=2;
if(x==1) return 0;
while(x%i){i++;}
if(i==x) return 1;
else return 0;
}
课后习题8.5
写一函数,使输入的一个字符串按反序存放,在主函数中输入输出反序后的字符串。
Source
#include<stdio.h>
#include<string.h>
char s[1000];
int main()
{
int len;
19
void transpose(char a[],int n);
gets(s);
len=strlen(s);
transpose(s,len);
puts(s);
}
void transpose (char a[],int n)
{
int i,j=n/2;
char t;
for(i=0;i<j;i++)
{
t=a[i];
a[i]=a[n-1-i];
a[n-1-i]=t;
}
}
课后习题8.7
写一个函数,输入一个四位数,要求输出这四个数字字符,但要求每两个数字间空格。 Source
#include<stdio.h>
int main()
{
int a,max(int x);
}
int max(int x)
{
if(x/10!=0) max(x/10);
printf("%d ",x%10);
} scanf("%d",&a); max(a);
20
实验七 指针
1. 实验目的
1) 通过实验进一步掌握指针的概念、会定义和使用指针变量。
2) 能正确使用数组的指针和指向数组的指针变量。
3) 能正确使用字符串的指针和指向字符串的指针变量。
4) 能正确使用指向函数的指针变量。
5) 了解指向指针的指针的概念及其使用方法。
2. 程序填空
(一)程序填空:输入三个整数,要求设计2个指针变量p1、p2 ,使p1指向这三个数
的最大值,p2指向最小者 ,并输出最大值和最小值。
#include <stdio.h>
int main ( )
{ int a,b,c, *p1, *p2, *p3;
scanf(“%d%d%d”,&a,&b,&c);
p1=; //p1指向a,b中较大者 if() p1=&c;
p2=; //p2指向a,b中较小者 if() p2=&c;
printf(“max=%d, min=%d\n”, *p1,*p2 );
return 0;
}
运行结果: 输入 1 2 3
(二)程序填空:利用指针,将数组a中的10个整数按相反顺序存放。源程序如下: #include <stdio.h>
int main ( )
{ int i,t, a[10],*p,*q;
输出21
printf("Input 10 integers:");
for(i=0; i<10; i++) scanf("%d", &a[i] );
; //p指向首元素,q指向最后一个元素
for( ; ; , //逐对交换对称元素 { t= *p; *p= *q; *q=t; }
for(i=0; i<10; i++) printf("%d ", a[i] );
printf("\n");
return 0;
}
3. 实验程序清单及运行结果
以下程序都要求用指针处理。
课后习题10.1
输入三个整数,按由小到大的顺序输出。
Source
#include<stdio.h>
int main()
{
void change(int *q1,int *q2,int *q3);
int a,b,c,*p1,*p2,*p3;
scanf("%d %d %d",&a,&b,&c);
p1=&a;p2=&b;p3=&c;
change(p1,p2,p3);
printf("%d %d %d",a,b,c);
}
void change(int *q1,int *q2,int *q3)
{
void swap(int *pt1,int *pt2);
if(*q1>*q2) swap(q1,q2);
if(*q1>*q3) swap(q1,q3);
if(*q2>*q3) swap(q2,q3);
22
}
void swap(int *pt1,int *pt2)
{
int t=*pt1;
*pt1=*pt2;
*pt2=t;
}
课后习题10.2
输入三个字符串,按由小到大的顺序输出
Source
#include<stdio.h>
#include<string.h>
void swap(char **p1,char **p2)
{ char *temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
int main()
{ char str[20],str1[20],str2[20];
char *ipoint1,*ipoint2,*ipoint3;
char **p1, **p2, **p3;
gets(str) ;
gets(str1);
gets(str2);
ipoint1=str; ipoint2=str1; ipoint3=str2; p1=&ipoint1; p2=&ipoint2; p3=&ipoint3;
if(strcmp(*p1,*p2)>0)
swap(p1,p2);
if(strcmp(*p1,*p3)>0)
swap(p1,p3);
if(strcmp(*p2,*p3)>0)
swap(p2,p3);
printf("%s\n%s\n%s\n",*p1,*p2,*p3);
}
23
课后习题10.5
有n人围成一圈,顺序排号。从第1个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来的第几号的那位。
Source
#include<stdio.h>
void check (int nub[],int n)
{
int *pt,i,j,k;
for(k=0,i=1;n-k>1;)
{for(pt=nub;pt<(nub+n);pt++,i++)
{if(*pt==0)
{i--;}
else if(i%3==0)
{*pt=0;
k++;
}
}
}
}
main()
{ int a[100],*p,n,i;
scanf("%d",&n);
for(p=a,i=1;p<a+n;p++)
*p=i++;
p=a;
check(p,n);
for(p=a;p<a+n;p++)
{ if(*p==0) continue;
printf("%d\n",*p);
}
getchar();
}
24
课后习题10.7
有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串。
Source
#include<stdio.h>
int main()
{
int n,m;
char a[100],*p;
scanf("%d",&n);
scanf("%s",a);
scanf("%d",&m);
printf("%s",a+m-1);
}
25
实验八 结构体与共用体
1. 实验目的
1) 掌握结构体类型变量的定义和使用。
2) 掌握结构体类型数组及指针的定义和使用。
3) 掌握链表的概念,初步学会对链表进行操作。
2. 程序填空
定义一个关于平面点的结构体类型,并定义一个含5个元素的结构体数组,用于存放5个平面点;然后输入这些点的坐标值,并统计位于半径为3、以原点为圆心的圆之内的点的个数。
#include <stdio.h>
struct point //定义结构体类型 struct point
{
;
};
int main ()
{
int i, num=0;
p[5]; //定义结构体数组p
printf("Input 5 points: \n"); for( i=0; i< 5; i++)
scanf("%lf%lf", , ); //输入5个点的坐标
for( i=0; i< 5; i++) //统计位于指定圆内的点的个数 if(< 9)
num++;
printf(" num= %d \n", num);
return 0;
} 26
运行程序,输入 1.0 2.1 -3 4.23 9.0 -8 4.5 -1 -0.25 -1.3
输出num= 2
3. 实验程序清单及运行结果
课后习题11.1
定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
Source
#include <stdio.h>
struct date
{
int year;
int month;
int day;
};
int leap_year(int a);
int cal_day(struct date a);
int leap_year(int a)
{
if(a%400==0||(a%4==0&&a%100!=0))
return 1;
else
return 0;
}
int cal_day(struct date a)
{
int sum=0,b[]={31,28,31,30,31,30,31,31,30,31,30,31} ;
for(int i=0;i<a.month-1;i++)
{sum+=b[i];}
if(a.month>2)
sum=sum+a.day+leap_year(a.year);
else
sum=sum+a.day;
return sum;
}
int main()
{
struct date a;
int n;
27
scanf("%d%d%d",&a.year,&a.month,&a.day);
n=cal_day(a);
printf("%d\n",n);
}
课后习题11.3
现有有N个学生的数据记录,每个记录包括学号、姓名、三科成绩。
编写一个函数input,用来输入一个学生的数据记录。
编写一个函数print,打印一个学生的数据记录。
在主函数调用这两个函数,读取N条记录输入,再按要求输出
Source
#include<stdio.h>
int N;
struct Student
{
char hao[50];
char name[50];
double score[3];
};
int main()
{
scanf("%d",&N);
void input(struct Student stu[]);
void print(struct Student stu[]);
struct Student stu[100],*p=stu;
input(p);
print(p);
return 0;
}
void input(struct Student stu[])
{
int i;
for(i=0;i<N;i++)
{
scanf("%s %s %lf %lf %lf",stu[i].hao,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
}
}
void print(struct Student stu[])
28
{
int i;
for(i=0;i<N;i++)
{
printf("%s,%s,%.0lf,%.0lf,%.0lf\n",stu[i].hao,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
}
}
课后习题11.5
有N个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入N个学生的数据,要求打印出3门课的总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课成绩)
Source
#include<stdio.h>
int main()
{
struct S
{
char hao[50];
char name[50];
double score[3];
};
struct S stu[100];
int N,i,j,sum1,sum2,sum3,max,k;
scanf("%d",&N);
sum1=0;sum2=0,sum3=0;
for(i=0;i<N;i++)
{
scanf("%s %s %lf %lf %lf",stu[i].hao,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
}
for(i=0;i<N;i++)
{
sum1=stu[i].score[0]+sum1;
sum2=stu[i].score[1]+sum2;
sum3=stu[i].score[2]+sum3;
}
29
printf("%.0lf %.0lf %.0lf\n",sum1/N*1.0,sum2/N*1.0,sum3/N*1.0);
max=stu[0].score[0]+stu[0].score[1]+stu[0].score[2];
for(i=1;i<N;i++)
{
if(max<(stu[i].score[0]+stu[i].score[1]+stu[i].score[2]))
max=stu[i].score[0]+stu[i].score[1]+stu[i].score[2],k=i;
}
printf("%s %s %.0lf %.0lf %.0lf",stu[k].hao,stu[k].name,stu[k].score[0],stu[k].score
[1],stu[k].score[2]);
}
课后习题11.8
已有a、b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排列。
Source
#include<stdio.h>
#include <malloc.h>
typedef struct Student
{
int id;
float score;
Student *next;
}*Stu;
void creatLink(Stu stu,int n){
Stu q=stu;
for(int i=0;i<n;i++){
scanf("%d %f",&q->id,&q->score);
if (i<n-1)
{
Stu p=(Stu) malloc(sizeof(Student));
q->next=p;
q=p;
}
}
q->next=NULL;
}
Stu sort(Stu stu1,Stu stu2,int n)
{
Stu head=(Stu) malloc(sizeof(Student));
head->next=stu1;
Stu q=stu1;
30
while (q->next)
{
q=q->next;
}
q->next=stu2;
for(int i=0;i<n-1;i++) {
q=head;
Stu p=q->next,t=p->next;
for(int j=0;j<n-i-1;j++) { if (p->id < t->id) {
q=p; p=t; t=t->next;
}
else {
q->next=t;
p->next=t->next;
t->next=p;
q=t; t=p->next;
}
}
}
return head->next;
}
void print(Stu stu)
{
Stu p=stu;
while(p){
printf("%d %.0f\n",p->id,p->score); p=p->next;
}
}
int main()
{
Stu stu1=(Stu) malloc(sizeof(Student)); Stu stu2=(Stu) malloc(sizeof(Student)); int m,n;
scanf("%d %d",&m,&n);
creatLink(stu1,m);
creatLink(stu2,n);
print(sort(stu1,stu2,m+n));
}
31