华南农业大学c语言程序设计上机指导书参考答案
上级实验参考答案#include "stdio.h"
#include"math.h"
int main()
{ int i;
double a, b;
for(i=0;i<=100000;++i)
{a=sqrt(i+100);
b=sqrt(i+168);
if((int)a==ceil(a)&&(int)b==ceil(b))
printf("%d\n",i);}
}
实验2 基本数据类型、运算和表达式
一、实验目的
1)掌握C 语言数据类型,熟悉如何定义一个整型和实型的变量,以及对它们赋值的方法。
2)掌握不同的类型数据之间赋值的规 。
3)学会使用C 的有关算术运算符,以及包含这些运算符的表达式,特别是自加 ++ )和自减 --)运
算符的使用。
4 )进一步熟悉C 程序的编辑、编译、连接和运行的过程。
二、实验内容
1. 变量的定义
[题目1117:变量定义,按要求完成程序]
下面给出一个可以运行的程序,但是缺少部分语句,请按右边的提示补充完整缺少的语句。
void 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", a, b, i,j);
}
[具体操作步骤]
1)将代码补充完整;
2)在TC 或VC++上运行通过;
3)在JudgeOnline 实验系统上提交;
2. 赋值表达式与赋值语句
[题目1118:赋值表达式与赋值语句,写出程序运行结果]
阅读下面程序,写出运行结果:
void main()
{ float a;
int b, c;
char d, e;
a=3.5;
b=a;
c=330;
d=c;
e='\\';
printf("%f,%d,%d,%c,%c", a,b,c,d,e);
}
运行结果为:
3.500000,3,330,J,\
[提示]赋值语句具有类型转换的功能,但可能会改变数据。
3. 基本运算
[题目1119:基本运算,写出程序运行结果]
阅读下面程序,写出运行结果:
void 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", a,b,c,d,e,f);
}
运行结果为:
0,2,1,15.000000,1.000000,1.500000
[提示]除法分整除与普通除法之分。
实验3 基本输入与输出
一、实验目的
1)熟练掌握putchar、getchar、printf、scanf 函数的使用方法。
2)掌握各种类型数据的输入输出的方法,能正确使用各种格式转换符。
二、实验内容
[题目1126:字符的输入与输出]
编程实现由键盘输入一个字符后,在屏幕上输出该字符。
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
a↙ +↙
[正确输出] [正确输出]
a +
参考程序:
#include "stdio.h"
main()
{ char ch;
ch = getchar();
putchar(ch);
}
[题目1127:计算加法]
编程实现由键盘输入一个加法式,输出正确的结果。两个加数均为整数)
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
10+20↙ -15+60↙
[正确输出] [正确输出]
30 45
参考程序:
#include "stdio.h"
main()
{ int a, b;
scanf("%d%*c%d", &a,&b);
printf("%d", a+b);
}
[题目1014:求圆面积]
由键盘输入圆半径r,请计算该圆的面积。注: 取3.14159,结果保留两位小数位;另外,程序只
要能对r 在0 到10000 范围的情况输出正确答案即可)
[第一组自测数据] [键盘输入]
65.2↙ [键盘输入]
[正确输出] 11.2↙
13355.02 [正确输出]
[第二组自测数据] 394.08
[提示]结果保留两位小数可采用printf 函数的格式控制字符来实现。
参考程序:
#include "stdio.h"
main()
{ float area,r;
scanf("%f",&r);
area=3.14159*r*r;
printf("%0.2f",area);
}
[题目1015:计算摄氏温度值]
从键盘输入一个华氏温度值,要求按格式输出其对应的摄氏温度值,精确到小数点后两位。
数学公式描述为:
5
C = (F - 32)
9
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
100↙ 100↙
[正确输出] [正确输出]
37.78 37.78
[提示]注意公式中的除为普通除法。
参考程序:
#include<stdio.h>
void main()
{ float f,c;
scanf("%f",&f);
c=5.0/9*(f-32);
printf("%.2f",c);
}
实验4 选择结构程序设计
一、实验目的
1)了解C 语言表示逻辑的方法 以0 代表“假”,以非0 代表“真”)。
2)学会正确使用逻辑运算符和逻辑表达式。
3)熟练掌握if 语句和switch 语句。
4 )结合程序掌握一些简单的算法。
二、实验内容
[题目1120:判断点是否在圆 ]
由键盘输入一个点的坐标, 要求编程判断这个点是否在单位圆上,点在圆上输出Y, 不在圆上输出
N 。使用小数点后3 位精度进行判断。
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
0.707,0.707↙ 0.5,0.5↙
[正确输出] [正确输出]
Y N
[提示] 1)平面上的点与圆的关系分为在圆内、在圆上、在圆外三种,本题要求判断是否在圆上;2)
判断两实数相等采用判断这两实数的差的绝对值小于规定误差精度 (本题为0.001)的方法实现。
参考程序:
#include "stdio.h"
#include "math.h"
void main()
{ float a,b;
scanf("%f,%f",&a,&b);
if(fabs(a*a+b*b-1)<1e-3)
printf("Y\n");
else printf("N\n");}
}
[题目1017:求数的位数]
由键盘输入一个不多于9 位的正整数,要求输出它是几位数。
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
349213↙ 10000↙
[正确输出] [正确输出]
6 5
[提示]划定一位数、二位数、…、九位数的范围, 后用if …else if …else 语句实现判断。
参考程序:
main()
{ int n,place;
scanf("%ld",&n);
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",place);
}
[题目1018:数的排序]
由键盘输入三个整数a、b、c,按从小到大的顺序输出这三个数。
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
65,45,90↙ 9,6,3↙
[正确输出] [正确输出]
45,65,90 3,6,9
参考程序:
#include<stdio.h>
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);
}
[题目1016:字符变换]
由键盘输入5 个字符,将其中的大写字母变成小写,其它类型的字符不变,并按输入顺序逐个输出。
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
ApPLe↙ a+B=5 ↙
[正确输出] [正确输出]
apple a+b=5
[提示]下面代码实现由键盘读入一个字符,并按题意处理后输出
char a;
a=getchar();
if(a>='A' && a<='Z') a=a+32;
putchar(a);
现在,题目要求处理5 个字符,怎么做呢?请自己思考……
参考程序:
#include <stdio.h>
main()
{ char a,b,c,d,e;
scanf("%c%c%c%c%c",&a,&b,&c,&d,&e);
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",a,b,c,d,e);
}
[题目1019:数的整除]
由键盘输入5 个整数,逐个判断它们能否被27 整除,能的输出“YES”,不能的输出“NO” 注意,
输出时,一个判断结果占一行,5 个数的判断共占5 行)。
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
8 27 17577 325 54↙ 8 27 17577 325 54↙
[正确输出] [正确输出]
NO NO
YES YES
YES YES
NO NO
YES YES
[提示]整除即除得余数为0
参考程序:
#include "stdio.h"
main()
{ int a,b,c,d,e;
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
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");
}
#include "stdio.h"
void main()
{int a,i;
for(i=0;i<6;i++)
{scanf("%d",&a);
if(a%27==0)
printf("YES");
else
printf("NO");
printf("\n"); }
[题目1020:正负奇偶判断]
由键盘输入非零整数x,判断该数正负,正数输出positive,负数输出negative,接着判断该数的奇
偶性,奇数输出odd,偶数输出even。
[第一组自测数据] negative
[键盘输入] odd
-43 ↙ [第二组自测数据]
[正确输出] [键盘输入]
98↙ positive
[正确输出] even
参考程序:
#include "stdio.h"
main()
{ int n;
scanf("%d",&n);
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"
void main()
{ float a,b,c;
char op;
scanf("%f%c%f", );
switch (op)
{ case '+': ;
case '-': _;
case '*': _;
case '/': __ ;
default: printf("error");
return ;
}
printf("result= ", c);
}
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
45*2↙ 50.1-23↙
[正确输出] [正确输出]
90 27.10
参考程序:
#include <stdio.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");
break;
}
printf("result=%.2f",c);
} 、
实验5 循环结构程序设计(一)
一、实验目的
1)熟悉掌握用while 语句、do-while 语句和for 语句实现循环的方法。
2)掌握在程序设计中用循环实现一些常用算法 (如穷举、迭代、递推等)。
二、实验内容
[题目1024:计算阶乘]
输入正整数n,计算n!,结果用长整型数表示 注n!=1*2*3*...*n)
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
5↙ 8↙
[正确输出] [正确输出]
120 40320
参考程序:
#include<stdio.h>
main()
{ long i,n=1,a;
scanf("%ld",&a);
for(i=1;i<=a;i++) n=n*i;
printf("%ld\n",n);
}
[题目1025:计算数列和]
有数列1,3,5,7,9,11,……
现要求由键盘输入n,计算输出该数列的前n 项和。
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
2↙ 5↙
[正确输出] [正确输出]
4 25
参考程序:
#include<stdio.h>
main()
{ long n,sum=0,i,t=1;
scanf("%ld",&n);
for(i=1;i<=n;i++)
{ sum=sum+t;
t=t+2;
}
printf("%ld\n",sum);
}
或#include <stdio.h>
main()
{ long n,sum;
scanf("%ld",&n);
sum=n*n;
printf("%ld",sum);
}
注:评判系统不对程序实现细节进行分析,只对运行结果进行评测。
[题目1026: 加一行字符中的数字]
由键盘输入一行字符 总字符个数从1 个至80 个均有可能,以回车符表示结束),将其中每个数字
字符所代表的数值累加起来,输出结果。
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
abc123↙ A6H7T+65↙
[正确输出] [正确输出]
6 24
[提示] 1)可以使用下面程序段逐个读入键盘输入的一行字符
char ch;
while((ch=getchar())!='\n')
{……}
2)数字字符转为对应的数值可用a=ch-'0'
参考程序:
#include<stdio.h>
main()
{ char c;
int s=0,a;
while((c=getchar())!='\n')
{ if(c>='0'&&c<='9')
{ a=c-48;
s=s+a;
}
}
printf("%d",s);
}
[题目1029:求最大公约数]
由键盘输入两个正整数m、n m、n 用长整数表示),计算它们的最大公约数。
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
16,24↙ 17,25↙
[正确输出] [正确输出]
8 1
[提示]公约数是既能整除m 又能整除n 的数,题目要求满足这一条件的最大的一个。
参考程序:
#include<stdio.h>
main()
{ long r,m,n,temp;
scanf("%ld,%ld",&m,&n);
while(m!=0)
{ r=n%m;
n=m;
m=r;
}
printf("%ld\n",n);
}
或
#include<stdio.h>
main()
{ long m,n,t,i,s;
scanf("%ld,%ld",&m,&n);
t=m>n?n:m;
for (i=t-1;i>=1;i--)
{ if (m%i==0&&n%i==0)
{ s=i; break; }
}
printf("%d",s);
}
[题目1030:字符变换]
由键盘输入一个句子 总字符个数从1 个至80 个均有可能,以回车符表示结束),将其中的大写字
符变成小写 其它类型的字符不变),最后输出变换后的句子。
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
How Are You? ↙ ThiS IS My fIrSt C ProgrAm!↙
[正确输出] [正确输出]
how are you? this is my first c program!
参考程序:
#include <stdio.h>
main()
{ char c;
while((c=getchar())!='\n')
{ if(c>='A'&&c<='Z')
c=c+32;
putchar(c);
}
}
[题目1037:计算数列和]
有数列:
编程实现,由键盘输入n,计算输出数列前n 项和。(结果保留四位小数)
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
20↙ 30↙
[正确输出] [正确输出]
32.6603 88.0403
参考程序:
#include<stdio.h>
main()
{ int i,t,n;
float a=2,b=1,s=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{ s=s+a/b;
t=a;a=a+b;b=t;
}
printf("%.4f\n",s);
}
[题目1044:输出最小值]
从键盘输入十个整数,输出最小值
[ 自测数据]
[键盘输入]
12 45 76 87 5 87 43 55 99 21↙
[正确输出]
5
参考程序:
#include "stdio.h"
main()
{ int i,t,min;
scanf("%d", &min);
for(i=1;i<10;i++)
{
scanf("%d", &t);
if(t<min) min=t;
}
printf("%d\n",min);
}
*[题目1031:统计单词个数]
由键盘输入一个句子 总字符个数从1 个至80 个均有可能,以回车符表示结束),以空格分割单词,
要求输出单词的个数。
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
How Are You? ↙ There are many students and many
[正确输出] trees!↙
3 [正确输出]
7
参考程序:
#include<stdio.h>
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",num);
}
*[题目1042:百万富翁]
一个百万富翁遇到一个陌生人,陌生人找他谈了一个换钱的计划。该计划如下:我每天给你m 元,
而你第一天只需给我一分钱。第二天我仍给你m 元,你给我2 分钱。第三天,我仍给你m 元,你给我4
分钱。依次类推,你每天给我的钱是前一天的两倍,直到一个月(38)天。百万富翁很高兴,欣 接受这
个契约。现要求,编写一个程序,由键盘输入m,计算多少天后,百万富翁开始亏钱。
[第一组自测数据] 18
[键盘输入] [第二组自测数据]
100↙ [键盘输入]
[正确输出] 10000↙
[正确输出] 25
参考程序:
#include <stdio.h>
#include <math.h>
main()
{ int n,m,i;
scanf ("%d",&m);
for (i=1;i<=38;i++)
if (0.01*(pow(2,i-1)-1)-i*m>=0) break;
printf("%d",i-1);
}
实验6 循环结构程序设计(二)
一、实验目的
1)进一步熟悉掌握用while 语句、do-while 语句和for 语句实现循环的方法。
2)掌握在程序设计中使用多重循环。
二、实验内容
[题目1028:求素数]
输出2 到200 之间 包括2、200)的所有素数 注:要求1 行1 个素数,按由小到大的顺序输出)。
[提示]采用双重循环,外层循环产生2 到200 之间的数,内层循环对数进行判断是否为素数。
参考程序:
# include<stdio.h>
# include<math.h>
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",m);
}
}
[题目1035:打印菱形]
由键盘输入正数n,要求输出2*n+1 行的菱形图案。要求菱形左边紧靠屏幕左边。
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
3↙ 2↙
[正确输出] [正确输出]
* *
*** ***
***** *****
******* ***
***** *
***
*
参考程序:
#include "stdio.h"
#include "math.h"
main()
{ int n,i,j,k;
scanf("%d",&n);
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");
}
}
[题目1038:打印图案]
由键盘输入正数n,要求输出中间数字为n 的菱形图案。要求菱形左边紧靠屏幕左边。
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
4 ↙ 3
[正确输出] [正确输出]
1 1
121 121
12321 12321
1234321 121
12321 1
121
1
参考程序:
# include<stdio.h>
# include<math.h>
main()
{ int n,i,j,k,h;
scanf("%d",&n);
for(i=-n+1;i<=n-1;i++)
{ for(j=0;j<abs(i);j++)
printf(" ");
for(k=1;k<=n-abs(i);k++) printf("%d",k);
for(h=n-abs(i)-1;h>=1;h--) printf("%d",h);
printf("\n");
}
}
实验7 数组
一、实验目的
1)掌握一维数组和二维数组的定义、赋值和输入输出方法。
2)掌握与数组有关的算法。
二、实验内容
[题目1039:倒序]
由键盘输入10 个整数,倒序输出。数字间由一个空格分隔)
[ 自测数据]
[键盘输入]
70 5 14 20 19 2 99 67 13 66↙
[正确输出]
66 13 67 99 2 19 20 14 5 70
参考程序:
#include<math.h>
main()
{ int a[10];
int i;
for(i=0;i<10;i++) scanf("%d",&a[i]);
for(i=9;i>=0;i--) printf("%d\n",a[i]);
}
[题目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"
main()
{ int a[20];
int i,t,p=0;
for(i=0;i<20;i++)
{ scanf("%d",&a[i]);
for(t=0;t<i;t++)
if(a[t]==a[i])break;
if(t==i)
p++;
}
printf("%d",p);
}
[题目1062:打印矩阵]
由键盘输入一个3×4 的矩阵,要求输出它的转置矩阵。
[ 自测数据]
[键盘输入] [正确输出]
1 6 9 3 1 1 1
1 1 0 2 6 1 9
1 9 8 9 9 0 8
3 2 9
参考程序:
#include<stdio.h>
main()
{ int a[3][4],b[4][3],i,j;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
{ scanf("%d",&a[i][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("\n");
}
}
*[题目1047:冒泡排序]
由键盘输入10 个数,用“冒泡法”对10 个数从小到大排序,并按格式要求输出。代码如下,请填充完
整。数字间由一个空格分隔。
#incude "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]);
}
[ 自测数据]
[键盘输入]
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];
int i,j,t;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
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 ",a[i]);
}
*[题目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);
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",c[max]);
for(i=max-1;i>=0;i--)
printf("%d",c[i]);
}
*[题目:找矩阵中的鞍点]
由键盘输入一个3 ×4 3 行4 列)的矩阵,输出矩阵中的鞍点 即在矩阵行中最大,列中最小的数)。
若没有鞍点,输出“NO ”字样。
[ 自测数据]
[键盘输入]
87 90 110 98↙
70 97 210 65↙
98 45 120 30↙
[正确输出]
110
参考程序:
#include <stdio.h>
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",&a[i][j]);
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",max);break;}
}
if(!flag)
printf("NO");
}
实验8 字符数组的应用
一、实验目的
1)掌握字符数组和字符串函数的使用。
2)掌握与字符串处理有关的算法。
二、实验内容
[题目1121:定义存贮字符串的字符数组]
在下面程序中填充定义字符数组的语句,使程序完整。
#include "string.h"
void main()
{ char s[80]; /*定义字符数组s*/
strcpy(s, "abcdefghijklmn");
printf("%s", s);
}
参考程序:
[题目1123:字符串的输入与输出]
下面程序实现从键盘读入字符串, 后输出到屏幕,请填充必要的语句。
#include "stdio.h"
void main()
{
char s[50];
printf("What's your name?");
gets(s); /* 由键盘读入字符串*/
printf("Your name is ", );
printf("%s", s); /*打印字符串*/
}
参考程序:
[题目1122:字符串的合并]
从键盘输入3 个字符串 每个字符串以回车符做为结束标志),将3 个字符串以输入先后顺序合并到
字符串s 中,请填空使用程序完整。
#include "stdio.h"
#include "string.h"
main()
{
char s[100]="";
char a[30];
gets(a); strcat(s, a);
gets(a); strcat(s, a);
gets(a); strcat(s, a);
/*可以写多行代码*/
printf("%s", s);
}
[ 自测数据]
[键盘输入] [正确输出]
123 123abc456
abc
456
参考程序:
[题目1050:寻找字符串]
由键盘输入两个字符串 假设第一个字符串必包含第二个字符串,如第一个字符串为ABCDEF,第
二个为CDE,则CDE 包含在ABCDEF 中),现要求编程输出第二字符串在第一行字符串中出现的位置。
(如果第二个字符串在第一个字符串中出现多次,则以最前出现的为准)
[第一组自测数据] [第二组自测数据]
[键盘输入] [键盘输入]
ABCDEFG ↙ hellhello!↙
DE ↙ hello↙
[正确输出] [正确输出]
4 5
[提示]方法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",i+1);
else printf("Not Found");
}
实验9 函数的基本应用
一、实验目的
1)掌握定义函数的方法。
2)掌握函数实参与 参的对应关系,以及“值传递”的方式。
3)掌握函数的嵌套调用和递归调用的方法。
4 )掌握全局变量和局部变量、动态变量、静态变量的概念和使用方法。
二、实验内容
[题目1059:函数定义]
下面是使用辗转相除法,求最大公约数的程序,请补充完整程序中函数的定义与调用。
#include "stdio.h"
int f(int m, int n)
{
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", f(a, b) );
}
[题目1083:编写函数计算阶乘]
下面程序实现由键盘读入整数n,计算并输出n !,请补充完整计算阶乘的函数。
long fanc(int a)
{ long i,n=1;
for(i=1;i<=a;i++) n=n*i;
return n;
}
void main()
{ int n;
scanf("%d", &n);
printf("%ld", fanc(n));
}
[题目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",f1(a));
}
程序运行结果为:
6 8 10
*[题目1084:编写递归调用函数,实现十进制数转二进制数]
下面程序,实现由键盘输入一个正整数(不大于100000000),输出其对应的二进制数 原码表示)。
请填空:
#include "stdio.h"
void fun( int i )
{ if (i>1)
fun(i/2) ;
printf("%d", i%2);
}
main()
{ int n;
scanf("%d", &n);
fun(n) ;
}
实验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", &a, &b);
pa=&a; pb=&b;
if(a<b) swap( pa, pb );
printf("%d %d\n",a,b);
}
[题目1065:数组中的指针]
设有如下数组定义:
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"
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", s);
i=f(s);
printf("%d", i);
}
[题目1125:定义结构体类型]
要求定义一个名为student 的结构体类型,其包含如下成员:
1)字符数组name,最多可存放10 个字符;
2)字符变量sex,用于记录性别;
3)整数类型变量num,用于记录学号;
4 )float 类型变量score,用于记录成绩;
并使下列代码完整。
/*定义结构体类型*/
struct student
{ char name[20];
char sex;
int num;
float score;
}
main()
{
struct student stu;
scanf("%s", stu.name);
scanf("%*c%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);
}
*实验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);
}
[题目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; /*指针*/
};
程序首先完成程序创建一个链表,要求补充完成实现将链表中各结点按学号由小到大排序的函数。
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","r"))= =NULL)
return 0;
while((ch=fgetc(fp))!=EOF )
{ if ('A'<=ch && ch<='Z')
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", "w"))==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);
}
[题目1107:文本文件操作_单词的排序]
在当前目录有文件“data1.in”,文件里存放有多个 总个数不超过10000 个)英文单词 每个英文单
词不会超过10 个字文字符),每行一个,单词未排序。现要求,将文件中的所有单词按字典顺序排序,
后将排序好的单词写入新建的文件answer.txt 中 注:文件存放于当前目录)。请完成程序,实现该功
能,
(如data1.in 文件中原内容如下)
hello
bye
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","r"))==NULL)
return 0;
if((fp2=fopen("answer.txt","w"))==NULL)
return 0;
while(fscanf(fp1,"%s",str[n])>0)
n++;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(str1,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],str1);
};
}
for(i=0;i<n;i++)
fprintf(fp2,"%s\n",str[i]);
fclose(fp1);
fclose(fp2);
}