2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > SCAU高级语言程序设计OJ

SCAU高级语言程序设计OJ

时间:2018-08-19 19:26:16

相关推荐

SCAU高级语言程序设计OJ

高级语言程序设计

实验四 选择结构堂前习题堂上练习单元测试实验五 循环结构(一)堂前习题堂上练习单元测试实验六 循环结构(二)堂前习题堂上练习单元测试实验七 数组的应用堂前习题堂上练习单元测试实验八 字符数组及串堂前练习堂上练习单元测试实验九 函数的应用堂前习题堂上练习单元测试实验十 指针与结构体堂前习题堂上练习实验十一 链表操作堂前习题堂上练习实验十二 文件操作堂前习题堂上练习

实验四 选择结构

堂前习题

1018 数的排序

Description

由键盘输入三个整数a、b、c,按从小到大的顺序输出这三个数。

#include <stdio.h>int main(){int a,b,c,t;scanf("%d,%d,%d",&a,&b,&c);if(a>b){t=a;a=b;b=t;}if(a>c){t=a;a=c;c=t;}if(b>c){t=b;b=c;c=t;}printf("%d,%d,%d",a,b,c);return 0;}

1016 字符变换

Description

由键盘输入5个字符,将其中的大写字符变成小写(其它类型的字符不变),最后,按输入顺序输出这5个字符。

#include <stdio.h>int main(){char c;c=getchar();int i;for(i=0;i<5;i++){if(c>='A'&&c<='Z'){c+=32;}printf("%c",c);c=getchar();}return 0;}

1019 数的整除

Descrption

由键盘输入5个整数,逐个判断它们能否被27整除,能的输出“YES”,不能的输出“NO”(注意,输出时,一个判断结果占一行,5个数的判断共占5行)。

#include<stdio.h>int main(){int i,a;for(i=0; i<5; i++){scanf("%d",&a);if(a%27==0)printf("YES\n");elseprintf("NO\n");}return 0;}

1020 正负奇偶判断

Description

由键盘输入非零整数x,判断该数正负,正数输出positive,负数输出negative,接着判断该数的奇偶性,奇数输出odd,偶数输出even。

输出格式

注意,正负判断结果与奇偶判断结果之间用回车符分隔

#include <stdio.h>int main(){int x;scanf("%d",&x);if(x>0){printf("positive\n");}else{printf("negative\n");}if(x%2==0){printf("even\n");}else{printf("odd\n");}return 0;}

1023 简单计算器

Description

下面程序是实现一个简单的运算器(保留两位小数点),如果由键盘输入10+50,计算机可以输出结果60.00;如果输入8*6,计算机输出48.00;如果输入20/4,计算机输出5.00;如果输入8-6,计算机输出2.00,请在空处填上适当的代码,运行通过后并提交。

#include <stdio.h>#include<stdlib.h>int main(){float a,b,c;char op;scanf("%f%c%f",&a,&op,&b);switch(op){case'+':c=a+b;break;case'-':c=a-b;break;case'*':c=a*b;break;case'/':c=a/b;break;default:printf("error");exit(0);}printf("result=%.2f",c);return 0;}

堂上练习

1007 判断平方数

Description

由键盘输入一个正整数,判断该数是否为平方数,是输出Y,否则输出N

#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){int n,x;scanf("%d",&n);x=sqrt(n);if(x*x==n)printf("Y\n");elseprintf("N\n");return 0;}

1017 求数的位数

Description

由键盘输入一个不多于9位的正整数,要求输出它是几位数。

#include <stdio.h>#include<math.h>int main(){int a,cnt=0;scanf("%d",&a);while(a>0){cnt++;a/=10;}printf("%d",cnt);return 0;}

1120 判断点是否在圆上

Description

由键盘输入一个点的坐标, 要求编程判断这个点是否在单位圆(圆心在坐标0,0)上,点在圆上输出Y, 不在圆上输出N。

使用小数点后3位精度进行判断。

#include <stdio.h>#include<math.h>int main(){float x,y;scanf("%f,%f",&x,&y);if(fabs(sqrt(x*x+y*y)-1)<1e-3){printf("Y\n");}else{printf("N\n");}return 0;}

单元测试

1 长方体与圆球

Time Limit:1000MS Memory Limit:65536K

题型: 编程题 语言: G++;GCC

描述

由键盘输入一个形如长方体的盒子的长、宽、高,以及一个圆球的半径,判断该盒子能否完全装下圆球,能输出Y,否则输出N.

输入格式

第一行长方体的三边长

第二行圆球的半径

输出格式

Y或N

#include <stdio.h>#include <math.h>int main(){double a,b,c,r;scanf("%lf %lf %lf %lf",&a,&b,&c,&r);if((a>2*r)&&(b>2*r)&&(c>2*r)){printf("Y\n");}else printf("N\n");return 0;}

实验五 循环结构(一)

堂前习题

1024 计算阶乘

Description

输入正整数n(n<12),计算n!(注n!=123*…*n)

#include <stdio.h>#include <stdlib.h>int main(){int i,n,sum=1;scanf("%d",&n);for(i=1; i<=n; i++)sum*=i;printf("%d",sum);return 0;}

1025计算简单数列和

Description

有数列1,3,5,7,9,11,……

现要求由键盘输入n,计算输出该数列的前n项和。(给的n不会超过10000)

#include <stdio.h>#include<math.h>int main(){int n;scanf("%d",&n);printf("%d",n*n);return 0;}

1044 输出最小值

Description

从键盘输入十个整数,输出最小值

#include <stdio.h>#include <stdlib.h>int main(){int i,a[10],min=10000;for(i=0; i<10; i++){scanf("%d",&a[i]);if(a[i]<min){min=a[i];}}printf("%d",min);return 0;}

堂上练习

1030 字符变换

Description

由键盘输入一个句子(字符个数不定,最多不超过80个,以’\n’结束),将其中的大写字符变成小写(其它类型的字符不变),

最后输出变换后的句子。

#include <stdio.h>int main(){char a;a=getchar();while(a!='\n'){if(a>='A'&&a<='Z'){a+=32;}printf("%c",a);a=getchar();}return 0;}

1037 计算数列和

Description

有数列:编程实现,由键盘输入n,计算输出数列前n项和。(结果保留四位小数,提示:要使用double,否则精度不够)

#include <stdio.h>int main(){double a1=1,a2=2,b1,a3;double t,n,sum=0;int i;scanf("%lf",&n);sum=a2/a1;for(i=2;i<=n;i++){t=a1+a2;sum+=t/a2;a1=a2;a2=t;}printf("%.4lf",sum);return 0;}

1029 求最大公约数

Description

由键盘输入两个正整数m、n(m、n<1000000),计算它们的最大公约数。

#include <stdio.h>#include <stdlib.h>#include<math.h>int main(){int m,n,t;scanf("%d,%d",&m,&n);while(n>0){if(m%n==0)break;t=m%n;m=n;n=t;}printf("%d",n);return 0;}

1031 统计单词个数

Description

写一个函数实现:输入一行字符,以空格分割单词,回车结束输入,输出单词的个数。

#include <stdio.h>#include <stdlib.h>int main(){char c;int a=1,cnt=1;c=getchar();while(c!='\n'){if(c==' ') {a=0;}else if(a==0){a=1;cnt++;}c=getchar();}printf("%d",cnt);return 0;}

1042 百万富翁

Description

一个百万富翁遇到一个陌生人,陌生人找他谈了一个换钱的计划。该计划如下:我每天给你m元,

而你第一天只需给我一分钱。第二天我仍给你m元,你给我2分钱。第三天,我仍给你m元,

你给我4分钱。依次类推,你每天给我的钱是前一天的两倍,直到一个月(30天)。

百万富翁很高兴,欣然接受这个契约。现要求,编写一个程序,由键盘输入m,

计算多少天后,百万富翁开始亏钱。

输入样例

100

输出样例

18

#include <stdio.h>#include <stdlib.h>#include<math.h>int main(){int m,n;float a,b,c;int i;scanf("%d",&m);c=0.01;for(i=1;i<=30;i++){a+=m;b+=c;c*=2;if(a<b){printf("%d",i);break;}}return 0;}

单元测试

1 求因子个数

描述

由键盘输入一个int类型的正整数n,求n有多少个不同的正整数因子。

注:能整除N的数称为N的因子

#include <stdio.h>#include <math.h>int main(){int n,i,num=0;scanf("%d",&n);for(i=1;i<=n;i++){if(n%i==0){num++;}}printf("%d",num);return 0;}

实验六 循环结构(二)

堂前习题

1035 打印菱形图案

Description

由键盘输入正数n(n<30),要求输出如下2*n+1行的菱形图案。

输出格式

菱形右边不留多余空格

输入样例

2

输出样例

#include <stdio.h>#include <stdlib.h>#include<math.h>int main(){int n,i,j;scanf("%d",&n);for(i=-n; i<=n; i++){for(j=-n; j<=n; j++){if(abs(i)+abs(j)<=n)printf("*");elseprintf(" ");}printf("\n");}return 0;}

堂上练习

1028 求素数

Description

输出2到200之间(包括2、200)的所有素数(注:要求1行1个素数,按由小到大的顺序输出)。

#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){int i,n;for(i=2;i<=200;i++){for(n=2;n<sqrt(i);n++){if(i%n==0){break;}}if (n>=sqrt(i)){printf("%d\n",i);}}return 0;}

1137 找满足要求的数字

Description

输出1到9999中能被7整除,而且至少有一位数字是5的所有数字

#include <stdio.h>#include <stdlib.h>int main(){int i,j;for(i=7;i<=9999;i+=7){j=i;while(j!=0){if(j%10==5){printf("%d\n",i);break;}j/=10;}}return 0;}

1038 打印图案

Description

由键盘输入正数n(n<10),要求输出如下中间数字为n的菱形图案。

输出格式

菱形右边不留多余空格

输入样例

4

输出样例

#include <stdio.h>#include <stdlib.h>int main(){int i,j,k,n;scanf("%d",&n);for(i=1-n;i<=n-1;i++){for(j=0;j<abs(i);j++){printf(" ");}for(k=1;k<=n-abs(i);k++){printf("%d",k);}for(k=n-abs(i)-1;k>0;k--)printf("%d",k);printf("\n");}}

单元测试

1 打印星号空心菱形

描述

由键盘输入n(n为正奇数,n<=50),打印输出如下图n行的星号空心菱形

例n=7

#include <stdio.h>#include <stdlib.h>int main(){int n,i,j,k;scanf("%d",&n);for(i=-(n-1)/2;i<=(n-1)/2;i++){for(j=0;j<abs(i);j++){printf(" ");}printf("*");if(abs(i)<(n-1)/2){for(k=0;k<n-2-2*abs(i);k++){printf(" ");}printf("*");}printf("\n");}return 0;}

实验七 数组的应用

堂前习题

1039 倒序

Description

由键盘输入10个整数,倒序输出。

#include <stdio.h>#include <stdlib.h>int main(){int a[10];int i,j,t;for(i=0;i<10;i++)scanf("%d",&a[i]);for(i=9;i>=0;i--){printf("%d\n",a[i]);}return 0;}

1062 打印矩阵

Description

由键盘输入一个3*4的矩阵,要求输出它的转置矩阵。

输入样例

1 6 9 3

1 1 0 2

1 9 8 9

输出样例

1 1 1

6 1 9

9 0 8

3 2 9

#include <stdio.h>#include <stdlib.h>int main(){int a[3][4],b[4][3];int i,j;for(i=0;i<3;i++){for(j=0;j<4;j++){scanf("%d",&a[i][j]);}}for(i=0;i<3;i++){for(j=0;j<4;j++){b[j][i]=a[i][j];}}for(i=0;i<4;i++){for(j=0;j<3;j++){printf("%d",b[i][j]);printf(" ");}printf("\n");}return 0;}

堂上练习

1047 冒泡排序

Description

由键盘输入10个数,用“冒泡法”对10个数从小到大排序,并按格式要求输出。代码如下,请填充完整。

输入样例

70 5 14 20 19 2 99 67 13 66

输出样例

2 5 13 14 19 20 66 67 70 99

#include "stdio.h"main(){int a[10], i, j, t;for(i=0;i<10;i++)scanf("%d",_______________________) ; for(_______________________){for(j=0;j<_______________________;j++)if (_______________________){_______________________}}for(i=0;i<10;i++)printf("%d ",a[i]);}

$line1$&a[i]$line2$i=0;i<9;i++$line3$9-i$line4$a[j]>a[j+1]$line5$t=a[j+1];a[j+1]=a[j];a[j]=t;

1040 统计不同数字的个数

Description

由键盘输入20个整数,统计不同数字的个数。

#include <stdio.h>#include <stdlib.h>#include<string.h>int main(){int i,j,cnt=0,num[20];for(i=0; i<20; i++){scanf("%d",&num[i]);for(j=0; j<i; j++){if(num[i]==num[j])break;}if(i==j)cnt++;}printf("%d",cnt);return 0;}

1051 找矩阵中的鞍点

Description

由键盘输入一个3*4(3行4列)的数字矩阵,其中任意两个数字均不相同。要求输出该数字矩阵中的鞍点(即在矩阵行中最大,列中最小的数)。

若没有鞍点,输出“NO”字样。

输入样例

87 90 110 98

70 97 210 65

99 45 120 30

输出样例

110

#include <stdio.h>#include <stdlib.h>int main(){int a[3][4];int x,y,i,j,t;int max,min;x=y=0;for(i=0;i<3;i++){for(j=0;j<4;j++){scanf("%d",&a[i][j]);}}for(i=0;i<3;i++){max=a[i][0];for(j=0;j<4;j++){if(a[i][j]>max){max=a[i][j];x=j;}}min=a[0][x];for(t=0;t<3;t++){if(a[t][x]<min){min=a[t][x];y=t;}}if(max==min){printf("%d",a[y][x]);break;}}if(max!=min){printf("NO");}return 0;}

1046 计算高精度加法

由键盘输入两个位数很长的整数(一行一个,最多不超过80位),试计算并输出这两个数的和。

#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){int a[100]={0},b[100]={0},c[100]={0},i,j=0,lx,ly,max;char x[100],y[100];scanf("%s",x);lx=strlen(x);for(i=0;i<lx;i++){a[i]=x[lx-1-i]-'0';}scanf("%s",y);ly=strlen(y);for(i=0;i<ly;i++){b[i]=y[ly-1-i]-'0';}if(lx>ly) max=lx;else max=ly;for(i=0;i<max;i++){c[i]=(a[i]+b[i]+j)%10;j=(a[i]+b[i]+j)/10;}if(j!=0){max++;c[max-1]=j;}for(i=max-1;i>=0;i--){printf("%d",c[i]);}return 0;}

单元测试

1 最小差值

描述

由键盘输入10个浮点数,任取其中两数相减求绝对值,求其中最小值(保留两位小数)

输入格式

10个浮点数,由空格分隔

输出格式

最小差值

输入样例

1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 9

输出样例

0.20

浮点数绝对值用fabs!!!

#include <stdio.h>#include <stdlib.h>#include<math.h>int main(){double a[10],min;int i,j;for(i=0;i<10;i++){scanf("%lf",&a[i]);}min=abs(a[1]-a[0]);for(i=0;i<10;i++){for(j=i+1;j<10;j++){if(fabs(a[i]-a[j])<min){min=fabs(a[i]-a[j]);}}}printf("%.2lf",min);return 0;}

实验八 字符数组及串

堂前练习

1121 定义存贮字符串的数组

Description

在下面程序中填充定义字符数组的语句,使程序完整。

#include "stdio.h"#include "string.h"int main(){_______________________/*define a array named s to store string*/strcpy(s, "abcdefghijklmn");printf("%s", s);return 0;}

$line1$char s[80];

1122 字符串的合并

Description

从键盘输入3个字符串(每个字符串以回车符做为结束标志),将3个字符串以输入先后顺序合并到字符串s中,

请填空使用程序完整。

#include "stdio.h"#include "string.h"main(){char s[100]="";char a[30];_______________________printf("%s", s);}

$block1$gets(a);strcat(s,a);gets(a);strcat(s,a);gets(a);strcat(s,a);$end1$

1123 字符串的输入与输出

Description

下面程序实现从键盘读入字符串,然后输出到屏幕,请填充必要的语句。

输入样例

Wang

输出样例

What’s your name?

Your name is Wang

#include "stdio.h"main(){char s[50];printf("What's your name?\n");_______________________ /*iput your name from the keyboard*/printf("Your name is ");printf("_______________________", s); /*output your name*/}

$line1$gets(s);$line2$%s

堂上练习

1145 回文串

Description

读入一行字符串(不多于80个字符,以回车结束),判断该字符串是否为回文串(即从左向右拼写与从

右向左拼写是一样的),是则输出Y,不是则输出N。

#include <stdio.h>#include <stdlib.h>#include<string.h>int main(){int i,j,k;char c[81];scanf("%s",c);k=strlen(c);for(i=0; i<k/2; i++){if(c[i]!=c[k-1-i])break;}if(i>=k/2)printf("Y\n");elseprintf("N\n");return 0;}

1050 寻找字符串

Description

由键盘输入两个字符串(假设第一个字符串必包含第二个字符串,如第一个字符串为ABCDEF,第二个为CDE,

则CDE包含在ABCDEF中),现要求编程输出第二字符串在第一行字符串中出现的位置。

(如果第二个字符串在第一个字符串中出现多次,则以最前出现的为准)

输入样例

ABCDEFG

DE

输出样例

4

#include <stdio.h>#include <string.h>int main(){char a[100],b[100];int i,j,x,y,k;gets(a);gets(b);x=strlen(a);y=strlen(b);for(i=0;i<x;i++){for(j=0;j<y;j++){if (a[i+j]!=b[j])break;}if (b[j]=='\0')break;}if (a[i]!='\0')printf("%d",i+1);return 0;}

单元测试

1 多少个Bubble

描述

读入一行字符串(不多于800个字符,以回车结束),统计其中Bubble出现了多少次

输入样例

Bubble if only Bubble.

输出样例

2

#include <stdio.h>#include <stdlib.h>#include<string.h>int main(){int i,j,cnt=0;char a[800];gets(a);char b[7]="Bubble";for(i=0;a[i]!='\0';i++){for(j=0;b[j]!='\0';j++){if(a[i+j]!=a[j])break;}if(b[j]=='\0'){cnt++;i+=6;}}printf("%d",cnt);return 0;}

实验九 函数的应用

堂前习题

1083 编写函数计算阶乘

Description

下面程序实现由键盘读入整数n,计算并输出n!,请补充完整计算阶乘的函数。

#include "stdio.h"_______________________main(){int n;scanf("%d", &n);printf("%ld", fanc(n));}

$block1$int fanc(int n){int sum=1,i;for(i=1;i<=n;i++){sum*=i;}return sum;}$end1$

1124 函数中的变量

Description

写出下面程序的运行结果:

int f1(int x){static int z=3,y=0;y++;z++;return(x+y+z);}main(){int a=1,k;for(k=0;k<3;k++) printf("%4d",f1(a));}

程序到此结束 请用下面程序输出你的答案(注意转义字符的正确表达)

#include “stdio.h”

main()

{

printf(“_______________________”);

}

$line1$6 8 10\n

堂上练习

1059 [填空题]函数定义

Description

下面是使用辗转相除法,求最大公约数的程序,请补充完整程序中函数的定义与调用,运行通过后提交代码。

输入样例

24 16

输出样例

8

#include "stdio.h"_______________________{int r;while ((r=m%n)!=0){m=n;n=r;}return n;}main(){int a, b, n;scanf("%d%d", &a, &b);printf("%d\n", _______________________);}

$line1$int GCM(int m,int n)$line2$GCM(a,b)

1084 [填空题]十进制数转二进制数

Description

下面程序,实现由键盘输入一个正整数(不大于100000000),输出其对应的二进制数(原码表示)。

请填空:

#include "stdio.h"_______________________main(){int n;scanf("%d", &n);binary(n);}

输入样例

12

输出样例

1100

$block1$void binary(int n){if(n/2>0){binary(n/2);}printf("%d",n%2);}$end1$

1151 求函数值

Description

输入x(x为整数),求函数值

函数定义如下:

F(x)=x x小于3

F(x)=F(x/3)*2 x大于等于3且x为3的倍数

F(x)=F((x-1)/3)+1 x大于等于3且x除3余1

F(x)=F((x-2)/3)+2 x大于等于3且x除3余2

#include <stdio.h>#include <stdlib.h>int F(int x){if (x<3) return x;else if(x%3==0) return F(x/3)*2;else if(x%3==1) return F((x-1)/3)+1;else if(x%3==2) return F((x-2)/3)+2;}int main(){int n;scanf("%d",&n);printf("%d",F(n));return 0;}

单元测试

1 求函数值2

描述

输入x(x为整数),求函数值F(x)

函数定义如下:

F(x)=x x小于2

G(x)=x x小于2

F(x)=G(x/2)*2 x大于等于2且x为偶数

F(x)=G((x-1)/2) x大于等于2且x为奇数

G(x)=G(x/2)+1 x大于等于2且x为偶数

G(x)=x x为奇数

#include <stdio.h>#include <stdlib.h>#include<string.h>int G(int x){int gx;if(x<2) gx=x;else if(x>=2&&x%2==0) gx=G(x/2)+1;else if(x>=2&&x%2!=0) gx=x;return gx;}int F(int x){int y;if(x<2) y=x;else if(x>=2&&x%2==0) y=G(x/2)*2;else if(x>=2&&x%2!=0) y=G((x-1)/2);return y;}int main(){int x;scanf("%d",&x);printf("%d",F(x));return 0;}

实验十 指针与结构体

堂前习题

1091 [填空]交换两数,由大到小输出

Description

下面程序,交换两数,使两数由大到小输出,请填空

#include "stdio.h"void swap(_______________________){int temp;temp=*p1;*p1=*p2;*p2=temp; } int main() {int a,b; int *pa,*pb;scanf("%d%d", &a, &b);pa=&a; pb=&b;if(a<b) swap(_______________________);printf("%d %d\n",a,b);}

$line1$int *p1,int*p2$line2$&a,&b

11128 字符串与指针

Descrption

请写出下列程序的运行结果

#include<stdio.h>int main( ){char string[30]="How_are_you" ;char *p=&string[0], *p2=string+8;printf("%s,%s\n" , p , p2 ) ;}

程序运行结果为:

#include <stdio.h>int main(){printf("_______________________");}

$line1$How_are_you,you

1125 定义结构体类型

Description

要求定义一个名为student的结构体类型,其包含如下成员:

(1)字符数组name,最多可存放10个字符;

(2)字符变量sex,用于记录性别;

(3)整数类型变量num,用于记录学号;

(4)float类型变量score,用于记录成绩;

并使下列代码完整。

#include "stdio.h"_______________________int main(){struct student stu;gets(stu.name);scanf("%c", &stu.sex);scanf("%d", &stu.num);scanf("%f", &stu.score);printf("%s\n", stu.name);printf("%c\n", stu.sex);printf("%d\n", stu.num);printf("%f\n", stu.score);return 0;}

$block1$struct student{char name[10];char sex;int num;float score;};$end1$

堂上练习

1092 [填空]函数实现求字符串长度

Description

下面程序实现由函数实现求字符串长度,再填空完成

#include "stdio.h"/*create function f*/_______________________int main(){char s[80];int i;scanf("%s", s);i=f(s);printf("%d", i);}

$block1$int f(const char*p){int cnt=0;while(*(p++)){cnt++;}return cnt;}$end1$

1065 数组中的指针

Description

设有如下数组定义:

int a[3][4]={{1,3,5,7},{9,11,13,15},{17,19,21,23}};

计算下面各项的值(设数组a的首地址为2000,一个int类型数占四个字节)。

(1)a[2][1] (2)a[1] (3)a (4)a+1 (5)*a+1(6)*(a+1) (7)a[2]+1

(8)*(a+1)+1 (9)*(*(a+2)+2)

编写一个程序直接输出你的答案,一行一个。

提示

注意:地址则输出地址,变量则输出变量值;输出格式,要求,一行一个答案,不允许多余空格

#include <stdio.h>#include <stdlib.h>int main(){printf("19\n\n2000\n\n\n\n2036\n\n21");return 0;}

实验十一 链表操作

堂前习题

1099链表的合并

Description

下面程序创建两个链表,然后将第二个链表合并到第一个链表未尾,但合并部分的代码未完成,请你完成这部分代码。

#include "stdio.h"#include "malloc.h"#define LEN sizeof(struct student)struct student{long num;int score;struct student *next;};struct student *create(int n){struct student *head=NULL,*p1=NULL,*p2=NULL;int i;for(i=1;i<=n;i++){p1=(struct student *)malloc(LEN);scanf("%ld",&p1->num); scanf("%d",&p1->score); p1->next=NULL;if(i==1) head=p1;else p2->next=p1;p2=p1;}return(head);}struct student *merge(struct student *head, struct student *head2){_______________________}void print(struct student *head){struct student *p;p=head;while(p!=NULL){printf("%8ld%8d",p->num,p->score);p=p->next;printf("\n");}}main(){struct student *head, *head2;int n;long del_num;scanf("%d",&n); head=create(n);print(head);scanf("%d",&n); head2=create(n);print(head2);head = merge(head, head2); print(head);}

$block1$struct student *p=NULL;p=head;while(p->next!=NULL){p=p->next;}p->next=head2;return(head);$end1$

堂上练习

1098 [填空]链表结点的插入

Description

完成插入链表结点的函数(按学号顺序),并调试通过、提交。

#include "stdio.h"#include "malloc.h"#define LEN sizeof(struct student)struct student{long num;int score;struct student *next;};struct student *create(int n){struct student *head=NULL,*p1=NULL,*p2=NULL;int i;for(i=1;i<=n;i++){p1=(struct student *)malloc(LEN);scanf("%ld",&p1->num); scanf("%d",&p1->score); p1->next=NULL;if(i==1) head=p1;else p2->next=p1;p2=p1;}return(head);}void print(struct student *head){struct student *p;p=head;while(p!=NULL){printf("%8ld%8d",p->num,p->score);p=p->next;printf("\n");}}struct student *insert(struct student *head, struct student *stud){_______________________}main(){struct student *head,*stu;int n;scanf("%d",&n); head=create(n);print(head);stu=(struct student *)malloc(LEN);scanf("%ld",&stu->num); scanf("%d",&stu->score); stu->next = NULL;head=insert(head,stu);print(head);}

$block1$struct student *p1=NULL, *p2=NULL, *p3=NULL;p1=head;p2=stud;if(head==NULL){head=p2;p2->next=NULL;}else{while(p1->num < p2->num && p1->next!=NULL){p3=p1;p1=p1->next;}if(p1->num >= p2->num)//在表头或表中间{if(head==p1)//在表头head=p2;else //表中间{p3->next = p2;p2->next = p1;}}else//表尾{p1->next=p2;p2->next=NULL;}}return(head);$end1$

1104 [填空题]链表的倒序

Description

下面程序,先创建一个链表,然后调用reverse函数,将链表中各结点变为倒序排列。请完成reverse函数,

#include "stdio.h"#include "malloc.h"#define LEN sizeof(struct student)struct student{long num;int score;struct student *next;};struct student *create(int n){struct student *head=NULL,*p1=NULL,*p2=NULL;int i;for(i=1;i<=n;i++){p1=(struct student *)malloc(LEN);scanf("%ld",&p1->num);scanf("%d",&p1->score);p1->next=NULL;if(i==1) head=p1;else p2->next=p1;p2=p1;}return(head);}void print(struct student *head){struct student *p;p=head;while(p!=NULL){printf("%8ld%8d",p->num,p->score);p=p->next;printf("\n");}}struct student *reverse(struct student *head){_______________________}main(){struct student *head,*stu;int n;scanf("%d",&n); head=create(n);print(head);head=reverse(head);print(head);}

$block1$struct student *p1=NULL, *p2=NULL, *p3=NULL;p2=head;p3=head->next;do{p1=p2;p2=p3;p3=p2->next;p2->next=p1;}while(p3!=NULL);head->next=NULL;return (p2);$end1$

1101 [填空题]链表的排序

Description

下面程序,先创建一个链表(链表中各结点未按学号由小到大排序),然后调用sort函数,将链表中各结点按学号由小到大排序。

#include "stdio.h"#include "malloc.h"#define LEN sizeof(struct student)struct student{long num;int score;struct student *next;};struct student *create(int n){struct student *head=NULL,*p1=NULL,*p2=NULL;int i;for(i=1;i<=n;i++){p1=(struct student *)malloc(LEN);scanf("%ld",&p1->num); scanf("%d",&p1->score); p1->next=NULL;if(i==1) head=p1;else p2->next=p1;p2=p1;}return(head);}void print(struct student *head){struct student *p;p=head;while(p!=NULL){printf("%8ld%8d",p->num,p->score);p=p->next;printf("\n");}}struct student *insert(struct student *head, struct student *stud){struct student *p0,*p1,*p2;p1=head; p0=stud;if(head==NULL){head=p0;}else{while( (p0->num > p1->num) && (p1->next!=NULL) ){p2=p1;p1=p1->next;}if( p0->num <= p1->num ){if( head==p1 ) head=p0;else p2->next=p0;p0->next=p1; }else {p1->next=p0;}}return(head);}struct student *del(struct student *head,long num){struct student *p1,*p2;p1=head;while(p1!=NULL){if(p1->num == num){if(p1 == head) head=p1->next;else p2->next=p1->next;free(p1);break;}p2=p1;p1=p1->next;}return(head);}struct student *sort(struct student *head){_______________________}main(){struct student *head,*stu;int n;scanf("%d",&n);head=create(n);print(head);head=sort(head);print(head);}

$block1$struct student *p1,*p2;p2=head;p1=head;p2=p2->next;p1->next=NULL;p1=p2;while(p2->next!=NULL){p2=p2->next;p1->next=NULL;head=insert(head,p1);p1=p2;}head=insert(head,p1);return (head);$end1$

实验十二 文件操作

堂前习题

1105 [填空]文本文件操作_字符读入

Description

在当前目录中存在文件名为"case1.in"的文本文件,现要求你使用fopen函数命令打开该文件,读出里面的所有字符, 遇到大写字母的,将其变为小写字母,其它字符不变,最后将所有字符按顺序在屏幕上输出。请填空完成程序, (注意,填空题,请不要使用return 0结束,否则会影响评判而判错)

(如case1.in内容如下)

Hello my Dear:

Have a GooD Time!

(在屏幕上输出结果如下)

hello my dear:

have a good time!

(提示,在提交前要测试自己的代码是否正确,可在源文件所有目录自己创建一个名为case1.in的文本文件,

在文件中自己打入一些字母,以便测试自己的代码是否正确)

#include "stdio.h"main(){FILE *fp;char ch;if((_______________________)==NULL)return 0;while(_______________________){if ('A'<=ch && ch<='Z')ch = ch + 32;_______________________;}fclose(fp);}

$line1$fp=fopen("case1.in","r")$line2$(ch=fgetc(fp)) !=EOF$line3$putchar(ch)

1106 文本文件操作_字符写入

Description

由键盘输入任意个字符(以连着的三个小写字符bye做为结束标志),将所有字符(包括bye),写入新建的文件answer.txt中(注:文件放在当前目录)。 请完成该功能,(注意,填空题,请不要使用return 0结束,否则会影响评判而判错)

(如键盘输入内容如下)

He, can you write the code?

Yes, you can.bye

No, you can’t.

(程序执行后,在文件answer.txt中内容如下)

He, can you write the code?

Yes, you can.bye

(注:因No, you can’t.在bye之后,所以不输出)

(注:代码中不要使用return及exit()函数,以免误判)

#include<stdio.h>main(){______________________}

$block1$char ch,ch1=' ',ch2=' ',ch3=' ';FILE *fp;fp=fopen("answer.txt","w");if(fp==NULL)return 1;while((ch=getchar())!=EOF){fputc(ch,fp);ch1=ch2;ch2=ch3;ch3=ch;if(ch1=='b'&&ch2=='y'&&ch3=='e')break;}fclose(fp);$end1$

堂上练习

11129 文本文件操作_读取与选择显示

Description

在当前目录中存在文件名为"case1.in"的文本文件,现要求打开该文件,读出里面的所有字符,只将其中的数字字符按先后顺序显示在屏幕上。

(如case1.in内容如下)

13 cats and 22 bikes

(在屏幕上输出结果如下)

1322

#include "stdio.h"main(){FILE *fp;char ch;if((_______________________)==NULL)return 0;while(_______________________){_______________________}fclose(fp);}

$block1$if(ch>='0'&&ch<='9')putchar(ch);$end1$$line1$fp=fopen("case1.in","r")$line2$(ch=fgetc(fp))!=EOF

1107 文本文件操作_单词的排序

Description

在当前目录有文件“case1.in”,文件里存放有多个(总个数不超过10000个)英文单词(每个英文单词不会超过10个字文字符), 每行一个,单词未排序。现要求,将文件中的所有单词按字典顺序排序,然后将排序好的单词写入新建的文件answer.txt中(注:文件存放于当前目录)。 请完成程序,实现该功能,(注意,填空题,请不要使用return 0结束,否则会影响评判而判错)

(如case1.in文件中原内容如下)

hello

bye

yes

(程序执行后,在文件answer.txt中内容如下)

bye

hello

yes

#include "stdio.h"#include "string.h"main(){_______________________}

$block1$int i,j,n=0;char w[10000][10],temp[10];FILE *fp;if((fp=fopen("case1.in","r"))==NULL) return 1;while((fscanf(fp,"%s",w[n]))!=EOF) n++;fclose(fp);for(i=0;i<n-1;i++){for(j=0;j<n-i-1;j++){if(strcmp(w[j],w[j+1])>0){strcpy(temp,w[j]);strcpy(w[j],w[j+1]);strcpy(w[j+1],temp);}}}if((fp=fopen("answer.txt","w"))==NULL) return 1;for(i=0;i<n;i++){fprintf(fp,"%s\n",w[i]);}fclose(fp);$end1$

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。