c程序设计语言第二版答案

合集下载

谭浩强--C语言程序设计(第二版)习题答案

谭浩强--C语言程序设计(第二版)习题答案

C语言程序设计(第2版)课后习题答案第一章1.请参照本章例题,编写一个C程序,输出以下信息:**************************Very good!**************************解:#include<stdio.h>void main(){printf("**************************");printf("\n");printf("Very good!\n");printf("\n");printf("**************************");}2.编写一个C程序,输入a、b、c三个值,输出其中最大值。

解:#include<stdio.h>void main(){int a,b,c,max;printf("请输入三个数a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if(max<b) max=b;if(max<c) max=c;printf("最大数为: %d",max);}第二章1.假如我国国民生产总值的年增长率为10%,计算10年后我国国民生产总值与现在相比增长多少百分比。

计算公式为P=(1+r)^n,r为年增长率;n为年数;P为与现在相比的百分比。

解:#include<stdio.h>#include<math.h>void main(){double P, r=0.1, n=10;P=pow((1+r), n);printf("%lf\n", P);}3.请编程序将"China"译成密码,译码规律是用原来字母后面的第4个字母代替原来的字母。

谭浩强 C语言程序设计(第二版)习题答案

谭浩强  C语言程序设计(第二版)习题答案
printf("\n 反序数字为: "); printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand); break; case 4: printf("%d,%d,%d,%d",thousand,hundred,ten,indiv); printf("\n 反序数字为: "); printf("%d%d%d%d\n",indiv,ten,hundred,thousand); break; case 3: printf("%d,%d,%d",hundred,ten,indiv);
利润大于 600000 元,小于 1000000 元(600000<I≤1000000)时,高于 600000
的部分按 1.5%提成;利润大于 1000000 元(I>1000000)时,超过 1000000 元的
部分按 1%提成。从键盘输入当年利润 I,求应发放奖金总数。要求:
(1)用 if 语句编程序;(2)用 switch 语句编程序。
#include<stdio.h> void main() { int a,b,c,temp,max; printf("请输入 3 个整数: "); scanf("%d,%d,%d",&a,&b,&c); temp=(a>b)?a:b; max=(temp>c)?temp:c; printf("3 个整数的最大数是%d\n",max); } 6. 给一个百分制成绩,要求输出等级'A'、'B'、'C'、'D'、'E'。90 分以上为'A',80~90 分为'B',70~79 分为'C',60 分以下为'D'。 解:

C语言程序设计(第二版)习题参考答案

C语言程序设计(第二版)习题参考答案

C语言程序‎设计习题参‎考答案习题 1一、判断题1.在计算机中‎,小数点和正‎负号都有专‎用部件来保‎存和表示。

2.二进制是由‎0和1两个‎数字组成的‎进制方式。

3.二进制数的‎逻辑运算是‎按位进行的‎,位与位之间‎没有进位和‎借位的关系‎。

4.在整数的二‎进制表示方‎法中,0的原码、反码都有两‎种形式。

5.有符号数有‎三种表示法‎:原码、反码和补码‎。

6.常用字符的‎A S CII‎码值从小到‎大的排列规‎律是:空格、阿拉伯数字‎、大写英文字‎母、小写英文字‎母。

解:1.F2.T 3.T 4.T 5.T 6.T二、单选题1.在计算机中‎,最适合进行‎数值加减运‎算的数值编‎码是。

A. 原码B. 反码C. 补码D. 移码2.已知英文小‎写字母m的‎A SCII‎码为十进制‎数109,则英文小写‎字母y的A‎S CII码‎为十进制数‎。

A. 112B. 120C. 121D. 1223.关于ASC‎II码,在计算机中‎的表示方法‎准确地描述‎是。

A. 使用8位二‎进制数,最右边一位‎为1B. 使用8位二‎进制数,最左边一位‎为1C. 使用8位二‎进制数,最右边一位‎为0D. 使用8位二‎进制数,最左边一位‎为04.设在机器字‎长4位,X=0111B‎,Y=1011B‎,则下列逻辑‎运算中,正确的是_‎_____‎_____‎。

A. X∧Y=1000B. X∨Y=1111C. X⊕Y=0011D. ¯Y=10005.下列叙述中‎正确的是()。

A.高级语言就‎是机器语言‎B.汇编语言程‎序、高级语言程‎序都是计算‎机程序,但只有机器‎语言程序才‎是计算机可‎以直接识别‎并执行的程‎序C.C语言因为‎具有汇编语‎言的一些特‎性,所以是汇编‎语言的一种‎D.C源程序经‎过编译、连接,若正确,执行后就能‎得到正确的‎运行结果6.用C语言编‎写的源程序‎经过编译后‎,若没有产生‎编译错误,则系统将()。

c 程序设计第二版课后习题答案

c  程序设计第二版课后习题答案

c 程序设计第二版课后习题答案C程序设计第二版课后习题答案C程序设计是计算机科学与技术专业中一门重要的课程,它涉及到了计算机程序设计的基本概念、语法和技巧。

而《C程序设计第二版》是一本经典的教材,它通过丰富的例子和习题,帮助学生巩固和扩展他们的编程知识。

在学习这门课程时,课后习题是非常重要的一部分。

通过解答习题,我们可以更好地理解和掌握所学的知识,并且提高我们的编程能力。

然而,有时候我们可能会遇到一些困难,无法找到正确的答案。

因此,本文将为大家提供一些C程序设计第二版课后习题的答案,希望能对大家的学习有所帮助。

1. 第一章习题第一章主要介绍了C语言的基本概念和语法。

在习题中,我们需要编写一些简单的程序来练习基本的输入输出操作。

以下是一些可能的答案:```c#include <stdio.h>int main() {int num;printf("请输入一个整数:");scanf("%d", &num);printf("您输入的整数是:%d\n", num);return 0;}```2. 第二章习题第二章主要介绍了C语言的数据类型和运算符。

在习题中,我们需要编写一些程序来练习不同数据类型的使用和运算符的操作。

以下是一些可能的答案:```c#include <stdio.h>int main() {int a = 10;float b = 3.14;char c = 'A';printf("a + b = %f\n", a + b);printf("a * b = %f\n", a * b);printf("a / b = %f\n", a / b);printf("c + 1 = %c\n", c + 1);return 0;}```3. 第三章习题第三章主要介绍了C语言的控制结构,包括条件语句和循环语句。

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

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

Chapter 1Exercise 1-1Run the “hello world” program on your system. Experiment with leaving out parts of the program, to see what error message you get.#include <stdio.h>int main(){printf("hello, ");printf("world");printf("\n");return 0;}Exercise 1-2Experiment to find out what happens when printf’s argument string contains \c, where c is some character not list above.Exercise 1-3Modify the temperature conversion program to print a heading above the table.#include<stdio.h>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;}Exercise 1-4Write a program to print the corresponding Celsius to Fahrenheit table.#include<stdio.h>int main(){float fahr, celsius;float lower, upper, step;lower = 0;upper = 300;step = 20;celsius = lower;while (celsius<= upper){fahr = 9.0*celsius/5.0+32;printf("%3.0f %6.1f\n", celsius,fahr);celsius = celsius + step;}return 0;}Exercise 1-5Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.#include<stdio.h>int main(){float fahr, celsius;float lower, upper, step;lower = 0;upper = 300;step = 20;fahr = upper;printf("Fahrenheit temperatures and their centigrade or Celsius equivalents\n");while (fahr >= lower){celsius = (5.0/9.0) * (fahr-32.0);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr - step;}return 0;}Exercise 1-6Verify that the expression getchar()!=EOF is 0 or 1.#include <stdio.h>main(){int c;c =(getchar() != EOF);printf("%d",c);return 0;}Exercise 1-7Write a program to print the value of EOF .#include <stdio.h>int main(){printf("%d",EOF);return 0;}Exercise 1-8Write a program to count blanks, tabs, and newlines. #include<stdio.h>int main(void){int blanks, tabs, newlines;int c;int done = 0;int lastchar = 0;blanks = 0;tabs = 0;newlines = 0;printf("输入0查看结果\n");while(done == 0){c = getchar();if(c == ' ')++blanks;if(c == '\t')++tabs;if(c == '\n')++newlines;if(c == '0'){if(lastchar != '\n'){++newlines;}done = 1;}lastchar = c;}printf("空格的个数为: %d\n制表符的个数为: %d\n换行符的个数为: %d\n", blanks, tabs, newlines);return 0;}Exercise 1-9Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.#include <stdio.h>int main(void){int c;int inspace;inspace = 0;while((c = getchar()) != EOF){if(c == ' '){if(inspace == 0){inspace = 1;putchar(c);}}if(c != ' '){inspace = 0;putchar(c);}}return 0;}Exercise 1-10Write a program to copy its input to its output, replacing each tab by \t , each backspace by \b , and each backslash by \\ . This makes tabs and backspaces visible in an unambiguous way.#include<stdio.h>int main(){int c;while((c=getchar())!=EOF){if (c=='\t')printf("\t");if (c=='\b')printf("\b");if (c=='\\')printf("\\\\");if (c!='\t')if (c!='\b')if (c!='\\')putchar(c);}return 0;}Exercise 1-11How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?Exercise 1-12Write a program that prints its input one word per line.#include <stdio.h>int main(void){int c;int asd;asd = 0;while((c = getchar()) != EOF){if(c == ' ' || c == '\t' || c == '\n'){if(asd == 0){asd = 1;putchar('\n');}}else{asd = 0;putchar(c);}}return 0;}Exercise 1-13Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging. #include <stdio.h>#define MAXWORDLEN 10int main(void){int c;int inspace = 0;long lengtharr[MAXWORDLEN + 1];int wordlen = 0;int firstletter = 1;long thisval = 0;long maxval = 0;int thisidx = 0;int done = 0;for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++){lengtharr[thisidx] = 0;}while(done == 0){c = getchar();if(c == ' ' || c == '\t' || c == '\n' || c == EOF){if(inspace == 0){firstletter = 0;inspace = 1;if(wordlen <= MAXWORDLEN){if(wordlen > 0){thisval = ++lengtharr[wordlen - 1];if(thisval > maxval){maxval = thisval;}}}else{thisval = ++lengtharr[MAXWORDLEN];if(thisval > maxval){maxval = thisval;}}}if(c == EOF){done = 1;}}else{if(inspace == 1 || firstletter == 1){wordlen = 0;firstletter = 0;inspace = 0;}++wordlen;}}for(thisval = maxval; thisval > 0; thisval--){printf("%4d | ", thisval);for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++) {if(lengtharr[thisidx] >= thisval){printf("* ");}else{printf(" ");}}printf("\n");}printf(" +");for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++){printf("---");}printf("\n ");for(thisidx = 0; thisidx < MAXWORDLEN; thisidx++){printf("%2d ", thisidx + 1);}printf(">%d\n", MAXWORDLEN);return 0;}Exercise 1-14Write a program to print a histogram of the frequencies of different characters in its input.#include <stdio.h>#define NUM_CHARS 256int main(void){int c;long freqarr[NUM_CHARS + 1];long thisval = 0;long maxval = 0;int thisidx = 0;for(thisidx = 0; thisidx <= NUM_CHARS; thisidx++){freqarr[thisidx] = 0;}while((c = getchar()) != EOF){if(c < NUM_CHARS){thisval = ++freqarr[c];if(thisval > maxval){maxval = thisval;}}else{thisval = ++freqarr[NUM_CHARS];if(thisval > maxval){maxval = thisval;}}}for(thisval = maxval; thisval > 0; thisval--){printf("%4d |", thisval);for(thisidx = 0; thisidx <= NUM_CHARS; thisidx++) {if(freqarr[thisidx] >= thisval){printf("*");}else if(freqarr[thisidx] > 0){printf(" ");}}printf("\n");}printf(" +");for(thisidx = 0; thisidx <= NUM_CHARS; thisidx++) {if(freqarr[thisidx] > 0){printf("-");}}printf("\n ");for(thisidx = 0; thisidx < NUM_CHARS; thisidx++) {if(freqarr[thisidx] > 0){printf("%d", thisidx / 100);}}printf("\n ");for(thisidx = 0; thisidx < NUM_CHARS; thisidx++){if(freqarr[thisidx] > 0){printf("%d", (thisidx - (100 * (thisidx / 100))) / 10 );}}printf("\n ");for(thisidx = 0; thisidx < NUM_CHARS; thisidx++){if(freqarr[thisidx] > 0){printf("%d", thisidx - (10 * (thisidx / 10)));}}if(freqarr[NUM_CHARS] > 0){printf(">%d\n", NUM_CHARS);}printf("\n");return 0;}Exercise 1-15Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.#include <stdio.h>float FtoC(float f){float c;c = (5.0 / 9.0) * (f - 32.0);return c;}int main(void){float fahr, celsius;int lower, upper, step;lower = 0;upper = 300;step = 20;printf("F C\n\n");fahr = lower;while(fahr <= upper){celsius = FtoC(fahr);printf("%3.0f %6.1f\n", fahr, celsius);fahr = fahr + step;}return 0;}Exercise 1-16Revise the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text.#include <stdio.h>#define MAXLINE 1000int getline(char line[ ], int maxline);void copy(char to[ ], char from[ ]);int main(void){int len;int max;char line[MAXLINE];char longest[MAXLINE];max = 0;while((len = getline(line, MAXLINE)) > 0){printf("%d: %s", len, line);if(len > max){max = len;copy(longest, line);}}if(max > 0){printf("Longest is %d characters:\n%s", max, longest);}printf("\n");return 0;}int getline(char s[], int lim){int c, i, j;for(i = 0, j = 0; (c = getchar())!=EOF && c != '\n'; ++i){if(i < lim - 1){s[j++] = c;}}if(c == '\n'){if(i <= lim - 1){s[j++] = c;}++i;}s[j] = '\0';return i;}void copy(char to[], char from[]){int i;i = 0;while((to[i] = from[i]) != '\0'){++i;}}Exercise 1-17Write a program to print all input lines that are longer than 80 characters. #include <stdio.h>#define MINLENGTH 81int readbuff(char *buffer) {size_t i=0;int c;while (i < MINLENGTH) {c = getchar();if (c == EOF) return -1;if (c == '\n') return 0;buffer[i++] = c;}return 1;}int copyline(char *buffer) {size_t i;int c;int status = 1;for(i=0; i<MINLENGTH; i++)putchar(buffer[i]);while(status == 1) {c = getchar();if (c == EOF)status = -1;else if (c == '\n')status = 0;elseputchar(c);}putchar('\n');return status;}int main(void) {char buffer[MINLENGTH];int status = 0;while (status != -1) {status = readbuff(buffer);if (status == 1)status = copyline(buffer);}return 0;}Exercise 1-18Write a program to remove all trailing blanks and tabs from each line of input, and to delete entirely blank lines.#include <stdio.h>#include <stdlib.h>#define MAXQUEUE 1001int advance(int pointer){if (pointer < MAXQUEUE - 1)return pointer + 1;elsereturn 0;}int main(void){char blank[MAXQUEUE];int head, tail;int nonspace;int retval;int c;retval = nonspace = head = tail = 0;while ((c = getchar()) != EOF) {if (c == '\n') {head = tail = 0;if (nonspace)putchar('\n');nonspace = 0;}else if (c == ' ' || c == '\t') {if (advance(head) == tail) {putchar(blank[tail]);tail = advance(tail);nonspace = 1;retval = EXIT_FAILURE;}blank[head] = c;head = advance(head);}else {while (head != tail) {putchar(blank[tail]);tail = advance(tail);}putchar(c);nonspace = 1;}}return retval;}Exercise 1-19Write a function reverse(s) that reverses the character string s . Use it to write a program that reverses its input a line at a time.#include <stdio.h>#define MAX_LINE 1024void discardnewline(char s[]){int i;for(i = 0; s[i] != '\0'; i++){if(s[i] == '\n')s[i] = '\0';}}int reverse(char s[]){char ch;int i, j;for(j = 0; s[j] != '\0'; j++){}--j;for(i = 0; i < j; i++){ch = s[i];s[i] = s[j];s[j] = ch;--j;}return 0;}int getline(char s[], int lim){int c, i;for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {s[i] = c;}if(c == '\n'){s[i++] = c;}s[i] = '\0';return i;}int main(void){char line[MAX_LINE];while(getline(line, sizeof line) > 0){discardnewline(line);reverse(line);printf("%s\n", line);}return 0;}Exercise 1-20Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX_BUFFER 1024#define SPACE ' '#define TAB '\t'int CalculateNumberOfSpaces(int Offset, int TabSize){return TabSize - (Offset % TabSize);}int getline(char s[], int lim){int c, i;for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)s[i] = c;if(c == '\n'){s[i] = c;++i;}s[i] = '\0';return i;}int main(void){char Buffer[MAX_BUFFER];int TabSize = 5;int i, j, k, l;while(getline(Buffer, MAX_BUFFER) > 0){for(i = 0, l = 0; Buffer[i] != '\0'; i++){if(Buffer[i] == TAB){j = CalculateNumberOfSpaces(l, TabSize);for(k = 0; k < j; k++){putchar(SPACE);l++;}}else{putchar(Buffer[i]);l++;}}}return 0;}Exercise 1-21Write a program entab that replaces strings of blanks with the minimum number of tabsand blanks to achieve the same spacing. Use the same stops as for detab . When either a tab or a single blank would suffice to reach a tab stop, which should be given preference?#include <stdio.h>#define MAXLINE 1000#define TAB2SPACE 4char line[MAXLINE];int getline(void);intmain(){int i,t;int spacecount,len;while (( len = getline()) > 0 ){spacecount = 0;for( i=0; i < len; i++){if(line[i] == ' ')spacecount++;if(line[i] != ' ')spacecount = 0;if(spacecount == TAB2SPACE){i -= 3;len -= 3;line[i] = '\t';for(t=i+1;t<len;t++)line[t]=line[t+3];spacecount = 0;line[len] = '\0';}}printf("%s", line);}return 0;}int getline(void){int c, i;extern char line[];for ( i=0;i<MAXLINE-1 && ( c=getchar()) != EOF && c != '\n'; ++i)line[i] = c;if(c == '\n'){line[i] = c;++i;}line[i] = '\0';return i;}Exercise 1-22Write a program to "fold" long input lines into two or more shorter lines after the last non-blank character that occurs before the n -th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.#include <stdio.h>#define MAXLINE 1000char line[MAXLINE];int getline(void);intmain(){int t,len;int location,spaceholder;const int FOLDLENGTH=70;while (( len = getline()) > 0 ){if( len < FOLDLENGTH ){}else{t = 0;location = 0;while(t<len){if(line[t] == ' ')spaceholder = t;if(location==FOLDLENGTH){line[spaceholder] = '\n';location = 0;}location++;t++;}}printf ( "%s", line);}return 0;}int getline(void){int c, i;extern char line[];for ( i=0;i<MAXLINE-1 && ( c=getchar()) != EOF && c != '\n'; ++i)line[i] = c;if(c == '\n'){line[i] = c;++i;}line[i] = '\0';return i;}Exercise 1-23Write a program to remove all comments from a C program. Don't forget to handle quoted strings and character constants properly. C comments do not nest.#include <stdio.h>#define MAXLINE 1000char line[MAXLINE];int getline(void);intmain(){int in_comment,len;int in_quote;int t;in_comment = in_quote = t = 0;while ((len = getline()) > 0 ){t=0;while(t < len){if( line[t] == '"')in_quote = 1;if( ! in_quote ){if( line[t] == '/' && line[t+1] == '*'){t=t+2;in_comment = 1;}if( line[t] == '*' && line[t+1] == '/'){t=t+2;in_comment = 0;}if(in_comment == 1){t++;}else{printf ("%c", line[t]);t++;}}else{printf ("%c", line[t]);t++;}}}return 0;}int getline(void){int c, i;extern char line[];for ( i=0;i<MAXLINE-1 && ( c=getchar()) != EOF && c != '\n'; ++i)line[i] = c;if(c == '\n'){line[i] = c;++i;}line[i] = '\0';return i;}Exercise 1-24Write a program to check a C program for rudimentary syntax errors like unbalanced parentheses, brackets and braces. Don't forget about quotes, both single and double, escape sequences, and comments. (This program is hard if you do it in full generality.) #include <stdio.h>#define MAXLINE 1000char line[MAXLINE];int getline(void);intmain(){int len=0;int t=0;int brace=0, bracket=0, parenthesis=0;int s_quote=1, d_quote=1;while ((len = getline()) > 0 ){t=0;while(t < len){if( line[t] == '['){brace++;}if( line[t] == ']'){brace--;}if( line[t] == '('){parenthesis++;}if( line[t] == ')'){parenthesis--;}if( line[t] == '\''){s_quote *= -1;}if( line[t] == '"'){d_quote *= -1;}t++;}}if(d_quote !=1)printf ("Mismatching double quote mark\n");if(s_quote !=1)printf ("Mismatching single quote mark\n");if(parenthesis != 0)printf ("Mismatching parenthesis\n");if(brace != 0)printf ("Mismatching brace mark\n");if(bracket != 0)printf ("Mismatching bracket mark\n");if( bracket==0 && brace==0 && parenthesis==0 && s_quote == 1 && d_quote == 1)printf ("Syntax appears to be correct.\n");return 0;}int getline(void){int c, i;extern char line[];for ( i=0;i<MAXLINE-1 && ( c=getchar()) != EOF && c != '\n'; ++i) line[i] = c;if(c == '\n'){line[i] = c;++i;}line[i] = '\0';return i;}。

c程序设计第二版完整答案

c程序设计第二版完整答案
而const定义则是象定义一个变量一样定义一个常量标识符系统要按照类型要求为该标识符分配内存单元同时在将常量放入单元时进行类型检查如果类型不匹配类型相容的会进行系统的类型转换不相容的则要提示错误
第一章C++基础知识习题
1.1判断下列标识符的合法性。
sin book5arry _name Example2.1 main
int array[5]={1,2,3,4,5}; char s[]="Hello";
答:"China"文字常量
constint n=10;常变量
int m=5;变量
'a'文字常量
char ch='a'变量
int array[5]={1,2,3,4,5};变量
char s[]="Hello";变量
1.4将下列算式或叙述用C++表达式描述。
}
result=1;
for(i=2;i<=n;i++){
jch*=i; //jch是i的阶乘,注意n!=n*(n-1)!这样可少一层循环
result+=jch;
}
cout<<result<<endl;
return 0;
}
2.8
#include <iostream>
using namespace std;
}
m-=3;
}
解答:注意continue语句的使用
初值
m=0
k=1
第1次循环后
m=5
k=2
第2次循环后
m=2
k=3
第3次循环后

C语言程序设计(第二版)答案

C语言程序设计(第二版)答案

参考答案习题二(P33)一.单选题1.C2.B3.D4.C5.A6.D7.D8.B9.B 10.D 11.D 12.C 13.C . 15. A 14题最后一句应为printf("%f\n",d*y);结果为2.2二.填空题1. 182. int float double3. 10 114. 八十六十5. %三.阅读程序题1.10,10,9,102.j=1,i=2k=3,i=3j=3,i=2k=1,i=1习题三(P52)一.单选题1.D2.C3.D4.B5.A6.B7.C8.A9.C 10.B 11. -1,37777777777,4294967295 二.填空题5. L6. -1三.阅读程序题1. 6 6 6.00 6.002. x=127,x= 127,x=177,x=7fY=123.4567 , y= 123.46 , y=123.456703. 2,14. 12345. 4,36. -6,-6四.程序设计题1.#include "stdio.h"#include "math.h"main(){float a,b,c,d,x1,x2;a=2;b=-3;c=-5;d=b*b-4*a*c;x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);printf("x1=%.2f,x2=%.2f\n",x1,x2);}2.#include <stdio.h>main(){ float a,v,s;scanf("%f",&a);v=a*a*a;s=6*a*a;printf("v=%.2f,s=%.2f\n",v,s);}3.#include <stdio.h>main(){ int a,b,c,t;scanf("%d%d%d",&a,&b,&c);printf("a=%d,b=%d,c=%d\n",a,b,c);t=c;c=b;b=a;a=t;printf("a=%d,b=%d,c=%d\n",a,b,c);}4.#include <stdio.h>main(){ char s1,s2;s1=getchar();s2=s1-32;printf("%c\n",s2);}习题四(P70)一.单选题1.C2.D3.D4.B5.A6.D7.D8.B9.C 10.A 11.B 12.D 13.passwarnerror .14.C 15. C 16.B 17. B 18. C二.填空题1. 非0 02. k==03. n%7==0 && n%8==0 else三.阅读程序题1. a=1,b=02. c=1四.程序设计题1.#include <stdio.h>main(){ int a,b;char c;printf("INPUT A+(-*/)B\n");scanf("%d%c%d",&a,&c,&b);switch(c){case'+':printf("%d+%d=%d\n",a,b,a+b);break;case'-':printf("%d-%d=%d\n",a,b,a-b);break;case'*':printf("%d*%d=%d\n",a,b,a*b);break;case'/':printf("%d/%d=%d\n",a,b,a/b);break;default:printf("INPUT ERROR!");}}2.#include <stdio.h>main(){ float x,y;scanf("%f",&x);if(x==0||x==2)y=0;else if(x>0) y=(x+1)/(x-2);else y=(x-1)/(x-2);printf("y=%f\n",y);}3.#include <stdio.h>main(){ int g;printf("请输入学生成绩:");scanf("%d",&g);printf("g=%d:",g);switch(g/10){case 10:printf("A\n");break;case 9:printf("B\n");break;case 8:printf("C\n");break;case 7:printf("D\n");break;case 6:printf("E\n");break;default:printf("F\n");}}4.#include <stdio.h>main(){ int x,n=0;scanf("%d",&x);if(x<=0||x>=10000)printf("Data Error!\n");else{if(x<10)n=1;else if(x<100)n=2;else if(x<1000)n=3;else n=4;}printf("n=%d\n",n);}习题五(P87)一.单选题1.B2.D3.C4.C5.B6.C7.D8.C9.A 10.D 11.B 12.B 13.B 14.D 15. C 二.填空题1. for语句while语句do-while语句2. a=14,y=263. k=14,n= -14. 395. s=196. 222227. 108. 79. 810. 5,5三.改错题应改为for(i=0;i<5;i++)j++;应改为int j=0;while(j<10){j++;i=j;} 注while(j<10)后不应有分号应改为while(j<10); 注while(j<10)后分号不能少s*=i;i++;应改为{s*=i;i++;}continue 应改为break四.程序设计题1.#include <stdio.h>main(){ int i,j,s=0;for(i=1,j=1;i<=100;i++,j=j*(-1))s=s+i*j;printf("s=%d\n",s);}1.(另一做法)#include<stdio.h>main(){int i,s;for(i=1,s=0;i<=100;i++)if(i%2==0)s=s-i;else s=s+i;printf ("%d\n",s);}2.#include <stdio.h>main(){ int i,j,s1=0,s2=0;for(i=0;i<10;i++){scanf("%d",&j);if(j>0)s1=s1+j;else s2=s2+j;}printf("s1=%d,s2=%d\n",s1,s2);}3.#include<stdio.h>main(){int i,s;for(i=6,s=0;i<=96;i=i+1)if(i%10==6||i/10==6)s=s+i;printf ("%d\n",s);}4.#include<stdio.h>main(){int i,a,b,c;for(i=100;i<=999;i++){a=(i%10);b=(i/10%10);c=(i/100);if(i==a*a*a+b*b*b+c*c*c)printf ("%d\n",i);}}5.#include <stdio.h>main( ){int i,j,k=0;for (i=1;i<=4;i++){for (j=1;j<=i;j++){k++;printf("%d",k%10);}printf("\n");}}6.#include <stdio.h>main( ){int i,j,k=0;for (i=-3;i<=3;i++){if(i<0)k=-i;else k=i;for(j=1;j<=k;j++)printf(" ");for(j=1;j<=7-2*k;j++)printf("*");printf("\n");}}习题六(P111)一.单选题1.D2.B3.C4.C5.C6.D7.A8.B二.填空题1. 20 0 192. 数组名3. 越界4. 65. j==k a[j][k]=1; a[j][k]=0;三.阅读程序题1. 6 5 43 2 12.aaabbbccc ddd3.2,2,1四.程序设计题1.#include<stdio.h>main(){ int a[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; int i,j,s=0;for(i=0;i<4;i++)for(j=0;j<4;j++)if(i==j||i+j==3)s+=a[i][j];printf("%d",s);}2.#include <stdio.h>{ char a[80];int i,j=5; /*假设删除位置为5*/gets(a);for(i=j-1;a[i]!='\0';i++)a[i]=a[i+1];a[i]='\0';puts(a);}3.#include <stdio.h>#include <string.h>main(){ char a[80];int i,j=5; /*假设插入位置为5*/char s='t'; /*假设插入字符为t*/gets(a);for(i=strlen(a);i>j;i--)a[i+1]=a[i];a[j]='t';puts(a);}4.#include<stdio.h>main(){ int a[3][5]={1,3,5,7,9,2,4,6,8,10,3,5,8,7,6}; int i,j,s1[3]={0},s2[5]={0};for(i=0;i<3;i++)for(j=0;j<5;j++)s1[i]+=a[i][ j];for(i=0;i<5;i++)for(j=0;j<3;j++)s2[i]+=a[ j][i];for(i=0;i<3;i++){for(j=0;j<5;j++)printf("%6d",a[i][j]);printf (" |%6d\n",s1[i]);}for(i=0;i<33;i++) printf("-");printf("\n");for(i=0;i<5;i++) printf("%6d",s2[i]);printf("\n");}5.#include<stdio.h>{ char s[3][80];int a=0,b=0,c=0,d=0,e=0,i,j;for(i=0;i<3;i++) gets(s[i]);for(i=0;i<3;i++)for(j=0;s[i][j]!='\0';j++)if(s[i][j]>='A'&&s[i][j]<='Z')a++;else if(s[i][j]>='a'&&s[i][j]<='z')b++;else if(s[i][j]>='0'&&s[i][j]<='9')c++;else if(s[i][j]==32)d++;else e++;printf("%3d%3d%3d%3d%3d",a,b,c,d,e);}习题七(P145)一.单选题1.A2.B3.C4.A5.C6.D7.C8.B9.B 10.C 11.D 二.填空题1. 该函数内局部2. 整型3. k<=breturn y;4. x[i]return (ave);fun(a,20)5. 1;add(n-1);add(n);6. n*f(n-1)0;f(i)三.阅读程序,写出运行结果1.10,20,302.643.84. hlo5. sum=55四.程序设计题1.#include <stdio.h>main(){float add(float, float), sub(float, float);float aver(float, float),a,b;scanf("%f,%f",&a,&b);printf("add=%f, sub=%f\n ", add(a,b), sub(a,b)); printf("aver =%f", aver(a,b));}float add(float x, float y) {return(x+y);}float sub(float x, float y) {return(x-y);}float aver(float x, float y) {return((x+y)/2);} 2.#include<stdio.h>main(){ int a,b,c,abmax(int,int);scanf("%d%d%d",&a,&b,&c);printf("max=%d\n",abmax(abmax(a,b),c)); }int abmax(int a,int b){if(a>b)return a;else return b;}3.#include <stdio.h>main(){float x,fun(float, int);int n;scanf("%f%d",&x,&n);printf("%f\n", fun(x,n));}float fun(float x, int n){float y;if(n==0)y=1;else y=x*fun(x,n-1);return y;}4.#include <stdio.h>main(){long f(int);int n,k;scanf("%d",&n);for(k=1;k<=n;k++)printf("%ld,",f(k));printf("\n");}long f(int n){long y;if(n<=2)y=1;else y=f(n-1)+f(n-2); return y;}5.#include <stdio.h>#include <string.h> main(){char a[50];int n;void fun(char x[ ],int n); gets(a);n=strlen(a); fun(a,n);puts(a);}void fun(char a[ ],int n) {int k,s;for(k=0;k<n/2;k++) {s=a[k];a[k]=a[n-k-1];a[n-k-1]=s;}}5. (另一做法)#include <stdio.h>#include <string.h> main(){char a[50];int n;void fun(char x[ ],int n); gets(a);n=strlen(a); fun(a,n);puts(a);}void fun(char a[ ],int n) {int k;char b[50];for(k=0;k<n;k++)b[n-1-k]=a[k];b[k]=a[k];strcpy(a,b);}6.#include <stdio.h>#define KK 100main(){char a[KK];long sjz(char a[]);int i=0,f1=0;printf("input a data:");gets(a);for(i=0;a[i]!='\0';i++){if(a[i]>='0'&&a[i]<='9'||a[i]>='A'&&a[i]<='F'||a[i]>='a'&&a[i]<='f') continue;else {f1=1;break;}}a[i]='\0';if(f1==1)printf("Data Error!\n");else printf("result is :%d\n",sjz(a));}long sjz(char a[]){long n=0,i;;for(i=0;a[i]!='\0';i++){if(a[i]>='0'&&a[i]<='9')n=n*16+a[i]-'0';if(a[i]>='A'&&a[i]<='F')n=n*16+a[i]-'A'+10;if(a[i]>='a'&&a[i]<='f')n=n*16+a[i]-'a'+10;}return n;}6.(另一做法)#include <stdio.h>#define KK 100main(){char str[KK],c;long sjz(char a[]);int i=0,f1=0;printf("input a data:");while((c=getchar())!='\n'&&i<KK){if(c>='0'&&c<='9'||c>='A'&&c<='F'||c>='a'&&c<='f')str[i++]=c;else f1=1;}str[i]='\0';if(f1==1)printf("Data Error!\n");else printf("result is :%d\n",sjz(str));}long sjz(char a[]){long n=0,i;;for(i=0;a[i]!='\0';i++){if(a[i]>='0'&&a[i]<='9')n=n*16+a[i]-'0';if(a[i]>='A'&&a[i]<='F')n=n*16+a[i]-'A'+10;if(a[i]>='a'&&a[i]<='f')n=n*16+a[i]-'a'+10;}return n;}习题八(P181) (注:无答案的题不在本课程讲授范围内)一.单选题1.D2.A3.C4.D5.C6.B7.C8.C9.C 13.D 14.B 15.A 16.C 17.A 二.填空题1. 指向取地址2. 2 +23. 286. *(p+5)7. ABCD A三.阅读程序题1.102.103.04. 3,65. 1 2 3 4四.程序填空题1.a,b,c或者&x,&y,&z max=*b或者max=y max=*c或者max=z2. ++ =*q ++ ++4. int *a,int *b b[j]=a[i] b[i++]5. *str+=3 *str>’z’&&*str<’a’||*str>’z’ a6. else 0 t[2*j+1]五.程序设计题1.#include <stdio.h>main(){int a[10],*p,*q,t;p=a;for(q=a;q<a+10;q++)scanf("%d",q);for(q=a;q<a+10;q++)printf("%5d",*q);printf("\n");q--;for(;p<q;p++,q--){t=*p;*p=*q;*q=t;}for(p=a;p<a+10;p++)printf("%5d",*p);printf("\n");}2.#include <stdio.h>#include <string.h>void main( ){ char a[50],*p1,*p2,t,n;gets(a);n=strlen(a);p1=a; p2=a+n-1;for(; p1<p2; p1++, p2--){t=*p1; *p1=*p2; *p2=t;}puts(a);}另一做法#include <stdio.h>main(){char str[20],*p=str;gets(str);while(*p)p++;p--;while(p>=str){printf("%c",*p);p--;}printf("\n");}3.#include <stdio.h>main(){int a[10],*p,*max,*min,t;for(p=a;p<a+10;p++)scanf("%d",p); for(p=a;p<a+10;p++)printf("%5d",*p); printf("\n");max=min=a;for(p=a+1;p<a+10;p++){if(*p>*max)max=p;if(*p<*min)min=p;}t=a[0];a[0]=*min;*min=t;t=a[9];a[9]=*max;*max=t;for(p=a;p<a+10;p++)printf("%5d",*p); printf("\n");}5.#include <stdio.h>int length(char *s){int n=0;while(*s){n++;s++;}return n;}main(){char str[20];int n;gets(str);n=length(str);printf("The string length is %d\n",n);}6.#include <stdio.h>main(){char str[81],*p=str,*q,t;gets(str);printf("The origenal string:\n");puts(str);for(p=str;*(p+1);p++)for(q=p+1;*q;q++)if(*q<*p){t=*p;*p=*q;*q=t;}printf("The result string:\n");puts(str);}习题九(P222) (注:无答案的题不在本课程讲授范围内)一.单选题1.D2.A3.B4.D 8.B 9.B 10.C二.填空题1. 结构体成员结构体指针指向2. 343. 224.ex三.阅读程序题1.92.10,x3.134. 46 40 415. 06. 3839。

谭浩强--C语言程序设计(第二版)习题答案

谭浩强--C语言程序设计(第二版)习题答案

C语言程序设计(第2版)课后习题答案第一章1.请参照本章例题,编写一个C程序,输出以下信息:**************************Very good!**************************解:#include<stdio.h>void main(){printf("**************************");printf("\n");printf("Very good!\n");printf("\n");printf("**************************");}2.编写一个C程序,输入a、b、c三个值,输出其中最大值。

解:#include<stdio.h>void main(){int a,b,c,max;printf("请输入三个数a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if(max<b) max=b;if(max<c) max=c;printf("最大数为: %d",max);}第二章1.假如我国国民生产总值的年增长率为10%,计算10年后我国国民生产总值与现在相比增长多少百分比。

计算公式为P=(1+r)^n,r为年增长率;n为年数;P 为与现在相比的百分比。

解:#include<stdio.h>#include<math.h>void main(){double P, r=0.1, n=10;P=pow((1+r), n);printf("%lf\n", P);}3.请编程序将"China"译成密码,译码规律是用原来字母后面的第4个字母代替原来的字母。

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

c程序设计语言第二版答案【篇一:c语言程序设计现代方法(第二版)习题答案】answers to selected exercises2. [was #2] (a) the program contains one directive (#include) and four statements (three calls of printf and one return).(b)parkinsons law:work expands so as to fill the timeavailable for its completion.3. [was #4]#include stdio.hint main(void){int height = 8, length = 12, width = 10, volume;volume = height * length * width;printf(dimensions: %dx%dx%d\n, length, width, height);printf(volume (cubic inches): %d\n, volume);printf(dimensional weight (pounds): %d\n, (volume + 165) / 166);return 0;}4. [was #6] heres one possible program:#include stdio.hint main(void){int i, j, k;float x, y, z;printf(value of i: %d\n, i);printf(value of j: %d\n, j);printf(value of k: %d\n, k);printf(value of x: %g\n, x);printf(value of y: %g\n, y);printf(value of z: %g\n, z);return 0;}when compiled using gcc and then executed, this program produced the following output:value of i: 5618848value of j: 0value of k: 6844404value of x: 3.98979e-34value of y: 9.59105e-39value of z: 9.59105e-39the values printed depend on many factors, so the chance that youll get exactly these numbers is small.5. [was #10] (a) is not legal because 100_bottles begins with a digit.8. [was #12] there are 14 tokens: a, =, (, 3, *, q, -, p, *, p, ), /, 3, and ;.answers to selected programming projects4. [was #8; modified]#include stdio.hint main(void){float original_amount, amount_with_tax;printf(enter an amount: );scanf(%f, original_amount);amount_with_tax = original_amount * 1.05f;printf(with tax added: $%.2f\n, amount_with_tax);return 0;}the amount_with_tax variable is unnecessary. if we remove it, the program is slightly shorter:#include stdio.hint main(void){float original_amount;printf(enter an amount: );scanf(%f, original_amount);printf(with tax added: $%.2f\n, original_amount * 1.05f);return 0;}chapter 3answers to selected exercises2. [was #2](a) printf(%-8.1e, x);(b) printf(%10.6e, x);(c) printf(%-8.3f, x);(d) printf(%6.0f, x);5. [was #8] the values of x, i, and y will be 12.3, 45, and .6, respectively. answers to selected programming projects1. [was #4; modified]#include stdio.hint main(void){int month, day, year;printf(enter a date (mm/dd/yyyy): );scanf(%d/%d/%d, month, day, year);printf(you entered the date %d%.2d%.2d\n, year, month, day); return 0;}3. [was #6; modified]#include stdio.hint main(void){int prefix, group, publisher, item, check_digit;printf(enter isbn: );scanf(%d-%d-%d-%d-%d, prefix, group, publisher, item,check_digit);printf(gs1 prefix: %d\n, prefix);printf(group identifier: %d\n, group);printf(publisher code: %d\n, publisher);printf(item number: %d\n, item);printf(check digit: %d\n, check_digit);/* the five printf calls can be combined as follows:printf(gs1 prefix: %d\ngroup identifier: %d\npublishercode: %d\nitem number: %d\ncheck digit: %d\n,prefix, group, publisher, item, check_digit);*/return 0;}chapter 4answers to selected exercises2. [was #2] not in c89. suppose that i is 9 and j is 7. the value of (-i)/j could be either –1 or –2, depending on the implementation. on the other hand, the value of -(i/j) is always –1, regardless of the implementation. in c99, on the other hand, the value of (-i)/j must be equal to the value of -(i/j).9. [was #6](a) 63 8(b) 3 2 1(c) 2 -1 3(d) 0 0 013. [was #8] the expression ++i is equivalent to (i += 1). the value of both expressions is i after the increment has been performed. answers to selected programming projects2. [was#4]#include stdio.hint main(void){int n;printf(enter a three-digit number: );scanf(%d, n);printf(the reversal is: %d%d%d\n, n % 10, (n / 10) % 10, n / 100); return 0;}chapter 5answers to selected exercises2. [was #2](a) 1(b) 1(c) 1(d) 14. [was #4] (i j) - (i j)6. [was #12] yes, the statement is legal. when n is equal to 5, it does nothing, since 5 is not equal to –9.10. [was #16] the output isonetwosince there are no break statements after the cases.answers to selected programming projects2. [was #6]【篇二:c语言与程序设计-第2章课后习题参考答案】txt>关键字(是)注释空白符八进制常量(是)三字符序列字符串常量(是)括号(是)2.2 c编译器可将下列每一个源字符串分解为哪些记号?(不必考虑记号组合是否合法)(1) x+++y x, ++, +, y(2) -0xabl -, 0xabl(3) 2.89e+12l 2.89e+12l(4) string+\foo\ string+ \foo\(5) x**2 x, *, *, 2(6) x??/ x??/(7) a?ba, ?, b(8) x--+=y x, --, +=, y(9) intx=+10 intx, =, +, 10(10) stringfoo string, foo(这道题当时改的时候有几个小题改得有错误,注意!)2.3 下列哪些不是标识符,为什么?标识符由字母、数字和下划线组成,但首字符必须是字母或下划线。

4th不是,以数字开头;sizeof 不是(标准c的关键字)_limit 是_is2是xyshould 是x*y不是,* 非法o_no_o_no 是temp-2 不是,- 非法isnt不是,非法enum 不是(标准c的关键字。

相关文档
最新文档