c语言详解(第五版)第四章程序设计项目答案

c语言详解(第五版)第四章程序设计项目答案
c语言详解(第五版)第四章程序设计项目答案

C语言详解(第五版)第四章程序设计项目答案

1.Keith的Sheet Music需要一个程序来实现其为音乐教师定制的折扣政策。程序提示用户输入购买总额并且表明购买者是不是教师。

#include

#include

#define discount_1 0.12

#define discount_2 0.1

#define tax_rate 0.05

void instruct_purchase(void);

double total(double total_purchase,char a[1]);

int main(void)

{void instruct_purchase(void);

double total(double,char);

double total_purchase,total_payment,total1;

char a;

instruct_purchase();

printf("Please enter the creer teacher(enter t) or not(enter n).>>>\n");

scanf("%c",&a);

printf("Please enter the total purchase.>>>\n");

scanf("%lf",&total_purchase);

total1=total(total_purchase,a);

printf("The total payment is %.2f.",total1);

system("pause");

return(0);}

void instruct_purchase(void)

{printf("This program figure a total bill.");

printf("The tax rate is $%.2f.\n",tax_rate);

printf("The discount of total purchase which is more than 100 dollars is $%.2f.\n",discount_1); printf("The discount of total purchase which is less than 100 dollars is $%.2f.\n",discount_2); printf("Please press or button after typing in each number.\n\n\n");

}

double total(double total_purchase,char a)

{double discount_total,total_payment,tax;

if(a=='t')

if(total_purchase>100)

{discount_total=total_purchase*discount_1;

tax=total_purchase*tax_rate;

total_payment=total_purchase+tax-discount_total;

}

else

{discount_total=total_purchase*discount_2;

tax=total_purchase*tax_rate;

total_payment=total_purchase+tax-discount_total;

}

else

{ tax=total_purchase*tax_rate;

total_payment=total_purchase+tax;

}

return(total_payment);

}

3.罗盘的度数换算为罗盘的方位。

#include

#include

void direction(double angle_degree);

int main(void)

{double angle_degree;

printf("Please enter the angleof the total station.>>>\n");

scanf("%lf",&angle_degree);

printf("The angle of the total station is %.2f??.\n\n\n",angle_degree); direction(angle_degree);

system("pause");

return(0);

}

void direction(double angle_degree)

{double angle;

if (angle_degree==0)

{printf("The direction is east(E).\n\n");

}

if(0

{printf("The direction is north(N) %.2f east(E).\n\n",angle_degree);

}

if (angle_degree==90)

{printf("The direction is north(N).");

}

if(90

{angle=180-angle_degree;

printf("The direction is north(N) %.2f west(W).\n\n",angle);

}

if(angle_degree==180)

{printf("The direction is west(W).\n\n");

}

if(180

{angle=angle_degree-180;

printf("The direction is south(S) %.2f west(W).\n\n",angle);

}

if(angle_degree==270)

{printf("The direction is south(S).\n\n");

}

if(270

{angle=angle_degree-270;

printf("The direction is north(N) %.2f east(E).\n\n",angle);

}

}

4.编写一个程序根据压缩气囊颜色的第一个字母确定压缩罐的内容。

#include

#include

void color(char fa);

int main(void)

{char fa;

printf("Please enter the first alphabet of the container.>>\n");

scanf("%c",&fa);

color(fa);

system("pause");

return(0);

}

void color(char fa)

{switch(fa)

{case'O':

case'o':

printf("The color suggest the content is ammonia gas.\n\n");

break;

case'B':

case'b':

printf("The color suggest the content is CO.\n\n");

break;

case'Y':

case'y':

printf("The color suggest the content is hydrogen.\n\n");

break;

case'G':

case'g':

printf("The color suggest the content is oxygen.\n\n");

break;

default:

printf("There is not the container.");

}}

5.对地震的理氏数进行描述。

#include

#include

void Richter_scale(double a);

int main(void)

{double a;

printf("Please enter the Richter scale of the earthquake.>>>");

scanf("%lf",&a);

Richter_scale(a);

system("pause");

return(0);

}

void Richter_scale(double a)

{ if(a>0&&a<5.0)

printf("破?坏|ì很¨1小?或¨?者?没?有?D.\n");

else if(a<5.5)

printf("一??些?破?坏|ì.\n");

else if(a<6.5)

printf("严?重?破?坏|ì;ê?墙?会¨¢倒ì1塌¨2.\n");

else if(a<7.5)

printf("灾?难?;ê?房¤?屋Y或¨?者?建?§筑t物?会¨¢倒ì1塌¨2.\n"); else if(a>=7.5)

printf("大?¨?灾?难?;ê?大?¨?部?分¤?建?§筑t物?被à?破?坏|ì.\n"); else

printf("There is not this magnititude of earthquake.\n");

}

(2)switch函数编写

#include

#include

char Richter_scale(double a);

void sf(char b);

int main(void)

{double a;

char b;

printf("Please enter the Richter scale of the earthquake.>>>"); scanf("%lf",&a);

b=Richter_scale(a);

sf(b);

system("pause");

return(0);

}

char Richter_scale(double a)

{ char c;

if(a>0&&a<5.0)

{c='A';

return(c);

}

else if(a<5.5)

{c='B';

return(c);

}

else if(a<6.5)

{c='C';

return(c);

}

else if(a<7.5)

{c='D';

return(c);

}

else if(a>=7.5)

{c='E';

return(c);

}

else if(a<=0)

{c='F';

return(c);

}

}

void sf(char b)

{switch(b)

{case'A':

printf("破?坏|ì很¨1小?或¨?者?没?有?D。?ê\n");

break;

case'B':

printf("一??些?破?坏|ì。?ê\n");

break;

case'C':

printf("严?重?破?坏|ì;ê?墙?会¨¢倒ì1塌¨2。?ê\n");

break;

case'D':

printf("灾?难?;ê?房¤?屋Y和¨a建?§筑t物?会¨¢倒ì1塌¨2。?ê\n"); break;

case'E':

printf("大?¨?灾?难?;ê?大?¨?部?分¤?建?§筑t物?被à?破?坏|ì。?ê\n"); break;

case'F':

printf("无T该?地ì?震e等쨨级?。?ê\n");

break;

}}

6.判断一个点在哪个象限

#include

#include

void quadrant(double a,double b);

int main(void)

{double a,b;

printf("Please enter the coordinate of the dot.>>");

scanf("%lf %lf",&a,&b);

quadrant(a,b);

system("pause");

return(0);

}

void quadrant(double a,double b)

{if(a==0&&b!=0)

{printf("(%.2f,%.2f) is on the y axis.\n",a,b);

}

if(a!=0&&b==0)

{printf("(%.2f,%.2f) is on the x axis.\n",a,b);

}

if(a>0&&b>0)

{printf("(%.2f,%.2f) is in quadrantⅠ.\n",a,b);

}

if(a>0&&b<0)

{printf("(%.2f,%.2f) is in the quadrant Ⅳ.\n",a,b);

}

if(a<0&&b>0)

{printf("(%.2f,%.2f) is in the quadrant Ⅱ.\n",a,b);

}

if(a<0&&b<0)

{printf("(%.2f,%.2f) is in the quadrant Ⅲ.\n",a,b);

}

}

7.一年中的某一天问题。

#include

#include

void order(int year,int month,int day,int e);

int leap(int year);

int main(void)

{void order(int,int,int,int);

int leap(int);

int year,month,day,e;

printf("Please enter the date(year month day).>>");

scanf("%d %d %d",&year,&month,&day);

e=leap(year);

order(year,month,day,e);

system("pause");

return(0);

}

int leap(int year)

{int a,b,c,d;

a=year%4;

b=year%100;

c=year%400;

if((a==0&&b==0)||c==0)

{d=1;}

else

{d=0;}

return(d);

}

void order(int year,int month,int day,int e)

{int dayth;

if(e==1)

{if(month==1)

{ dayth=day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==2)

{ dayth=31+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==3)

{ dayth=31+29+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==4)

{ dayth=31+29+31+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==5)

{ dayth=31+29+31+30+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==6)

{ dayth=31+29+31+30+31+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==7)

{dayth=31+29+31+30+31+30+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==8)

{dayth=31+29+31+30+31+30+31+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==9)

{dayth=31+29+31+30+31+30+31+31+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==10)

{ dayth=31+29+31+30+31+30+31+31+30+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==11)

{dayth=31+29+31+30+31+30+31+31+30+31+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==12)

{ dayth=31+29+31+30+31+30+31+31+30+31+30+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

}

else

{if(month==1)

{ dayth=day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==2)

{ dayth=31+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==3)

{ dayth=31+28+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==4)

{ dayth=31+28+31+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==5)

{ dayth=31+28+31+30+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==6)

{ dayth=31+28+31+30+31+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==7)

{dayth=31+28+31+30+31+30+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==8)

{dayth=31+28+31+30+31+30+31+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==9)

{dayth=31+28+31+30+31+30+31+31+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==10)

{ dayth=31+28+31+30+31+30+31+31+30+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==11)

{dayth=31+28+31+30+31+30+31+31+30+31+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

if(month==12)

{ dayth=31+28+31+30+31+30+31+31+30+31+30+day;

printf("This day is the %d day of %d year.\n",dayth,year);

}

}

}

9.居民用电

#include

#include

#define R_price 0.052

#define C_price 0.045

#define I_price_P 0.065

#define I_price_NP 0.028

void type_consumption(char type,double e_consumption);

void R_consumption(double e_consumption);

void C_consumption(double e_consumption);

void I_consumption(double e_consumption);

int main(void)

{double e_consumption;

char type;

printf("The alphabet R means reside,C means commercial,I means industry.>>\n"); printf("Please enter the application type (R,C or I).>>\n");

scanf("%c",&type);

printf("Please enter the consumption of electricity.>>\n");

scanf("%lf",&e_consumption);

type_consumption(type,e_consumption);

system("pause");

return 0;

}

void type_consumption(char type,double e_consumption)

{

if(type=='R')

{R_consumption(e_consumption);}

else if (type=='C')

{C_consumption(e_consumption);}

else if(type=='I')

{I_consumption(e_consumption);}

}

void R_consumption(double e_consumption)

{double total1;

total1=6+e_consumption*R_price;

printf("The total amount is %.2f dollars.",total1);

}

void C_consumption(double e_consumption)

{double total1;

total1=60+(e_consumption-1000)*C_price;

printf("The total amount is %.2f dollars.",total1);

}

void I_consumption(double e_consumption)

{ double total1;

char peak_time;

printf("Please enter peak_time or not.(Y or N).>>\n");

getchar();

scanf("%c",&peak_time);

if(peak_time=='Y')

{

total1=76+(e_consumption-1000)*I_price_P;

printf("The total amount is %.2f dollars.",total1);

}

else

{total1=40+(e_consumption-1000)*I_price_NP;

printf("The total amount is %.2f dollars.\n",total1);

}

}

10烤面包问题。

#include

#include

void instruct(char type,char size,char mannual);

int main(void)

{char size,mannual,type;

printf("Please enter the type of the bread.(white-W,sweet-S)>>\n");

scanf("%c",&type);

printf("If the client request to multiply the size(Yes or Not,enter Y or N).>>\n"); getchar();

scanf("%c",&size);

printf("If the client request to bake mannually.(Yes or Not,enter Y or N).>>\n"); getchar();

scanf("%c",&mannual);

instruct(type,size,mannual);

system("pause");

return 0;

}

void instruct(char type,char size,char mannual)

{if(type=='W')

{ if (size=='N')

{if(mannual=='N')

{printf("basic dough 15 minutes\n");

printf("basic fermentation 60 minutes\n");

printf("secondary dough 18 minutes\n");

printf("secondary fermentation 20 minutes\n");

printf("Bread formation 2 seconds\n");

printf("last fermentation 75 minutes\n");

printf("bakement 45 minutes\n");

printf("cooling 30 minutes\n");

}

else

{printf("basic dough 15 minutes\n");

printf("basic fermentation 60 minutes\n");

printf("secondary dough 18 minutes\n");

printf("secondary fermentation 20 minutes\n");

printf("Bread formation 2 seconds\n");

printf("Please do the mannual work.\n");

}}

else if (size=='Y')

{if(mannual=='N')

{printf("basic dough 15 minutes\n");

printf("basic fermentation 60 minutes\n");

printf("secondary dough 18 minutes\n");

printf("secondary fermentation 20 minutes\n");

printf("Bread formation 2 seconds\n");

printf("last fermentation 75 minutes\n");

printf("bakement 90 minutes\n");

printf("cooling 30 minutes\n"); }

else

{printf("basic dough 15 minutes\n");

printf("basic fermentation 60 minutes\n");

printf("secondary dough 18 minutes\n");

printf("secondary fermentation 20 minutes\n");

printf("Bread formation 2 seconds\n");

printf("Please do the mannual work.");

}}

}

else if(type=='S')

{ if (size=='N')

{if(mannual='N')

{printf("basic dough 20 minutes\n");

printf("basic fermentation 60 minutes\n");

printf("secondary dough 33 minutes\n");

printf("secondary fermentation 30 minutes\n");

printf("Bread formation 2 seconds\n");

printf("last fermentation 75 minutes\n");

printf("bakement 35 minutes\n");

printf("cooling 30 minutes\n"); }

else

{printf("basic dough 20 minutes\n");

printf("basic fermentation 60 minutes\n");

printf("secondary dough 33 minutes\n");

printf("secondary fermentation 30 minutes\n");

printf("Bread formation 2 seconds\n");

printf("Please do the mannual work.");

}}

else if (size=='Y')

{if(mannual='N')

{printf("basic dough 20 minutes\n");

printf("basic fermentation 60 minutes\n");

printf("secondary dough 33 minutes\n");

printf("secondary fermentation 30 minutes\n");

printf("Bread formation 2 seconds\n");

printf("last fermentation 75 minutes\n");

printf("bakement 70 minutes\n");

printf("cooling 30 minutes\n");

}

else

{printf("basic dough 15 minutes\n");

printf("basic fermentation 60 minutes\n");

printf("secondary dough 18 minutes\n");

printf("secondary fermentation 20 minutes\n");

printf("Bread formation 2 seconds\n");

printf("Please do the mannual work.\n");

}}

}

}

11.下面的表格给出了几种物质的通常沸点。编写一个程序,通过沸点识别物质。#include

#include

double with_x_percent(double ref,double data,double x);

int main(void)

{double a,ref,data,x;

printf("Please enter the ref.>>\n");

scanf("%lf",&ref);

printf("Please enter the data.>>\n");

scanf("%lf",&data);

printf("Please enter the x.>>\n");

scanf("%lf",&x);

a=with_x_percent(ref,data,x);

if(a==1)

printf("The substance is within the scope of provision.\n");

else

printf("Substance Unknown.");

system("pause");

return(0);

}

double with_x_percent(double ref,double data,double x)

{if((data>=ref-x*0.01*ref)||(data<=ref+x*0.01*ref))

return(1);

}

《C程序设计语言》样卷

韩山师范学院2011年专升本插班生考试样卷 计算机科学与技术专业高级语言程序设计试卷(A卷) 一、填空题(每空1分,共10分) 1.C程序的基本单位是________。 2.C语言源程序文件的后缀是________。 3.C语言中的标识符由________、________和字母组成。 4.设y为float型变量,执行表达式y=6/5之后,y的值是________。 5.在C语言中,要求运算量必须是整型的运算符是___________。 6.如果函数值的类型与返回值的类型不一致时,应该以___________为准。7.已知int a=8,*p=&a;,则*p的值是___________。 8.把一些不同类型的数据作为一个整体来处理时,常用___________。9.若x=2,y=3,则x|y<<2的结果是___________。 二、单项选择题(每小题1.5分,共30分) A.顺序结构、选择结构、循环结构B.递归结构、循环结构、转移结构C.嵌套结构、递归结构、顺序结构D.循环结构、转移结构、顺序结构2.在一个C语言的源程序中,以下叙述正确的是()。

A.必须有除主函数外其他函数B.可以有多个主函数 C.必须有一个主函数D.可以没有主函数 3.以下叙述正确的选项是()。 A.C语言的源程序不必通过编译就可直接执行 B.C语言中的每条语句最终都将被转换成二进制的机器指令 C.C语言程序经编译形成的二进制代码可以直接执行 D.C语言中的函数不可以单独进行编译 4.算法是指为解决某个特定问题而采取的正确且有限的步骤,下面不属于算法的5个特性的是( )。 A.有零个输入或多个输入B.高效性C.有穷性D.确定性5.以下能正确定义且赋初值的语句是( )。 A.int n1=n2=10; B.char c=32; C.float f=f+1.1; D.double x=12.3E2.5 6.有以下程序: main() { char a='a',b; printf("%c",++a); printf("%c\n",b=a++); } 程序运行后的输出结果是( )。 A.bb B.bc C.ab D.ac 7.以下程序段的输出结果是( )。 int a=1234; printf("%2d ",a); A.12 B.34 C.1234 D.提示出错 8.有以下程序:

国家开放大学C语言程序设计A第一次形考任务及答案

一、选择题(共40分,每小题2分) 题目1 在每个C语言程序中都必须包含有这样一个函数,该函数的函数名为()。 A. main B. MAIN C. name D. function 题目2 C语言源程序文件的缺省扩展名为()。 A. cpp B. exe C. obj D. c 题目3 由C语言目标文件连接而成的可执行文件的缺省扩展名为()。 A. cpp B. exe C. obj D. c 题目4 程序运行中需要从键盘上输入多于一个数据时,各数据之间应使用的分隔符为()。 A. 空格或逗号 B. 逗号或回车 C. 逗号或分号

D. 空格或回车 题目5 每个C语言程序文件的编译错误被分为()。 A. 1类 B. 2类 C. 3类 D. 4类 题目6 不符合C语言规定的复合语句是()。 A. {} B. {;} C. {x=0;} D. {y=10} 题目7 C语言程序中的基本功能模块为()。 A. 表达式 B. 标识符 C. 语句 D. 函数 题目8 在一个函数定义中,函数头包括的3个部分依次为函数类型、函数名和()。 A. 表达式 B. 语句 C. 参数表

D. 函数体 题目9 在一个程序文件中,若要使用#include命令包含一个用户定义的头文件,则此头文件所使用的起止定界符为一对()。 A. 尖括号 B. 双引号 C. 单引号 D. 花括号 题目10 在C语言程序中,多行注释语句使用的开始标记符为()。 A. // B. /* C. */ D. ** 题目11 在printf()函数调用的格式字符串中,若使用格式符为“%c”,则对应输出的数据类型为()。 A. char B. int C. float D. double 题目12 在printf()函数调用的格式字符串中,若使用格式符为“%5d”,则规定对应输出的数据占用的字符位置个数为()。

C语言程序设计第四版第六章答案_谭浩强

1、用筛选法求100之内的素数。解: #include #include int main() {int i,j,n,a[101]; for (i=1;i<=100;i++) a[i]=i; a[1]=0; for (i=2;i int main() {int i,j,min,temp,a[11]; printf("enter data:\n"); for (i=1;i<=10;i++)

{printf("a[%d]=",i); scanf("%d",&a[i]); } printf("\n"); printf("The orginal numbers:\n"); for (i=1;i<=10;i++) printf("%5d",a[i]); printf("\n"); for (i=1;i<=9;i++) {min=i; for (j=i+1;j<=10;j++) if (a[min]>a[j]) min=j; temp=a[i]; a[i]=a[min]; a[min]=temp; } printf("\nThe sorted numbers:\n"); for (i=1;i<=10;i++) printf("%5d",a[i]); printf("\n"); return 0; } 3、求一个3×3的整型矩阵对角线元素之和。解: #include int main() { int a[3][3],sum=0; int i,j; printf("enter data:\n"); for (i=0;i<3;i++) for (j=0;j<3;j++) scanf("%3d",&a[i][j]); for (i=0;i<3;i++) sum=sum+a[i][i]; printf("sum=%6d\n",sum);

C语言程序设计第三版谭浩强课后习题答案完整版

1.6 编写一个程序,输入a、b、c 三个值,输出其中最大值。 课后习题答案完整版 第一章 1.5 请参照本章例题,编写一个C 程序,输出以下信息: ************************** Very Good! ************************** 解:mian() {int a,b,c,max; printf( “请输入三个数a,b,c:\n ” ); scanf( “%d,%d,%”d ,&a,&b,&c); C语言程序设计第三版谭浩强 解: mian() {printf( ”); “************************** printf( “”X “ n” ); printf( “Very Good!” \ n”); printf( “”X “ n” ); printf( “************************** ); max=a; if(max

{char #include c1='a',c2='b',c3= 'c',c4= ' \101 ',c5= ' 116'; printf( “a%cb%c n”,c1,c2,c 3); printf( “ b%c %c” ,c4,c5); } 解: aa 口bb 口口口cc 口口口口口口abc A 口N 3.7 要将"China" 译成密码,译码规律是:用原来字母后面的第 4 个字母代替原来的字母.例如,字母"A" 后面第 4 个字母是"E" . "E"代替"A"。因此,"China"应译为"Glmre" 。请编一程序,用赋初值的方法使cl 、c2、c3、c4、c5 五个变量的值分别为, ' C'、h'、i '、n'、a'经过运算,使cl、c2、c3、c4、c5分别变为'G'、' I '、' m >' r'、’ e',并输出。main() { char c1=' C' ,c2=' h' ,c3= ' i ' ,c4= ' n' ,c 5=' a' ; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; printf(" 密码是%c%c%c%c%c\n",c1,c2,c3,c4,c5); } 运行结果: 密码是GImre 3.9 求下面算术表达式的值。 解: 1 )x+a%3*(int)(x+y)%2/4

C语言程序设计习题参考答案第四章(数组)

第四章数组参考答案 一、选择题:1、 B 2、C 3、D 4、C 5、C 6、B 7、D 8、B 9、B 10、A 二、填空题: 1、首地址 2、按行存放 3、一个字符 4、′\0′ 5、字符数组名或字符串 6、9 0 7、6 8、j-1 str[j-1] 9、62 10、s1[i]=s2[i]; 三、改错题 1、错误语句:int a[3][ ]={2,4,6,8,10,12,14,16,18}; 正确语句:int a[ ][3]={2,4,6,8,10,12,14,16,18}; 2、错误语句:if (str[2]>string) string=str[2]; 正确语句:if (strcmp(str[2],string)>0) strcpy(string,str[2]); 3、错误语句:char c[5]={'C','h ','i','n','a '}; 正确语句:char c[6]={'C','h ','i','n','a '};或char c[ ]={“China”}; 4、错误语句:int a[3]={3*0} ; 正确语句:int a[4]; 5、错误语句:scanf(“%d%d%d”,&a); 正确语句:for (i=0; i<3; i++) scanf(“%d”,&a[i]); 或scanf(“%d%d%d”, &a[0], &a[1], &a[2]); 四、编程题 1、用数组来处理,求解Fibonacci数列前40项:1,1,2,3,5,8,13,21…。 #include void main() { int i; int t[40]={1,1}; for(i=2;i<40;i++) t[i]=t[i-2]+t[i-1]; for(i=0;i<40;i++) { if(i%5==0) printf("\n"); printf("%15d",t[i]); } } 2、用选择法对20个整数排序(由大到小)。 #include void main() {int i,j,min,t,x[20]; for(i=0;i<20;i++) scanf("%d",&x[i]); for(i=0;i<19;i++) {min=i; for(j=i+1;j<20;j++) if(x[min]>x[j])min=j; t=x[i];

C程序设计语言 (第二版) 课后答案第一章

Chapter 1 Exercise 1-1 Run the “hello world” program on your system. Experiment with leaving out parts of the program, to see what error message you get. #include int main() { printf("hello, "); printf("world"); printf("\n"); return 0; } Exercise 1-2 Experiment to find out what happens when printf’s argument string contains \c, where c is some character not list above. Exercise 1-3 Modify the temperature conversion program to print a heading above the table. #include int main() { float fahr, celsius; float lower, upper, step; lower = 0; upper = 300; step = 20; fahr = lower; printf("Fahrenheit temperatures and their centigrade or Celsius equivalents\n"); while (fahr <= upper) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } return 0; }

C语言程序设计入门经典例题

1、加法练习程序:由用户通过键盘输入加数和被加数,程序显示加法式子,用户通过键盘作答后,程序给出正确与错误提示信息。要求:利用C的选择语句if条件语句或switch 开关语句,键盘输入数据前,程序会出被输入数据的信息提示。 #include void main(){ int a,b,c; printf("please put an addend:\n");scanf("%d",&a); printf("please put an augend:\n");scanf("%d",&b); printf("please answer the question:a+b=?");scanf("%d",&c); if(a+b==c) printf("very good!"); else printf("wrong! "); } 个人感觉这个程序最不好的地方是,不管回答正确还是回答错误,程都会立即结束,所以我用了do…while实现一个回答错误之后获得再次回答机会的循环,则程序修改如下: #include void main(){ int a,b,c; printf("请输入一个加数:\n");scanf("%d",&a); printf("请输入一个被加数:\n");scanf("%d",&b); do { printf("请回答:a+b=");scanf("%d",&c); if(a+b!=c) printf("请再想一想\n"); } while(a+b==c) printf("回答正确!"); } 2、判闰年程序:判断某一年是否闰年? 要求:程序首先提示用户从键盘输入4位数年份,程序能显示“XXXX年是闰年”或“XXXX年不闰年”的输出信息。 算法:year 是闰年,即year能被4整除但不能被100整除,或 year 能被 400 整除。其对应的C逻辑表达式为:(year % 4 = = 0 && year % 100 != 0) || (year % 400 = = 0) 或: (!(year % 4) && year % 100) || !(year % 400 ) 程序中可设置1个变量leap来代表是否闰年的信息,闰年:leap=1; 不闰年:leap=0。 #include void main() int year; printf("请输入4位数年份:");scanf("%d",&year); if(year%4==0&&year%100!=0||year%400==0)//闰年就是能被4整除但不能被100整除及能被400整除的

C语言程序设计(谭浩强)第四版-课后答案

第一章程序设计和C语言【第15页】 1-5 #include int main ( ) { printf ("**************************\n\n"); printf(" Very Good!\n\n"); printf ("**************************\n"); return 0; } 1-6 #include int main() {int a,b,c,max; printf("please input a,b,c:\n"); scanf("%d,%d,%d",&a,&b,&c); max=a; if (max #include int main() {float p,r,n; r=0.1; n=10; p=pow(1+r,n); printf("p=%f\n",p); return 0; }

3-2-1 #include #include int main() {float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5; p=1000; r5=0.0585; r3=0.054; r2=0.0468; r1=0.0414; r0=0.0072; p1=p*((1+r5)*5); // 一次存5年期 p2=p*(1+2*r2)*(1+3*r3); // 先存2年期,到期后将本息再存3年期 p3=p*(1+3*r3)*(1+2*r2); // 先存3年期,到期后将本息再存2年期 p4=p*pow(1+r1,5); // 存1年期,到期后将本息存再存1年期,连续存5次 p5=p*pow(1+r0/4,4*5); // 存活期存款。活期利息每一季度结算一次 printf("p1=%f\n",p1); // 输出按第1方案得到的本息和 printf("p2=%f\n",p2); // 输出按第2方案得到的本息和 printf("p3=%f\n",p3); // 输出按第3方案得到的本息和 printf("p4=%f\n",p4); // 输出按第4方案得到的本息和 printf("p5=%f\n",p5); // 输出按第5方案得到的本息和 return 0; } 3-2-2 #include #include int main() {double r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5; p=1000; r5=0.0585; r3=0.054; r2=0.0468; r1=0.0414; r0=0.0072; p1=p*((1+r5)*5); // 一次存5年期 p2=p*(1+2*r2)*(1+3*r3); // 先存2年期,到期后将本息再存3年期 p3=p*(1+3*r3)*(1+2*r2); // 先存3年期,到期后将本息再存2年期 p4=p*pow(1+r1,5); // 存1年期,到期后将本息存再存1年期,连续存5次

C语言程序设计第三版习题库答案

C 语言程序设计(第三版)习题库 1、设圆半径r=,圆柱高h=3,求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积。用scanf 输入数据,输出计算结果,输出时要求文字说明,取小数点后两位数字。请编程序。 #include<> main(){ floatr,h,C1,Sa,Sb,Va,Vb; scanf(__”%f ”__,&r); scanf(”%d ”,__&h _);; C1=2**r; Sa=*r*r; Sb=4*Sa; Va=4**r*r*r/3; Vb=Sa*h; printf(___”Cl=%.2fSa=%.2fSb=%.2fVa=%.2fVb=%.2f ”,Cl,Sa,Sb,Va,Vb ); } 2、输入一个华氏温度,要求输出摄氏温度。公式为c=5(F-32)/9 输出要求有文字说明,取位2小数。 #include<> main(){ floatF,c; scanf("%f",&F); ____c=5*(F-32)/9______; printf("c=%.2f",c); } 3、有一函数:?? ???≥-<≤-<=10113101121x x x x x x y 写一程序,输入x 值,输出y 值。 #include<> main(){ intx,y; printf("输入x :"); scanf("%d",&x); if(x<1){/*x<1*/ y=x; printf("x=%3d,y=x=%d\n",x,y);

}elseif(____x<10_______){/*1≤x-10*/ _____y=2*x-1_______; printf("x=%3d,y=2*x-1=%d\n",x,y); }else{/*x≥10*/ y=3*x-11; printf("x=%3d,y=3*x-11=%d\n",x#include"" main() { intx,y; scanf("%d",&x); if(x<1) {y=x;} elseif(x>=1&&x<10) {y=2*x-1;} else {y=3*x-11;} printf("%d",y); }#include"" main() { intx,y; scanf("%d",&x); if(x<1) {y=x;} elseif(x>=1&&x<10) {y=2*x-1;} else {y=3*x-11;} printf("%d\n",y); }#include"" main() { intx,y; scanf("%d",&x); if(x<1) {y=x;} elseif(x>=1&&x<10) {y=2*x-1;} else {y=3*x-11;} printf("%d",y); }scanf("%d",&x);

C语言程序设计教程 清华大学出版社 陈明主编 第4章答案

第4章习题解答 一、选择题 1.在C语言中,函数返回值的类型最终取决于 A)函数定义时的函数首部所说明的函数类型B)return语句中表达式值的类型C)调用函数时主调函数所传递的实参类型D)函数定义时形参的类型 解析:函数返回值类型最终取决于定义函数时所定义的函数类型。 答案:A 2.设函数Fun()的定义形式为: void Fun(char ch, float x){} 则以下对函数Fun()的调用语句中,正确的是。 A)Fun("abc", 3.0); B)t = Fun('D', 16.5); C)Fun('65', 2.8); D)Fun(32, 32); 解析:选项A中"abc"是字符串,与形参不匹配:由于Fun函数的返回值类型为void,所以选项B中进行了赋值操作是不正确的;在选项C中,'65'是不合法的字符形式,单引号中应该是单个字符。只有选项D才是合法的函数调用形式。 答案:D 3.有以下程序: /* 文件路径名:ex4_1_3\main.c */ #include /* 标准输入/输出头文件*/ int f1(int x, int y) { return x > y ? x : y; } int f2(int x, int y) { return x > y ? y : x; } int main(void) /* 主函数main() */ { int a = 4, b = 3, c = 5, d = 2, e, f, g; /* 定义变量*/ e = f2(f1(a, b), f1(c, d)); f = f1(f2(a, b), f2(c, d));/* 调用函数*/ g = a + b + c + d - e - f; /* 算术运算*/ printf("%d,%d,%d\n", e, f, g); /* 输出e,f,g */ return 0; /* 返回值0, 返回操作系统*/ } 程序运行后的输出结果是。 A)4,3,7 B)3,4,7 C)5,2,7 D)2,5,7 解析:函数f1用于求两个数中的最大数,而函数f2用于求两个数中的最小数。因此有:e=f2(f1(a,b),f1(c,d))=f2(f1(4,3),f1(5,2))= f2(4,5)=4; f=f1(f2(a,b),f2(c,d)) =f1(f2(4,3),f2(5,2)) =f1(3,2)=3; g=a+b+c+d-e-f=4+3+5+2-4-3=7。

(完整版)C程序设计语言复习题(试题及答案版)

一.填空题 26.C#源程序的后缀名为______.cs________。 26.C#中每个int 类型的变量占用____4___个字节的内存。 26.C#的每行语句以________分号_______结尾。 26.布尔型的变量可以赋值为关键字_____true__________或_____false_________。 26.如果int x的初始值为5,则执行表达式x - =3之后,x的值为_____2_________。 26.do...while语句在执行循环体_____之后________测试语句是否满足循环条件。 26.关键字_______class________表示一个类的定义。 26.如果一个类包含一个或多个抽象方法,它是一个_________抽象_____________类。 26.try块运行后,总是会执行_________finally_____________块中的代码。 26.一个数组如果有两个索引值,那么它是__________二维__________数组。 二.单项选择题 1.在对SQL Server 数据库操作时应选用()。 A、SQL Server .NET Framework 数据提供程序; B、OLE DB .NET Framework 数据提供程序; C、ODBC .NET Framework 数据提供程序; D、Oracle .NET Framework数据提供程序; 2.下列选项中,()是引用类型。 A、enum类型 B、struct类型 C、string类型 D、int类型 3.C#的数据类型有() A、值和调用类型; B、值和引用类型; C、引用和关系类型; D、关系和调用类型 4.下列描述错误的是() A、类不可以多重继承而接口可以; B、抽象类自身可以定义成员而接口不可以; C、抽象类和接口都不能被实例化; D、一个类可以有多个基类和多个基接口; 5.下列关于构造函数的描述正确的是() A、构造函数可以声明返回类型。 B、构造函数不可以用private修饰 C、构造函数必须与类名相同 D、构造函数不能带参数 6.int[][] myArray3=new int[3][]{new int[3]{5,6,2},new int[5]{6,9,7,8,3},new int[2]{3,2}}; 那么myArray3[2][2]的值是( )。 A、9 B、2 C、6 D、越界 7.接口是一种引用类型,在接口中可以声明(),但不可以声明公有的域或私有的成员变量。 A、方法、属性、索引器和事件; B、方法、属性信息、属性; C、索引器和字段; D、事件和字段; 8.在https://www.360docs.net/doc/de15056343.html,中,对于Command对象的ExecuteNonQuery()方法和ExecuteReader()方法,下面叙述错误 的是()。 A、insert、update、delete等操作的Sql语句主要用ExecuteNonQuery()方法来执行; B、ExecuteNonQuery()方法返回执行Sql语句所影响的行数。 C、Select操作的Sql语句只能由ExecuteReader()方法来执行; D、ExecuteReader()方法返回一个DataReder对象; 9.Winform中,关于ToolBar控件的属性和事件的描述不正确的是( )。 A、Buttons属性表示ToolBar控件的所有工具栏按钮 B、ButtonSize属性表示ToolBar控件上的工具栏按钮的大小,如高度和宽度 C、DropDownArrows属性表明工具栏按钮(该按钮有一列值需要以下拉方式显示)旁边是否显示下箭 头键 D、ButtonClick事件在用户单击工具栏任何地方时都会触发

《C语言程序设计》课后习题答案(第四版)谭浩强

第1章程序设计和C语言1 1.1什么是计算机程序1 1.2什么是计算机语言1 1.3C语言的发展及其特点3 1.4最简单的C语言程序5 1.4.1最简单的C语言程序举例6 1.4.2C语言程序的结构10 1.5运行C程序的步骤与方法12 1.6程序设计的任务14 1-5 #include int main ( ) { printf ("**************************\n\n"); printf(" Very Good!\n\n"); printf ("**************************\n"); return 0; } 1-6#include int main() {int a,b,c,max; printf("please input a,b,c:\n"); scanf("%d,%d,%d",&a,&b,&c); max=a; if (max

2.5结构化程序设计方法34 习题36 第章最简单的C程序设计——顺序程序设计37 3.1顺序程序设计举例37 3.2数据的表现形式及其运算39 3.2.1常量和变量39 3.2.2数据类型42 3.2.3整型数据44 3.2.4字符型数据47 3.2.5浮点型数据49 3.2.6怎样确定常量的类型51 3.2.7运算符和表达式52 3.3C语句57 3.3.1C语句的作用和分类57 3.3.2最基本的语句——赋值语句59 3.4数据的输入输出65 3.4.1输入输出举例65 3.4.2有关数据输入输出的概念67 3.4.3用printf函数输出数据68 3.4.4用scanf函数输入数据75 3.4.5字符数据的输入输出78 习题82 3-1 #include #include int main() {float p,r,n; r=0.1; n=10; p=pow(1+r,n); printf("p=%f\n",p); return 0; } 3-2-1 #include #include int main() {float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5; p=1000;

国家开放大学C语言程序设计A第一次形考任务与答案(20201127230652)

一、选择题(共40分,每小题2分) 1 题目1 在每个C语言程序中都必须包含有这样一个函数,该函数的函数名为()* A.main B. MAIN C. name D. function 题目2 1 C语言源程序文件的缺省扩展名为()。 A. cpp B. exe C. obj * D.c 题目3 1 由C语言目标文件连接而成的可执行文件的缺省扩展名为()。 A. cpp B. exe C. obj D. c 题目4

程序运行中需要从键盘上输入多于一个数据时,各数据之间应使用的分隔符为() A. 空格或逗号 B. 逗号或回车 C. 逗号或分号

* D.空格或回车 题目5 1 每个C语言程序文件的编译错误被分为()。 A. 1类 ” B.2 类 C. 3类 D. 4类 题目6 不符合C语言规定的复合语句是()。 A. {} B. {;} C. {x=0;} + D.{y=10} 题目7 1 C语言程序中的基本功能模块为()。 A. 表达式 B. 标识符 C. 语句 D. 函数 题目8 在一个函数定义中,函数头包括的3个部分依次为函数类型、函数名和()

B. 语句 C. 参数表 D. 函数体 题目9 1 在一个程序文件中,若要使用#include命令包含一个用户定义的头文件,则此头文件所使用 的起止定界符为一对()。 A. 尖括号 * B.双引号 C. 单引号 D. 花括号 题目10 L 在C语言程序中,多行注释语句使用的开始标记符为()。 A. // + B./* C.*/ D ** 题目11 1 在printf()函数调用的格式字符串中,若使用格式符为“%C',则对应输出的数据类型为()。

C语言程序设计第四版第七章答案-谭浩强

第七章函数 7.1写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果两个整数由键盘输入。 maxyueshu(m,n) int m,n; { int i=1,t; for(;i<=m&&i<=n;i++) {if(m%i==0&&n%i==0) t=i; } return(t); } minbeishu(m,n) int m,n; {int j; if(m>=n) j=m; else j=n; for(;!(j%m==0&&j%n==0);j++); return j; } main() {int a,b,max,min; printf("enter two number is: "); scanf("%d,%d",&a,&b); max=maxyueshu(a,b); min=minbeishu(a,b); printf("max=%d,min=%d\n",max,min); } 7.2求方程的根,用三个函数分别求当b2-4ac大于0、等于0、和小于0时的根,并输出结果。从主函数输入a、b、c的值。 #include"math.h" float yishigen(m,n,k) float m,n,k; {float x1,x2; x1=(-n+sqrt(k))/(2*m); x2=(-n-sqrt(k))/(2*m); printf("two shigen is x1=%.3f and x2=%.3f\n",x1,x2); } float denggen(m,n) float m,n; {float x; x=-n/(2*m); printf("denggen is x=%.3f\n",x); }

C语言程序设计教程第三版(李凤霞)习题答案

教材习题答案 第一章 习题 一、单项选择题 1. C 2. B 3. B 4. C 5. D 6. A 7. C 8. A 二、填空题 1. 判断条件 2. 面向过程编程 3. 结构化 4. 程序 5. 面向对象的程序设计语言 6. 基本功能操作、控制结构 7. 有穷性 8. 直到型循环结构 9. 算法 10.可读性 11.模块化 12.对问题的分解和模块的划分

习题 一、单项选择题 1. B 2. D 3. C 4. B 5. A 6. A 7. B 8.C 二、填空题 1. 主 2. C编译系统 3. 函数、函数 4. 输入输出 5. 头 6. .OBJ 7. 库函数 8. 文本 第三章 习题 一、单项选择题 1. D 2. B 3. A

5. C 6. D 7. D 8. B 9. B 10.C 11.A 12.D 13.C 14.C 15.C 16.A 17.C 18.C 19.C 20.D 21.A 22.D 23.D 24.D,A 25.D 26.A 27.B 二、填空题 1. 补码 2. 308 10 - ±

~)308 10 ,15 —6 2. 308 10 - ± (~)308 10 ,15

6 3. 逻辑 4. 单目,自右向左 5. 函数调用 6. a 或 b ( 题目有错 , 小括号后面的 c<=98 改成( c>=97&&c<=98 )就可以得到所给的答案了) 7.

8. 65 , 89 第四章 习题 一、单项选择题 1. D 2. C 3. D 4. A 5. D 6. B 7. A 8. C 9. B 10.B 二、填空题 1. 一

C语言程序设计教程第四章练习试题解析[1]

单选题 1、关于C语言中的函数,下列描述正确的是() A.函数的定义可以嵌套,但函数的调用不可以嵌套 B.函数的定义不可以嵌套,但函数的调用可以嵌套 C.函数的定义和函数的嵌套均不可以嵌套 D.函数的定义和函数的调用均不可以嵌套 答案:B 解析:函数的定义不可以嵌套,但函数的调用可以嵌套 2、定义一个函数:exce((v1, v2), (v3,v4,v5),v6); 在该函数调用时,实参的个数为()个 A.3 B.4 C.5 D.6 答案:A 解析:(v1, v2), (v3,v4,v5)和v6一共三个实参 3、关于C语言中print()函数与scanf()函数,下列描述中正确的是() A.printf()函数可以向任何地方输出数据 B.printf()只向控制台输出数据 C.scanf()只能输入英文字母和数字 D.scanf()函数可以识别输入的空格 答案:B 解析:printf()是向控制台输出的函数 4、在C语言中,内部函数需要添加的关键字是() A.extern B.static C.this D.auto 答案:B 解析:在定义内部函数时,需要在函数的返回值类型前面添加static关键字(又称为静态函数)。 5、当调用时,会调用内部函数 A.当调用时,会调用内部函数 B.当调用时,会调用外部函数 C.当调用时,会调用两次,先调用内部函数再调用外部函数 D.都不调用,会报错 答案:A 解析:当内部函数与外部函数重名时,会优先调用内部函数 6、在C语言中,声明外部函数需要添加的关键字是() A.extern B.static C.this

答案:A 解析:声明外部函数的方式是在函数的返回值类型前面添加extern关键字 7、关于C语言中的局部变量,下列描述中错误的是() A.局部变量就是在函数内部声明的变量 B.局部变量只在函数内部有效 C.局部变量只有当它所在的函数被调用时才会被使用 D.局部变量一旦被调用,其生存周期持续到程序结束 答案:D 解析:当函数调用结束时局部变量就会失去作用 8、关于C语言中的全局变量,下列描述中正确的是() A.全局变量的作用域一定比局部变量的作用域范围大 B.静态类别变量的生存周期贯穿于整个程序的运行期间 C.函数的形参都属于全局变量 D.未在定义语句中赋初值的auto变量和static变量的初值都是随机值 答案:B 解析:选项1不对:如果程序中只有一个主函数,则在整个程序运行中,局部变量都在起作用; 选项3不对:除了C++的引用类型参数,所有函数的形参都是局部变量; 选项4不对:static变量定义时就存储地全局区,初始化为0; 9、当全局变量与局部变量重名时,那么在调用时() A.局部变量会被屏蔽 B.全局变量会被屏蔽 C.都不会调用,系统会报错 D.会调用两次,先调用局部变量,再调用全局变量 答案:B 解析:当局部变量与全局变量重名时,全局变量会被屏蔽。 10、在C语言中,关于变量的作用域,下列描述中错误的是() A.局部变量只在整个函数的运行周期中有效 B.全局变量的作用域为整个程序的运行周期 C.当全局变量与局部变量重名时,局部变量会屏蔽掉全局变量 D.全局变量会覆盖掉所有与它重名的局部变量 答案:D 解析:当全局变量与局部变量重名时,全局变量会被屏蔽掉 11、在C语言中,如果在一个函数的复合语句中定义了一个变量,则该变量() A.只在该复合语句中有效,在该复合语句外无效 B.在该函数中任何位置都有效 C.在本程序的原文件范围内均有效 D.此定义方法错误,其变量为非法变量 答案:D 解析:1、2与3错:函数复合语句中定义的变量在该复合语句中引用;但如果函数含有该变量,则在函数中可以引用该变量 4对:在C++中在复合语句中定义变量为合法,但在C语言中是非法的 12、C语言程序的基本单位是()。

C语言程序设计实习报告

手机通讯录管理系统一、设计题目的任务和内容 任务:本程序是非数值计算型算法设计,我设计出了通讯录管理系统的基本功能,并设计了简单的界面。本程序主要考察对自定义函数的熟悉程度,本程序主要使用的是数组的相关操作,包括结构体数组的输入、输出、查找、删除等。我设计的这个通讯录系统也初步实现了人员信息的显示、删除、查找、修改、添加等必要的功能。 具体要求: ◆建立通讯录信息,信息包含、姓名、分类(1、办公类2、个人类3、商务类)、电话、 电子邮箱等; ◆将通讯录保存在文件中; ◆查看功能:可按1、办公类2、个人类3、商务类分类查询,当选中其中一类时,显示出 此类所有数据中的姓名和电话号码。 ◆增加功能:能录入新数据。当录入重复姓名和电话号码时,则提示数据录入重复并取消 录入;当通讯录中超过15条信息时,存储空间已满,不能录入新数据;录入的新数据能按递增的顺序自动进行条目编号。 ◆修改功能:选中某个人的姓名是,可对此人的相应数据进行修改。 ◆删除功能:选中某个人的姓名是,可对此人的相应数据进行删除并自动调整后续条目编 号。 系统功能需求分析: 查询:查询通讯录记录 添加:添加通讯录记录 显示:显示通讯录记录 删除:删除通讯录记录 修改:修改通讯录记录

二、总体设计思路 本系统首先设计出了主函数,然后根据去要实现的功能分别设计拉,显示、删除、查找、修改、添加功能的子函数。在显示的函数设计过程中首先运用拉文件的读取等必要的知识通过把文件的内容读取到内存然后打印出来。删除函数的设计是首先从文件内找到要删除的人,然后通过写一个实现删除目的的FOR循环将该人从内存里删除,然后再用写的方式打开文件,将内存内的信息写入文件,实现删除。查找也是先打开文件,用一个字符串对比的方法找出用户要查找的人,然后打印出来。修改也大体和删除相同,添加函数在设计时通过申请一片空间,然后通过指针将需要添加的信息添加进去,从而实现添加功能。 系统功能模块图: 输入新信息:可输入新的数据。 添加:可以添加通讯录记录,依次输入姓名、电话号码、分类、电子邮箱后,会提示是否继续添加。 删除:输入姓名删除,输入欲删除的那个人的名字后,会自动删除他(她)的记录内容 查询:可按1、办公类2、个人类3、商务类分类查询 修改:输入欲修改的那个人的名字后,再依次输入姓名、电话号码、分类、电子邮箱即可完成修改。 下面就是总的程序流程图:

相关文档
最新文档