c语言课后答案-电子科技大学出版社Word版
C语言程序设计教程 课后习题参考答案

else if( carType == 2 ) //富康
{
if( distance < 3 )
totalMoney = 8.0;
else
totalMoney = 8 + fukang * (distance – 3);
}
else if( carType == 3 ) //富康
{
if( distance < 3 )
(3) 7BAB4BAB1BC
(4) ABABABC
(5) ******
******
******
******
4.
(1) a != b
(2) n / 10
(3) scanf(“%d”, &a);
5.(3)行int fac = 1, sum = 0;
6.
(1)
#include <stdio.h>
main( )
r = 2.5;
h = 3.5;
v = 3.14*r*r*h;
printf(“v=%lf\n”, v);
}
(2)
#include <stdio.h>
main( )
{
char ch;
ch = getchar( );
printf(“%c\n”, ch + 32);
}
(3)
#include <stdio.h>
scanf("%d", &carType);
printf("请输入您乘车的总路程:");
scanf("%lf", &distance);
if( carType == 1)//夏利
c语言课后习题答案

c语言课后习题答案C语言课后习题答案C语言作为一门广泛应用于编程领域的高级编程语言,对于学习者来说是必不可少的一门课程。
在学习过程中,课后习题是巩固知识、提高编程能力的重要环节。
然而,有时候我们可能会遇到一些难以解答的问题,或者对于某些题目的解答存在疑惑。
在这篇文章中,我将为大家提供一些C语言课后习题的答案,并解释一些常见问题。
1. 编写一个程序,从键盘输入一个整数,判断该数是否为素数。
解答:```c#include <stdio.h>int isPrime(int num) {if (num <= 1) {return 0;}for (int i = 2; i * i <= num; i++) {if (num % i == 0) {return 0;}}return 1;}int main() {int num;printf("请输入一个整数:");scanf("%d", &num);if (isPrime(num)) {printf("%d是素数。
\n", num);} else {printf("%d不是素数。
\n", num);}return 0;}```以上代码中,我们定义了一个函数`isPrime`,用于判断一个数是否为素数。
在`main`函数中,我们从键盘输入一个整数,并调用`isPrime`函数进行判断。
如果返回值为1,则说明该数是素数,否则不是素数。
2. 编写一个程序,计算并输出1到100之间所有能被3整除但不能被5整除的数之和。
解答:```c#include <stdio.h>int main() {int sum = 0;for (int i = 1; i <= 100; i++) {if (i % 3 == 0 && i % 5 != 0) {sum += i;}}printf("1到100之间所有能被3整除但不能被5整除的数之和为:%d\n", sum);return 0;}```以上代码中,我们使用`for`循环遍历1到100之间的所有整数,使用条件判断语句`if`筛选出能被3整除但不能被5整除的数,并将它们累加到`sum`变量中。
(完整版)数据结构_c语言描述(第二版)答案_耿国华_西安电子科技大学

第1章绪论2.(1)×(2)×(3)√3.(1)A(2)C(3)C5.计算下列程序中x=x+1的语句频度for(i=1;i<=n;i++)for(j=1;j<=i;j++)for(k=1;k<=j;k++)x=x+1;【解答】x=x+1的语句频度为:T(n)=1+(1+2)+(1+2+3)+……+(1+2+……+n)=n(n+1)(n+2)/66.编写算法,求一元多项式p n(x)=a0+a1x+a2x2+…….+a n x n的值p n(x0),并确定算法中每一语句的执行次数和整个算法的时间复杂度,要求时间复杂度尽可能小,规定算法中不能使用求幂函数。
注意:本题中的输入为a i(i=0,1,…n)、x和n,输出为P n(x0)。
算法的输入和输出采用下列方法(1)通过参数表中的参数显式传递(2)通过全局变量隐式传递。
讨论两种方法的优缺点,并在算法中以你认为较好的一种实现输入输出。
【解答】(1)通过参数表中的参数显式传递优点:当没有调用函数时,不占用内存,调用结束后形参被释放,实参维持,函数通用性强,移置性强。
缺点:形参须与实参对应,且返回值数量有限。
(2)通过全局变量隐式传递优点:减少实参与形参的个数,从而减少内存空间以及传递数据时的时间消耗缺点:函数通用性降低,移植性差算法如下:通过全局变量隐式传递参数PolyValue(){ int i,n;float x,a[],p;printf(“\nn=”);scanf(“%f”,&n);printf(“\nx=”);scanf(“%f”,&x);for(i=0;i<n;i++)scanf(“%f ”,&a[i]); /*执行次数:n次*/p=a[0];for(i=1;i<=n;i++){ p=p+a[i]*x; /*执行次数:n次*/x=x*x;}printf(“%f”,p);}算法的时间复杂度:T(n)=O(n)通过参数表中的参数显式传递float PolyValue(float a[ ], float x, int n){float p,s;int i;p=x;s=a[0];for(i=1;i<=n;i++){s=s+a[i]*p; /*执行次数:n次*/p=p*x;}return(p);}算法的时间复杂度:T(n)=O(n)第2章线性表习题1.填空:(1)在顺序表中插入或删除一个元素,需要平均移动一半元素,具体移动的元素个数与插入或删除的位置有关。
C++课后习题答案(电大,李宁主编)【范本模板】

第一章C++语言概述自测题P21。
1—1 CP8 1。
2-1 形式参数表 1.2-2 main 1。
2—3 ;1。
2-4 } 1.2—5 main 1.2-6 显示器输出流1。
2-7 键盘输入流1。
2—8 头文件 1.2—9 int f( );1.2—10 124 1。
2-11 × 1。
2—12 ×1.2-13 √ 1。
2—14 √ 1.2—15 ×1。
2—16 √ 1。
2-17 ABCFG 1.2-18 D1.2—19 DP11 1。
3—1 OBJ 1。
3—2 source.cpp习题一(P12)1—11.C 2.类型修饰符3.main4.语句以分号(;)结束5.{6.<〈7.〉〉8..h 9.source.obj10.Main 11.char *Hi(const char *); 12.331-21.√2.√3.×4.×5.√6.×7.√8.×1-31.ABFI 2.D 3.ABCD 4.B1—4# include 〈iostream。
h>void main( ){cout <<”C++语言程序设计"<〈endl;}1—5#include <iostream。
h〉void main(){int i,j,k;cout <<"请输入3个整数:";cin 〉>i>>j>>k;cout <<endl〈〈i〈〈”+”<〈j<<”+”〈〈k<〈”=”〈<i+j+k〈<endl;}第二章基本数据类型与表达式自测题P152。
1—1 常量变量 2.1-2 存储格式所能实施的操作P18 2。
2—1 √ 2.2—2 × 2.2-3 8 (常量123456789123L超过了长整型表示的2。
C语言课后全部答案

第一章C语言初步一、选择题1.C2.D3.D4.A5.D6.B7.B 8.B 9.C 10.A二、思考与应用题1.A.字符串常量B.字符串常量C.整型常量D.浮点型常量(double)2.A.long doubleB.long intC.floatD.字符型常量3.A.整型常量B.字符型常量C.字符串常量D.字符串常量4.FirstExample: 10,w is Yz is 5.125.int main(void){return 0;}6.#include<stdio.h>int main(void){print f("Hello World");return 0;}7.#include<stdio.h>int main(void){printf("We are to learn correct");printf("C language here");return 0;}/*main*/8.#include<stdio.h>int main(void){/*Local Definition*/int a;float b;char c;/*Statements*/printf("The end of the program."); return 0;}/*main*/9.#include<stdio.h>int main(void){/*Local Definition*/int a;float b;char c,d;/*Statements*/printf("The end of the program."); return 0;}/*main*/10.#include<stdio.h>int main(void){/*Local Definition*/int a;char b,c,d;float e,f;/*Statements*/printf("The end of the program."); return 0;}/*main*/11.A.char option;B.int sum=0;C.float produce=1;12.A.short code;B.float salesTax=.0825;C.double sum=0;13.#include<stdio.h>int main(void){float cost=172.53;printf("The sales total is: $ %6.2f\n",cost);printf("^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^");return 0;}14.#include<stdio.h>int main(void){printf("******\n");printf("******\n");printf("******\n");printf("******\n");return 0;}15.#include<stdio.h>int main(void){printf("*\n");printf("**\n");printf("***\n");printf("****\n");return 0;}16.#include<stdio.h>int main(void){int x1=1;int x2=10;int x3=100;int x4=1000;int x5=10000;printf("%d %d %d %d %d\n",x1,x2,x 3,x4,x5);printf("%f %f %f %f %f\n",x1,x2,x 3,x4,x5);return 0;}17.#include<stdio.h>int main(void){int x;printf("Please enter an integer:\n");scanf("%d",&x);printf("The number as a character:%c\n",x);printf("The number as a decimal:%d\n",x);printf("The number as a float:%f\n",x);return 0;}三、编程题1.#include<stdio.h>int main(void){int x1;int x2;int x3;printf("Please enter three numbers:");scanf("%d%d%d",&x1, &x2, &x3);printf("\nYour numbers forword:\n");printf("%8d\n%8d\n%8d\n",x1,x2,x3 );printf("Your numbers reversed:\n");printf("%8d\n%8d\n%8d\n",x3,x2,x1 );return 0;}2.#include<stdio.h>int main(void){int x1;int x2;int x3;int x4;int x5;int x6;int x7;int x8;int x9;int x10;printf("Please enter 10 numbers:\n");scanf("%d%d%d%d%d%d%d%d%d%d",&x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8, &x9, &x10);printf("\nYour numbers are:\n");printf("%8d%8d\n",x1,x10);printf("%8d%8d\n",x2,x9);printf("%8d%8d\n",x3,x8);printf("%8d%8d\n",x4,x7);printf("%8d%8d\n",x5,x6);return 0;}3.#include<stdio.h>int main(void){int x1;int x2;int x3;int x4;int x5;int x6;int x7;int x8;int x9;printf("Input:\n");printf(" ");scanf("%d%d%d%d%d%d%d%d%d",&x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8, &x9);printf("\nOutput\n");printf("%8d,%d,%d\n",x1,x2,x3);printf("%8d,%d,%d\n",x4,x5,x6);printf("%8d,%d,%d\n",x7,x8,x9);return 0;}第二章表达式与简单程序设计一、选择题1.A2.D3.C4.B5.B6.D7.B二、思考与应用题1.A.2 B.8 C.8 D.0 E.11 2.A.4,4B.4,4 C.4,4 D.2,3 E.4,53.A.12 B.-27 C.14 D.2 E.4 4.A.22.52 B.4.5 C.13.333333 D.0.882353 E.0.075.ABCEF6.A.5 B.2 C.4 D.1 E.7.A.5 B.2 C.4 D.-3 E.6 8.A.5 B.294 C.4 D.29 E.9 9. 50 50 100 2510.#include<stdio.h>int main(void){float x;float y;float p;float s;float total;printf("Please enter x and y:\n");scanf("%f%f",&x,&y);p=x*y;s=x+y;total=s*s+p*(s-x)*(p+y);printf("total=%f",total);return 0;}11.#include<stdio.h>int main(void){int x,y,z;printf("Please enter two integers:\n");scanf("%d%d",&x,&y);z=x*y;printf("%d*%d=%d",x,y,z);return 0;}12.#include<stdio.h>int main(void){float x;int y;printf("Please enter a floating point number:\n");scanf("%f",&x);y=(int)x%10;printf("%d",y);return 0;}13.#include<stdio.h>int main(void){float x;int y;printf("Please enter a floating point number:\n");scanf("%f",&x);y=(int)x/10%10;printf("%d",y);return 0;}14.#include<stdio.h>int main(void){float x;float y;float c;float s;p rintf("Please enter length and width:\n");s canf("%f%f",&x,&y);c=2*(x+y);s=x*y;p rintf("The circumference is %f\n",c);p rintf("The measure of area is %f",s);r eturn 0;}15.#include<stdio.h>int main(void){int a;f loat r;p rintf("Please enter an angle(integer):\n");s canf("%d",&a);r=a/57.295779;p rintf("%d°is %.5f radians",a,r);r eturn 0;}16.#include<stdio.h>int main(void){float c;float F;printf("Please enter centigrade:\n");scanf("%f",&c);F=32+(c*180.0/100.0);printf("Fahrenheit is %f ",F);return 0;}17.(1)double m,v,KinEn;KinEn=m*v*v/2;(2)double b,c,res;res=(b+c)/(2*b*c);18.(1)#include<stdio.h>int main(void){int x=25;x=x+5;printf("%d\n",x);x=x+5;printf("%d\n",x);return 0;}(2)#include<stdio.h>int main(void){int x=10;x=x+2;printf("%d\n",x);x=x+2;printf("%d\n",x);return 0;}(3)#include<stdio.h>int main(void){int x=32;x=x*2;printf("%d\n",x);x=x*2;printf("%d\n",x);return 0;}三、编程题1.A.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x/12;printf("%f inches is %f feet ",x,y);return 0;}B.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x/36;printf("%f inches is %f yards ",x,y);return 0;}C.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x*2.54;printf("%f inches is %f centimeters ",x,y);return 0;D.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x/39.37;printf("%f inches is %f meters ",x,y);return 0;}2.#include<stdio.h>int main(void){int fib1;int fib2;int fib3;fib1=13+21;fib2=21+fib1;fib3=fib1+fib2;printf("%d,%d,%d",fib1,fib2,fib3);return 0;}3.#include<stdio.h>int main(void){int x;int b1;int b2;int b3;int b4;int b5;printf("Please enter an integer(0-32767):\n");scanf("%d",&x);b1=x%10;b2=x/10%10;b3=x/100%10;b4=x/1000%10;b5=x/10000%10;printf("%4d%4d%4d%4d%4d\n",b5,b4,b3,b2,b1);printf("%4d%4d%4d%4d%\n",b4,b3,b2,b1);printf("%4d%4d%4d\n",b3,b2,b1);printf("%4d%4d\n",b2,b1);printf("%4d",b1);return 0;}第三章分支程序设计一、选择题1.C2.D3.A4.B5.B6.A7.C8.C 9.A二、思考与应用题1.A.0 B.1 C.1 D.0 E.0 2.A.x>=y B.x<y C.x!=y D.x==y E.x>y3.A.0 B.1 C.1 D.1 E.0 4.x=4,y=3,z=25.x=4,y=1,z=26.x=4,y=2,z=27.x=4,y=1,z=28.x=0,y=0,z=19.x=2,y=0,z=210.x=0,y=0,z=111.x=0,y=0,z=112.x=-1,y=0,z=013.x=0,y=1,z=014.x=1,y=3,z=115.x=1,y=3,z=116.x=4,y=3,z=017.if(aChar=='E'){c++;printf("Value is E\n");}18.switch(ch){case ‘E’:case ‘e’:countE++;break;case ‘A’:case ‘a’:countA++;break;case ‘I’:case ‘i’:countI++;break;default:printf("Error-Not A,E,or I\a\n"); }三、编程题1.#include<stdio.h>main( ){int x,x1,x2,x3;char grade;printf("please enter three text scores(0-100):\n");scanf("%d%d%d",&x1,&x2,&x3);x=(x1+x2+x3)/3;if(x>=90)grade='A';else if(x>=70&&x<90)if(x3>90)grade='A';elsegrade='B';else if(x>=50&&x<70)if((x2+x3)/2>70)grade='C';elsegrade='D';else if(x<50)grade='F';printf("%c\n",grade);}2.#include<stdio.h>main( ){int year;printf("please enter a year:\n");scanf("%d",&year);if(year%4==0&&year%100!=0||year%400==0)printf("%d is a leap year!\n",year);elseprintf("%d is a non-leap year!\n",year);}3.#include<stdio.h>main( ){i ntyearNow,monthNow,dayNow,year,month,day,x;printf("Please enter the date today(month day year):\n");scanf("%d%d%d",&monthNow,&dayNow,&yearNow);printf("Please enter your birthday(month day year):\n");s canf("%d%d%d",&month,&day,&year);if(monthNow<month)x=yearNow-year-1;else if(monthNow==month)if(dayNow<day)x=yearNow-year-1;elsex=yearNow-year;elsex=yearNow-year;printf("You are %d years old!\n",x);}4.#include<stdio.h>#include<stdlib.h>#include<time.h>main( ){int number,Ynumber;srand(time(NULL));number=rand()%20+1;printf("I am thinking of a number between 1 and 20.\n");printf("Can you guess what it is?");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{printf("sorry.The number was %d.\n",number);printf("You should have gotten it by now.\n");printf("Better luck next time.");}}}}}}5.#include<stdio.h>main( ){int year,month,day,x=0,y,z;printf("Please enter your birthday:(year month day)\n");scanf("%d%d%d",&year,&month,&day);y=((year-1)*365+((year-1)/4)-((year-1)/100) +((year-1)/400))%7;switch(month){case 12:x=x+30;case 11:x=x+31;case 10:x=x+30;case 9:x=x+31;case 8:x=x+31;case 7:x=x+30;case 6:x=x+31;case 5:x=x+30;case 4:x=x+31;case 3:x=x+28;case 2:x=x+31;case 1:x=x+0;}if(year%4==0&&year%100!=0||year%400==0)day++;z=(x+day+y)%7;switch(z){case 0:printf("The day of your birthday is Sunday!");break;case 1:printf("The day of your birthday is Monday!");break;case 2:printf("The day of your birthday is Tuseday!");break;case 3:printf("The day of your birthday is Wednesday!");break;case 4:printf("The day of your birthday is Thursday!");break;case 5:printf("The day of your birthday is Friday!");break;case 6:printf("The day of your birthday is Saturday!");break;}}6.#include<stdio.h>#include<math.h>main( ){int a,b,c,p;float x1,x2;printf("Please enter a,b,c:\n");scanf("%d%d%d",&a,&b,&c);p=b*b-4*a*c;if(a==0)if(b==0)printf("There is no solution!");else{x1=-(float)c/b;printf("The solution is x1=%f",x1);}elseif(p<0)printf("There is no real solution!");else{x1=-b+sqrt(p)/(2*a);x2=-b-sqrt(p)/(2*a);printf("The solutions are x1=%f,x2=%f",x1,x2);}}。
C语言课后习题参考答案

C语言课后习题参考答案第一章:基础知识问题1:C语言的发展历史及特点C语言是由贝尔实验室的肯·汤普逊和丹尼斯·里奇在20世纪70年代初开发的一种高级程序设计语言。
它以其简洁、高效和可移植性而闻名。
C语言被广泛应用于系统级编程、嵌入式系统和科学计算等领域,并成为其他编程语言的基础。
问题2:C语言的基本数据类型及其存储范围C语言的基本数据类型包括字符型(char)、整型(int)、浮点型(float)、双精度浮点型(double)和空类型(void)。
它们的存储范围如下:- char:-128 到 127 或 0 到 255- int:-32,768 到 32,767 或 0 到 65,535- float:3.4E-38 到 3.4E38- double:1.7E-308 到 1.7E308- void:无特定存储范围问题3:如何在C语言中输出语句?在C语言中,可以使用printf函数来输出语句。
它的基本语法为:```cprintf("要输出的内容");```问题4:定义变量的语法规则在C语言中,定义变量的语法规则如下:```c数据类型变量名;```例如,定义一个整型变量x:```cint x;```问题5:如何进行变量的赋值?变量的赋值可以使用赋值运算符(=)。
例如,将一个整型常量10赋值给变量x:```cx = 10;```第二章:控制结构问题1:if语句的语法结构if语句用于根据条件执行不同的代码块。
它的基本语法结构如下:```cif (条件) {// 条件为真时执行的代码}```问题2:switch语句的用法及语法结构switch语句用于根据不同的表达式值执行相应的代码块。
它的基本用法及语法结构如下:```cswitch (表达式) {case 值1:// 当表达式的值等于值1时执行的代码break;case 值2:// 当表达式的值等于值2时执行的代码break;default:// 当表达式的值与所有case都不匹配时执行的代码break;}```问题3:循环语句的分类及语法结构C语言中的循环语句包括for循环、while循环和do-while循环。
C语言程序设计实验答案解析

-前言-/*非常感谢度娘以及各位网上C语言高手的支持,才能让敝人完成此文档的整理。
本文档集合了本人、度娘、众网友的力量,其中代码的正确率约为90%(不正确的有标注)。
为回报度娘及众网友的帮助,本文档免费下载。
*//*配“电子科技大学出版社(周信东主编)”的C语言程序设计实验*//*努力吧,骚年以及学妹们!*//*整理ed by 口玉刀一 of GUET.*/=====================实验一 C语言程序初步=====================1.----------------------------已知a=5,b=4,c=6,求出s并换行#include"stdio.h"main(){int a,b,c,s;a=5;b=4;c=6;s=(a+b+c)/3;printf("a=%d,b=%d,c=%d\n,s=%d",a,b,c,s);}2.-------------------------------输入一个数字求他的平方#include<stdio.h>main(){int r,s;scanf("%d",&r);s=r*r;printf("s=%d\n",s);}3.--------------------------------(1)#include<stdio.h>main(){printf(" *\n");printf("***\n");printf(" *\n");}(2)#include<stdio.h>{int v;int a,b,c; //a,b,c aer sides,v is volume of cubea=3;b=3;c=5;v=a*b*c;printf("v=%d\n",v);}=================================实验二数据类型、运算符和表达式=================================1.(1)--------------------------------------------------//总觉得打印结果怪怪的,DO YOU THINK SO?main(){char s1='3',s2='4',s3='5';int c1=101,c2=102;printf("3%c\t4%c\t5%c\n",s1,s2,s3); //3%c为输出3和%cprintf("s1=%d\ts2=%d\ts3=%d\n",s1,s2,s3);//注意哦,s1,s2,s3是char!而%d:输入输出为整形 %ld 长整型 %hd短整型 %hu无符号整形 %u %lu%s:输入输出为字符串 %c字符%f:输入输出为浮点型 %lf双精度浮点型printf("c1=%d\t~%c\n",c1,c1); //换码符'\t',表示水平制表位(horizeontal tab),它的作用是将光标移到最接近8的倍数的位置printf("c2=%d\t~%c\n",c2,c2); //注意c1,c2的类型}(2)//运行结果为8.300000 %是求余数先运算x-y,把结果转换为int型的有利于四则运算main(){float x=8.3,y=4.2,s;int a=7;s=x+a%5*(int)(x-y)%2/3;printf("s=%f",s);}main(){int i,j,p,q;i=3;j=6;p=i++;q=--j;printf("%d,%d,%d,%d\n",i,j,p,q);p=i--+3;q=++j-4;printf("%d,%d,%d,%d",i,j,p,q);}(4)main(){int m=15,n=9,s;m*=3+2;printf("%d",m);n+=n-=n*=n;printf("%d",n);printf("%d\n",(s=6*5,m+n+s));printf("%d\n",s);}2.---------------------------------------------------- (1)#include<stdio.h>#define MM 40main(){int a=3,b=6,t;t=MM/(3+6);printf("%d%d",t,MM);}//原来printf语句打成了print,printf语句中少了一个%d.(2)#include<stdio.h>{int m;double x;scanf("%d",&m);x=3.14*m*m;printf("%f",x);}//注意分号,%f转义(3)main(){int m=8,n=5;printf("%d",m*=m-n);printf("%d,%d,%d",m=m+1,n-1,m+n);}3.---------------------------------------------------- main(){int a=9,b=5;a+=b;b=a-b;a-=b;printf("%d%d",a,b);}4.---------------------------------------------------- //4.输入a,b,c三个变量,计算数学公式(a*b)/cmain(){int a,b,d;float s,c;scanf("%d%d%f",&a,&b,&c);d=a*b;s=d/c;printf("%f",s);}=================================实验三顺序结构和输入输出方法=================================1.---------------------------------------------------(1)main(){int m=345,t;float n=2.56;t=2.56*100;printf("m=%d,n*100=%d\n",m,t);}(2)main(){int a,b,c,s,v;scanf("%d%d%d",&a,&b,&c);s=a*b;v=a*b*c;printf("%d,%d,%d\n",a,b,c);printf("s=%d,v=%d\n",s,v);}2.----------------------------------------------------(1)main(){int m,n;float x=3.5,y=6.2;scanf("%d,%d",&m,&n);printf("%6d,%6d",m,n); /*每个数的输出宽度为6,两个数之间逗号建个。
c语言课后答案_电子科技大学出版社

c语言程序设计教程答案第一章C语言概述一、简答题1.(1)语言简洁、紧凑,使用方便、灵活;(2)运算符丰富;(3)具有丰富的数据类型;(4)具有结构化的控制语句;(5)语法限制不太严格,程序设计自由度大;(6)C 语言允许直接访问物理地址,能进行位(bit)操作,能实现汇编语言的大部分功能,可以直接访问硬件;(7)生成目标代码质量高,程序执行效率高;(8)用C语言写的程序可移植性好,基本上不作修改就能用于各种型号的计算机和各种操作系统。
2.顺序结构,分支结构,循环结构3.所谓算法就是为解决一个问题而采取的方法和步骤。
算法的特性:有穷性、确定性、输入、输出、可行性。
表示算法的方式:(1)用自然语言表示;(2)用流程图表示;(3)用N—S流程图表示;(4)用伪代码表示;(5)用计算机语言表示。
二、算法1.瓶子A里盛有醋瓶子B里盛有酱油有一个空瓶C将A中的醋倒入C将B中的酱油倒入A将C中的醋倒入B 2.输入一个数放在a中max = a以下步骤重复9次:输入一个数放在a中如果a>max,max=a打印max的值3.如果a<btemp=aa=bb=temp如果 c>atemp = aa=cc=temp否则如果c>btemp=bb=cc=temp打印a,b,c的值4.i=1sum=0以下程序循环100遍:sum=sum+ii=i+1打印sum的值5.如果(n除以3的余数为0 并且n除以5的余数为0)n能够同时被3和5整除否则n不能够同时被3和5整除6.i=101以下语句循环50遍:j=2flag=1当j<(i除以2的整数部分)时循环以下语句:如果i除以j的余数为零flag=0退出该循环如果flag==1打印i的值i=i+27.如果m<ntemp=mm=nn=tempi=n做以下循环,直到m,n能够被i整除:如果m能够被i整除并且n能够被i整除i即是m和n的最大公约数跳出循环否则i=i-1打印i的值8.data=b*b—4*a*c如果data>0否则如果data=0x1=x2=—b/2否则无实数解三、编程题1.main(){printf(“##############################\n”);printf(“How do you do?\n”);printf(“##############################\n”);}2.main(){int a,b,c;scanf(“%d%d%d”,&a,&b,&c);printf(“sum=%d”,a+b+c);}第二章数据类型与表达式一、单项选择题1. B2. C3. A4. A5. D6. B7.A8.C9.D二、写出下列程序的运行结果1.aabb cc abcAN2.ab97983. 9,11,9,104.a=2,b=3,c=25.s1=2.500000s2=3三、编程题main(){char c1='c',c2='h',c3='i',c4='n',c5='a';c1=c1+4;c2=c2+4;c3=c3+4;c4=c4+4;c5=c5+4;printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5);}第三章顺序程序设计选择题1)B 2)D 3)D 4)A 5)A6)B 7)B 8)A 9)D 10)C 11)D 12)A填空题1)b 2)261 3)25 21 37 4)abc 5)5.0,4,c=3 6)3编程题#define PI 3.1415926main( ){float h, r, l, sv, sz ;printf(“请输入圆的半径r,圆柱高h:\n”) ;scanf(“%f,%f”,&r, &h) ;l=2* PI *r ;s= PI *r*r ;sv=3.0/4.0* PI *r*r*r ;sz= PI *r*r*h ;printf(“圆周长为:l=%6.2f, 圆面积为:s=%6.2f\n”, l, s) ;printf(“求体积为:sv=%6.2f, 圆柱体积为:sz=%6.2f\n”, sv, sz) ;}2、main( ){float c, f ;printf(“请输入一个华氏温度:\n”) ;scanf(“%f”, &f) ;c=5.0/9.0*(f-32) ;printf(“摄氏温度为:%6.2f\n”, c) ;}第四章循环程序设计一、选择题1.D2.D3.D4.C5.D6.A7.B8.B9.A 10.D.11.B 12.A 13.B14.C 15.C 16.A 17.A 18.B 19.D 20.D 21.A二、填空题1.5858582.03.184.89215.20,06.67.1,3,28.Year%400==0year%100!=0count++三、编程题1.main(){long a;scanf(“%ld”,&a);if (a>=10000&&a<=99999) printf(“这是个五位数”);else if (a>=1000&&a<=9999) printf(“这是个四位数”);else if (a>=100&&a<=999) printf(“这是个三位数”);else if (a>=10&&a<=99) printf(“这是个二位数”);else if (a>=0&&a<=9)printf(“这是个一位数”);elseprintf(“输入错误”);}3.main(){int a;int g,s,b;//个位、十位、百位printf("水仙花数有:\n");for(a=100;i<=999;a++){g=a%10;s=a/10%10;b=a/100;if (a==g*g*g+s*s*s+b*b*b) printf("%d ",a);}printf("\n");}4、main(){int i,n;printf("input n:");scanf("%d",&n);printf("\n%d=",n);for(i=2;i<=n;i++){while(i!=n)if(n%i==0){printf("%d*",i);n/=i;}else break;}printf("%d\n",n);}5、#include “stdio.h”main(){char c;int w,x,y,z;w=x=y=z=0;while((c=getchar())!=’\n’){if((c>’a’&&c<’z’)||(c>’A’&&c<’Z’)) w++;else if (c==’ ’)x++;else if (c>’0’&&c<’9’)y++;else z++;}printf(“英文字母个数:%d\n”,w);printf(“空格个数:%d\n”,x);printf(“数字个数:%d\n”,y);printf(“其他字母个数:%d\n”,z);}6、main(){float h=100,t=100;int j;for(j=1;j<10;j++){t=t/2;h=h+t*2;}printf(“共经过:%f 米\n”,h);printf(“第十次反弹%f 米\n”,t/2);}第五章数组一、单项选择题1. B2. A3. C4. C5. D6. B7.C8.B9.C 10. B二、写出下列程序的运行结果1.S=3682.*************************3.S1=18S2=104.!margorP5.数字0的出现次数是:3 数字1的出现次数是:2 数字2的出现次数是:2 数字3的出现次数是:2 数字4的出现次数是:0 数字5的出现次数是:1 数字6的出现次数是:1 数字7的出现次数是:1 数字8的出现次数是:1 数字9的出现次数是:1 三、编程题1.#include "stdio.h"main(){int a[11];int i,j,t;printf("input 10 number:\n");for (i=1;i<11;i++)scanf("%d",&a[i]);printf("\n");for (i=1;i<=9;i++)for (j=1;j<=10-i;j++)if (a[j]<a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}printf("the sorted number is:\n"); for (i=1;i<11;i++)printf("%4d",a[i]);}2.#include "stdio.h"main(){int a[11],i,b;printf("imput 10 sorted number:\n"); for (i=0;i<10;i++)scanf("%d",&a[i]);printf("input an integer:\n");scanf("%d",&b);for (i=9;i>=0&&a[i]>b;i--)a[i+1]=a[i];a[i+1]=b;printf("sorted numbers:\n");for (i=0;i<11;i++)printf("%5d",a[i]);}3.#include "stdio.h"main(){int a[5],i,j,t;printf("input 5 integer:\n");for (i=0;i<5;i++) /*输入5个整数*/scanf("%d",&a[i]);for (i=0,j=4;i<j;i++,j--) /*逆序存放*/{t=a[i];a[i]=a[j];a[j]=t;}for (i=0;i<5;i++) /*逆序存放后重新输出*/printf("%5d",a[i]);}4.#include "stdio.h"main(){int i,j,a[10][10]={{1},{1,1}};for (i=2;i<10;i++) /*给二维数组每个元素赋值*/{a[i][0]=1;for (j=1;j<=i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];}for (i=0;i<10;i++) /*输出二维数组*/{for (j=0;j<=i;j++)printf("%-5d",a[i][j]);printf("\n");}}5.#include "stdio.h"main(){int a[3][4],i,j,max,row,col;printf("input 3*4 matrix:\n");for (i=0;i<3;i++) /*输入3*4矩阵*/ for (j=0;j<4;j++)scanf("%d",&a[i][j]);max=a[0][0]; row=0; col=0;for (i=0;i<3;i++) /*寻找矩阵中的最大值及其行列号*/for (j=0;j<4;j++)if (max<a[i][j]){max=a[i][j]; row=i, col=j;}printf("\n数组中最大的数是%d,其行号是%d,列号是%d\n",max,row,col);}6.#define N 3#define M 4#include "stdio.h"main(){int a[N][M],i,j,k,max,row,col;printf("input %d*%d matrix: \n",N,M);for (i=0;i<N;i++) /*输入二维矩阵*/ for (j=0;j<M;j++)scanf("%d",&a[i][j]);for (i=0;i<N;i++) /*求鞍点*/{max=a[i][0]; row=i; col=0;for(j=1;j<M;j++) /*求矩阵中每一行的最大值及其所行列号*/if (max<a[i][j]){max=a[i][j]; col=j;}for (k=0;k<N;k++) /*判断每一行的最大值在其所在列是否最大*/if (a[k][col]<max)break;if (k==N) /*得到鞍点*/{printf("the point is %d,row=%d,col=%d\n",max,row,col);break;}}if (i==N) /*没有鞍点*/printf("no point\n");}7.#include "stdio.h"main(){int num=0,word=0;/*word=0表示未出现单词,如出现单词就置word为1.num用来统计单词个数*/char c;printf("please input a string :\n");while ((c=getchar())!='\n')if (c==' ')word=0;else if (word==0){word=1; num++;}printf("There are %d words in the line\n",num);}8.#include "stdio.h"main(){int i,j,uppn,lown,dign,span,othn;/*uppn,lown,dign,span,othn分别存放英文大写字母、小写字母、数字、空格和其他字符的个数*/char text[3][80];uppn=lown=dign=span=othn=0;for(i=0;i<3;i++){gets(text[i]);for(j=0;j<80&&text[i][j]!='\0';j++){if(text[i][j]>='A'&&text[i][j]<='Z')uppn++;else if(text[i][j]>='a'&&text[i][j]<='z')lown++;else if(text[i][j]>='0'&&text[i][j]<='9')dign++;else if(text[i][j]==' ')span++;elseothn++;}}for(i=0;i<3;i++)printf("%s\n",text[i]);printf("uppn=%d\n",uppn);printf("lown=%d\n",lown);printf("dign=%d\n",dign);printf("span=%d\n",span);printf("othn=%d\n",othn);}9.#include "stdio.h"main(){int i,j;char str1[20],str2[20];printf("input two strings:\n");gets(str1);gets(str2);j=strlen(str1); /*求字符串1的长度*/for (i=0;str2[i]!='\0';i++,j++) /*字符串合并*/str1[j]=str2[i];str1[j]='\0'; /*加上字符串结束标志*/puts(str1);}10.#include "stdio.h"main(){int i,n;char str1[20],str2[20];printf("input two strings:(no more than 20 characters)\n");gets(str1); gets(str2);n=strlen(str1)<strlen(str2)?strlen(str1):strlen(str2); /*n中存放较短字符串的长度*/for (i=0;i<n&&str1[i]==str2[i];i++);printf("%d\n",str1[i]-str2[i]);}第六章函数与编译预处理一、单项选择题1. C2. B3. B4. B5. A6. B7.A8.C9.D 10. A二、写出下列程序的运行结果1.92.4,B8,B3.-4.0000004.a=6,b=55.48三、编程题1.main(){ int prime(int n);int n;printf("input n(n>0):\n");scanf("%d",&n);if (prime(n))printf("%d is a sushu\n",n);else printf("%d is not a sushu\n",n); }int prime(int n){int flag=1,i;for (i=2;i<=n/2&&flag==1;i++)if (n%i==0) flag=0;return(flag);}2.#define N 3 convert(int array[3][3]) { int i,j,t;for (i=0;i<N-1;i++)for (j=i+1;j<N;j++){t=array[i][j];array[i][j]=array[j][i]; array[j][i]=t;}}main(){int i,j;int a[N][N];printf("input a:\n");for (i=0;i<N;i++)for (j=0;j<N;j++) scanf("%d",&a[i][j]); printf("Array a:\n"); for (i=0;i<N;i++) {for (j=0;j<N;j++) printf("%5d",a[i][j]); printf("\n");}convert(a);printf("a de zhuanzhi is:\n");for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",a[i][j]);printf("\n");}}3. #include <stdio.h>#include <string.h>main(){char str[100];printf("input a string:\n") ;gets(str);inverse(str);printf("the reversed string is:%s\n",str); }inverse(char str[]){char t;int i,j;for (i=0,j=strlen(str)-1;i<j;i++,j--) {t=str[i];str[i]=str[j];str[j]=t;}}4. #include <stdio.h>concat(char str1[],char str2[]) { int i=0,j;while (str1[i]!='\0')i++;for (j=0;str2[j]!='\0';i++,j++)str1[i]=str2[j];str1[i]='\0';}main(){char str1[100],str2[100];gets(str1);gets(str2);concat(str1,str2);puts(str1);}5. main(){char str[80];printf("input a string (4 ge shu zi zi fu):\n"); scanf("%s",str);insert(str);printf("result is:\n%s\n",str);}insert(char str[]){int i;for (i=strlen(str);i>0;i--){str[2*i]=str[i];str[2*i-1]=' ';}}6. #include "stdio.h"int i,ndight,nwhite,nletter,nother;count(char str[]){ndight=nwhite=nletter=nother=0;for (i=0;str[i]!='\0';i++)if (str[i]>='0'&&str[i]<='9')ndight++;else if ((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')) nletter++;else if (str[i]==' ')nwhite++;else nother++;}main(){char text[80];printf("input a string:\n");gets(text);count(text);printf("ndight=%d,nletter=%d,nwhite=%d,nother=%d\n",n dight,nletter,nwhite,nother);}7. #define N 10#include <stdio.h>sort(char str[]){int i,j;char t;for (i=1;i<N;i++)for (j=0;j<N-i;j++)if (str[j]>str[j+1]){t=str[j];str[j]=str[j+1] ;str[j+1]=t;} }main(){char str[N];int i ;printf("Input 10 ge zi fu:\n"); gets(str);sort(str);printf("The sorted result:\n") ;for(i=0;i<N;i++)printf("%c",str[i]);}8.这题较复杂#include <stdio.h>#include <string.h>#define N 10void input_e(int num[],char name[N][8]){int i;for (i=0;i<N;i++){ printf("input gong hao:");scanf("%d",&num[i]);printf("input name:");getchar();gets(name[i]);}for (i=0;i<N;i++)printf("%5d%10s\n",num[i],name[i]);}void sort(int num[],char name[N][8]) /*选择法排序*/ {int i,j,min,temp1;char temp2[8];for (i=0;i<N-1;i++){min=i;for (j=i+1;j<N;j++)if (num[j]<num[min]) min=j;temp1=num[i];strcpy(temp2,name[i]);num[i]=num[min];strcpy(name[i],name[min]);num[min]=temp1;strcpy(name[min],temp2);}printf("the sorted result:\n");for (i=0;i<N;i++)printf("%5d%10s\n",num[i],name[i]);}void search(int n,int num[],char name[N][8]) /*折半查找法*/{int top,bott,mid,find;find=0;top=0;bott=N-1;if ((n<num[0])||(n>num[N-1]))find=-1;while ((find==0)&&(top<=bott)){mid=(bott+top)/2;if (n==num[mid]){find=1; printf("%d name is:%s\n",n,name[mid]);}else if (n<num[mid])bott=mid-1;else top=mid+1;}if ((find==-1)||(find==0))printf("%d is not found.\n",n);}main(){int num[N],number,c,flag;char name[N][8];input_e(num,name);sort(num,name);for (flag=1;flag;){printf("please input chazhao de gonghao:"); /*输入查找的工号*/scanf("%d",&number);search(number,num,name);printf("continue Y/N?"); /*是否继续查找*/getchar();c=getchar();if (c=='N'||c=='n')flag=0;}}9. #include "stdio.h"#define MAX 10main(){char str[MAX];char c;int i;i=0;printf("input number(16 jinzhi): "); /*输入一个十六进制的数*/while((c=getchar())!='\n'&&i<MAX){ str[i]=c;i++;}str[i]='\0';printf("result is :%d\n",htod(str));}int htod(char s[]){int i,n;n=0;for(i=0;s[i]!='\0';i++){if (s[i]>='0'&&s[i]<='9')n=n*16+s[i]-'0';if (s[i]>='a'&&s[i]<='f')n=n*16+s[i]-'a'+10;if (s[i]>='A'&&s[i]<='F')n=n*16+s[i]-'A'+10;}return(n);}10. #define SWAP(a,b) t=a;a=b;b=t main(){int a,b,t;printf("input a,b:") ;scanf("%d,%d",&a,&b);SWAP(a,b);printf("result:a=%d,b=%d\n",a,b);}11. #define SURPLUS(a,b) ((a)%(b)) main(){int a,b;printf("input a,b:");scanf("%d,%d",&a,&b);printf("result is: %d\n",SURPLUS(a,b)); }12. main(){int a,b,c;printf("input a,b,c:");scanf("%d,%d,%d",&a,&b,&c);printf("max=%d\n",max(a,b,c));}int max(int x,int y,int z){int t;t=(x>y ? x : y);return(t>z?t:z);}#define MAX(x,y) ((x)>(y)?(x):(y)) main(){int a,b,c;printf("input a,b,c:");scanf("%d,%d,%d",&a,&b,&c);printf("max=%d\n",MAX(MAX(a,b),c));}13. #include "stdio.h"#define CHANGE 1#define MAX 80main(){char str[MAX];int i;printf("input a string:\n");gets(str);#if (CHANGE){ for (i=0;str[i]!='\0';i++)if (str[i]>='a'&&str[i]<'z'||str[i]>='A'&&str[i]<'Z') str[i]=str[i]+1;else if (str[i]=='z'||str[i]=='Z')str[i]=str[i]-25;}#endifprintf("%s\n",str);}第七章指针一、选择题1)A 2)D 3)D 4)C 5)B 6)B 7)B 8)C 9)B 10)C 11)A 12)A 13)A 14)C 15)B 16)A 17)C 18)B 19)D 20)B二、阅读下面程序,写出程序运行结果1)abcdeedcba 2)1113151719 3)(TurboC中是11,97,11 )(VisualC是9,9 7,11)4)3 6 5)6385三、编程题1、main( ){ int a[10],i,temp,*p=a;printf("Please input array a:\n");for(i=0;i<10;i++)scanf("%d",&a[i]);printf("array a:\n");for(i=0;i<10;i++)printf("%4d",a[i]);for(i=0;i<5;i++){ temp=p[i];p[i]=p[10-i-1];p[10-i-1]=temp;}printf("\n Now array a:\n");for(i=0;i<10;i++)printf("%4d",a[i]);}2、main( ){ int a[3][3],*p,i,j;printf("please input matrix:\n"); for(i=0;i<3;i++)for(j=0;j<3;j++)scanf("%d",&a[i][j]);p=&a[0][0];move(p);printf("\n Now matrix:\n");for(i=0;i<3;i++){ for(j=0;j<3;j++)printf("%4d",a[i][j]);printf("\n");}}move(int *q){ int i,j,t;for(i=0;i<3;i++)for(j=i;j<3;j++){ t=*(q+3*i+j);*(q+3*i+j)=*(q+3*j+i);*(q+3*j+i)=t;}}3、#include <stdlib.h>#include <alloc.h>#include <string.h>#include <stdio.h>main( ){int binary();void insert();char*temp,*ptr1[6]={"BASIC","DATA","PASCAL","SQL","USE"} ;int i;ptr1[5]=malloc(20);printf("\n");printf("original string:\n");for(i=0;i<5;i++)printf("%s\n",ptr1[i]);printf("input search string:\n"); temp=malloc(20);gets(temp);i=binary(ptr1,temp,5);printf("i=%d\n",i);insert(ptr1,temp,5,i);printf("output strings:\n");for(i=0;i<6;i++)printf("%s\n",ptr1[i]);}int binary(char *ptr[],char *str,int n) {int hig,low,mid;low=0;hig=n-1;if(strcmp(str,ptr[0])<0) return(0);if(strcmp(str,ptr[hig])>0) return(n);while(low<=hig){mid=(low+hig)/2;if(strcmp(str,ptr[mid])<0)hig=mid-1;else if(strcmp(str,ptr[mid])>0)low=mid+1;else return(mid);}return(low);}void insert(char *ptr[],char *str,int n,int i) {int j;for(j=n;j>i;j--)strcpy(ptr[j],ptr[j-1]);strcpy(ptr[i],str);}4、main( ){ int i;char *p[6],str[6][20];for(i=0;i<6;i++)p[i]=str[i];printf("Input 6 strings:\n"); for(i=0;i<6;i++)scanf("%s",p[i]);sort(p);printf("Now the strings:\n"); for(i=0;i<6;i++)printf("%s\n",p[i]);}sort(char *q[ ]){ int i,j;char *t;for(i=0;i<5;i++)for(j=0;j<5-i;j++)if(strcmp(*(q+j),*(q+j+1))>0) { t=*(q+j);*(q+j)=*(q+j+1);*(q+j+1)=t;}}5、main( ){ int m;char str1[20],str2[20],*p1,*p2; printf("please input two strings:\n"); scanf("%s",str1);scanf("%s",str2);p1=str1;p2=str2;m=stringcmp(p1,p2);printf("result is %d:\n",m);}stringcmp(char *p1,char *p2){ int i=0;while(*(p1+i)==*(p2+i))if(*(p1+(i++))=='\0') return(0); return((*p1+i)-*(p2+i));}6、main( ){ char str1[30],str2[30],str3[100]; int i=0,j=0,k=0;printf("please input two strings:\n"); scanf("%s",str1);scanf("%s",str2);while(str1[i]!='\0' && str2[j]!='\0'){ str3[k++]=str1[i++];str3[k++]=str2[j++];}while(str1[i]!='\0')str3[k++]=str1[i++];while(str2[j]!='\0')str3[k++]=str2[j++];str3[k]='\0';printf("\n%s\n",str3);}第八章结构体一、选择题1、C2、B3、C4、D5、B6、A7、D8、C9、C 10、B二、填空题1、342、03、514、10,x5、364041三、编程题1、struct complex{float x; /*实部*/float y; /*虚部*/}main(){struct complex c1={5,3},c2={2,6}; float t1,t2;t1=c1.x*c2.x - c1.y*c2.y;t2=c1.x*2.y + c1.y*c2.x;printf(“The two complexs product is:”); printf(“%.2f%+.2f i\n”,t1,t2);}2. struct student{char name[10]; /*姓名*/int number; /*学号*/float x; /*成绩*/float y; /*成绩*/}main( ){struct student s[45];int i;void max();void average();void sort();for (i=0;i<45;i++)scanf(“%s%d%f%d”,s[i].name,&s[i].number,&s[i].x,&s[i]. y);/*输入学生的姓名、学号、两门课程成绩*/max(s,45);average(s,45);sort(s,45);}void max(struct student *a,int n){float temp;char na[10];int i ,num;temp=a[0].x+a[0].y;for (i=1;i<n;i++)if(temp<a[i].x+a[i].y) /*求总分最高的学生姓名、学号*/{temp=a[i].x+a[i].y;strcmp(na,a[i].name);num=a[i].number;}printf(“%s,%d”,na,num);}void average(struct student *b,int n){float aver1,aver2,s1,s2;int i ;s1=s2=0;for(i=0;i<45;i++) /*求每门课程的总成绩*/ {s1+=b[i].x;s2+=b[i].y;}aver1=s1/45;aver2=s2/45;for(i=0;i<45;i++)if(b[i].x<aver1&&b[i].y<aver2)/*输出两门课程都低于平均成绩的学生*/printf(“%s,%d\n”,b[i].name,b[i].nu mber);}void sort(struct student *c,int n){float t;int i,j ;struct student temp;for (i=0;i<n-1;i++) /*改进的选择法排序可参考课本P89页*/{k=i ;for(j=i+1;j<n;j++)if(c[k].x<c[j].x)k=j;if(k!=i){temp=c[i];c[i]=c[k];c[k]=temp;}for(i=0;i<n;i++)printf(“%s,%.2f\n”,c[i].n,c[i].x);}3. typedef struct Ljian{int pname;int wnum;struct Ljian *next;}Part;/*用户自定义零件结构体类型*/# include “stdio.h”main( ){int n=1;Part *head,*p1,*p2;head=NULL;p1=( Part * ) malloc (sizeof(struct Ljian));/*创建第一个结点*/scanf(“%d%d”,&p1->pname,&p1->wnum);p1->next=NULL;while(n<=10) /*将结点加入链表*/ {if(n==1)head=p1; /*是第一个结点,作表头*/ elsep2->next=p1; /*不是第一个结点,作表尾*/ p2=p1;p1=( Part *) malloc(sizeof(struct Ljian));/*开辟下一个结点*/scanf(“%d%d”,&p1->pname,&p1->wnum);p1->next=NULL;n++;}}4.#include<stdlib.h>#include<stdio.h>#include<conio.h>#define LEN sizeof(struct student)typedef struct student{ long num; /*学号*/float score; /*成绩*/struct student *next;} STU; /*用户自定义学生结构体类型*/STU *Create ( ) /*创建链表* / {int n=0;STU *p1,*p2,*head;head=NULL;p1=(STU *)malloc(LEN); /*创建第一个结点*/scanf("%ld,%f",&p1->num,&p1->score);p1->next=NULL;while(p1->num!=0) /*将结点加入链表*/{++n;if(n==1) /*是第一个结点,作表头*/head=p1;else /*不是第一个结点,作表尾*/p2->next=p1;p2=p1;p1=(STU *)malloc(LEN); /*开辟下一个结点*/scanf("%ld,%f",&p1->num,&p1->score);p1->next=NULL;}free(p1); /*释放最后一个结点所占的内存*/return (head); /*返回链表的头指针*/}void print(STU *head) /*输出链表* /。
大学C语言课本课后习题相应答案及详细解答

5-1#include"stdio.h" void main(){char c1,c2;printf("c1:");c1=getchar();if(c1>='a'&&c1<='z') c2=c1-32;else if(c1>='A'&&c1<='Z') c2=c1+32;else c2=c1;printf("=>%c\n",c2);}5-2#include"stdio.h"void main(){char c;printf("c:");c=getchar();if(c>='A'&&c<='Z')if(c=='A') printf("没有前面的字母!");else if(c=='Z') printf("没有后面的字母!!");else printf("前面的字母=%c,后面的字母=%c",c-1,c+1);printf("\n");} 5-3#include"stdio.h"void main(){int s;char g;printf("s:");scanf("%d",&s);if(s>=0&&s<=100){switch(s/10){case 9:case 10: g='A';break;case 8: g='B';break;case 7: g='C';break;case 6: g='D';break;default: g='E';}printf("g=%c\n",g);}else printf("成绩不在百分制范围内!\n"); }5-4#include"stdio.h"void main(){int y,m,d,dok;int yy,mm,dd;printf("y,m,d:");scanf("%d,%d,%d",&y,&m,&d);switch(m){case 1: case 3: case 5: case 7: case 8: case 10: case 12:if(d>0&&d<=31) dok=4;else dok=0;break;case 4: case 6: case 9: case 11:if(d>0&&d<=30) dok=3;else dok=0;break;case 2: if(y%4==0&&y%100!=0||y%400==0)if(d>0&&d<=29)dok=2;else dok=0;elseif(d>0&&d<=28)dok=1;else dok=0;break;default: dok=0;}if(dok==0) printf("月份或日期不对!\n");else{switch(dok){case 1: if(d==28){yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;case2:if(d==29){yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;case3:if(d==30){yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;case 4:if(d==31)if(m==12){yy=y+1;dd=1;mm=1;}else{yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;}printf("Tomorrow:%d年%d月%d日\n",yy,mm,dd);}}5-5#include"stdio.h"void main(){int a,b,c,t;printf("a,b,c:");scanf("%d,%d,%d",&a,&b,&c);if(a+b>c&&b+c>a&&a+c>b){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;}if(a==b&&b==c) printf("等边三角形.");else if(a==b||b==c||a==c) printf("等腰角形.");else if(c*c==a*a+b*b) printf("直角三角形.");else printf("任意三角形.");printf("\n");}else printf("不能构成三角形!\n");}6-1#include"stdio.h"void main(){int a,b,m,n,k;printf("m,n:");scanf("%d,%d",&m,&n);a=m;b=n;k=a%b;while(k){a=b;b=k;k=a%b;}printf("%d\n",(m*n/b));}6-2#include"stdio.h"void main(){int n;int i,j,s;printf("n=");scanf("%d",&n);for(i=1;i<=n;i++){s=0;for(j=1;j<i;j++)if(i%j==0) s=s+j;if(s==i) {printf("%d:",i);for(j=1;j<i;j++)if(i%j==0) printf("%d ",j);printf("\n");}}} 6-3#include"stdio.h"#include"math.h"void main(){double x,y,zd,zx,x1,x2;zd=zx=50;for(x=0;x<=2;x=x+0.2){y=2*pow(x,3)-3*pow(x,4)+6*pow(x,5)-4*x+50;if(y>zd){ zd=y;x1=x;}if(y<zx){ zx=y;x2=x;}}printf("x=%f,max=%f\n",x1,zd);printf("x=%f,min=%f\n",x2,zx);}6-4#include"stdio.h"void main(){double x,sum=0,a,b,c;int i;printf("x=");scanf("%lf",&x);a=x;b=1.0;c=1.0;for(i=1;i<=10;i++){sum=sum+a/(b*c);a=a*x*x;b=b+2;c=c*i;}printf("y(%.2lf)=%lf\n",x,sum);}7-1/*选择排序*/#include"stdio.h"void main(){int i,j,n,k,temp;int a[10];printf("n(<10):");scanf("%d",&n);printf("Original:");for(i=0;i<n;i++) scanf("%d",&a[i]);for(i=0;i<n-1;i++){ /*趟*/k=i;for(j=i+1;j<n;j++) /*比较次数*/if(a[j]<a[k]) k=j;/*<升序,>?*/if(k!=i){temp=a[i];a[i]=a[k];a[k]=temp;}}printf("Ordered:");for(i=0;i<n;i++) printf("%d ",a[i]);printf("\n");}7-2#include"stdio.h"void main(){int a[3][3];int i,j,s=1;printf("Input:\n");for(i=0;i<3;i++)for(j=0;j<3;j++){scanf("%d",&a[i][j]);if(i==j) s=s*a[i][j];}printf("s=%d\n",s);} 7-3/*杨辉三角*/#include"stdio.h"void main(){int x[7][7];int i,j;for(i=0;i<7;i++) {x[i][0]=1;x[i][i]=1;}for(i=2;i<7;i++)for(j=1;j<i;j++)x[i][j]=x[i-1][j]+x[i-1][j-1];for(i=0;i<7;i++){for(j=0;j<=i;j++)printf("%3d",x[i][j]);printf("\n");}}7-4#include<stdio.h>#include<string.h>void main(){char str[21];int i,j;printf("str:");gets(str);for(i=0,j=strlen(str)-1;i<=j;i++,j--)if(str[i]!=str[j]) break;if(i>j) printf("%s是回文\n",str);else printf("%s不是回文\n",str);}7-5/*输入一维数组的10个元素,并将最小值与第1个数交换,最大值与最后一个数交换,然后输出交换后的结果*/#include<stdio.h>void main(){int a[10],i,zx,zd;printf("Input:\n");zx=zd=0;for(i=0;i<10;i++){scanf("%d",&a[i]);if(a[i]<a[zx]) zx=i;if(a[i]>a[zd]) zd=i;}if(zx!=0){int t;t=a[0];a[0]=a[zx];a[zx]=t;}if(zd!=9){int t;t=a[9];a[9]=a[zd];a[zd]=t;}for(i=0;i<10;i++)printf("%d ",a[i]);printf("\n");} 8-2#include"stdio.h"double xexp(double x,int n){double c;if(n==0) c=1.0;else c=x*xexp(x,n-1);return c;}void main(){int n;double x;printf("x:");scanf("%lf",&x);printf("n:");scanf("%d",&n);printf("Result=%g\n",xexp(x,n));}8-3#include"stdio.h"int find(int x[],int n,int y){int i,pos=-1;for(i=0;i<n;i++)if(x[i]==y) pos=i;return pos;}void main(){int a[10]={11,1,13,15,18,7,19,27,3,8};int i,y,pos;for(i=0;i<10;i++) printf("%d ",a[i]);printf("\ny:");scanf("%d",&y);pos=find(a,10,y);if(pos==-1) printf("Not found!\n");else printf("Position=%d\n",pos);}8-1#include"stdio.h"#include"conio.h" //getch()#include"stdlib.h" //srand(),rand(),system("cls") #include"time.h" //time()void main(){void init(int a[][5],int m,int n);void input(int a[][5],int m,int n);void output(int a[][5],int m,int n);int min(int b[][5],int m,int n);int a[5][5],ch='0';while(1){system("cls"); //清屏printf("1.初始化 2.键盘输入0.结束程序:");printf("\n");if(ch=='0') break;else if(ch=='1'){init(a,5,5);output(a,5,5);}else if(ch=='2'){input(a,5,5);output(a,5,5);}else printf("Error!\n");printf("Min element:%d\n",min(a,5,5));printf("按任意键继续!\n");getch();}}void init(int a[][5],int m,int n){int i,j;srand(time(0)); //time(0)表示以当前的时间做种子,增加每次运行的随机性for(i=0;i<5;i++)for(j=0;j<5;j++)a[i][j]=rand()%100; //随机数范围:0~32767,将它控制在0~99的范围}void input(int a[][5],int m,int n){int i,j;printf("Input Array 5X5:\n");for(i=0;i<m;i++)for(j=0;j<n;j++)scanf("%d",&a[i][j]);}void output(int a[][5],int m,int n){int i,j;printf("Output Array 5X5:\n");for(i=0;i<5;i++){for(j=0;j<5;j++)printf("%2d ",a[i][j]);printf("\n");}}int min(int b[][5],int m,int n){int i,j,zx;zx=b[0][0];for(i=0;i<m;i++)for(j=0;j<n;j++)if(i==j&&b[i][j]<zx) zx=b[i][j];return zx;}8-4#include"stdio.h"float pave,nave;void saver(float a[],int n){int i,z,f;float psum,nsum;psum=nsum=0.0;z=f=0;for(i=0;i<n;i++)if(a[i]<0) {nsum=nsum+a[i];z++;}else if(a[i]>0) {psum=psum+a[i];f++;}else continue;pave=(z!=0?psum/z:0.0);nave=(f!=0?nsum/f:0.0);}void main(){float a[10]={1.0,11.0,3.0,-1.5,-5.5,-2};saver(a,10);printf("pave=%.1f,nave=%.2f\n",pave,nave); } 8-5#include"stdio.h"#include"math.h"void p1(int a,int b){printf("has two equal roots:%\n",-b/(2*a));}void p2(int a,int b, int disc){float x1,x2;x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);printf("Has distant real roots:% and %\n",x1,x2);}void p3(int a,int b, int disc){float real,image;real=-b/(2*a);image=sqrt(-disc)/(2*a);printf("Has complex roots:\n");printf("%+%8.4fi\n",real,image);printf("%-%8.4fi\n",real,image);}void main(){ int a,b,c,disc;printf("a,b,c:");scanf("%d,%d,%d",&a,&b,&c);disc=b*b-4*a*c;if(fabs(disc)<=1e-6) p1(a,b);else if(disc>1e-6) p2(a,b,disc);else p3(a,b,disc);}8-6#include"stdio.h"#include"stdlib.h"#include"conio.h"#include"time.h"void main(){void printaverage(int score[][5],int m,int n);void printname(int score[][5],int m,int n);int score[10][5];int i,j;srand(time(0));for(i=0;i<10;i++)for(j=0;j<5;j++)score[i][j]=50+rand()%50+1;printf("Output Students' score:\n");printf("Course 1 2 3 4 5\n");printf("-------------------------\n");for(i=0;i<10;i++){printf("No.%2d:",i+1);for(j=0;j<5;j++)printf("%3d ",score[i][j]);printf("\n");}printaverage(score,10,5);printname(score,10,5);} void printaverage(int score[][5],int m,int n){int i,j,sum;printf("\nAverage score:\n");for(i=0;i<m;i++){sum=0;for(j=0;j<n;j++)sum=sum+score[i][j];printf("No.%d:%.1f\n",i+1,sum/5.0);}}void printname(int score[][5],int m,int n){int i,j,max0,row0;for(j=0;j<5;j++){max0=score[0][j];for(i=0;i<10;i++)if(score[i][j]>max0){max0=score[i][j];row0=i;}printf("Course %d,max score=%d,student:No.%d\n",j+1,max0,row0+1);}}大学英语I词汇练习题Choose the best one to complete each sentence.1. She felt like ____C___ frustration, but she was determined not to lose her self-control.A. to cry out ofB. to cry forC. crying out ofD. crying for2. The method he used turned out to be ___A____ in improving the students' English.A. effectiveB. abilityC. responseD. explicit3. Measures had to be taken in face of the housing problem that ____C____ in the city.A. foundedB. raisedC. aroseD. produced4. Without electronic computers, much of today's advanced technology ___D____.A. haven't been achievedB. wouldn't be achievedC. hadn't been achievedD. wouldn't have been achieved5. Jane said to her husband, "Don't worry. There is no cause for __B______ about our daughter'sability to manage herself."A. careB. concernC. attentionD. love6. The tap won't ____A____, and there is water all over the floor.A. turn offB. turn downC. turn onD. turn up7. Wearing the right shoes and clothes ____B____ being fit can make all the difference.A. in additionB. as well asC. alsoD. too8. She didn't try to do anything for her daughter, and ____B____ it's too late now.A. in caseB. in any caseC. at this caseD. in case of9. The girl said she hated _____C___ he smiled at her.A. that wayB. this wayC. the wayD. all the way10. I'll call the hotel. I'll tell them we'll ____A____ tomorrow morning and stay there for two nights.A. check inB. check outC. check offD. check over11. My father didn't go to New York; the doctor suggested that he ___C____ there.A. not to goB. won'tC. not goD. not to go to12. She _____C____ him to find answers to her problems.A. learned fromB. came intoC. leaned onD. subjected to13. The large wings of that bird ______B_____ it to fly high and fast.A. makeB. enableC. forceD. realize14. If he ____D___ the policeman honestly, he would not have been arrested.A. would answerB. answerC. should answerD. had answered15. I intended ___A____ you last Sunday, but I had no time.A. to have called onB. calling onC. to be calling onD. to be called on16. If you try to learn too many things at a time, you may get ____D___.A. alarmedB. scaredC. surprisedD. confused17. I haven't read this book, and my brother hasn't ____A____.A. eitherB. neitherC. alsoD. too18. No sooner ____B_____ than the truck started off.A. his luggage was loadedB. had his luggage been loadedC. loaded his luggageD. his luggage was being loaded19. At the end of the game, the whole crowd ____C____ their feet and cheered wildly.A. emerged fromB. rose fromC. got toD. stood up20. Darren has decided to give ___D_____ football at the end of this season.A. inB. forC. offD. up21. Neither his friends nor his mother ____B____ his marriage to that girl.A. acceptB. acceptsC. agreeD. agrees22. Most people believe that he is quite _____A___ of lying to get out of trouble.A. capableB. enabledC. ableD. skilled23. We were told that most of our luggage would be _____B___ by sea.A. approachedB. transportedC. handledD. communicated24. The Broadcasting Museum also offers Saturday workshops to _____B___ children with the world of radio.A. contriveB. acquaintC. acquireD. admit25. Without a passport, leaving the country is ____D___.A. out of questionB. without questionC. in the questionD. out of the question26. She states her views very ___A______.A. definitelyB. definiteC. infiniteD. infinitely27. You need to ____D____ the employers that you can do the job.A. convictB. confuseC. confirmD. convince28.The little boy was so lucky to have ___B_____ the earthquake.A. surviveB. survived C .surviving D. survival29. It is ___B___ that later in life you will see each other again.A. likeB. likelyC. likedD. seemed30.Women’s social ____B____ has changed much over the years.A. stateB. statusC. statesD. statement31.The novel _____D____ the imagination of thousands of readers.A. caterB. cateredC. captureD. captured32.There are good chances of ______D______ working in this firm.A. prospectB. portionC. positionD. promotion33.If you are ______A_____ a night owl, having the first class start in the afternoon might be thebest thing that could ever happen to you.A. more ofB. more thanC. much moreD. more34. The conference discussed the fair ______A_____of income and wealth.A. distributionB. distributeC. contributionD. contribute35. I was so ___C__ in today's history lesson that I didn't understand anything.A. amazedB. confusingC. confusedD. amazing36. ___B___ in England, Anne Bradstreet both admired and imitated several English poets.A. Having born and educatedB. Born and educatedC. Since born and educatedD. To be born and educated37. You will see this product ___B___ wherever you go.A. to be advertisedB. advertisedC. advertiseD. advertising38. I never forget my __B__ with you for the first time.A. to meetB. meetingC. to have metD. having to be meeting39.She was so angry that she left like __B__ something at him.A. to throwB. throwingC. to have thrownD. having thrown40. We are ___C__ in different kinds of extracurricular activities on campus.A. having involvedB. to involveC. involvedD. involving41.Let me assure you that it was not done ______D_____.A. intensiveB. intensivelyC. intentionalD. intentionally42.This medicine can ____A____ the pain in no time.A. lessenB. lessC. looseD. loosen43.Advertising is often the most ____A____ method of product promotion.A. effectiveB. effectedC. emotionalD. efficient44.It is at 5 a.m. _C___ the train from Beijing to Guangzhou will arrive.A. forB. withC. thatD. when45.Things might have been much worse if the mother _B___ on her right to keep the baby.A. has been insistingB. had insistedC. would insistD. insisted46.Had he worked harder, he __B__ the exams.A. must have got throughB. would have got throughC. permitted are freshmenD. are permitted freshmen47.God knows, how can I ____A____ six exams a week?.A. endureB. tolerateC. bear D .suffer48.All their attempts ______C______ the child from the burning building were in vain.A. rescuedB. to have rescueC. to rescueD. in rescuing49,Could you find someone ______C_______ for me to play tennis with?A. to fitB. fitC. fittingD. suit50. The smooth space flight marks a big ______D______ for Chinese scientists.A. breakB. breakingC. breakupD. breakthrough51 Economic crises destroy the capitalist system, and they grow in size and ____A____.A. durationB. duringC. endureD. enduringpanies will have to do more than this if they are to ___B_____ the earthquake.A. survivedB. surviveC. survivesD. surviving53. The baby felt ____C____ by her parents.A. hateB. loveC. ignoredD. ignore54. There are good chances of ______D______ in this firm.A. moveB. motiveC. motionD. promotion55. She tried to ______D______ our friendship.A. underminedB. keptC. maintainedD. undermine56. The new airport will ______C___ tourism.A. helpedB. useC. facilitateD. benefited57. They were full of ____B____ when they saw my new car.A. jealousB. envyC. pitifulD. loves58. This medicine can ____A____ the pain in no time.A. lessenB. lessC. leastD. little59.He _______A_____ her on her last physics paper.A. complimentedB. complimentC. completeD. completed60. Mrs. Hill is _______B____ on Tom’s marrying Susan.A. awareB. keenC. kindD. keep61. Let me assure you that it was not done ______C_____.A. internationalB. purposeC. intentionallyD. intentional62.The novel _____D_____ the imagination of thousands of readers.A. seizeB. catchC. captureD. captured63. If you yell ___A____ someone, you should apologize.A. atB. outC. ofD. under64. He came over and ____B____ a piece of paper into my hand.A. shoveB. shovedC. shovesD. shoving65. The girl student whose father is a senior engineer used to ___A____ abroad.A. studyB. studyingC. studiesD. studied66.The doctor tried to help ____D____ the function of his limbs.A. storeB. storesC. restoresD. restore67. Women’s social ____B____ has changed much over the years.A. placeB. statusC. landD. work68. It was he ___A___ knew he didn’t need to be afraid to make mistakes at that moment.A. whoB. whomC. whichD. whose69. A dropped cigarette is being ____C____ for the fire.A. firedB. fireC. blamedD. blame70. Advertising is often the most _____D___ method of promotion.A. effectB. affectC. affectedD. effective71.In time you’ll _____A_____ the beauty of this language.A. appreciateB. appreciatesC. appreciatedD. appreciating72. ______B___ is his favorite toy is hard for the kid to decide.A. whoB. whichC. howD. where73. After dinner, I took a ____A____ around the park.A. strollB. divingC. runD. walking74. The boy really enjoyed the useful and ____B____ work in science.A. creationB. creativeC. createdD. creator75. Some professions are _____A_______ with people who have not grown up.A. stuffedB. stuffC. stiffD. stiffed76. Economic crises destroy the capitalist system, and they grow in size and ____A____.A. durationB. duringC. endureD. enduring77. The new airport will ____C_____ tourism.A. helpedB. useC. facilitateD. benifited78. Let me assure you that it was not done _____C______.A. internationalB. purposeC. intentionallyD. intentional79. The girl student whose father is a senior engineer used to ___A____ abroad.A. studyB. studyingC. studiesD. studied80. Advertising is often the most ____D____ method of promotion.A. effectB. affectC. affectedD. effective81. It was he ___A___ knew he didn’t need to be afraid to make mistakes at that moment.A. whoB. whomC. whichD. whosepanies will have to do more than this if they are to _____B___ the earthquake.A. survivedB. surviveC. survivesD. surviving83. The baby felt ____C____ by her parents.A. hateB. loveC. ignoredD. ignore84. If you yell ___A____ someone, you should apologize.A. atB. outC. ofD. under85. Mrs. Hill is ______B_____ on Tom’s marrying Susan.A. awareB. keenC. kindD. keep86. _____A____ college takes hard work!A. SurvivingB. SurviveC. SurvivesD. To surviving87. It is absolutely ____A___ for the human beings to do something to save the earth.A. necessaryB. necessarilyC. unnecessaryD. unnecessarily88. An employer is more ____D____ to choose the candidate with high grades.A. likeB. probablyC. possiblyD. likely89. I don’t like this young woman. She seems a social ___D____.A. flowerB. workerC. negotiatorD. butterfly90. How should this ___B____ girl see the coldness of the letter?A. blindB. innocentC. warmD. pretty91. Be careful not to ___B____ yourself in their local conflicts.A. attendB. involveC. take part inD. participate92. Home prices are holding ____C___ in most regions, and people live with steady income.A. flexibleB. fierceC. stableD. shortage93. He was ______C______ by a newspaper reporter at that time.A. being interviewB. interviewC. being interviewedD. to interview94. They were full of ____C____ when they saw my new car.A. enviousB. envilyC. envyD. envies95. ________C_________, it is a very stable pattern producing results through effort .A. ContraryB. ConverseC. On the contraryD. Diverse96. The teacher doesn’t permit _____B____ in classA. smokeB. smokingC. to smokeD. to have a smoke97. The man in the corner confessed to ___A____ a lie to the manager of the company.A. have toldB. be toldC. being toldD. having told98. I never forget __B____ you for the first time.A. to meetB. meetingC. to have metD. having to be meeting.99. _______A___ a man has, ___________ he wants.A. The more, the moreB. More, moreC. The more, moreD. More, the more100. I see no _____B____ of his success.A. possibleB. possibilityC. possibilitiesD. probable101. He could be ____B____ about everything else in the world, but not about Manet, his loving child.A. visualB. criticalC. favoriteD. essential102. Some see themselves as the provider of ideas, ____C____ others view their role as essentially managerial.A. whenB. thereforeC. whileD. otherwise103. The idea ____A____ to him so much that he took it without hesitation.A. appealedB. interestedC. drewD. attracted104. The teacher evaluated the performance of each of his students who ____B____ evaluated his performance.A. by turnB. in turnC. at turnD. on turn105. Disease _____A____ during the journey and many passengers had to be rushed to hospital for treatment.A. broke outB. broke downC. started offD. started out106. The products have been _____A_____ to strict tests before leaving the factory.A. subjectedB. adjustedC. objectedD. constricted107. Our television set is out of order. Could you come and _____C_____ it for us?A. see throughB. see outC. see toD. see over108. _____C_____ they have taken matters into their hands, the pace of events has quickened.A. As thatB. So thatC. Now thatD. For that109. Our neighbor said if we made more noise he would _____B____ us to the police.A. inform ofB. complain aboutC. report toD. care for110. People working in the government should not _____A______ business affairs that might affect their political decisions.A. engage inB. hope forC. choose betweenD. pick on111. It's not easy to ______B______ to the top in show business.A. make outB. make itC. make upD. make them112. Carter is in charge of the office while I'm ______C______.A. leavingB. on leavingC. on leaveD. on relief113. We must ____B____ our attention on the question of reducing our cost.A. payB. focusC. absorbD. promote 114. Snap judgments, if ___B_____, have usually been considered signs of immaturity or lack of common sense.A. taking seriouslyB. taken seriouslyC. take seriouslyD. to be taken seriously115. Being with his family for a few days, I gained one or two insights ____A____ the reason he behaves the way he does.A. intoB. ofC. onD. off116. ___D___ your age or knowledge of the language, you'll be 100% involved in your studies from the first lesson to the last.A. As a result ofB. In spiteC. Despite ofD. Regardless of117. He said he had caught a bad cold and told me to ____D____ him.A. stay atB. stay upC. stay inD. stay away from118. Good managing of a company ___A_____ great efforts.A. calls forB. calls outC. calls inD. calls at119. I've looked ___C_____ all my papers but I still can't find the contract.A. uponB. outC. throughD. in120. When I'm going out in the evening I use the bike if I can, ___B_____ the car.A. regardless ofB. more thanC. other thanD. rather than121. We can't speak ___A____ our teacher.A. too highly ofB. too high ofC. highly of tooD. to highly of122. No one in our class ____D___ in sports than he.A. are more interestedB. are much interestedC. is much interestedD. is more interested123. As a teacher you have to ____B____ your methods to suit the needs of slower children.A. enlargeB. adjustC. affectD. afford124. ________A___ the hotel before soldiers armed with pistols and clubs charged into the crowd.A. Scarcely had they leftB. Scarcely they had leftC. Scarcely they leftD. Scarcely left they125. In the film, the peaceful life of a monk _____D_____ the violent life of a murderer.A. is compared withB. is compared toC. is contrasted toD. is contrasted with126 I didn't realize the food problem was so _____B___ in this city; with winter coming, many people would starve to death without more help.A. essentialB. criticalC. explicitD. effective127. Female socialization places importance on getting along with others, _____B___ male socialization places importance on becoming independent.。
c 第三版课后习题答案

c 第三版课后习题答案C 第三版课后习题答案C语言是一门广泛应用于计算机编程领域的编程语言,它的学习对于计算机科学和软件工程的学生来说是非常重要的。
C语言第三版是一本经典的教材,它为学生提供了丰富的习题来巩固所学的知识。
在这篇文章中,我将为大家提供C语言第三版课后习题的答案,希望能对大家的学习有所帮助。
1. 第一章:开始学习C语言在第一章中,主要介绍了C语言的基本概念和语法规则。
以下是一些常见的习题及其答案:1.1 填空题:a) C语言是一种______语言。
答案:结构化b) 在C语言中,用于存储数据的基本单元是______。
答案:变量1.2 程序设计题:编写一个C程序,要求用户输入两个整数,然后输出它们的和。
答案:```c#include <stdio.h>int main() {int num1, num2, sum;printf("请输入两个整数:");scanf("%d%d", &num1, &num2);sum = num1 + num2;printf("它们的和为:%d\n", sum);return 0;}```2. 第二章:C语言基础第二章主要介绍了C语言的基本数据类型、运算符和控制结构。
以下是一些习题及其答案:2.1 选择题:a) 下列哪个是C语言的基本数据类型?1. float2. string3. boolean4. char答案:4. charb) 下列哪个运算符用于求两个整数的商?1. +2. -3. *4. /答案:4. /2.2 程序设计题:编写一个C程序,判断一个整数是否为偶数。
答案:```c#include <stdio.h>int main() {int num;printf("请输入一个整数:");scanf("%d", &num);if (num % 2 == 0) {printf("这是一个偶数。
(word完整版)C语言程序设计课后习题1-8参考答案

C语言程序设计课后习题1—8参考答案习题1参考答案一、简答题1、冯诺依曼计算机模型有哪几个基本组成部分?各部分的主要功能是什么?答:冯诺依曼计算机模型是由运算器、控制器、存储器、输入设备、输出设备五大功能部件组成的。
运算器又称算术逻辑部件,简称ALU,是计算机用来进行数据运算的部件。
数据运算包括算术运算和逻辑运算。
控制器是计算机的指挥系统,计算机就是在控制器的控制下有条不紊地协调工作的.存储器是计算机中具有记忆能力的部件,用来存放程序和数据.输入设备是用来输入程序和数据的部件。
输出设备正好与输入设备相反,是用来输出结果的部件。
2、简述计算机的工作原理。
答:计算机的工作原理可简单地概括为:各种各样的信息,通过输入设备,进入计算机的存储器,然后送到运算器,运算完毕把结果送到存储器存储,最后通过输出设备显示出来。
整个过程由控制器进行控制。
3、计算机软件系统分为哪几类?答:软件内容丰富,种类繁多,通常根据软件用途将其分为两大类:系统软件和应用软件。
系统软件是指管理、监控、维护计算机正常工作和供用户操作使用计算机的软件。
这类软件一般与具体应用无关,是在系统一级上提供的服务。
系统软件主要包括以下两类:一类是面向计算机本身的软件,如操作系统、诊断程序等。
另一类是面向用户的软件,如各种语言处理程序(像BC、VC等)、实用程序、字处理程序等。
在操作系统的基础上运行。
4、什么叫软件?说明软件与硬件之间的相互关系。
答:软件是指计算机程序及有关程序的技术文档资料。
两者中更为重要的是程序,它是计算机进行数据处理的指令集,也是计算机正常工作最重要的因素。
在不太严格的情况下,认为程序就是软件。
硬件与软件是相互依存的,软件依赖于硬件的物质条件,而硬件则需在软件支配下才能有效地工作.在现代,软件技术变得越来越重要,有了软件,用户面对的将不再是物理计算机,而是一台抽象的逻辑计算机,人们可以不必了解计算机本身,可以采用更加方便、更加有效地手段使用计算机。
大学生C语言课后习题全部答案详解

\
main()
{
int y,m,d,yt,mt,dt,age;
printf("请按格式2010-12-10输入你的生日");
scanf("%d-%d-%d",&y,&m,&d);
printf("请按格式2010-12-10输入今天的日期");
scanf("%d-%d-%d",&yt,&mt,&dt);printf(源自| 0——退出|\n");
printf("|------------------------|\n");
printf("请输入选项\n");
ch=getch();
putch(ch);
}
#include<stdio.h>
#include<conio.h>
main()
{
//定义
int a=0,b=0,he=0,cha=0,ji=0;
{printf("%4d",i);
n++;
}
}
printf("\n");
printf("%d",n);
}#include<stdio.h>
#include<math.h>
main()
{
int n=0,s=1,i=0;
printf("input:");
scanf("%d",&n);
for(i=1;i<=n;i++)
数据结构_c语言描述(第二版)答案_耿国华_西安电子科技大学(完整资料).doc

【最新整理,下载后即可编辑】第1章 绪 论2.(1)×(2)×(3)√3.(1)A (2)C (3)C5.计算下列程序中x=x+1的语句频度for(i=1;i<=n;i++)for(j=1;j<=i;j++)for(k=1;k<=j;k++) x=x+1;【解答】x=x+1的语句频度为:T(n)=1+(1+2)+(1+2+3)+……+(1+2+……+n )=n(n+1)(n+2)/66.编写算法,求 一元多项式p n (x)=a 0+a 1x+a 2x 2+…….+a n x n 的值p n (x 0),并确定算法中每一语句的执行次数和整个算法的时间复杂度,要求时间复杂度尽可能小,规定算法中不能使用求幂函数。
注意:本题中的输入为a i (i=0,1,…n)、x 和n,输出为P n (x 0)。
算法的输入和输出采用下列方法(1)通过参数表中的参数显式传递(2)通过全局变量隐式传递。
讨论两种方法的优缺点,并在算法中以你认为较好的一种实现输入输出。
【解答】(1)通过参数表中的参数显式传递优点:当没有调用函数时,不占用内存,调用结束后形参被释放,实参维持,函数通用性强,移置性强。
缺点:形参须与实参对应,且返回值数量有限。
(2)通过全局变量隐式传递优点:减少实参与形参的个数,从而减少内存空间以及传递数据时的时间消耗缺点:函数通用性降低,移植性差算法如下:通过全局变量隐式传递参数PolyValue(){ int i,n;float x,a[],p;printf(“\nn=”);scanf(“%f”,&n);printf(“\nx=”);scanf(“%f”,&x);for(i=0;i<n;i++)scanf(“%f ”,&a[i]); /*执行次数:n次*/p=a[0];for(i=1;i<=n;i++){ p=p+a[i]*x; /*执行次数:n次*/x=x*x;}printf(“%f”,p);}算法的时间复杂度:T(n)=O(n)通过参数表中的参数显式传递float PolyValue(float a[ ], float x, int n){float p,s;int i;p=x;s=a[0];for(i=1;i<=n;i++){s=s+a[i]*p; /*执行次数:n次*/p=p*x;}return(p);}算法的时间复杂度:T(n)=O(n)第2章线性表习题1.填空:(1)在顺序表中插入或删除一个元素,需要平均移动一半元素,具体移动的元素个数与插入或删除的位置有关。
电子科技大学14秋《C语言》在线作业2答案

14秋《C语言》在线作业2一,单选题1. 设变量a b c d 和y都已经正确定义并赋值,若有以下if 语句: if(a<b) if(c==d) y=0; else y=1; 给语句所表示的含义是( )A. y=0 a<b且c=d; y=1 a>=bB. y=0 a<b且c=d; y=1 a>=b 且c!=dC. y=0 a<b且c=d; y=1 a<b 且c!=dD. y=0 a<b且c=d; y=1 c!=d?正确答案:C2. 以下正确的叙述是( )A. 在C程序中,每行中只能写一条语句B. 若a是实型变量,C程序中允许赋值a=10,因此实型变量中允许存放整型数C. 在C程序中,无论是整数还是实数,都能被准确无误地表示D. 在C程序中,%是只能用于整数运算的运算符?正确答案:D3. 有以下程序: #include <string.h> main() {char p[ ]={‘a’,’b’,’c’},q[10]={‘a’,’b’,’c’}; printf(“%d%d\n”,strlen(p),strlen(q)); } 以下叙述中正确的是()A. 在给p和q数组置初值时,系统回自动添加字符串结束符,故输出的长度都为3B. 由于p数组中没有字符串结束符,长度不能确定,但q数组中字符串长度为3C. 由于q数组中没有字符串结束符,长度不能确定,但p数组中字符串长度为3D.正确答案:B4. 标准库函数fgets(s,n,f)的功能是( )A. 从文件f中读取长度为n的字符串存入s中B. 从文件f中读取长度小于n的字符串存入s中C. 从文件f中读取n个字符串存入s中D. 从文件f中读取长度为n-1的字符串存入s中?正确答案:B5. 若i为整型变量,则以下循环执行次数是( ): for (i=2;i==0;) printf("%d",i-- );A. 无限次B. 0次C. 1 次。
(完整word版)C语言课后习题答案(最终)

第0章习题1. 将下列十进制数分别转化为二进制数、八进制数和十六进制数:(1)128 (2)511 (3)1024 (4)65535 (5)1048575答:(1)10000000、200、80(2)111111111、777、1FF(3)10000000000、2000、400(4)1111111111111111、177777、FFFF(5)11111111111111111111、3777777、FFFFF2. 将下列二进制数转化为十进制数和十六进制数:(1)1100110101B (2)101101.1011B答:(1)821、335(2)45.6875、2D.B3。
写出下列数的原码、反码、补码:15、-20、—27/32答:(1)00001111、00000000、00001111(2)10010100、11101011、11101100(3)1。
1101100、1。
0010011、1.00101004. 16位无符号定点整数的数值表示范围为多少?8位补码的表示范围是多少?16位补码的表示范围是多少?答:0~65535、—128~127、—32768~327675.1968年Dijkstra提出结构化程序设计的思想的原因是什么?简要回答结构化程序设计的经典定义.答:结构化程序设计概念的提出主要是源于程序结构的层次性与模块化使得构造出来的软件具有良好的可理解性和可维护性,随着软件规模的扩大与复杂性的提高,程序的可维护性成为程序设计者们关注的重要问题之一。
如果一个程序的代码块仅仅通过顺序、选择和循环这3种基本控制结构进行连接,并且每个代码块只有一个入口和一个出口,则称这个程序是结构化的。
6.C程序在内存中存储在哪儿?计算机的内存空间是如何分区的?分区存放不同类型的数据的目的是什么?答:C语言程序属于应用程序,程序代码本身存放在应用程序区,程序运行时处理的数据存放在应用程序数据区。
计算机的内存空间主要分为3个区:系统程序区、应用程序区和数据区,其中数据区又分为系统程序数据区和应用程序数据区两类。
C语言教材课后习题参考答案.docx

}
}
}
4、
main()
{int i, a, min, max;
scanf(“%d" , &a);
min=a;max=a;
for(i=2;i<=100;i++)
{ scanf(“,&a);
if (a<min) min=a;
if (a>max) inax=a;
}
printf (<<Max=%d, Min=%d\nv, max, min);
3、存储单元、符号地址、内存地址
4、十、十六、八
5、double(双精度型)
6、8
7、5.500000
8、a二-32768
9、+0017,021,0x11
三、写程序运行结果
3257
32 57
7.8& -345.12,7. 8765,-345.1230
7.87654e+00,-3. 5e+02
a,97, 141,61
main()
{int x, y;
for(x=20;x<=100;x++)
for (y=20;y<=100;y++)
if(x*x+y二二1053&&x+y*y二二873)
printf (i(x=%dy=%d\n” , x, y);
}
7、
main()
{
long int k, g, s=0;
printf ("Please input an integer:,z);
Printf(is not a quadratic);
c语言全部章节习题答案

c语言全部章节习题答案C语言是一门广泛应用于计算机编程的高级编程语言。
它的简洁和强大使得它成为了许多程序员的首选语言。
然而,学习一门新的编程语言并不总是容易的,特别是对于初学者来说。
为了帮助那些正在学习C语言的人们,本文将提供C语言全部章节习题的答案,以期为读者提供一些指导和帮助。
在学习C语言时,习题是巩固所学知识的重要方式。
通过解决习题,学生可以加深对C语言的理解,并提高他们的编程技能。
然而,有时候习题的解答并不容易找到,特别是对于初学者来说。
因此,本文将提供C语言教材中的全部章节习题的答案,以供读者参考。
在第一章中,我们学习了C语言的基本概念和语法。
习题主要涉及变量的声明和赋值,以及简单的算术运算。
以下是第一章习题的答案:1. 声明一个整型变量x,并将其赋值为10。
答案:int x = 10;2. 声明一个字符型变量ch,并将其赋值为'A'。
答案:char ch = 'A';3. 声明两个整型变量a和b,并将其相加的结果赋值给变量c。
答案:int a = 5;int b = 3;int c = a + b;第二章主要介绍了C语言中的控制结构,如条件语句和循环语句。
以下是第二章习题的答案:1. 编写一个程序,判断一个整数是否为偶数。
如果是偶数,输出"Even",否则输出"Odd"。
答案:#include <stdio.h>int main() {int num;printf("Enter an integer: ");scanf("%d", &num);if(num % 2 == 0) {printf("Even");} else {printf("Odd");}return 0;}2. 编写一个程序,计算1到100之间所有偶数的和。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
将A中的醋倒入C
将B中的酱油倒入A
将C中的醋倒入B
2.输入一个数放在a中
max = a
以下步骤重复9次:
输入一个数放在a中
如果a>max,max=a
打印max的值
3.如果a<b
temp=a
a=b
b=temp
如果c>a
temp = a
a=c
c=temp
否则
如果c>b
temp=b
b=c
c=temp
2.顺序结构,分支结构,循环结构
3.所谓算法就是为解决一个问题而采取的方法和步骤。
算法的特性:有穷性、确定性、输入、输出、可行性。
表示算法的方式:(1)用自然语言表示;(2)用流程图表示;(3)用N—S流程图表示;(4)用伪代码表示;(5)用计算机语言表示。
二、算法
1.瓶子A里盛有醋
瓶子B里盛有酱油
int g,s,b;//个位、十位、百位
printf("水仙花数有:\n");
for(a=100;i<=999;a++)
{g=a%10;
s=a/10%10;
b=a/100;
if (a==g*g*g+s*s*s+b*b*b)
printf("%d ",a);
}
printf("\n");
}
4、
main()
}
2.main()
{
int a,b,c;
scanf(“%d%d%d”,&a,&b,&c);
printf(“sum=%d”,a+b+c);
}
第二章 数据类型与表达式
一、单项选择题
1. B 2. C 3. A 4. A 5. D 6. B 7.A 8.C 9.D
二、写出下列程序的运行结果
1.
aabb cc abc
}
第三章 顺序程序设计
选择题
1)B 2)D 3)D 4)A 5)A 6)B 7)B 8)A 9)D 10)C 11)D 12)A
填空题
1)b 2)261 3)25 21 37 4)abc 5)5.0,4,c=3 6)3
编程题
#define PI 3.1415926
main( )
{
float h, r, l, sv, sz ;
c
第一章 C语言概述
一、简答题
1.(1)语言简洁、紧凑,使用方便、灵活;(2)运算符丰富;(3)具有丰富的数据类型;(4)具有结构化的控制语句;(5)语法限制不太严格,程序设计自由度大;(6)C语言允许直接访问物理地址,能进行位(bit)操作,能实现汇编语言的大部分功能,可以直接访问硬件;(7)生成目标代码质量高,程序执行效率高;(8)用C语言写的程序可移植性好,基本上不作修改就能用于各种型号的计算机和各种操作系统。
如果i除以j的余数为零
flag=0
退出该循环
如果flag==1
打印i的值
i=i+2
7.如果m<n
temp=m
m=n
n=temp
i=n
做以下循环,直到m,n能够被i整除:
如果m能够被i整除并且n能够被i整除
i即是m和n的最大公约数
跳出循环
否则
i=i-1
打印i的值
8.data=b*b—4*a*c
如果data>0
AN
2.
ab
9798
3. 9,11,9,10
4.
a=2,b=3,c=2
5.
s1=2.500000
s2=3
三、编程题
main()
{
char c1='c',c2='h',c3='i',c4='n',c5='a';
c1=c1+4;
c2=c2+4;
c3=c3+4;
c4=c4+4;
c5=c5+4;
printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5);
printf(“求体积为:sv=%6.2f, 圆柱体积为:sz=%6.2f\n”, sv, sz) ;
}
2、
main( )
{
float c, fห้องสมุดไป่ตู้;
printf(“请输入一个华氏温度:\n”) ;
scanf(“%f”, &f) ;
c=5.0/9.0*(f-32) ;
printf(“摄氏温度为:%6.2f\n”, c) ;
else if (a>=100&&a<=999)
printf(“这是个三位数”);
else if (a>=10&&a<=99)
printf(“这是个二位数”);
else if (a>=0&&a<=9)
printf(“这是个一位数”);
else
printf(“输入错误”);
}
3.
main()
{
int a;
printf(“请输入圆的半径r,圆柱高h:\n”) ;
scanf(“%f,%f”,&r, &h) ;
l=2* PI *r ;
s= PI *r*r ;
sv=3.0/4.0* PI *r*r*r ;
sz= PI *r*r*h ;
printf(“圆周长为:l=%6.2f, 圆面积为:s=%6.2f\n”, l, s) ;
8.Year%400==0
year%100!=0
count++
三、编程题
1.
main()
{
long a;
scanf(“%ld”,&a);
if (a>=10000&&a<=99999)
printf(“这是个五位数”);
else if (a>=1000&&a<=9999)
printf(“这是个四位数”);
}
第四章 循环程序设计
一、选择题
1.D 2.D 3.D 4.C 5.D 6.A 7.B 8.B 9.A 10.D.11.B 12.A 13.B 14.C 15.C 16.A 17.A 18.B 19.D 20.D 21.A
二、填空题
1.585858
2.0
3.18
4.8921
5.20,0
6.6
7.1,3,2
{int i,n;
printf("input n:");
scanf("%d",&n);
printf("\n%d=",n);
for(i=2;i<=n;i++)
{while(i!=n)
if(n%i==0)
打印a,b,c的值
4.i=1
sum=0
以下程序循环100遍:
sum=sum+i
i=i+1
打印sum的值
5.如果(n除以3的余数为0 并且 n除以5的余数为0)
n能够同时被3和5整除
否则
n不能够同时被3和5整除
6.i=101
以下语句循环50遍:
j=2
flag=1
当j<(i除以2的整数部分)时循环以下语句:
否则 如果data=0
x1=x2=—b/2
否则
无实数解
三、编程题
1.main()
{
printf(“##############################\n”);
printf(“ How do you do? \n”);
printf(“##############################\n”);