c语言程序设计第四版第九章答案谭浩强

合集下载

C语言程序设计第四版谭浩强课后习题答案完整版《C语言程序设计》精品课程

C语言程序设计第四版谭浩强课后习题答案完整版《C语言程序设计》精品课程

C语言程序设计第四版谭浩强课后习题答案完整版《C语言程序设计》精品课程1. 5请参照本章例题,编写一个C程序,输出以下信息:**************************Very Good!**************************解:mian(){printf(“**************************”);printf(“\n”);printf(“Very Good!\n”);printf(“\n”);printf(“**************************”);}1.6 编写一个程序,输入a、b、c三个值,输出其中最大值。

解:mian(){int a,b,c,max;printf(“请输入三个数a,b,c:\n”);scanf(“%d,%d,%d”,&a,&b,&c);max=a;if(max max=b;if(maxmax=c;printf(“最大数为:“%d”,max);}第三章3.6写出以下程序运行的结果。

main(){char c1=’a’,c2=’b’,c3=’c’,c4=’1’,c5=’6’;printf(“a%cb%c\tc%c\tabc\n”,c1,c2,c3);printf(“\t\b%c %c”,c4,c5);}解:aaㄩbbㄩㄩㄩccㄩㄩㄩㄩㄩㄩabcAㄩN3.7 要将'China'译成密码,译码规律是:用原来字母后面的第4个字母代替原来的字母.例如,字母'A'后面第4个字母是'E'.'E'代替'A'。

因此,'China'应译为'Glmre'。

请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输出。

(完整word版)c程序设计(第四版)谭浩强-课后答案(完整版).pdf

(完整word版)c程序设计(第四版)谭浩强-课后答案(完整版).pdf

C 语言课后习题答案 -第四版 - 谭浩强 (1-7 )第一章#include <stdio.h>int main ( )**\n\n");printf(" Very Good!\n\n");printf (return 0; #include <stdio.h>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<b)max=b;if (max<c)max=c;printf **\n");printf("The largest number is %d\n",max); return 0;}第3章#include <stdio.h>#include <math.h> int main(){float p,r,n;r=0.1;n=10;p=pow(1+r,n); printf("p=%f\n",p); return 0; }#include <stdio.h>#include <math.h> int main()p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;{float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p1=p*((1+r5)*5); // 一次存5 年期p2=p*(1+2*r2)*(1+3*r3); // 先存2 年期,到期后将本息再存3 年期// 先存3 年期,到期后将本息再存2 年期p3=p*(1+3*r3)*(1+2*r2);p4=p*pow(1+r1,5); // 存1 年期,到期后将本息存再存1 年期,连续存5 次p5=p*pow(1+r0/4,4*5); // 存活期存款。

谭浩强c语言9章课后习题答案

谭浩强c语言9章课后习题答案
{
printf(" 习题9.3 求三角形的面积\n=================================\n");
printf("请输入三角的三边,用空格隔开:\n");
float a,b,c,s,area;
scanf("%f%f%f",&a,&b,&c);
printf(" 请输入三个数(用空格隔开):\n");
int a,b,c,max;
scanf("%d%d%d",&a,&b,&c);
MAX(a,b,c,max);
printf(" 最大的数为:\n%d",max);
return 0;
}
string[i]+=1;
else if(string[i]=='z'||string[i]=='Z')
string[i]-=25;
}
}
#endif
printf("输出文本行为:\n%s",string);
printf("请输入三个数(用空格隔开):\n");
scanf("%f%f%f",&a,&b,&c);
printf("每行一个数:\n");
printf(FORMAT1,a);
printf("每行两个数:\n");
printf(FORMAT2,a,b);
printf(" 请输入三个数(用空格隔开):\n");

C程序设计(第四版)谭浩强-课后习题答案-共11章(同名1796)

C程序设计(第四版)谭浩强-课后习题答案-共11章(同名1796)

C程序设计(第四版)谭浩强-课后习题答案-共11章(同名1796)第1章程序设计和C语言11.1什么是计算机程序11.2什么是计算机语言11.3C语言的发展及其特点31.4最简单的C语言程序51.4.1最简单的C语言程序举例61.4.2C语言程序的结构101.5运行C程序的步骤与方法121.6程序设计的任务141-5 #include <stdio.h>int main ( ){ printf ("**************************\n\n");printf(" Very Good!\n\n");printf ("**************************\n");return 0;}1-6#include <stdio.h>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<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max); return 0;}第2章算法——程序的灵魂162.1什么是算法162.2简单的算法举例172.3算法的特性212.4怎样表示一个算法222.4.1用自然语言表示算法222.4.2用流程图表示算法222.4.3三种基本结构和改进的流程图262.4.4用N S流程图表示算法282.4.5用伪代码表示算法312.4.6用计算机语言表示算法322.5结构化程序设计方法34习题36第章最简单的C程序设计——顺序程序设计373.1顺序程序设计举例373.2数据的表现形式及其运算39 3.2.1常量和变量393.2.2数据类型423.2.3整型数据443.2.4字符型数据473.2.5浮点型数据493.2.6怎样确定常量的类型513.2.7运算符和表达式523.3C语句573.3.1C语句的作用和分类573.3.2最基本的语句——赋值语句59 3.4数据的输入输出653.4.1输入输出举例653.4.2有关数据输入输出的概念67 3.4.3用printf函数输出数据68 3.4.4用scanf函数输入数据75 3.4.5字符数据的输入输出78习题823-1 #include <stdio.h>#include <math.h>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 <stdio.h>#include <math.h>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); // 存活期存款。

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

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

// 一次存 5 年期 // 先存 2 年期,到期后将本息再存 // 先存 3 年期,到期后将本息再存
3 年期 2 年期
p4=p*pow(1+r1,5); p5=p*pow(1+r0/4,4*5); printf("p1=%10.2f\n",p1);
// 存 1 年期,到期后将本息存再存 1 年期,连续存 5 次 // 存活期存款。活期利息每一季度结算一次
return 0;
}
3-8-2
#include <stdio.h> int main() {
char c1,c2; int i1,i2; printf(" 请输入两个字符 c1,c2:"); scanf("%c,%c",&c1,&c2); i1=c1; i2=c2; printf(" 按字符输出结果 :\n"); printf("%c,%c\n",i1,i2); printf(" 按整数输出结果 :\n"); printf("%d,%d\n",c1,c2); return 0; } 3-8-3 #include <stdio.h> int main() { char c1,c2; int i1,i2; printf(" 请输入两个整数 i1,i2:"); scanf("%d,%d",&i1,&i2); c1=i1; c2=i2; printf(" 按字符输出结果 :\n"); printf("%c,%c\n",c1,c2); printf(" 按整数输出结果 :\n"); printf("%d,%d\n",c1,c2); return 0; } 3-8 #include <stdio.h> int main() { char c1,c2; printf(" 请输入两个字符 c1,c2:"); c1=getchar(); c2=getchar(); printf(" 用 putchar 语句输出结果为 :"); putchar(c1); putchar(c2); printf("\n"); printf(" 用 printf 语句输出结果为 :");

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

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

C程序设计第四版谭浩强完整版课后习题答案Revised by Chen Zhen in 2021C程序设计(第四版)(谭浩强)第一章课后习题答案#include<>代码均调试成功,若有失误大多不是代码问题.自已找找.int main(){printf("Welcome to \n");return 0; }#include<>int main(){int a,b,sum;a=5;b=4;sum=a+b;printf("The sum is %d .\n",sum);return 0;}P008 调用函数比较两个数的大小.#include<>int main(){int max(int x,int y); int a,b,c;scanf("%d,%d",&a,&b); c=max(a,b); printf("The max is %d .\n",c);return 0;}int max(int x,int y) {int z; if (x>y)z=x;elsez=y;return(z); }P015 三个数的大小.(数字0表示课后练习题)#include<>int main(){int a,b,c,d; int max(int x , int y , int z); printf("Please input 3 numbers :\n");scanf("%d %d %d",&a,&b,&c);d=max(a,b,c); printf("The max is :%d .\n",d); }int max(int x , int y , int z){int m;if (x>y && x>z) m=x;if (y>x && y>z)m=y;if (z>y && z>x)m=z;return (m); }C程序设计(第四版)(谭浩强)第2章课后习题答案算法——程序的灵魂P017 计算机1-5相乘的积.#include<>int main(){int i,s=1; for(i=1;i<6;i++) {s=s*i; n",s);return 0;}#include<> int main(){int i,s=1; for(i=1;i<12;i++) 可以是i=i+2{if(i%2!=0) s=s*i;elsecontinue; }printf("The sum is %d .\n",s);return 0;}暂时没法做.年的概念是地球围绕太阳一周的时间(所谓公转周期)称为一年,这个周期是相当稳定的,很长时间也不会变动1秒,但是真正的一年是天(目前)。

C语言程序设计-谭浩强-第9章预处理命令

C语言程序设计-谭浩强-第9章预处理命令
例 #define S (r) PI*r*r 例 #define POWER(x) x*x 相当于定义了不带参宏S,代表字符串“(r) PI*r*r” x=4; y=6; z=POWER(x+y); 宏展开:z=x+y*x+y; 一般写成: #define POWER(x) ((x)*(x)) 宏展开: z=((x+y)*(x+y));
17/14
§9.1 宏定义 宏体可缺省,表示宏名
不带参数宏定义
定义过或取消宏体
一般形式: 宏体] 一般形式: #define 宏名 [宏体 宏体 功能:用指定标识符 宏名)代替字符序列 宏体) 用指定标识符(宏名 代替字符序列(宏体 功能 用指定标识符 宏名 代替字符序列 宏体
如 #define 定义位置:任意YES 1 一般在函数外面) 定义位置 任意(一般在函数外面 任意 一般在函数外面 #define 例 NO 0 YES #define 1 作用域:从定义命令到文件结束 作用域 从定义命令到文件结束 main() #define PI 3.1415926 #define OUT printf(“Hello,World”); { …….. YES原作用域 #undef可终止宏名作用域 可 } 格式: 格式: #undef 宏名 #undef YES 宏展开:预编译时,用宏体替换宏名 用宏体替换宏名---不作语法检查 宏展开:预编译时 用宏体替换宏名 不作语法检查 #define YES 0 max() 引号中的内容与宏名相同也不置换 YES新作用域 {…….. if(x==YES)WIDTH 80 如 例 #define WIDTH 80 printf(“correct!\n”); 例 #define 宏定义可嵌套, 宏定义可嵌套,不能递归 } else if (x==NO)( WIDTH+40 ) printf(“error!\n”); #define #define LENGTH 3.14159 例 #define PI LENGTH WIDTH+40 宏定义中使用必要的括号() 宏定义中使用必要的括号() 展开后: if(x==1) 展开后var=LENGTH*2; : var=LENGTH*2;printf(“correct!\n”); (×) printf(“2*PI=%f\n”,PI*2); 例 #define MAX MAX+10 × else 宏展开: 宏展开: :printf(“2*PI=%f\n”,3.14159*2); 宏展开 宏展开:var= ((x==0) ) * 2;printf(“error!\n”); 宏展开: if 80+40 宏展开 :var= 80+40*2;

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

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

第1章程序设计和C语言11.1什么是计算机程序11.2什么是计算机语言11.3C语言的发展及其特点31.4最简单的C语言程序51.4.1最简单的C语言程序举例61.4.2C语言程序的结构101.5运行C程序的步骤与方法121.6程序设计的任务141-5 #include <stdio.h>int main ( ){ printf ("**************************\n\n");printf(" Very Good!\n\n");printf ("**************************\n");return 0;}1-6#include <stdio.h>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<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max); return 0;}第2章算法——程序的灵魂162.1什么是算法162.2简单的算法举例172.3算法的特性212.4怎样表示一个算法222.4.1用自然语言表示算法222.4.2用流程图表示算法222.4.3三种基本结构和改进的流程图262.4.4用流程图表示算法282.4.5用伪代码表示算法312.4.6用计算机语言表示算法322.5结构化程序设计方法34习题36第章最简单的C程序设计——顺序程序设计37 3.1顺序程序设计举例373.2数据的表现形式及其运算393.2.1常量和变量393.2.2数据类型423.2.3整型数据443.2.4字符型数据473.2.5浮点型数据493.2.6怎样确定常量的类型513.2.7运算符和表达式523.3C语句573.3.1C语句的作用和分类573.3.2最基本的语句——赋值语句593.4数据的输入输出653.4.1输入输出举例653.4.2有关数据输入输出的概念673.4.3用printf函数输出数据683.4.4用scanf函数输入数据753.4.5字符数据的输入输出78习题823-1 #include <stdio.h>#include <math.h>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 <stdio.h>#include <math.h>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); // 存活期存款。

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

第九章9.1定义一个结构体变量(包括年、月、日)。

计算该日在本年中是第几天,注意闰年问题。

解:Struct{int year;int month;int day;}date;main(){int days;printf(“Input year,month,day:”);scanf(“%d,%D,%d”,&date.year,&date.month,&date.day);switch(date.month){case 1: days=date.day; break;case 2: days=date.day+31; break;case 3: days=date.day+59; break;case 4: days=date.day+90; break;case 5: days=date.day+120; break;case 6: days=date.day+31; break;case 7: days=date.day+181; break;case 8: days=date.day+212; break;case 9: days=date.day+243; break;case 10: days=date.day+273; break;case 11: days=date.day+304; break;case 12: days=date.day+334; break;}if((date.year%4==0&&date.year%100!=0||date.year%400==0)&&date.month>=3)days+=1; printf(“\n%d/%d is the %dth day in%d.”,date.month,data.day,days,date,year);}9.2写一个函数days,实现上面的计算。

由主函数将年、月、日传递给days 函数,计算后将日数传回主函数输出。

解:struct y_m_d{int year:int month;int day;}date;intdays(struct y_m_d date1){int sum;switch(data.month){case 1:sum=date1.day; break;case 2:sum=date1.day+31; break;case 3:sum=date1.day+59; break;case 4:sum=date1.day+90; break;case 5:sum=date1.day+120; break;case 6:sum=date1.day+151; break;case 7:sum=date1.day+181; break;case 8:sum=date1.day+212; break;case 9:sum=date1.day+243; breakcase 10:sum=date1.day+243; breakcase 11:sum=date1.day+243; breakcase 12:sum=date1.day+243; break}};9.3编写一个函数print,打印一个学生的成绩数,该数组中有5个学生的数据记录,每个记录包括num、name、sore[3],用主函数输入这些记录,用print函数输出这些记录。

解:#define N 5struct student{char num[6];char name[8];int score[4];}stu[N];main(){int I,j ;for(I=0;I<N;I++){printf(“\Input score of student %d:\n”,I+1);printf(“no.:”);scanf(“%s”,stu[i].num);printf(“name:”);scanf(“%s”,stu[i].name);for(j=0;j<3;j++){printf(“score%d:”j+1);scanf(“%d”,&stu[i].score[j]);}printf(“\n”);}print(stu);}print(struct student stu[6]){int I,j;printf(“%5s%10s”,stu[i].num,stu[i].name);for(j=0;j<3;j++)printf(“%9d”,stu[i].score[j]);print(“\n”);}9.4在上题的基础上,编写一个函数input,用来输入5个学生的数据记录。

解:#define N 5struct student{char num[6];char name[8];int score[4]}stu[N];input(struct student stu[]){int I,j;for(I=0;I<N;I++){printf(“input scores of student %d:\n”,I+1);printf(“NO.:”);scanf(“%s”,stu[i].num);printf(“name: ”);scanf(“%s”, stu[i].name);for(j=0;j<3;j++){printf(“score%d:”,j++);scanf(“%d”, &stu[i].score[j]);}}printf(“\n”);}}9.5 有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入10个学生的数据,要求打印出3门课的总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课成绩)解:#define N 10struct student{char num[6]char name[8]int score[4]float avr;}stu[N];main(){int I,j,max,maxi,sum;float average;for(I=0;I<N;I++){printf(“\nInput scores of student %d:\n”,I+1);printf(“NO.:”);scanf(“%s”,stu[i].num);printf(“name”);scanf(“%s”,stu[i].name);for(j=0;j<3;j++){printf(“score %d:”,j+1);scanf(“%d”, &stu[i].score[j]);}}average=0;max=0;maxi=0;for(i=0;i<3;i++){sum=0;for(j=0;j<3;j++)sum+=stu[i].score[j];stu[i].avr=sum/3.0;average+=stu[i].avr;if(sum>max){max=sum;maxi=I;}}average/=N;printf(“NO. name score1 score2 score3 average\n”);for(I=0;I<N;I++){printf(“%5s%10s”,stu[i].num, stu[i].name);for(j=0;j<3;j++)printf(“%9d”,stu[i].score[j]);printf(“%8.2f\n”,stu[i].avr);}printf(“average=%6.2f\n”,average);printf(“The highest score is:%s,score total:%d.”stu[maxi].name,max);}9.6 编写一个函数new,对n个字符开辟连续的存储空间,此函数应返回一个指针,指向字符串开始的空间。

New(n)表示分配n个字节的内存空间。

解:new函数如下:#define NULL 0#define NEWSIZE 1000char newbuf[NEWSIZE];char *newp=newbuf;char *new(int n){if (newp+n<=newbuf+ NEWSIZE){ newp= newp+n;return(newp-n);}elsereturn(NULL);}9.7写一函数free,将上题用new函数占用的空间释放。

Free(p)表示将p指向的单元以后的内存段释放。

解:#define Null o#define NEWSIZE 1000char newbuf[NEWSIZE];char *newp=newbuf;free(char *p){if((p>=newbuf)&&(p<newbuf+NEWSIZE))newp=p;}9.8已有a、b亮光链表,每个链表中的结点包括学好、成绩。

要求把两个链表合并,按学号升序排列。

解:#include<stdio.h>#define NULL 0#define LENsizeof(struct student)strut student{long num;int scor;struct student *next};struct student listA,listB;int n,sum=0;main(){struct student *creat(void);struct student *insert(struct student *,struct student *);void print(struct student *);stuct student *ahead , *bhead,*abh;printf(“\ninput list a:\n”);ahead=creat();sum=sum+|n;abh=insert(ahead,bhead);print(abh);}struct student *creat(void){struct student *p1,*p2,*head;n=0;p1=p2=(struct student *)malloc(LEN);printf(“input number&scores of student:\n”);printf(“if number Is 0,stop inputing.\n”);scanf(“%ld,%d”,&p1->num,&p1->score);head=NULL;while(p1->num!=0){n=n+1;if(n==1)head=p1;else p2->next =p1;p2=p1;p1=(struct student *)malloc(LEN);scanf(“%ld,,%d”,&p1->num,&p1->score);}p2->next=NULL;return(head);}struct student *insert(struct student *ah,struct student *bh){struct student *pa1 , *pa2,*pb1,*pb2;pa2=pa1=ah;pb2=pb1=bh;do{while((pb1->num>pa1->num)&&(pa1->next!=NULL)){pa2=pa1;pa1=pa1->next;}if(pb->num<=pa1->num){if(ah=pa1)ah=pb1;else pa2->next=pb1;pb1=pb1->next;pb2->next=pa1;pa2=pb2;pb2=pb1;}}while((pa1->next!=NULL)||(pa1==NULL&&pb1!=NULL));if((pb1->num>pa1->num)&&(pa1->next==NULl))ap1->next=pb1;return(ah);}void print(struct student *head){struct student *p;printf(“%ld%d\n”,p->num,p->score);p=p->next;while(p!=NULL);}9.9 13个人围成一圈,从第1个人开始顺序报号1、2、3。

相关文档
最新文档