您的当前位置:首页正文

C语言程序设计实验答案

来源:汇意旅游网
C 语言程序设计

上机实验指导与习题(第二版)

参考答案

(仅供教师内部参考)

实验 1 C 语言程序初步 一、实验目的

(1)了解所用的计算机系统的基本操作方法,学会独立使用该系统。 (2)了解在该系统上如何编辑、编译、连接和运行一个C 程序。 (3)通过运行简单的C 程序,初步了解C 程序的特点。 (4)在教师的指导下,学会使用JudgeOnline 实验系统。 二、实验内容

1. 运行第一个C 程序 略

2. 在JudgeOnline 系统中提交实现了计算a+b 功能的程序 略 2

实验2 基本数据类型、运算和表达式 一、实验目的 (1)掌握C 语言数据类型,熟悉如何定义一个整型和实型的变量,以及对它们赋值的方法。 (2)掌握不同的类型数据之间赋值的规律。

(3)学会使用C 的有关算术运算符,以及包含这些运算符的表达式,特别是自加(++)和自减(--)运 算符的使用。

(4)进一步熟悉C 程序的编辑、编译、连接和运行的过程。 二、实验内容

[题目1117:变量定义,按要求完成程序]

下面给出一个可以运行的程序,但是缺少部分语句,请按右边的提示补充完整缺少的语句。 #include \"stdio.h\" main() {

int a, b; /*定义整型变量a 和b*/ int i, j; /*定义实型变量i 和j*/ a=5; b=6;

i=3.14; j=i*a*b;

printf(\"a=%d,b=%d,i=%f,j=%f\\n\}

[具体操作步骤]

(1)将代码补充完整;

(2)在TC 或VC++上运行通过;

1 / 36

(3)在JudgeOnline 实验系统上提交; [题目6568:在显示屏上显示指定字符]

要求编写一个程序,在显示屏上显示如下内容(全为半角字符,且不包含空格): C:\\ABC.TXT

[提示] 注意转义字符在程序中的使用。 参考程序:

#include \"stdio.h\" main()

{ printf(\"C:\\\\ABC.TXT\"); } 3

[题目1118:赋值表达式与赋值语句,写出程序运行结果] 阅读下面程序,写出运行结果: #include \"stdio.h\" main() { float a; int b, c; char d, e; a=3.5; b=a; c=330; d=c; e='\\141';

printf(\"%f,%d,%d,%c,%c\}

运行结果为: 3.500000,3,330,J,a

[提示]赋值语句具有类型转换的功能,但可能会改变数据。 [题目1119:基本运算,写出程序运行结果] 阅读下面程序,写出运行结果: #include \"stdio.h\" main()

{ int a, b, c; float d=15, e, f; a=35%7; b=15/10; c=b++; e=15/10; f=d/10;

printf(\"%d,%d,%d,%f,%f,%f\}

运行结果为:

0,2,1,15.000000,1.000000,1.500000 [提示]除法分整除与普通除法之分。 4

2 / 36

实验3 基本输入与输出 一、实验目的

(1)熟练掌握putchar、getchar、printf、scanf 函数的使用方法。

(2)掌握各种类型数据的输入输出的方法,能正确使用各种格式转换符。 二、实验内容

[题目1126:字符的输入与输出]

编程实现由键盘输入一个字符后,在屏幕上输出该字符。 [第一组自测数据] [键盘输入] a↙

[正确输出] a

[第二组自测数据] [键盘输入] +↙

[正确输出] +

参考程序:

#include \"stdio.h\" main() { char ch; ch = getchar(); putchar(ch); }

[题目1127:计算加法]

编程实现由键盘输入一个加法式,输出正确的结果。(两个加数均为整数) [第一组自测数据] [键盘输入] 10+20↙ [正确输出] 30

[第二组自测数据] [键盘输入] -15+60↙ [正确输出] 45

参考程序:

#include \"stdio.h\" main() { int a, b;

scanf(\"%d%*c%d\printf(\"%d\}

[题目1014:求圆面积]

3 / 36

由键盘输入圆半径r,请计算该圆的面积。(注:π 取3.14159,结果保留两位小数位;另外,程序只

要能对r 在0 到10000 范围的情况输出正确答案即可) [第一组自测数据] [键盘输入] 2

65.2↙ [正确输出] 13355.02

[第二组自测数据] [键盘输入] 11.2↙ [正确输出] 394.08

[提示]结果保留两位小数可采用printf 函数的格式控制字符来实现。 参考程序:

#include \"stdio.h\" main()

{ float area,r; scanf(\"%f\area=3.14159*r*r; printf(\"%0.2f\}

[题目1015:计算摄氏温度值]

从键盘输入一个华氏温度值,要求按格式输出其对应的摄氏温度值,精确到小数点后两位。 数学公式描述为: ( 32) 9

C = 5 F ?

[第一组自测数据] [键盘输入] 100↙

[正确输出] 37.78

[第二组自测数据] [键盘输入] 100↙

[正确输出] 37.78

[提示]注意公式中的除为普通除法。 参考程序:

#include main() { float f,c;

scanf(\"%f\

4 / 36

c=5.0/9*(f-32); printf(\"%.2f\} 3

实验4 选择结构程序设计 一、实验目的

(1)了解C 语言表示逻辑的方法(以0 代表“假”,以非0 代表“真”)。 (2)学会正确使用逻辑运算符和逻辑表达式。 (3)熟练掌握if 语句和switch 语句。 (4)结合程序掌握一些简单的算法。 二、实验内容

[题目1120:判断点是否在圆上]

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

N。使用小数点后3 位精度进行判断。 [第一组自测数据] [键盘输入] 0.707,0.707↙ [正确输出] Y

[第二组自测数据] [键盘输入] 0.5,0.5↙ [正确输出] N

[提示](1)平面上的点与圆的关系分为在圆内、在圆上、在圆外三种,本题要求判断是否在圆上;(2)

判断两实数相等采用判断这两实数的差的绝对值小于规定误差精度(本题为0.001)的方法实现。 参考程序:

#include \"stdio.h\" #include \"math.h\" main() { float a,b;

scanf(\"%f,%f\if(fabs(a*a+b*b-1)<1e-3) printf(\"Y\\n\"); else printf(\"N\\n\"); }

[题目1017:求数的位数]

由键盘输入一个不多于9 位的正整数,要求输出它是几位数。 [第一组自测数据] [键盘输入] 349213↙

5 / 36

[正确输出] 6

[第二组自测数据] [键盘输入] 10000↙ [正确输出] 5

[提示]划定一位数、二位数、…、九位数的范围,然后用if … else if … else 语句实现判断。 4

参考程序:

#include \"stdio.h\" main()

{ int n,place; scanf(\"%ld\

if(n>99999999) place=9; else if(n>9999999) place=8; else if(n>999999) place=7; else if(n>99999) place=6; else if(n>9999) place=5; else if(n>999) place=4; else if(n>99) place=3; else if(n>9) place=2; else place=1;

printf(\"%ld\\n\}

[题目1018:数的排序]

由键盘输入三个整数a、b、c,按从小到大的顺序输出这三个数。 [第一组自测数据] [键盘输入] 65,45,90↙ [正确输出] 45,65,90

[第二组自测数据] [键盘输入] 9,6,3↙ [正确输出] 3,6,9

参考程序:

#include main()

{ int a,b,c,t;

scanf(\"%d,%d,%d\if(a>b) {t=a;a=b;b=t;} if(a>c) {t=a;a=c;c=t;}

6 / 36

if(b>c) {t=b;b=c;c=t;} printf(\"%d,%d,%d\}

[题目1016:字符变换]

由键盘输入5 个字符,将其中的大写字母变成小写,其它类型的字符不变,并按输入顺序逐个输出。

[第一组自测数据] [键盘输入] ApPLe↙ [正确输出] apple

[第二组自测数据] [键盘输入] a+B=5↙ [正确输出] a+b=5

[提示]下面代码实现由键盘读入一个字符,并按题意处理后输出 char a;

a=getchar();

if(a>='A' && a<='Z') a=a+32; 5

putchar(a);

现在,题目要求处理5 个字符,怎么做呢?请自己思考…… 参考程序:

#include main()

{ char a,b,c,d,e;

scanf(\"%c%c%c%c%c\if(a<='Z'&&a>='A') a=a+32; if(b<='Z'&&b>='A') b=b+32; if(c<='Z'&&c>='A') c=c+32; if(d<='Z'&&d>='A') d=d+32; if(e<='Z'&&e>='A') e=e+32;

printf(\"%c%c%c%c%c\}

[题目1019:数的整除]

由键盘输入5 个整数,逐个判断它们能否被27 整除,能的输出“YES”,不能的输出“NO”(注意,

输出时,一个判断结果占一行,5 个数的判断共占5 行)。 [第一组自测数据] [键盘输入]

8 27 17577 325 54↙ [正确输出] NO

7 / 36

YES YES NO YES

[第二组自测数据] [键盘输入]

8 27 17577 325 54↙ [正确输出] NO YES YES NO YES

[提示]整除即除得余数为0 参考程序:

#include \"stdio.h\" main()

{ int a,b,c,d,e;

scanf(\"%d %d %d %d %d\if(a%27==0) printf(\"YES\\n\");else printf(\"NO\\n\"); if(b%27==0) printf(\"YES\\n\");else printf(\"NO\\n\"); if(c%27==0) printf(\"YES\\n\");else printf(\"NO\\n\"); if(d%27==0) printf(\"YES\\n\");else printf(\"NO\\n\"); if(e%27==0) printf(\"YES\\n\");else printf(\"NO\\n\"); }

[题目1020:正负奇偶判断]

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

偶性,奇数输出odd,偶数输出even。 [第一组自测数据] [键盘输入] -43↙

[正确输出] negative odd 6

[第二组自测数据] [键盘输入] 98↙

[正确输出] positive even

参考程序:

#include \"stdio.h\"

8 / 36

main() { int n;

scanf(\"%d\

if(n<0)printf(\"negative\\n\"); else printf(\"positive\\n\"); if(n%2==0)printf(\"even\\n\"); else printf(\"odd\\n\"); }

[题目1023:简单计算器]

下面程序是实现一个简单的运算器(保留两位小数点),如果由键盘输入10+50,计算机可以输出结

果60.00;如果输入8*6,计算机输出48.00;如果输入20/4,计算机输出5.00;如果输入8-6,计算机

输出2.00,请在空处填上适当的代码,运行通过后并提交。 #include \"stdio.h\" main()

{ float a,b,c; char op;

scanf(\"%f%c%f\switch (op) { case '+': ; case '-': _; case '*': _; case '/': __ ;

default: printf(\"error\"); return ; }

printf(\"result= \}

[第一组自测数据] [键盘输入] 45*2↙ [正确输出] 90

[第二组自测数据] [键盘输入] 50.1-23↙ 7

[正确输出] 27.10 参考程序:

#include int main() { float a,b,c; char op;

9 / 36

scanf(\"%f%c%f\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\"); break; }

printf(\"result=%.2f\} 8

实验5 循环结构程序设计(一) 一、实验目的

(1)熟悉掌握用while 语句、do-while 语句和for 语句实现循环的方法。 (2)掌握在程序设计中用循环实现一些常用算法(如穷举、迭代、递推等)。 二、实验内容

[题目1024:计算阶乘]

输入正整数n,计算n!,结果用长整型数表示(注n!=1*2*3*...*n) [第一组自测数据] [键盘输入] 5↙

[正确输出] 120

[第二组自测数据] [键盘输入] 8↙

[正确输出] 40320

参考程序:

#include main()

{ long i,n=1,a; scanf(\"%ld\

for(i=1;i<=a;i++) n=n*i; printf(\"%ld\\n\}

[题目1025:计算数列和]

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

现要求由键盘输入n,计算输出该数列的前n 项和。 [第一组自测数据] [键盘输入] 2↙

[正确输出]

10 / 36

4

[第二组自测数据] [键盘输入] 5↙

[正确输出] 25 9

参考程序:

#include main()

{ long n,sum=0,i,t=1; scanf(\"%ld\for(i=1;i<=n;i++) { sum=sum+t; t=t+2; }

printf(\"%ld\\n\} 或

#include main()

{ long n,sum; scanf(\"%ld\sum=n*n;

printf(\"%ld\}

注:评判系统不对程序实现细节进行分析,只对运行结果进行评测。 [题目1026:累加一行字符中的数字]

由键盘输入一行字符(总字符个数从1 个至80 个均有可能,以回车符表示结束),将其中每个数字

字符所代表的数值累加起来,输出结果。 [第一组自测数据] [键盘输入] abc123↙ [正确输出] 6

[第二组自测数据] [键盘输入] A6H7T+65↙ [正确输出] 24

[提示](1)可以使用下面程序段逐个读入键盘输入的一行字符 char ch;

11 / 36

while((ch=getchar())!='\\n') {……}

(2)数字字符转为对应的数值可用a=ch-'0' 参考程序:

#include main() { char c; int s=0,a;

while((c=getchar())!='\\n') { if(c>='0'&&c<='9') { a=c-48; s=s+a; } }

printf(\"%d\} 10

[题目1029:求最大公约数]

由键盘输入两个正整数m、n(m、n 用长整数表示),计算它们的最大公约数。 [第一组自测数据] [键盘输入] 16,24↙ [正确输出] 8

[第二组自测数据] [键盘输入] 17,25↙ [正确输出] 1

[提示]公约数是既能整除m 又能整除n 的数,题目要求满足这一条件的最大的一个。 参考程序:

#include main()

{ long r,m,n,temp;

scanf(\"%ld,%ld\while(m!=0) { r=n%m; n=m; m=r; }

printf(\"%ld\\n\} 或

#include

12 / 36

main()

{ long m,n,t,i,s;

scanf(\"%ld,%ld=m>n?n:m; for (i=t;i>=1;i--)

{ if (m%i==0&&n%i==0) { s=i; break; } }

printf(\"%d\}

[题目1030:字符变换]

由键盘输入一个句子(总字符个数从1 个至80 个均有可能,以回车符表示结束),将其中的大写字

符变成小写(其它类型的字符不变),最后输出变换后的句子。 [第一组自测数据] [键盘输入]

How Are You?↙ [正确输出] how are you?

[第二组自测数据] [键盘输入]

ThiS IS My fIrSt C ProgrAm!↙ [正确输出]

this is my first c program! 11

参考程序:

#include main() { char c;

while((c=getchar())!='\\n') { if(c>='A'&&c<='Z') c=c+32; putchar(c); } }

[题目1037:计算数列和] 有数列:

编程实现,由键盘输入n,计算输出数列前n 项和。(结果保留四位小数) [第一组自测数据] [键盘输入] 20↙

[正确输出] 32.6603

[第二组自测数据]

13 / 36

[键盘输入] 30↙

[正确输出] 88.0403 参考程序:

#include main() { int i,t,n;

float a=2,b=1,s=0; scanf(\"%d\for(i=1;i<=n;i++) { s=s+a/b; t=a;a=a+b;b=t; }

printf(\"%.4f\\n\}

[题目1044:输出最小值]

从键盘输入十个整数,输出最小值 [自测数据] [键盘输入]

12 45 76 87 5 87 43 55 99 21↙ [正确输出] 5 12

参考程序:

#include \"stdio.h\" main()

{ int i,t,min;

scanf(\"%d\for(i=1;i<10;i++) {

scanf(\"%d\if(tprintf(\"%d\\n\}

*[题目1031:统计单词个数]

由键盘输入一个句子(总字符个数从1 个至80 个均有可能,以回车符表示结束),以空格分割单词,

要求输出单词的个数。 [第一组自测数据] [键盘输入]

How Are You?↙ [正确输出]

14 / 36

3

[第二组自测数据] [键盘输入]

There are many students and many trees!↙ [正确输出] 7

参考程序:

#include main()

{ int i,num=0,word=0; char c;

for(i=0;(c=getchar())!='\\n';i++) if(c==' ')word=0; else if(word==0) { word=1; num++; }

printf(\"%d\}

*[题目1042:百万富翁]

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

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

分钱。依次类推,你每天给我的钱是前一天的两倍,直到一个月(38)天。百万富翁很高兴,欣然接受这

个契约。现要求,编写一个程序,由键盘输入m,计算多少天后,百万富翁开始亏钱。 [第一组自测数据] [键盘输入] 100↙

[正确输出] 18

[第二组自测数据] [键盘输入] 10000↙ 13

[正确输出] 25 参考程序:

#include #include main() { int n,m,i;

scanf (\"%d\

15 / 36

for (i=1;i<=38;i++)

if (0.01*(pow(2,i-1)-1)-i*m>=0) break; printf(\"%d\} 14

实验6 循环结构程序设计(二) 一、实验目的

(1)进一步熟悉掌握用while 语句、do-while 语句和for 语句实现循环的方法。 (2)掌握在程序设计中使用多重循环。 二、实验内容

[题目1028:求素数]

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

[提示]采用双重循环,外层循环产生2 到200 之间的数,内层循环对数进行判断是否为素数。

参考程序:

# include # include main() { int m,k,i;

for(m=2;m<=200;m++) { k=sqrt(m);

for(i=2;i<=k;i++) if(m%i==0) break;

if(i>k) printf(\"%d\\n\} }

[题目1035:打印菱形]

由键盘输入正数n,要求输出2*n+1 行的菱形图案。要求菱形左边紧靠屏幕左边。 [第一组自测数据] [键盘输入] 3↙

[正确输出] * *** ***** ******* ***** *** *

[第二组自测数据] [键盘输入] 2↙

[正确输出]

16 / 36

* *** ***** *** * 15

参考程序:

#include \"stdio.h\" #include \"math.h\" main() { int n,i,j,k;

scanf(\"%d\

for(i=1;i<=2*n+1;i++) { k=abs(n+1-i);

for(j=1;j<=k;j++) printf(\" \");

for(j=1;j<=2*n+1-2*k;j++) printf(\"*\"); printf(\"\\n\"); } }

[题目1137:找满足要求的数字]

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

小到大的顺序输出。

[提示]判断一个数中是否有数字5,首先要掌握拆数字的方法,一种可行算法如下:

一个整数a,使用a%10,可以得到a 的个位数,然后使用a=a/10,可以将a 中的个位数字除去,

上述过程重复则可以得到原数a 中其它位上的数字。 参考程序:

#include \"stdio.h\" main() { int i, j;

for(i=7; i<=9999; i=i+7) { j=i;

while(j!=0)

{ if(j%10==5) break; j=j/10; }

if(j!=0) printf(\"%d\\n\} }

[题目1038:打印图案]

由键盘输入正数n,要求输出中间数字为n 的菱形图案。要求菱形左边紧靠屏幕左边。 [第一组自测数据] [键盘输入]

17 / 36

4↙

[正确输出] 1 121 12321 1234321 12321 121 1

[第二组自测数据] [键盘输入] 3↙

[正确输出] 1 121 12321 121 1 16

参考程序:

# include # include main()

{ int n,i,j,k,h; scanf(\"%d\

for(i=-n+1;i<=n-1;i++) { for(j=0;jfor(k=1;k<=n-abs(i);k++) printf(\"%d\for(h=n-abs(i)-1;h>=1;h--) printf(\"%d\printf(\"\\n\"); } } 17

实验7 数组 一、实验目的

(1)掌握一维数组和二维数组的定义、赋值和输入输出方法。 (2)掌握与数组有关的算法。 二、实验内容 [题目1039:倒序]

由键盘输入10 个整数,倒序输出。(数字间由一个空格分隔) [自测数据] [键盘输入]

70 5 14 20 19 2 99 67 13 66↙

18 / 36

[正确输出]

66 13 67 99 2 19 20 14 5 70 参考程序:

#include main() { int a[10]; int i;

for(i=0;i<10;i++) scanf(\"%d\for(i=9;i>=0;i--) printf(\"%d\\n\}

[题目1062:打印矩阵] 由键盘输入一个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 main()

{ int a[3][4],b[4][3],i,j; for(i=0;i<3;i++) for(j=0;j<4;j++)

{ scanf(\"%d\b[j][i]=a[i][j]; }

for(i=0;i<4;i++) { for(j=0;j<3;j++) 18

printf(\"%d \printf(\"\\n\"); } }

*[题目1047:冒泡排序]

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

整。 数字间由一个空格分隔。 #incude \"stdio.h\" main()

19 / 36

{ 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 \}

[自测数据] [键盘输入]

70 5 14 20 19 2 99 67 13 66↙ [正确输出]

2 5 13 14 19 20 66 67 70 99 参考程序:

#include main() { int a[10]; int i,j,t;

for(i=0;i<10;i++) scanf(\"%d\for(i=0;i<9;i++) { for(j=0;j<9-i;j++) if(a[j]>a[j+1])

{ t=a[j]; a[j]=a[j+1]; a[j+1]=t; } }

for(i=0;i<10;i++) printf(\"%d \} 19

[题目1040:统计不同数字个数]

由键盘输入20 个整数,统计不同数字的个数。 [自测数据] [键盘输入]

70 5 14 22 19 2 99 67 13 66 5 93 44 38 22 11 39 22 33 11↙ [正确输出] 16

[提示]上述答案中,因为5 有1 个重复数字,11 有1 个重复数字,22 有2 个重复数字,故不同数字有16

个,分别是70 5 14 22 19 2 99 67 13 66 93 44 38 11 39 33 参考程序: #include\"stdio.h\"

20 / 36

main() { int a[20]; int i,t,p=0;

for(i=0;i<20;i++) { scanf(\"%d\for(t=0;tprintf(\"%d\}

*[题目1046:计算高精度加法]

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

1234567890123456789↙ 987654321098765↙ [正确输出]

1235555544444555554 参考程序:

#include \"stdio.h\" #include \"string.h\" main()

{ int a[100]={0},b[100]={0},c[100]={0}; char s[101];

int i=0,n1=0,n2=0,max=0,e=0; gets(s);

n1=strlen(s);

for(i=n1-1;i>=0;i--) a[n1-1-i]=s[i]-'0'; gets(s);

n2=strlen(s); 20

for(i=n2-1;i>=0;i--) b[n2-1-i]=s[i]-'0'; if(n1>n2) max=n1; else max=n2;

for(i=0;i<=max;i++) { c[i]=(a[i]+b[i]+e)%10; e=(a[i]+b[i]+e)/10; }

if(c[max]>0) printf(\"%d\for(i=max-1;i>=0;i--)

21 / 36

printf(\"%d\}

*[题目1051:找矩阵中的鞍点] 由键盘输入一个3×4(3 行4 列)的矩阵,输出矩阵中的鞍点(即在矩阵行中最大,列中最小的数)。

若没有鞍点,输出“NO”字样。 [自测数据] [键盘输入] 87 90 110 98↙ 70 97 210 65↙ 98 45 120 30↙ [正确输出] 110

参考程序:

#include main()

{ int i,j,k,a[3][4],max,maxj,flag; for(i=0;i<3;i++) for(j=0;j<4;j++)

scanf(\"%d\for(i=0;i<3;i++) { max=a[i][0]; maxj=0;

for(j=0;j<4;j++) if(a[i][j]>max)

{max=a[i][j]; maxj=j; } flag=1;

for(k=0;k<3;k++) if(max>a[k][maxj]) { flag=0; break; } if(flag)

{printf(\"%d\}

if(!flag)

printf(\"NO\"); } 21

实验8 字符数组的应用 一、实验目的

(1)掌握字符数组和字符串函数的使用。 (2)掌握与字符串处理有关的算法。 二、实验内容

[题目1121:定义存贮字符串的字符数组]

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

22 / 36

#include \"string.h\" void main()

{ char s[80]; /*定义字符数组s*/ strcpy(s, \"abcdefghijklmn\"); printf(\"%s\}

参考程序:

#include \"string.h\" #include void main()

{ char s[80]; /*定义字符数组s*/ strcpy(s, \"abcdefghijklmn\"); printf(\"%s\}

[题目1123:字符串的输入与输出]

下面程序实现从键盘读入字符串,然后输出到屏幕,请填充必要的语句。 #include \"stdio.h\" void main() {

char s[50];

printf(\"What's your name?\"); gets(s); /*由键盘读入字符串*/ printf(\"Your name is \

printf(\"%s\打印字符串*/ }

参考程序:

[题目1122:字符串的合并] 从键盘输入3 个字符串(每个字符串以回车符做为结束标志),将3 个字符串以输入先后顺序合并到

字符串s 中,请填空使用程序完整。 #include \"stdio.h\" #include \"string.h\" main() {

char s[100]=\"\"; char a[30];

gets(a); strcat(s, a); 22

gets(a); strcat(s, a); gets(a); strcat(s, a); /*可以写多行代码*/ printf(\"%s\}

[自测数据]

23 / 36

[键盘输入] 123 abc 456

[正确输出] 123abc456

[题目1145:回文串]

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

与从右向左拼写是一样的),是输出Y,否则输出N。 [第一组自测数据] [键盘输入] aba↙

[正确输出] Y

[第二组自测数据] [键盘输入] abc↙

[正确输出] N

参考程序:

#include \"stdio.h\" #include \"string.h\" main() { int i, len; char buf[100]; gets(buf);

len = strlen(buf); for(i=0; iif(i>=len/2) printf(\"Y\"); else

printf(\"N\"); }

[题目1050:寻找字符串]

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

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

(如果第二个字符串在第一个字符串中出现多次,则以最前出现的为准) [第一组自测数据] [键盘输入]

24 / 36

ABCDEFG↙ DE↙

[正确输出] 4

[第二组自测数据] [键盘输入] hellhello!↙ hello↙ [正确输出] 5 23

[提示]方法1:建立双重循环,外层循环变量指示第一个串的查找起始位置,内层循环从起始位置开始判

断第二个字符中是否出现在此处;方法2:使用字符串函数strstr()。 参考程序:

#include \"stdio.h\" main() { int i,j;

char a[80], b[80]; gets(a); gets(b);

for(i=0;a[i]!='\\0';i++) { for(j=0;b[j]!='\\0';j++) if(a[i+j]!=b[j]) break; if(b[j]=='\\0') break; }

if (a[i]!='\\0') printf(\"%d\else printf(\"Not Found\"); } 24

实验9 函数的基本应用 一、实验目的

(1)掌握定义函数的方法。

(2)掌握函数实参与形参的对应关系,以及“值传递”的方式。 (3)掌握函数的嵌套调用和递归调用的方法。

(4)掌握全局变量和局部变量、动态变量、静态变量的概念和使用方法。 二、实验内容

[题目1059:函数定义]

下面是使用辗转相除法,求最大公约数的程序,请补充完整程序中函数的定义与调用。 #include \"stdio.h\" int f(int m, int n) { int r;

while ((r=m%n)!=0) {

25 / 36

m=n; n=r; }

return n; }

main() {

int a, b, n;

scanf(\"%d%d\printf(\"%d\\n\}

[题目1083:编写函数计算阶乘]

下面程序实现由键盘读入整数n,计算并输出n!,请补充完整计算阶乘的函数。 #include \"stdio.h\" long fanc(int a) { long i,n=1;

for(i=1;i<=a;i++) n=n*i; return n; }

main() { int n; 25

scanf(\"%d\printf(\"%ld\}

[题目1124:函数中的变量] 写出下面程序的运行结果: 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\}

程序运行结果为: 6 8 10

*[题目1084:编写递归调用函数,实现十进制数转二进制数]

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

26 / 36

#include \"stdio.h\" void fun( int i ) { if (i>1) fun(i/2) ;

printf(\"%d\}

main() { int n;

scanf(\"%d\fun(n) ; } 26

实验10 指针与结构体 一、实验目的

(1)通过实验进一步掌握指针的概念,会定义和使用指针变量。 (2)能正确使用数组的指针和指向数组的指针变量。 (3)能正确使用字符串的指针和指向字符中的指针变量。 (4)掌握结构体类型变量的定义和使用。 二、实验内容

[题目1091:交换两数,由大到小输出]

下面程序,交换两数,使两数由大到小输出,请填空 #include \"stdio.h\"

void swap( int *p1, int *p2 ) { int temp; temp=*p1; *p1=*p2; *p2=temp; }

int main()

{ int a,b; int *pa,*pb; scanf(\"%d%d\pa=&a; pb=&b;

if(a[题目1065:数组中的指针] 设有如下数组定义:

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

计算下面各项的值(设数组a 的首位置为2000,一个int 类型数占四个字节)。注意:位置则输出位置,

变量则输出变量值;用printf 命令输出答案,要求一行一个答案,不允许多余空行及空格。 (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)

27 / 36

27

参考程序:

#include \"stdio.h\" main()

{ printf(\"19\\n\"); printf(\"2016\\n\"); printf(\"2000\\n\"); printf(\"2016\\n\"); printf(\"2004\\n\"); printf(\"2016\\n\"); printf(\"2036\\n\"); printf(\"2020\\n\"); printf(\"21\"); }

[题目1092:函数实现求字符串长度]

下面程序以指针方式传递参数,由函数实现求字符串长度,请填空完成 #include \"stdio.h\" #include \"string.h\" int f(char *p) { return strlen(p); }

int main() { char s[80]; int i;

scanf(\"%s\i=f(s);

printf(\"%d\}

[题目1125:定义结构体类型]

要求定义一个名为student 的结构体类型,其包含如下成员: (1)字符数组name,最多可存放10 个字符; (2)字符变量sex,用于记录性别;

(3)整数类型变量num,用于记录学号; (4)float 类型变量score,用于记录成绩; 并使下列代码完整。 /*定义结构体类型*/ struct student { char name[20]; char sex; int num; float score; } 28 main()

28 / 36

{

struct student stu;

scanf(\"%s\scanf(\"%*c%c\scanf(\"%d\scanf(\"%f\printf(\"%s\\n\printf(\"%c\\n\printf(\"%d\\n\printf(\"%f\\n\} 29

*实验11 链表 一、实验目的

(1)理解链表的概念。

(2)掌握结构体、指针在链表中的运用。

(3)掌握链表的常用操作,包括创建、显示、添加等。 二、实验内容

[题目1098:链表结点的插入] 有结构体类型定义, struct student

{ long num; /*学号*/ int score; /*成绩*/

struct student *next; /*指针*/ };

程序首先完成创建两个链表,要求补充完成按学号顺序插入链表结点的函数: struct student *insert(struct student *head, struct student *stud) {

struct student *p0,*p1,*p2; p1=head; p0=stud;

if(head==NULL){head=p0;p0->next=NULL;} 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;p0->next=NULL;} }

return(head);

29 / 36

}

[题目1099:链表的合并] 有结构体类型定义, struct student

{ long num; /*学号*/ int score; /*成绩*/

struct student *next; /*指针*/ };

程序首先完成创建两个链表,要求补充完成实现将第二个链表合并到第一个链表未尾的函数。

struct student *merge(struct student *head, struct student *head2) {

struct student *p1; p1=head;

while(p1->next!=NULL)p1=p1->next; p1->next=head2; return(head); }

[题目1104:链表的倒序] 有结构体类型定义, struct student

{ long num; /*学号*/ int score; /*成绩*/

struct student *next; /*指针*/ };

程序首先完成程序创建一个链表,要求补充完成实现将链表中各结点变为倒序排列的函数。 struct student *reverse(struct student *head) {

struct student *p1,*p2,*p3; p2=head;p3=head->next; do

{ p1=p2;p2=p3;p3=p2->next;p2->next=p1; }

while(p3!=NULL); head->next=NULL; return(p2); }

[题目1101:链表的排序] 有结构体类型定义, struct student

{ long num; /*学号*/ int score; /*成绩*/

struct student *next; /*指针*/ };

30 / 36

程序首先完成程序创建一个链表,要求补充完成实现将链表中各结点按学号由小到大排序的函数。

struct student *sort(struct student *head) {

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); }

*实验12 文件 一、实验目的

(1)学会使用文件打开、关闭、读、写等文件操作函数。 二、实验内容

[题目1105:文本文件操作_字符读入]

在当前目录中存在文件名为\"data1.in\"的文本文件,现要求你使用fopen 函数命令打开该文件,读出

里面的所有字符,遇到大写字母的,将其变为小写字母,其它字符不变,最后将所有字符按顺序在屏幕

上输出。请填空完成程序 (如data1.in 内容如下) Hello my Dear: Have a GooD Time!

(在屏幕上输出结果如下) hello my dear: have a good time! 程序如下,请填空, #include \"stdio.h\" main()

{ FILE *fp; char ch;

if(( fp=fopen(\"data1.in\return 0;

while( (ch=fgetc(fp))!=EOF ) { if ('A'<=ch && ch<='Z')

31 / 36

ch = ch + 32; putchar(ch) ; }

fclose(fp); }

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

件,在文件中自己打入一些字母,以便测试自己的代码是否正确 [题目1106:文本文件操作_字符写入]

由键盘输入任意个字符(以连着的三个小写字符bye 做为结束标志),将所有字符(包括bye),写

入新建的文件answer.txt 中(注:文件放在当前目录)。请完成该功能, (如键盘输入内容如下) He, can you write the code? Yes, you can.bye

(程序执行后,在文件answer.txt 中内容如下) He, can you write the code? Yes, you can.bye 参考程序:

#include \"stdio.h\" main()

{ FILE *fp;

char ch, ch1=' ', ch2=' ', ch3=' ';

if((fp=fopen(\"answer.txt\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); }

[题目1107:文本文件操作_单词的排序]

在当前目录有文件“data1.in”,文件里存放有多个(总个数不超过10000 个)英文单词(每个英文单

词不会超过10 个字文字符),每行一个,单词未排序。现要求,将文件中的所有单词按字典顺序排序,

然后将排序好的单词写入新建的文件answer.txt 中(注:文件存放于当前目录)。请完成程序,实现该功 能,

(如data1.in 文件中原内容如下) hello bye

32 / 36

yes

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

参考程序:

#include \"stdio.h\" #include \"string.h\" main()

{ FILE *fp1,*fp2;

char str[1000][11],str1[11]; int n=0,i,j;

if((fp1=fopen(\"data1.in\return 0;

if((fp2=fopen(\"answer.txt\return 0;

while(fscanf(fp1,\"%s\for(i=0;iif(strcmp(str[i],str[j])>0) {

strcpy(str1,str[i]); strcpy(str[i],str[j]); strcpy(str[j],str1); }; }

for(i=0;ifprintf(fp2,\"%s\\n\fclose(fp1); fclose(fp2); } 完

#include //杨辉三角 #define N 6 void main() { int a[N][N]; int i=0,j=0;

for(i=0;ia[i][j]=a[i-1][j-1]+a[i-1][j];

33 / 36

for(i=0;i{ for(j=0;j<=i;j++)printf(\"%d\ printf(\"\\n\"); }

(2)

#include int main() { int i,j,n,a[255]={1,1},b[255]={1},flag=0; printf(\"请输入一个小于30的正整数n...\\n\"); n_input: scanf(\"%d\ if(n>=30||n<1) { printf(\"输入错误,请重新输入!\\n\"); goto n_input; } for(i=1;i<=n;i++) { for(j=0;j杨辉三角

#include int main() { int i,j,n,m,a[255]={1,1},b[255]={1},flag=0;

n_input: scanf(\"%d\ n=m-1;

34 / 36

} printf(\"1\\n\"); if(n>=30||n<1){ printf(\"输入错误,请重新输入!\\n\"); goto n_input; } for(i=1;i<=n;i++) { for(j=0;j#include \"stdio.h\" #include \"string.h\" int main() { int n; char a[80] ; gets(a); int i,j,flag; n=strlen(a);

i=0; j=n-1; flag=0; // characters qty need inserted while ( (flag<2) // only need 0 or 1 character && ((i+1)<=(j-1)) // have character to compare ) { if (a[i]==a[j]) {i++; j--;}

else if (a[i]==a[j-1]) {flag++; i++; j-=2;} else if (a[i+1]==a[j]) {flag++; i+=2; j--;} else flag=2; }

if (flag<2) printf(\"Y\"); else printf(\"N\"); return 0; }

函数中的变量

\\x20\\x20\\x20\\x36\\x20\\x20\\x20\\x38\\x20\\x20\\x31\\x30

35 / 36

<9>

#include int G(int x) {

if(x < 2 || x%2!=0) return x;

return G(x/2)+1; }

int F(int x) {

if(x < 2) return x;

if(x>= 2 && x%2==0) return G(x/2)*2;

if(x>= 2 && x%2!=0) return G( (x-1)/2 ); }

void main() {

int x;

scanf(\"%d\printf(\"%d\\n\}

友情提示:部分文档来自网络整理,供您参考!文档可复制、编制,期待您的好评与关注!

36 / 36

因篇幅问题不能全部显示,请点此查看更多更全内容