c语言程序设计教程(第2版)课后题及模拟题参考答案

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

c语言程序设计教程(第2版)课后题及模拟题参考答案
习题1 (4)
1-1 填空题 (4)
1-2 思考题 (4)
1-3 编程题 (5)
习题2 (6)
2-1 单选题 (6)
2-2 思考题 (6)
习题3 (7)
3-1 选择题 (7)
3-2 填空题 (7)
3-3 编程题 (8)
习题4 (11)
4-1单选题 (11)
4-2填空题 (11)
4-3 编程题 (11)
习题5 (16)
5-1单选题 (16)
5-2填空题 (16)
5-3 编程题 (16)
习题6 (22)
6-1单选题 (22)
6-2填空题 (22)
习题7 (25)
7-1单选题 (25)
7-2填空题 (25)
7-3 编程题 (25)
习题8 (26)
8-1单选题 (26)
8-2填空题 (26)
8-3 编程题 (26)
习题9 (30)
9-1单选题 (30)
9-2填空题 (30)
9-3 编程题 (30)
习题10 (38)
10-1单选题 (38)
10-2填空题 (38)
10-3 编程题 (38)
习题11 (41)
11-1单选题 (41)
11-2填空题 (41)
习题12 (42)
12-1单选题 (42)
12-2 填空题 (42)
实验1 熟悉Visual C++6.0可视化集成开发环境 (43)
实验2 顺序结构程序设计 (43)
实验3 选择结构程序设计 (43)
实验4 循环结构程序设计 (44)
实验5 函数 (47)
实验6 数组 (54)
实验7 指针 (58)
实验8 结构体和共用体 (61)
实验9 文件 (63)
实验10 综合编程 (64)
模拟试卷(一)参考答案 (65)
模拟试卷(二)参考答案 (67)
习题1
1-1 填空题
1. 函数
2. 主函数main(),主函数main()
3. 主函数main()
4. 函数首部,函数体
5. {, }
6. /*, */
7. 顺序结构,选择结构,循环结构
8. .c, .obj, .exe
1-2 思考题
1. 结构化程序设计是指:为使程序具有一个合理的结构以保证程序正确性而规定的一套如何进行程序设计的原则。

其基本结构包括顺序结构、选择结构和循环结构三种。

2. 算法是对具体问题求解步骤的一种描述。

计算机算法的表达工具通常采用以下几种方法:(1)用自然语言表示算(2)用流程图表示算法(3)用伪代码表示算法(4)用程序设计语言表示算法
3. 语言简洁、紧凑,使用方便、灵活; 支持结构化程序设计;运算符丰富;数据类型丰富;较强的编译预处理功能;C语言的可移植性好;C语言本身既有一般高级语言的优点,又有低级(汇编)语言的特点;语法限制不太严格,程序设计自由度大。

1-3 编程题
1. 试参照本章例题编写计算梯形面积的C语言程序,梯形的上底、下底和高分别用a,b,h表示,并用a=10,b=20,h=5测试所编写的程序。

#include "stdio.h"
main()
{ float a, b, h, s;
a=10; b=20;
h=5; s=(a+b)*h/2;
printf("s=%f\n" , s );
}
2. 编写程序显示如图1.11所示信息。

图1.11 显示信息
#include "stdio.h"
main()
{ printf("******************************\n");
printf("* hello world *\n");
printf("******************************\n"); }
2-1 单选题
1~5 DBDCA 6~10 DCABA 11~12 CA 2-2 思考题
1.2.000000
2.1,0.5
3.9,2
4.6
5.100,d
6.(1)20 (2)0 (3)60
7. (1)10,6,4 (2)6,9,15 (3)3,60,83
8. 55
3-1 选择题
1-5 BDABC 6-10 ADCAC 11-12 BB 3-2 填空题
1. 3
2. 0261
3. 0x10
4. 2, 1 互换a,b的值
5. 6.6
6. –003
8. 7
7. 5.0,4,c=3<Enter>
8. i=10,j=20<Enter>
9. (1) 65
(2) 65,A
(3) 56.123400,123.456001
(4) 3.141600
(5) 8765.432
(6) 5.864000e+002
(7) 3.141600e+000
(8) 3.1416
(9) 8765
(10) 3.1416,8765.43
10. a=2 b=5x=8.8 y=76.34c1=65 c2=97
3-3 编程题
1. 编写程序实现从键盘输入两个十进制整型数据10和8给变量x和y,并按下列格式 输出。

x y
十进制数10 8
八进制数12 10
十六进制数 a 8
#include "stdio.h"
main()
{ int x, y ;
scanf("%d%d" , &x, &y);
printf(" x y\n");
printf("%4d%4d\n", x, y );
printf("%4o%4o\n", x, y );
printf("%4x%4x\n", x, y );
}
2. 编写一个程序,输入一个大写英文字符('B'~'Y'),输出它的前导字符、该字符本身及其后续字符。

#include "stdio.h"
main()
{ char c;
scanf(“%c”, &c );
printf(“%c, %c, %c\n”, c-1, c, c+1 );
}
3. 编写一个程序,输入一个3位正整数,要求反向输出对应的整数,如输入123,则输出321。

编写程序并给出相应的程序流程图。

#include "stdio.h"
main()
{ int a, a1, a2, a3;
scanf(“%d”, &a );
a1=a%10 ; a=a/10;
a2=a%10; a=a/10;
a3=a%10;
printf(“%d%d%d\n”, a1, a2, a3);
}
4. 编写程序,读入3个double型数据,求它们的平均值,保留此平均值小数点后1位数,对小数点后第2位数进行四舍五入,最后输出结果。

#include "stdio.h"
main()
{ double a, b, c, ave;
scanf(“%lf%lf%lf”, &a, &b, &c );
ave=(a+b+c)/3 ;
printf(“%8.2f\n”, ave);
}
5. 编写程序,读入3个整数给变量a、b、c,然后交换它们的值,把a 原来的值给b,把b原来的值给c,把c原来的值给a。

#include "stdio.h"
main()
{ int a, b, c, t;
scanf(“%d%d%d”, &a, &b, &c );
t=a; a=c; c=b ; b=t ;
printf(“a=%d, b=%d, c=%d\n”, a, b, c );
}
习题4
4-1单选题
1~5 AADAD 6~10 ACB BB 11~12BA
4-2填空题
1. 1
2. (1) a>0 || b>0 (2) x>0 && x<=10
(3) a==1.5 && b==1.5 && c==1.5 (4)p<a || p<b || p<c
3.(1)0 (2)1 (3)1 (4)0 (5)1
4. (max=a>b?a:b)>c?max:c
5.-4
6.1
7.5,0,3
8. (1) (a==0) (2) (b==0) (3) (disc<0)
4-3 编程题
1.输入3个实型数值a、b、c,如果能用它们作为三角形的3条边形成一个三角形,则输出三角形的面积,并画出实现该算法的N-S流程图并编程实现。

#include "stdio.h"
#include “math.h”
main()
{ float a,b,c,s,area;
scanf(“%f,%f,%f”,&a,&b,&c);
if (a+b>c && a+c>b && b+c>a)
{ s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(“%f”,area);
}
}
2.输入整数x、y,若x2+y2>1000,则输出x2+y2百位以上的数字,否则输出两数之和。

#include "stdio.h"
main()
{ int x,y;
scanf(“%d,%d”,&x,&y);
if (x*x+y*y>1000)
printf(“%d\n”,(x*x+y*y)/100);
else
printf(“%d\n”,x+y);
}
3. 输入一个整数,判断它能否同时被3、5、7整除,并输出“yes”或“no”字样。

#include "stdio.h"
main()
{ int x,
scanf(“%d ”,&x);
if(x%3==0 && x%5==0 && x%7==0)
printf(“yes\n ”);
else
printf(“no\n ”);
}
4.对任意输入的x ,用下式计算并输出y 的值。

2sin(),22,2≤≤2
2x x x x y x x x ⎧-<-⎪⎪=+-⎨>
#include "stdio.h"
#include “math.h ”
main()
{ float x,y;
scanf(“%f ”,&x);
if(x<-2) y=x*x-sin(x);
else if (x<=2) y=pow(2,x)+1;
else y=sqrt(x*x+x+1);
printf(“%f\n ”,y);
}
5.编写程序输入一个5位整数,判断它是不是回文数。

回文数是指一个数从右到左和从左到右的对应数码相同,如12321是回文数,个位与万位相同,十位与千位相同。

#include "stdio.h"
main( )
{long ge,shi,qian,wan,x;
scanf("%ld",&x);
wan=x/10000;
qian=x%10000/1000;
shi=x%100/10;
ge=x%10;
if (ge==wan&&shi==qian)/*个位等于万位并且十位等于千位*/
printf("this number is a huiwen\n");
else
printf("this number is not a huiwen\n");
}
6. 编写程序用于计算某运输公司的运费。

设每公里每吨货物的基本运费为p,货物重量为w,路程为s(单位为km),折扣为d,总费用计算公式为:f=p*w*s*(1d)
运费计算标准见表4.5。

表4.5 某运输公司运费计算标准
#include "stdio.h"
main()
{float p,w,s,d,f;
scanf(“%f,%,%f”,p,s,w); if (s>3000) d=0.15
else if( s>=2000) d=0.1; else if(s>=1000) d=0.08; else if(s>=500) d=0.05; else if(s>=250) d=0.02; else d=0
f=p*w*s*(1-d);
printf(“%f”,f);
}
习题5
5-1单选题
1~5 CDABA 6~10 ABDDB 11~14 DBCB 5-2填空题
1.20
2.333
3.(1) i<10 (2) j%3!=0
4. (1) flag*(float)k/(k+1) (2) flag=-flag 5.(1) max=x (2) x!=-1 (3) scanf("%d", &x) 5-3 编程题
1. 用3种循环语句编写程序实现下列算式:(1)1+2⨯2+3⨯3+…+100⨯100
(2)e=1111
……
1!2!3!!n
+++++
,当最后一项的值小于106时为止。

(1)
#include "stdio.h" main()
{ int i; lont s=0;
for(i=1;i<=100;i++)
s+=i*i;
printf(“%ld”,s);
(2)
#include "stdio.h"
main()
{ int i=1; lont p=1;float s=0;
do{
s+=1.0/p;
p*=++i;
}while(1.0/p>1e-6)
printf(“%f”,s);
}
2. 编写程序求两个正整数的最大公约数。

#include "stdio.h"
main()
{int m,n,t,a,b;
scanf(“%d,%d”,&m,&n);
if (m<n){ t=m;m=n;n=t;}
a=m;b=n;
t=m%n;
while(t)
{ m=n; n=t;t=m%n;}
printf(”%d,%d”,n,a*b/n);
3. 编写程序求一个整数的任意次方的最后3位数,即求x y的最后3位数。

#include "stdio.h"
main()
{int x,y,s=1;
scanf(”%d,%d”,&x,&y);
for(;y>0;y--)s*=x;
printf(”%d,%d,%d\n”,s%10,s/10%10,s/100%10);
}
4. 百鸡问题。

用100元钱买100只鸡,其中,公鸡每只5元,母鸡每只3元,小鸡每3只1元。

编写程序输出各种买法。

#include "stdio.h"
main()
{ int x,y,z;
for( x=1; x<20;x++)
for( y=1;y<33;y++)
{ z=100-x-y;
if (z>=0 && (5*x+3*y+z/3)-100<1e-5)
printf(”x=%d,y=%d,z=%d\n”,x,y,z);
}
5. 编写程序分别打印如下图形:
* ***
*****
********
(a)
(b) (a) #include "stdio.h"
main()
{int j,k ;
for( j=1 ;j<=4 ;j++)
{for(k=1;k<=4-j;k++)printf(" "); printf("****") ;
printf("\n") ;
}
}
(b) #include "stdio.h"
main()
{int j,k ;
for( j=1 ;j<=4 ;j++)
{for(k=1;k<=4-j;k++)printf(" "); for(k=1 ;k<=2*j-1 ;k++) * * * *
* * * *
* * * *
* * * *
printf("*") ;
printf("\n") ;
}
}
6. 请编写程序求100~999之间所有的水仙花数。

水仙花数的含义是指这样的一个3位数,其各位数字的立方和等于该数本身。

例如,371=33+73+13,所以371是一个水仙花数。

程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。

#include "stdio.h"
main()
{
int i,j,k,n;
printf("'water flower'number is:");
for(n=100;n<1000;n++)
{
i=n/100;/*分解出百位*/
j=n/10%10;/*分解出十位*/
k=n%10;/*分解出个位*/
if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
{
printf("%-5d",n);
} } printf("\n"); }
习题6
6-1单选题
1-5 CBBAD 6-10 DBCCD 11-15 DCABC
6-2填空题
1. 120
2. x
3 . 3,2,2,3
4. fac/i
5. 8,17
6. 9
7. 1.0/(i*i)
8. fun-in:30,20,10
fun-end:1015,35,1050
10,20,30
9. 012345
10. 93636
6-3 编程题
1. 编写一函数把字符串中的小写字母转换成大写字母,其他字符不变。

void zhuan( )
{ char ch;
while((ch=getchar())!=’\n’)
{ if(ch>=’a’&& ch<=’z’) ch=ch-32;
putchar(ch);
}
}
2. 编写一函数求e=1111
……1!2!3!!n
+++++
float expp(int n)
{ int k, fac=1;
float sum=0;
for(k=1; k<=n; k++)
{ fac*=k;
sum+=1.0/fac ;
}
return(sum);
}
3. 编写一函数求一个整数的任意次方的最后3位数,即求x y的最后3位数。

int xy3( int x, int y)
{ int k, num=1;
for(k=1;k<=y ; k++)
num*=x ;
num=num%1000 ;
return num ;
}
4. 有5个人坐在一起,第5个人说他比第4个人大2岁,第4个人说他比第3个人大2岁,第3个人说他比第2个人大2岁,第2个人说他比第1个人大2岁,第1个人说他10岁,请问第5个人多大?
int age( int n)
{ int c;
if(n==1) c=10 ;
else c=age(n-1)+2 ;
return c ;
}
7-1单选题
1-5 DBCCB 6-8 BDC
7-2填空题
1. c
2. 60
3. 1000 10
4. 16
7-3 编程题
1. 编写程序计算下列公式中的f值,使用带参数的宏来实现。

#include “math.h”
#define F(a) a*a+ sqrt(3*a*a+2*a+1)
main()
{ float x, f;
scanf(“%f”, &x );
f=4.5/F(exp(x))+F(cos(x))+F(sqrt(x))/F(x*x) ;
printf(“%f\n”, f);
}
8-1单选题
1~15 CADAC
6~10 CCDAB 11~15 CBBCD 8-2填空题
1 (1)
2
3
4
5 (2) 10010 (3) QuickC
(4) 2. (1) j+=2 (2) a[i]>a[j]
3. (1) r+b[k] (2) *x
8-3 编程题
1.设数组a 中的元素都为正整数,编程求其中偶数的个数和偶数的平均值。

#include "stdio.h"
#define N 10
main()
{ int a[N]={1,2,3,4,5,6,7,8,9,0},osum=0, ocount=0, j;
for(j=0;j<10;j++)
if(!( j%2)){ ocount++; osum+=a[j];} printf(“ocount =%d, osum =%d\n ”, ocount, osum);
}
2. 有一个已排好序的数组,输入一个数,要求按原来排序的规律将它插入数组中,插入后数组仍然有序,请编程实现。

10000 01000 00100 00010 00001
#include "stdio.h"
#define N 10
main()
{ int a[N]={10,20,30,40,50,60,70,80,90}, j, k, x;
scanf(“%d”,&x);
for(j=0;j<N;j++)
if (x<a[j]) break;
for(k=N-1; k>j; k--) a[k]=a[k-1];
a[j]=x;
for(j=0;j<N;j++) printf(“%d”,a[j]);
}
3.编程求一个矩阵对角元素之和,其中矩阵元素值由数组初始化时给定。

#include "stdio.h"
#define M 3
main()
{int a[M][M]={{1,2,3},{4,5,6},{7,8,9}},j,sum1=0,sum2=0;
for( j=0;j<M;j++)
{sum1+=a[j][j];
sum2+=a[j][M-(j+1)]; }
printf(“%d,%d\n”,sum1,sum2);
}
4. 编一个程序检查二维数组是否对称(即对所有的i和j,都有a[i][j]=a[j][i])。

#include "stdio.h"
#define M 3
main()
{int a[M][M]={{1,2,3},{2,4,5},{3,5,6}},j,k,flag=1;;
for( j=0;j<M;j++)
for(k=0;k<M;k++)
if (a[j][k]!=a[k][j]) { flag=0; break;}
if (flag) printf(“ok”);
else printf(”NO”);
}
5.编程比较两个字符串的大小(不用strcmp( )函数)。

#include "stdio.h"
#include “string.h”
main()
{ char c1[10],c2[10],j;
gets(c1); gets(c2);
for(j=0; (c1[j]==c2[j]) && c1[j] && c2[j]; j++);
printf(“%d\n”,c1[j]-c2[j]);
}
6. 有一篇文章,共有3行文字,每行有80个字符。

要求分别统计出其中英文大写字母、小写字母、数字、空格及其他字符的个数。

#include "stdio.h"
#include ”string.h”
#define M 3
#define N 80
main()
{ char a[M][N],j,k,n[5]={0};
for( j=0;j<M;j++) gets(a[j]);
for(j=0;j<M;j++)
for(k=0;a[j][k];k++)
if( a[j][k]>=’A’&& a[j][k]<=’Z’) n[1]++;
else if (a[j][k]>=’a’&& a[j][k]<=’z’) n[2]++;
else if (a[j][k]>=’0’&& a[j][k]<=’9’) n[3]++;
else if (a[j][k]=’’) n[4]++;
else n[5]++;
for(j=0;j<5;j++)
printf(“%4d”, n[j]);
}
习题9
9-1单选题
1~5 DDACB
6~10 ACBAD 11~15CDB CC 9-2填空题
1 . (1) 2,1 (2) 10#30# (3) FOUR ,O (4) 60
2. (1) 49 (2) 2 (3)2 (4) 7 5 3 1 9 (5)15
(6)
(7)
3. (1) *x (2) t
4. (1) '\0'或0 (2) n++或n+=1或n=n+1
5. 024
9-3 编程题
1. 按以下要求编写一个程序。

定义三个变量用于存放输入的三个整1,1,1, 1,1
3,3,3, 3,3 1 0 0 0 1
0 1 0 1 0
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1
数;另定义三个指向整型变量的指针变量,并利用它们实现将输入的三个整数按由小到大的顺序输出。

#include "stdio.h"
main()
{
int n1,n2,n3;
int *pointer1,*pointer2,*pointer3;
printf("please input 3 number:n1,n2,n3:");
scanf("%d,%d,%d",&n1,&n2,&n3);
pointer1=&n1;
pointer2=&n2;
pointer3=&n3;
if(n1>n2) swap(pointer1,pointer2);
if(n1>n3) swap(pointer1,pointer3);
if(n2>n3) swap(pointer2,pointer3);
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
}
swap(p1,p2)
int *p1,*p2;
{int p;
p=*p1;*p1=*p2;*p2=p;
}
2. 编写函数,得到两个整数相除的商和余数。

要求通过指针在函数间传递商和余数这两个数据。

#include "stdio.h"
main()
{ int a,b,d ;double c;
void fun(int a,int b,float *c, int *d);
scanf("%d,%d",&a,&b);
fun(a,b,&c,&d);
printf("%lf,%d\n",c,d);
}
void fun(int a,int b,double *c, int *d)
{ if (b) { *c=(double)a/b; *d=a%b;}}
3. 输入10个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换。

编写三个函数:①输入10个数;②进行处理;③输出10个数。

#include "stdio.h"
main()
{void input (int *); void output (int *); void chuli (int *);
int a[10];
input(a); chuli(a); output(a);
}
void input(int *a)
{ int j;
for(j=0;j<10;j++)
scanf("%d", a+j);
}
void chuli(int *a)
{int j,minl=0,maxl=0;
for(j=0;j<10;j++)
{ if(a[maxl]<*(a+j)) maxl=j;
if(a[minl]>*(a+j)) minl=j;
}
j=a[0]; a[0]=a[minl];a[minl]=j;
j=a[9];a[9]=a[maxl];a[maxl]=j;
}
void output(int *a)
{int j;
for(j=0;j<10;j++)
printf("%4d", *(a+j));
printf("\n");
}
4.按下列要求输入和输出下列数据阵列:输入阵列如下: 1 2 3 4
5 6 7 8
9 10 11 12
输出阵列如下:
12 11 10 9
8 7 6 5
4 3 2 1
# define M 3
#define N 4
main()
{ int a[M][N]={1,2,3,4,5,6,7,8,9,10,11,12},k,j,*p=a,t;
for(k=0,j=M*N-1;k<j;k++,j--)
{ t=*(p+k); *(p+k)=*(p+j); *(p+j)=t;}
for (k=0;k<M;k++)
{for(j=0;j<N;j++)
printf("%4d ",a[k][j]);
printf(“\n”);
}
}
5.. 编写一个函数,求一个字符串的长度。

在main( )函数中输入字符串,并输出其长度。

#include "stdio.h"
main()
{
int len,length(p);
char *str[20];
printf("please input a string:\n");
scanf("%s",str);
len=length(str);
printf("the string has %d characters.\n",len);
}
int length(p)
char *p;
{
int n=0;
while(*p!='\0')
{
n++;
p++;
}
return n;
}
6. 请编写一个程序,运行时输出命令行参数的个数及参数名。

#include “stdio.h”
main(int argc,char *argv[])
{ int k;
printf(“argc=%d”,argc);
for (k=1;k<argc; k++)
printf(“%s”,argv[k]);
}
7。

用指针数组操作将输入的5个字符串按由小到大的顺序。

#include "stdio.h"
#include "string.h"
main()
{
char *str1[5],ch[5][20],k; void sort(char **);
for(k=0;k<5;k++)
{str1[k]=ch[k];
gets(str1[k]);}
sort(str1);
for(k=0;k<5;k++)
puts(str1[k]);
}
void sort(char **str1)
{ int k,j,t;char *c;
for(k=0;k<4;k++)
{ t=k;
for(j=k+1;j<5;j++)
if(strcmp(*(str1+t),*(str1+j))>0) t=j;
c=*(str1+t);
*(str1+t)=*(str1+k) ; *(str1+k)=c ;
}
}
习题10
10-1单选题
1-5 CDBBB 6-10 BBBAD 11-15 CCBDA
10-2填空题
1. 所有结构体成员所占存储空间的总和
2. 与占用存储空间最大的那个成员相等
3. (1) 结构体(2) 3 (3) sa.a (4) 9 (5) psa=&sa
4. 80
5. struct node
6. 0
10-3 编程题
1. 定义一个包含20个学生基本情况(包括学号、姓名、性别、C语言成绩)的结构体数组,编程实现下列功能:
(1)输入20个学生的学号、姓名、性别、C语言成绩;
(2)分别统计男女生的人数,求出男、女生的平均成绩;
(3)按照学生的C语言成绩从高到底进行排序。

struct student
{
long num;
char name[20];
char sex;
float score;
};
main()
{ struct student s[20], temp;
int j,k, man=0, woman=0;
float summan=0,sumwoman=0, aveman, avewoman;
for(k=0; k<20; k++)
{ scanf("%ld %s %c%f",&s[k].num,s[k].name,&s[k].sex,&s[k].score);
if(s[k].sex==’m’)
{ summan+=s[k].score; man++;}
else
{ sumwoman+=s[k].score;woman++ ;}
}
aveman=summan/man;
avewoman=sumwoman/woman;
printf("%d\t%f\t%d\t%f\n",man,aveman,woman,avewoman);
for(k=0; k<19; k++)
for(j=0;j<20-k;j++)
if(s[j].score<s[j+1].score)
{ temp=s[j];s[j]=s[j+1];s[j+1]=temp;}
printf("the sorted numbers:\n");
for(k=0;k<20;k++)
printf("%ld\t%s\t%c\t%5.1f\n",s[k].num,s[k].name,s[k].sex,s[k].score);}
11-1单选题
1-4 BADD
11-2填空题
1. 3d3d330
2. (1) 28 (2) 20 (3) 0 (4) -9
3. (1) 251 (2) 42 (3) 209 (4) –295 (5) 848
12-1单选题
1-5 BCDCA 6-8 ADA
12-2 填空题
1. rewind(文件指针)
2. "d1.dat","rb"
3. stdin
4. 文本文件二进制文件
5. (1)"w" (2) str[i]-32 (3) "r"
6. fopen
7. Hell
8. (1) "r" (2) fgetc(fp) (3) time++
实验1 熟悉Visual C++6.0可视化集成开发环境略
实验2 顺序结构程序设计

实验3 选择结构程序设计
(1)略
(2)略
(3)#include "stdio.h"
main()
{
float a,b;
printf("Please input the data a and b:\n");
scanf("%f%f",&a,&b);
if(a>10.0)
a=a-10.0;
else
{ a=a+10.0;
if(b>a) ;
else b=a-b;
}
printf("a=%f, b=%f\n",a,b);
}
实验4 循环结构程序设计
(1)略
(2)略
(3)编写一个程序,打印输出半径为1~10的圆的面积,若面积为40~90则予以打印,否则,不予打印。

#include "stdio.h"
main()
{ float r=0, area=0;
while(r<10 && area<90)
{ if (area>40)
printf("r=%f, area=%f\n",r,area);
r=r+1;
area=3.1415926*r*r;
}
}
(4)从键盘输入一批整数,统计其中不大于100的非负数数值的个数。

(用while循环实现。


#include "stdio.h"
main()
{ int x,i=0;
scanf("%d",&x);
while(x>=0)
{ if(x<=100)
i++;
scanf("%d",&x);
}
printf("the number is:%d\n",i);
}
(5)用π/4=11/4+1/51/7+1/9…公式求π的近似值,直到最后一项的绝对值小于104为止。

#include "math.h"
main()
{ double k=1.0,n=1.0,pi=0,t=1.0;
while (fabs(t)>=1e-4)
{ pi=pi+t;
n=n+2.0;
k=-k;
t=k/n;
}
pi=pi*4;
printf("pi=%lf\n",pi);
}
(6)解决猴子吃桃问题。

猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。

第二天早上又将剩下的桃子吃掉一半,又多吃了一个。

以后每天早上都吃了前一天剩下的一半零一个。

到第10天早上想再吃时,只剩一个桃子了。

求第一天猴子共摘了多少个桃子。

#include <stdio.h>
main()
{ int i,m,n;
for (n=1,i=1;i<10;i++)
{ m=2*n+2;
n=m; }
printf("total=%d\n",m);
}
(7)取彩球问题。

现有12个彩球——3个白球、3个红球、6个黑球,从中任意取n(2≤n≤12)个球,求所有不同的取法。

#include "stdio.h"
main()
{
int n,white,red,yellow,count=0;
printf("Please input the number of ball took:");
scanf("%d",&n);
printf("white red yellow\n");
for(white=0;white<=3;white++)
for(red=0;red<=3;red++)
{ yellow=n-white-red;
if(yellow>=0 && yellow<=6)
{ printf("%6d%6d%6d\n",white,red,yellow);
count++; }
}
printf("Total:%d\n",count);
}
实验5 函数
(1)略
(2)略
(3)求两个整数的最大公约数和最小公倍数,要求:用一个函数求最大公约数,用另一个函数求最小公倍数,分别采用用全局变量和不用全局变量两种方法做。

使用全局变量:
#include "stdio.h"
int x,y;
int hef()
{ int u=x,v=y,a,b;
if(u>v)
{ a=u; u=v; v=a; }
while((b=u%v)!=0)
{ u=v; v=b; } return(v);
}
int led(int x,int y,int h) { return(x*y/h);}
main()
{
int h,l;
scanf("%d%d",&x,&y); h=hef(x,y);
printf("HCF=%d\n",h); l=led(x,y,h);
printf("LCD=%d\n",l); }
不用全局变量:#include "stdio.h"
int hef(int x,int y)
{
int a,b;
if(y>x)
{ a=x; x=y; y=a; }
while((b=x%y)!=0)
{ x=y; y=b; }
return(y);
}
int led(int x,int y,int h)
{ return(x*y/h);}
main()
{ int x,y,h,l;
scanf("%d%d",&x,&y);
h=hef(x,y);
printf("HCF=%d\n",h);
l=led(x,y,h);
printf("LCD=%d\n",l);
}
(4)计算s= 1/11/2+1/3…+1/99991/10000,分别采用下列各种方法,每种方法单独用一个函数实现,最后加以比较:
①从左到右各项相加;
②从右到左各项相加;
③从左到右各个正项和负项分别相加;
④从右到左各个正项和负项分别相加。

#include "stdio.h"
void lsum(int n);
void rsum(int n);
void lsumc(int n);
void rsumc(int n);
main()
{ lsum(10000);
rsum(10000);
lsumc(10000);
rsumc(10000);
}
void lsum(int n)
{ int i,k=1;
double sum=0,t;
for(i=1;i<=n;i++)
{ t=1.0*k/i;
sum+=t;
k=-k;
}
printf("the lsum is:%lf\n",sum); }
void rsum(int n)。

相关文档
最新文档