搜索
您的当前位置:首页正文

哈工程C语言教材课后答案

来源:意榕旅游网
第一章

1.2 有三个数A、B、C,设计算法,求三个数中最大的数并输出。(可用自然语言或流程图表示)。 #include void main() {

int a,b,c,max;

printf(\"please input three numbers:\\n\"); scanf(\"%d%d%d\ max=a; if(b>a) max=b; if(c>max) max=c;

printf(\"the answer is %d\\n\ }

1.3 设计算法,求N个整数的平均值。 #include void main() {

int N,i;

float aver,a,s=0;

printf(\"please input the amount of the number:\\n\"); scanf(\"%d\ for(i=0;iprintf(\"please input the number\\n\"); scanf(\"%f\ s=s+a; }

aver=s/N;

printf(\"the average is %f\ }

第二章

1. 填空题⑴换行、回车⑵ 整型 变量_⑶ 整型⑷ _6_⑸2.5 2. 选择题

⑴_A_⑵_A_⑶A_⑷_B_⑸_D⑹C⑺ C ⑻ D ⑼ C ⑽ D ⑾B⑿ D⒀ B ⒁ D 3. ⑴答案:6,0,6⑵答案:8,10,16⑶答案:3.500000⑷答案:6 ⑸答案:1⑹答案:261⑺答案:16⑻答案:0 4. 编程题

⑴ 输入长方形的长和宽,输出长方形的周长和面积。 #include

void main() {

int a,b,l,s;

scanf(―%d%d‖,&a,&b); l=(a+b)*2; s=a*b;

printf(―%d,%d‖,l,s); }

⑵ 输入一个字符,输出其ASCII代码。 #include void main() { char a;

printf(\"please input the char \"); scanf(\"%c\

printf(\"the ASCII is %d\}

⑶ 输入3个整数,计算并输出它们的平均值。 #include void main() {

int a,b,c; float aver;

scanf(“%d%d%d”,&a,&b,&c); aver=float(a+b+c)/3; printf(―%f‖,aver); }

⑷ 已知整型变量a、b、c的值,根据以下算式编写程序求y的值。

y = 3.8×(b+ac) 6a 2

#include #include void main() { int a,b,c;

printf(\"please input the num of a b c: \"); scanf(\"%d %d %d\

printf(\"the y is %f\}

5.设a和n已定义为整型变量,a=12,求下面表达式运算后a的值。

⑴ a+=a 24 ⑵ a-=2 10 ⑶ a*=2+3 60

⑷ a/=a+a 0 ⑸ a%=(n%=2),n的值等于5 0 ⑹ a+=a-=a*=a 0

第三章

一、选择题1_C_2.C_3.C4.D5.D_

二、填空题1.%f_ 2 、 %s 3 、12,56,789.000000 4、6e,16_ 5、1,1,3___ 三、编程题

1.编写一个程序,从键盘上输入3个数,求其和并输出。 #include void main() {

int a,b,c,d;

printf(\"please input the num of a,b,c:\"); scanf(\"%d%d%d\ d=a+b+c;

printf(\"the sum of a,b and c is %d\ }

2.输入一个正整数,分别输出它的八进制和十六进制数形式。 #include void main() {

int m;

printf(\"please input the m:\"); scanf(\"%d\

printf(\"\\n ba jin zhi shu zhi shi : %o\

printf(\"\\n shi liu jin zhi shu zhi shi : %x\}

3.编程把11325秒转换成“小时:分钟:秒”的形式 #include void main() {

int a=11325; int h,m,s; h=a/3600;

m=a%3600/60; s=a%3600%60;

printf(\"%d:%d:%d\ }

4.编程序,求a+︱b︱的值,a,b为任意数。本题可调用求绝对值的函数fabs,此函数包含在math.h文件中。 #include #include void main() {

double a,b;

printf(\"please input the num of a and b:\"); scanf(\"%lf%lf\

printf(\"\\n a+|b|= %lf\ }

5. 编程序,求平面上两点之间的距离。设第1个点的坐标为(x1,y1),第2点的坐标为(x2,y2),则这两个点的距离为:d=(x1x2)2(y1y2)2 #include #include void main() {

float x1,y1,x2,y2,d;

printf(\"\\n shu ru di 1 ge dian de zuo biao : \"); scanf(\"%f%f\

printf(\"\\n shu ru di 2 ge dian de zuo biao : \"); scanf(\"%f%f\

d=sqrt(pow((x1-x2),2)+pow((y1-y2),2)); printf(\"\\n ju li d wei : %f \}

第四章

一、阅读程序,回答问题

1.答案:1 2.答案:6 3、 B 4.答案:6.000000 5答案:8 6.答案:2 7.答案:k=11 二、填空

1.答案:a==b或b==a或!(a!=b)或a=b或b=a 2.答案: (1) <0 (2) !=0

3.答案: (1) max1.输入一个整数,判断其为奇数还是偶数。 #include void main() { int a ; printf(\"Please input an integer:\") ; scanf(\"%d\ if (a%2==0) printf(\"ni shu ru de shu %d shi ou shu . \\n\ else printf(\"ni shu ru de shu %d shi ji shu . \\n\}

2.编写一个程序,实现功能是:输入一个实数,按1输出此数的相反数,按2输出此数的平方根,按3输出此数的平方。 #include #include

void main() { double f ; int a ; printf(\"qing shu ru yi ge shu :\") ; scanf(\"%f\ printf(\"\\n qing shu ru 1 huo 2 huo 3\\n\") ; printf(\"\\n an 1 shu chu xiang fan shu \") ; printf(\"\\n an 2 shu chu ping fang gen \") ; printf(\"\\n an 3 shu chu ping fang \") ; scanf(\"%d\ if (a==1) printf(\"%f\\n\ else if (a==2) printf(\"%f\\n\ else if (a==3) printf(\"%f\\n\ else printf(\"shu ru cuo wu!\") ; }

3.输入字符,输出其类型。ASCII值小于32的为控制字符,在―0‖和―9‖之间的为数字,在―A‖和―Z‖之间为大写字母,在―a‖和―z‖之间为小写字母,其余则为其它字符。

#include void main() {

char a;

scanf(―%c‖,&a); if(a<32&&a>0)

printf(―this is a Control Character‖); if(a>=‘0‘&&a<=‘9‘)

printf(―this is a number‖); if(a>‘A‘&&a<‘Z‘)

printf(―this is a capital letter‖) if(a>‘a‘&&a<‘z‘)

printf(―this is a lowercase number‖); else

printf(―this is an other character‖); }

4.输入某年某月某日,判断这一天是这一年的第几天。 #include void main() { int a,b,c,k,s=0;

}

printf(\"qing shu ru nian, yue, ri:\") ; scanf(\"%d,%d,%d\

if ((a%4 == 0 && a%100 != 0) || a%400 == 0) k = 29 ; else k = 28 ; switch(b) { case 12: s+=30 ; case 11: s+=31 ; case 10: s+=30 ; case 9 : s+=31 ; case 8 : s+=31 ; case 7 : s+=30 ; case 6 : s+=31 ; case 5 : s+=30 ; case 4 : s+=31 ; case 3 : s+=k ; case 2 : s+=31 ; case 1 : s+=c ; }

printf(\"ni shu ru de shu shi zhe nian de di %d tian.\

第五章

一、单选题1 C2. C 3. D 4. C5. B 6.C7. A8.D 9. C 10 C 二、写程序运行结果

1.答案:1 ,2 ,3 2.答案:52 3.答案:s=6 4.答案:1,-2 5.答案:a=16 y=60 6.答案:6

三、程序填空

1.答案:(1) cx=getchar() (2) front!=‗ ‘ (3) cx 2.答案:(1) s=0 (2) m%n==0 (3) m==s 3.答案:(1) j=1 (2) k<=6

4.答案:(1) 50 (2) n=2 (3) h/2 5.答案:(1) a (2) i<=n (3) t*10 四、程序改错

1.答案:(1) int n,k=0; (2) k=k*10+n%10; 2.答案:(1) for(i=0;i<10;i++)(2) j%3!=0 3.答案:(1) double k=1,t=1;(2) k=k+1/t; 4.答案:(1) i=2;(2) k--; 5.答案:(1)h=(float)(5-2)/n; (2) a=a+h; 五、编写程序

1.编一程序,将2000年到3000年中的所有闰年年份输出并统计出闰年的总年

数,要求每10个闰年放在一行输出。

#include void main() {

int year, k=0 ;

for (year = 2000; year<=3000; year++)

{if ((year%4 == 0 && year%100 != 0) || year%400 == 0) { k++; printf(\"%6d\ if (k%10 == 0) printf(\"\\n\"); } }

printf(\"\\nrun nian de zong nian shu shi %d\ }

2.若有如下公式:

试根据上述公式编程计算π的近似值(精确到10-6)。 #include #include void main() {float pi,a,b; int i=1; a=0; b=1;

while(b>=0.000001) { b=1/(float)(i*i); a+=b; i++;}

pi=sqrt(6*a); printf(\"%f\\n\}

3.用0~9之间不同的3个数构成一个3位数,统计输出共有多少种方法? #include void main() { int i=0, j=0, k=0,n=0; for (i =1; i < 10; i++) { for(j=0;j<10;j++) {

if (i==j) continue ; for (k=0;k<10;k++) { if (i==k || j==k ) continue ; n++ ; } } } printf(\"%d\}

4.找出1~99之间的全部同构数。同构数是这样一组数:它们出现在平方数的右边。例如:5是25右边的数,25是625右边的数,5和25都是同构数。 #include void main() {

int i,j,k ; k=10;

for (i=1; i<100;i++) {

if (i==10) k=100; j = i*i ; if (j%k==i) printf(\"%d\\ } }

5.猴子吃桃问题。猴子第1天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了个。第2天又将剩下的桃子吃掉一半,又多吃了一个。以后每天都吃了前一天剩下的一半零一个。到第10天再想吃时,只剩下一个桃子了。问第1天共摘多少个桃子?。 #include void main() {

int day, x ; x = 1;

for (day = 1; day <=9; day++) x = (x + 1)*2 ;

printf(\"di yi tian hou zi zhai le %d ge tao zi\}

6.打印以下图形

* * * * * * * * * *

#include void main()

{

int i, j ,k;

for (i=1; i<=4; i++) {

for (j=1;j<=4-i;j++) printf(\" \") ; printf(\"*\") ;

for (j=1;j<=i-1;j++) { printf(\" *\") ; }

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

7.百钱百鸡问题。公鸡五元一只,母鸡三元一只,鸡雏一元三只。若用100元买100只鸡,可买公鸡、母鸡和鸡雏各多只? #include void main() {

int i , j , k ;

for (i = 0; i <= 100/5; i++) {

for (j = 0; j <= 100/3; j++) { for (k = 0; k <= 100; k++) { if (k%3 != 0) continue ; if ((5*i + j*3 + k/3 == 100) && (i + j + k ==100)) { printf(\"gong ji mu ji chu ji wei %d, %d, %d.\\n\ } } } } }

8.一个正整数与3的和是5的倍数,与3的差是6的倍数,编写一个程序求符合条件的最小数。 #include void main() {

int k=1;

while( !( ((k+3)%5 == 0) && ((k-3)%6 ==0) ) ) k++ ;

printf(\"%d\}

9.从键盘输入20个整型数,统计其中负数个数并求所有正数的平均值。 #include void main() { int a, i , j =0, k=0 ; float s = 0 ; printf(\"qing shu ru 20 ge shu: \") ; for (i = 1; i <= 20; i++) { scanf(\"%d\ if(a < 0) k++ ; if (a > 0) { j++ ; s+=a ; } } s = s/(float)j ; printf(\"fu shu de ge shu shi %d.\\n\ printf(\"zhe shu de ping jun zhi shi %f.\\n\

}10.父亲今年30岁,儿子6岁,经过多少年后,父亲的年龄是儿子的两倍? #include void main() {int i,j,k; i=30; j=6; k=0;

while(!(i+k==(j+k)*2)) k++; printf(\"%d\}

第六章

一、选择题

1._C_ 2.C__ 3._D__ 4._C__ 5._C_ 6._B_ 7._D 8. _A_ 二、填空题 1.答案:b!=0 2.答案:15 三、 编程题

1.编写一个求x的y次方的函数。 #include int func(int x,int y); void main() {int a,b,c;

printf(\"please input the two numbers: \\n\"); scanf(\"%d%d\ c=func(a,b);

printf(\"the answer is \\n\"); printf(\"%d\\n\}

int func(int x,int y) { int i, s=1;

for(i=1;i<=y;i++) s=s*x; return(s); }

2.编写函数,要求去掉字符串中所有空格。 # include # include void f(char s[]) { int i,j; i=j=0;

while(s[i])

{if(s[i]!=' ')s[j++]=s[i]; i++; } s[j]='\\0'; }

void main() {

char s[80]; gets(s); f(s); puts(s); }

3.编写函数,判断一个字符串是否是回文,如是返回1,否则返回-1(回文是指这个字符串逆置后不变,如aba就是回文。) # include # include int f(char s[],int n) {

int i,f; f=1; i=0;

while(f&&i} return f; }

void main() {

char s[80]; gets(s);

if(f(s,strlen(s))==1) printf(\"\\n Yes \"); else

printf(\"\\n No \"); }

4.编写函数fun求1!+2!+3!+…+n! 的和,在main函数中由键盘输入n值,并输出运算结果。 #include int fun(int n); void main() {int n,c;

printf(\"please input the number: \\n\"); scanf(\"%d\ c=fun(n);

printf(\"the answer is \\n\"); printf(\"%d\\n\}

int fun(int n) {int i,a,s; s=1; a=0;

for(i=1;i<=n;i++) {

s=s*i; a=a+s; }

return(a); }

5.编写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数并输出结果。两个整数由键盘输入 #include int func(int x,int y); int fun(int x,int y); void main() {int x,y,c,d;

printf(\"please input the two numbers: \\n\"); scanf(\"%d%d\

c=func(x,y); d=fun(x,y);

printf(\"the answer is \\n\");

printf(\"zui da gong yue shu shi %d\\n\ printf(\"zui xiao gong bei shu shi %d\}

int func(int x,int y) { int t; t=x%y; while(t) { x=y; y=t; t=x%y; }

return(y); }

int fun(int x,int y) {

return (x*y/func(x,y)); }

第七章

一、选择题 1. D 2. B 3. D 4. D 5. A

二、阅读程序写出结果

1. 答案:1 2. 答案:12345 3. 答案: t*M 4. 答案:7078 9198 三、程序填空

1. 答案:(1) i—(2) i>=1(3) i 2. 答案:(1) i<=9(2) i<=9(3) i%3==0 四、程序改错

1.答案:(1) int a[3]={0};(2) for(i=1;i<3;i++) a[0]=a[0]+a[i];

2. 答案:(1) for(i=0;ifor(i=0;i<5;i++) for(j=i;j<5;j++) c[i][j]='*'; for(i=0;i<5;i++) {

for(j=0;j<5;j++)

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

9. 计算二维数组主对角线元素之和。

#include void main() {

int a[3][3],i,j,x;

printf(\"please input the numbers:\\n\"); for(i=0;i<3;i++) for(j=0;j<3;j++)

scanf(\"%d\ x=0;

for(i=0;i<3;i++) x=x+a[i][i];

printf(\"the answer is %d\\n\}

10. 用―折半‖查找法,在一组按降序排列的数中查找一个值为K的元素。若有,输出YES;若无,输出NO。

#include void main() {

int a[10]={10,9,8,7,6,5,4,3,2,1},l,r,m,k; printf(\"please input the numbers:\\n\"); scanf(\"%d\ l=0; r=9;

m=(1+r)/2; while(l<=r) if(a[m]==k) break; else {

if(k>a[m])

r=m-1; else

l=m+1; m=(l+r)/2; } if(l<=r)

printf(\"\\n YES \"); else

printf(\"\\n NO \"); }

11. 编写一个程序计算一个字符串的长度。(不使用库函数)

#include #include void main() {

char s[80]; int n=0; gets(s);

while(s[n])n++;

printf(\"length = %d\}

12. 比较两个字符串S1、S2的大小。(不使用库函数)若S1>S2 ,输出1;若S1=S2,输出0;若S1#include #include void main() {

char s1[80],s2[80]; int i,n; gets(s1); gets(s2); i=0;

while(s1[i]&&s2[i]&&s1[i]==s2[i])i++; if(!s1[i]||s1[i]if(!s2[i]||s1[i]>s2[i]) n=1;

if(!s1[i]&&!s2[i]) n=0;

printf(\"jie guo shi %d\}

13. 用―冒泡‖法将一组数按升序排序。

#include void main()

{

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

x=a[i]; a[i]=a[i+1]; a[i+1]=x; }

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

14. 在一个二维数组中形成并输出如下矩阵 1 1 1 1 1 2 1 1 1 1 3 2 1 1 1 4 3 2 1 1 5 4 3 2 1

#include void main() {

int a[5][5]; int i,j;

for(i=0;i<5;i++) for(j=0;j<5;j++) if(i<=j) a[i][j]=1; else

a[i][j]=i-j+1; for(i=0;i<5;i++) {

for(j=0;j<5;j++)

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

15. 有一个3╳4的矩阵,求其中的最大元素的值及位置。

#include void main() {

int a[3][4],i,j,x,l,r;

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

scanf(\"%d\ x=a[0][0]; l=r=0;

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

x=a[i][j]; l=i; r=j; }

printf(\"Max number is %d,position is %d,%d\\n\

第九章

一、选择题 1.( D ) 2. B 3.( D )

二、写出以下程序的运行结果 (1)答案:6 (2)答案:6 (3)答案:2,3 (4)答案:6 (5)答案: Zhao,m,85,90 三、改错题

1、答案:(1) 最后加分号“;”。 (2) 改为scanf(\"%d %d\ (3) 改为printf(\"The point you input is:x=%d,y=%d\ 2、答案:

( 1) 改为struct People peo[];

(2)改为scanf(\"%c\(3)改为scanf(\"%c\"&,peo[i].sex);

(4)改为scanf(\"%s\

(5) 改为:printf(\"The %d People's name is:%s,sex:%c,position:%s,age:%d \\n\四、编程题

(1) 设计一个通信录的结构体类型,并画出该结构体变量在内存的存储形式。 struct photonote {

char name[20]; char number[20]; };

(2)用结构体变量表示矩形,编写矩形面积函数,矩形周长函数,输入矩形长宽函数,输出矩形长宽函数,输入矩形的长宽并进行面积和周长的计算。 #include

struct juxing {

float chang; float kuan; };

struct juxing shuru() {

struct juxing r;

printf(\" shu ru ju xing chang kuan :\\n\"); scanf(\"%f %f\ return r; }

void shuchu(struct juxing r) {

printf(\" chang shi : %f\\n\ printf(\" kuan shi : %f\\n\}

float mianji(struct juxing r) {

return r.chang*r.kuan; }

float zhouchang(struct juxing r) {

return 2*r.chang+2*r.kuan; }

void main() {

struct juxing a; a=shuru(); shuchu(a);

printf(\" mian ji wei : %f\\n\

printf(\" zhou chang wei : %f\\n\}

(3)设有学生情况登记表如表9.3所示,用选择法对该表按成绩从小到大排序。 #include struct Student{ int num;

char name[8]; char sex; int age; float score;

};

void main(){ int i,j; int min;

struct Student temp; struct Student stu[10]={{101,\"zhang\{104,\"zhao\\for(i=0;i<9;i++){ min = i ; for(j=i;j<9;j++){

if(stu[i].score } }

temp=stu[min]; stu[min]=stu[i]; stu[i]=temp; }

for(i=0;i<=9;i++) {

printf(\"num=%d,name=%s,sex=%c,age=%d,score=%f\\n\.sex,stu[i].age,stu[i].score); }

getchar(); }

(4)某班有20名学生,每名学生的数据包括学号、姓名、3门课的成绩,从键盘输入20名学生的数据,要求打印出每门课的平均成绩,以及每名学生的平均成绩并输出最高分的学生的数据(学号、姓名、3门课、平均成绩)。

#include struct student {char num[4]; char name[8]; float score[3]; float ave; };

void main() {

struct student s1[20]; int i,j;

float avg1=0,avg2=0,avg3=0,sum=0;

for(i=0;i<20;i++)

scanf(\"%s%s%f%f%f\1],&s1[i].score[2]); for(i=0;i<20;i++) {

avg1+=s1[i].score[0]; avg2+=s1[i].score[1]; avg3+=s1[i].score[2];

s1[i].ave=(s1[i].score[0]+s1[i].score[1]+s1[i].score[2])/3; }

avg1/=20; avg2/=20; avg3/=20;

printf(\"2 men ke de ping jun cheng ji : %f ,%f ,%f\\n\ sum=s1[0].ave; j=0;

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

if(s1[j].avefor(i=0;i<20;i++)

printf(\" ping jun cheng ji : %f \\n\

printf(\"ZuiGaoFen--XueHao:%s, XingMing:%s, ChengJi :%f %f %f, PingJun:%f\\n\].score[2],s1[j].ave); }

(5)统计候选人总得票数。假设有5名候选人,每次输入一个得票候选人的名字,要求最后按从大到小输出每个人的得票数排名。

#include

#include struct note {

char name[20]; int count; };

void main() {

struct note notes[5]={\"zhao\char tou[20]; int i,j;

for(i=0;i<20;i++) {

scanf(\"%s\ for(j=0;j<20;j++)

if(!strcmp(tou,notes[j].name)) notes[j].count++; }

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

if(notes[i].count} for(i=0;i<5;i++) printf(\"XingMing : %s, PiaoShu: %d\\n\}

(6)定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。

#include struct date {

int year; int month; int day; };

void main() {

struct date date1; int i,sum=0;

scanf(\"%d,%d,%d\ for(i=1;isum+=31;

if(date1.month>2) sum-=3;

if((date1.year%400==0) || (date1.year%4==0 && date1.year%100!=0)) sum++;

sum+=date1.day;

printf(\" shi di %d tian \\n\}

第十章

一、单项选择 1.( B 2.( A ) 3.( B ) 4.( B ) 5.( B ) 二、阅读程序写结果

1.答案: 19 2.答案:k=2 a=4 b=3 3.答案:6 4.答案: 0 1 3 6 三、程序填空

1.答案:(1) p=a; (2) p=a+4;或p-- 2.答案:(1) p=s; (2) ++p

3.答案:(1) *k=p (2) findmax(a,10,&k) 4.答案:(1) *t!='\\0'或*t (2) *(s++)=*(t++)或*s++=*t++ (3) a,b 5.答案:(1)*p!='\\0'或*p (2)a[i++]=*p (3)a[i]='\\0';b[j]='\\0'或a[i]=b[j]='\\0' 四、程序问答 1.答案:(1) 7(2) 首地址 2.答案:(1) 12,9,6(2) 从大到小排序(降序排列)(3) 结果仍是12,9,6,但此处输出结果不是排序后的结果,输出的是输入顺序 的数字。 3.答案:(1)aace(2)把字符数组中的t删除掉(3)atace 五、程序改错 1.答案:(1)scanf(\"%s\(2)strcpy(max,str);(3)strcmp(max,str); (4)strcpy(max,str); 2.答案:(1)t=*s;(2)*(p-1)=*p;(3)*(p-1)=t; 3.答案:(1)*p=*q;(2)*q='\\0';

六、编程

以下程序均要求使用指针来实现。

1. 编程判断输入的一串字符是否为“回文”,是则输出Yes,否则输出No。所谓“回文”,是指正读和倒读都一样的字符串。如\"ratar\"就是回文。 #include #include void main() {

char str[100],*p,*q; int flag=1;

scanf(\"%s\ p=str;

q=str+(strlen(str)-1); while(pif(*(p++)!=*(q--)) {flag=0; break; }

if(flag==1) printf(\"Yes\\n\"); else printf(\"No\\n\"); }

2.strcat函数用来连接两个字符串,如: char s1[20]=\"holiday \

则strcat(s1,s2); 可以将s2中的字符串连接到s1字符串的后面。此时s1中的字符串变为\"holiday economy\"。请自行编写函数mystrcat,完成上述功能。

#include #include

void mystrcat(char *p,char *q) {

while (*p) p++; while (*q)

*p++=*q++; *p='\\0'; }

void main() {

char s1[20],s2[20]; char *p=s1,*q=s2; gets(s1); gets(s2);

mystrcat(p,q);

printf(\"%s\\n\ }

2. 有5个候选人参与选举,共100张选票,每张选票上只能推选一个人。编程统计每个候选人的得票数,并输出结果。

#include #include void main() {

char * name[5]={\"zhang\ char note[10]; int j;

int count1=0,count2=0,count3=0,count4=0,count5=0; for(j=0;j<10;j++) { scanf(\"%s\

if(!strcmp(name[0],note) ) count1++; if(!strcmp(name[1],note) ) count2++; if(!strcmp(name[2],note) ) count3++; if(!strcmp(name[3],note) ) count4++; if(!strcmp(name[4],note) ) count5++; }

printf(\" Wu Ge Ren De De Piao Fen Bie Wei: \\n\");

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

3. 统计一段英文短文中出现的单词的个数。单词间以空格分隔,可以有多个空格。

#include #include void main() { char a[39] ; char *p=a;

int count=0,flag=0; gets(a);

if(*p==' ') flag=1; while (*p !='\\0') {

if(*p==' ' && *(p+1)!=' '&&*(p+1)!='\\0') count++; p++; }

if(flag==1) printf(\"the num of words is:%d\\n\ else printf(\"the num of words is:%d\\n\}

4. 对一组整数降序排序。要求排序功能由调用函数实现。

#include void main() {

void sort(int a[],int n); int a[10],i; int *p=a;

for(i=0;i<=9;i++) scanf(\"%d\sort(p,10);

printf(\"pai xu hou shun xu shi:\\n\"); for(i=0;i<10;i++) printf(\"%d \ printf(\"\\n\"); }

void sort(int a[],int n) {

int temp,i,j;

for(i=0;ifor(j=i+1;jtemp=a[i]; a[i]=a[j]; a[j]=temp; } }

第十一章

一、

单项选择题

1 .( D ) 2.( D ) 3.( C )

二、阅读程序写结果

1.答案:复制f1到f2 2.答案: 121314252627 三、填空题 1.

文本文件 二进制文件 2.

返回―NULL‖ 3.

读取单个字符 四、程序问答题

1.文件的分类?答案:系统文件 用户文件 文本文件 二进制文件文件 随机文件 源文件 目标文件 可执行文件 2. 使用文件的一般步骤?

答案:打开文件 –> 操作文件-> 关闭文件 五、编程题

1.编写程序,从键盘输入200个字符,存入名为‖f1.txt‖的磁盘文件中。

#include void main() { FILE *fp; char ch; int i;

if((fp=fopen(\"f:\\\\f1.txt\{

printf(\"File can not open!\\n\"); exit(0);

顺序 }

for(i=0;i<200;i++) {ch=fgetchar(); fputc(ch,fp); }

fclose(fp); }

2.编写程序,从CCW.TXT文本文件中读出每一个字符,将其加密后写入CCW1.TXT文件中,加密的方法是每个字节的内容减10。

#include void main() {

FILE *fp1,*fp2; char ch; int m;

if((fp1=fopen(\"f:\\\\ccw.txt\ {

printf(\"File1 can not open!\\n\"); exit(0); }

if ((fp2=fopen(\"f:\\\\ccw1.txt\ {

printf(\"File2 cant not open!\\n\"); exit(0); }

while(!feof(fp1)) {ch=fgetc(fp1);

ch=ch-10; fputc(ch,fp2); }

fclose(fp2); fclose(fp1);

if ((fp2=fopen(\"f:\\\\ccw1.txt\ {

printf(\"File2 cant not open!\\n\"); exit(0); }

while(!feof(fp2)) {

ch=fgetc(fp2); putchar(ch); }

fclose(fp2); }

3.编写程序,能将一个磁盘文件1的内容复制到另一个磁盘文件2中,即模仿copy命令的功能。

# include void main() {FILE *f1,*f2; int k;

char c1,c2;

if ((f1=fopen(\"f:\\\\ccw.txt\{ printf(\"file can not open!\\n\"); exit(0); }

if ((f2=fopen(\"f:\\\\ccw1.txt\{ printf(\"file can not open!\\n\"); exit(0); }

for(k=1;k<=1000;k++) { if(feof(f1)) break; fputc(fgetc(f1),f2); }

fclose(f1); fclose(f2); }

4.编写程序,用于显示指定的文本文件的内容,每20行暂停一下。

#include void main() {

FILE *fp; int i=0; char ch;

if((fp=fopen(\"f:\\\\f1.txt\{

printf(\"File can not open!\\n\"); exit(0); }

while(!feof(fp)) {

ch=fgetc(fp);

putchar(ch); if(ch=='\\n') i++;

if(i>0&&i%20==0) getchar(); }

fclose(fp); }

5, 从键盘输入一行字符串,将其中的小字母全部转换成大写字母,然后输出到一个磁盘文件\"test\"中保存。 #include void main() {

FILE *fp1,*fp2; char ch;

if((fp1=fopen(\"f:\\\\file1.txt\ {

printf(\"File1 can not open!\\n\"); exit(0); }

if ((fp2=fopen(\"f:\\\\file2.txt\ {

printf(\"File2 cant not open!\\n\"); exit(0); }

while(!feof(fp1)) {

ch=fgetc(fp1); fputc(ch,fp2); }

fclose(fp1); fclose(fp2); }

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

Top