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语言程序设计(第二版)习题答案

利润大于 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语言程序设计习题参考答案习题 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的AS CII码为十进制数。
A. 112B. 120C. 121D. 1223.关于ASCII码,在计算机中的表示方法准确地描述是。
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程序设计第二版课后习题的答案,希望能对大家的学习有所帮助。
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程序设计语言 (第二版) 课后答案第一章

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++基础知识习题
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语言程序设计(第二版)答案

参考答案习题二(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语言程序设计(第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语言程序设计(第二版)习题参考答案

printf("%d*%d=%d\n",a,a,z);
}
*3.仿照例 1.3 编程,输入两个数后,输出其中较小值。
解:#include<stdio.h>
float min(float x, float y)
{ float m;
if (x<y) m=x;
else m=y;
return m;
}
void main()
6.常用字符的 ASCII 码值从小到大的排列规律是:空格、阿拉伯数字、大写英文字母、
小写英文字母。
解:1.F 2.T 3.T 4.T 5.T
6.T
二、单选题
1.在计算机中,最适合进行数值加减运算的数值编码是
。
A. 原码
B. 反码
C. 补码
D. 移码
2.已知英文小写字母 m 的 ASCII 码为十进制数 109,则英文小写字母 y 的 ASCII 码为
面积 s(s x)(s y)(s z)
其中 s
x yz 2
程序如下: #include <stdio.h>
#include <math.h>
void main()
{
double x,y,z,s,dime;
scanf("%lf%lf%lf",&x,&y,&z);
dime=sqrt(s*(s-x)*(s-y)*(s-z));
程序如下:
#include <stdio.h>
void main()
{
int a,b,c,s,z;
printf("Please input a b c:\n");
C语言程序设计(第二版)习题参考答案

C语言程序设计习题参考答案习题 1一、判断题1.在计算机中,小数点和正负号都有专用部件来保存和表示。
2.二进制是由0和1两个数字组成的进制方式。
3.二进制数的逻辑运算是按位进行的,位与位之间没有进位和借位的关系。
4.在整数的二进制表示方法中,0的原码、反码都有两种形式。
5.有符号数有三种表示法:原码、反码和补码。
6.常用字符的ASCII码值从小到大的排列规律是:空格、阿拉伯数字、大写英文字母、小写英文字母。
解:1.F2.T 3.T 4.T 5.T 6.T二、单选题1.在计算机中,最适合进行数值加减运算的数值编码是。
A. 原码B. 反码C. 补码D. 移码2.已知英文小写字母m的ASCII码为十进制数109,则英文小写字母y的ASCII码为十进制数。
A. 112B. 120C. 121D. 1223.关于ASCII码,在计算机中的表示方法准确地描述是。
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语言编写的源程序经过编译后,若没有产生编译错误,则系统将()。
A.生成可执行文件B.生成目标文件C.输出运行结果D.自动保存源文件7.下列叙述中不正确的是()。
A.main函数在C程序中必须有且只有一个B. C程序的执行从main函数开始,所以main函数必须放在程序最前面C. 函数可以带参数,也可以不带参数。
c语言程序设计教程第二版课后习题答案

c语言程序设计教程第二版课后习题答案【篇一:c语言程序设计教程_李含光_郑关胜_清华大学出版社习题答案习题答案[完美打印版]】1.单项选择题(1)a (2)c(3)d (4)c (5)b 2.填空题(1)函数(2)主函数(main)(3)printf() , scanf()第2章习题参考答案1.单项选择题1-5 cbccc 6-10 cdcdc 11-13 dbb 2.填空题(1)1(2)26 (3)6 , 4 , 2 (4)10 , 6(5)3.000000 (6)双精度(double)(7)9 (8)字母,数字,下划线(9)13.700000 (10)11(11)((m/10)%10)*100+(m/100)*10+m%10(12)0 (13)10 ,9 ,11(15)(x0y0)||(x0z0)||(y0||z0)(16)double (17)x==0(18)sqrt(fabs(a-b))/(3*(a+b))(19)sqrt((x*x+y*y)/(a+b))第3章习题参考答案1.单项选择题1-5 cccdd 6-10 bcdbc11-15 bcbbb16 a 2.填空题(1)用;表示结束(2){ }(3)y=x0?1:x==0?0:-1(4)y%4==0y%100!=0||y%400==0(5)上面未配对(6)default 标号(7)while , do while , for(8)do while(9)本次(10)本层 3.阅读程序,指出结果(1)yes(2)*(3)abother(4)28 70(5)2,0(6)8(7)36 (8)1(9)3,1,-1,3,1,-1(10)a=12 ,y=12(11)i=6,k=4 (12)1,-2 4.程序填空(1)x:y , u:z(2)m=n , m!=0,m=m/10(3)teps , t*n/(2*n+1) , printf(“%lf\n”,2*s) (4)m%5==0 ,printf(“%d\n”,k) (5)cx=getchar() , cx!=front , cx(6)double s=0, 1.0/k , %lf (7)s=0 , sgmin, 5.编程题(1). #include stdio.h int main() {double x,y; scanf(%lf,x); if(x1) y=x;else if(x=1.0x10) y=2*x-11; elsey=3*x-11;printf(%lf\n,y); return 0; } (2).#include stdio.h int main() {double x,y,z,min;scanf(%lf%lf%lf,x,y,z); if(xy) min=y; else min=x; if(minz)min=z;printf(min=%lf\n,min); return 0; } (3).#include stdio.h int main() {int y,m,d,flag,s=0,w,i;scanf(%d%d%d,y,m,d);flag=(y%4==0y%100!=0||y%400==0);w=((y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400)%7;for(i=1;i=m;i++) {switch(i) {case 1:s=d;break; case 2:s=31+d;break; case 3:s=59+d;break; case 4:s=90+d;break; case 5:s=120+d;break; case6:s=151+d;break; case 7:s=181+d;break; case8:s=212+d;break; case 9:s=243+d;break; case10:s=273+d;break; case 11:s=304+d;break; case12:s=334+d;break;} }s=(w+s)%7; if(s==0)printf(星期日\n); elseprintf(星期%d\n,s); return 0; }(4).#include stdio.h int main() {float p,r;scanf(%f,p); if(p=10) r=p*0.1;else if(p10p=20) r=10*0.1+(p-10)*0.075; else if(p20p=40)r=10*0.1+10*0.075+(p-20)*0.05; else if(p40p=60)r=10*0.1+10*0.075+20*0.05+(p-40)*0.03;else if(p60p=100)r=10*0.1+10*0.075+20*0.05+20*0.03+(p-60)*0.015; else if(p100)r=10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(p-100)*0.01; printf(%f\n,r); return 0; } (5).#include stdio.h int main() {char c;while((c=getchar())!=\n) {if(c=ac=z) c=c-32; putchar(c);}return 0; } (6).#includestdio.h int main() {int m,k=2;printf(输入一个正整数:\n); scanf(%d,m); while(km) if(m%k==0) {printf(%4d,k); m=m/k; } else k++;printf(%4d\n,m); return 0; } (7).#includestdio.h int main() {int a,n,s=0,p=0,i;scanf(%d %d,n,a); for(i=1;i=n;i++) {p=p*10+a; s=s+p; }printf(%d\n,s); return 0; } (8).#includestdio.h int main(){int i,j,k;for(i=1;i=9;i++) for(j=0;j=9;j++) for(k=0;k=9;k++)printf(%5d,100*i+10*j+k); return 0; }(9).#includestdio.h #includemath.h int main() {float a=-10,b=10,x,f1,f2,f; f1=(((2*a-4)*a+3)*a)-6; f2=(((2*b-4)*b+3)*b)-6; do {x=(a+b)/2;f=(((2*x-4)*x+3)*x)-6; if(f*f10) { b=x; f2=f; } else { a=x;f1=f; }}while(fabs(f)=1e-6); printf(%6.2f\n,x); return 0; }(10).#includestdio.h#includemath.h int main() {int n=2;double eps,t,s=0,x;scanf(%lf %lf,x,eps); t=x; s=t;while(fabs(t)=eps) {t=-t*(2*n-3)*x*x/(2*n-2); s=s+t/(2*n); n++; }printf(%d,%lf\n,n,s); return 0; }(11).#includestdio.h int main() {unsigned long s,t=0,p=1; scanf(%u,s); while(s!=0) {if((s%10)%2!=0) {t=t+(s%10)*p; p=p*10; }s=s/10; }printf(%u\n,t); return 0; }第4章习题参考答案1.单项选择题1-5 dddbd 6-10 badcd 11-14 bdab 2.填空题(1)2(2)嵌套,递归(3)全局变量,局部变量,静态变量,动态变量(4)auto , static , register , extern (5)外部变量(6)编译,运行 3.阅读程序,指出结果(1)15(2)5(3)5,4,3 (4)i=5 i=2 i=2 i=4 i=2(5)求水仙花数(6)-5*5*5(7)30 (8)0 10 1 11 2 124.程序填空(1)float fun(float , float) , x+y,x-y, z+y,z-y (2)x , x*x+1 (3)s=0 , a=a+b 5.编程题(1).while(s!=0) #includestdio.h { unsigned int fun(unsigned int);p=p+s%10; int main() s=s/10; { } unsigned int s; return p; scanf(%u,s); } printf(%u\n,fun(s)); (2). return 0;#includestdio.h } #includestdlib.h unsigned int fun(unsignedint s) #includemath.h { void f1(float,float,float,float); unsigned int p=0; void f2(float,float,float,float);【篇二:《c语言程序设计》课后习题答案(第四版)谭浩强】t>1.1什么是计算机程序11.2什么是计算机语言11.3c语言的发展及其特点31.4最简单的c语言程序51.4.1最简单的c语言程序举例61.4.2c语言程序的结构101.5运行c程序的步骤与方法121.6程序设计的任务141-5 #include stdio.hint main ( ){ printf (**************************\n\n);printf( very good!\n\n);printf (**************************\n);return 0;}1-6#include stdio.hint main(){int a,b,c,max;printf(please input a,b,c:\n);scanf(%d,%d,%d,a,b,c);max=a;if (maxb)max=b;if (maxc)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程序设计——顺序程序设计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.hint 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.hint 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语言程序设计教程(第二版)电子工业出版社,黄皮书课后答案

习题11-1 填空题1.函数2.主函数main(),主函数main()3.主函数main()4.函数首部,函数体5.{, }6./*, */7.顺序结构,选择结构,循环结构8..c, .obj, .exe1-2 思考题1.结构化程序设计是指:为使程序具有一个合理的结构以保证程序正确性而规定的一套如何进行程序设计的原则。
顺序结构,选择结构,循环结构2.算法是对具体问题求解步骤的一种描述。
计算机算法的表达工具通常采用以下几种方法:(1)用自然语言表示算(2)用流程图表示算法(3)用伪代码表示算法(4)用程序设计语言表示算法3.语言简洁、紧凑,使用方便、灵活; 支持结构化程序设计;运算符丰富;数据类型丰富;较强的编译预处理功能;C语言的可移植性好;C语言本身既有一般高级语言的优点,又有低级(汇编)语言的特点;语法限制不太严格,程序设计自由度大。
4. 略5. 略1-3 编程题1.main(){ float a=3, b=4, c=5, s, area;s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c));p rintf(“area=%f ” , area );}2.main(){ printf(“******************************”);printf(“* hello world *”);printf(“******************************”);}习题22-1 单选题1~5 DBDCA 6~10 DCABA 11~14 BCA A2-2 思考题1.2.000002.1,0.53.9,24.65.100,d6.(1)20 (2)0 (3)607. (1)10,6,4 (2)6,9,15 (3)3,60,838. 559.70习题33-1单选题1-5BBDAB 6-10DDBDC 11-15AADCA 16-20CBACC 21-25ABDBB 3-2填空题1. 32.02613.0x104.05. 2, 1 互换a,b的值6. 6.67.–038.79. 5.0,4,c=3<Enter>10.i=10,j=20<Enter>11.(1) 65(2) 65,A(3) 56.123400,123.456001(4) 3.141600(5) 8765.432(6) 5.86400e+02(7) 3.14160e+00(8) 3.1416(9) 8765(10) 3.1416,8765.4312.a=2 b=5x=8.8 y=76.34c1=65 c2=97 3-3 编程题1.main(){ int x, y ;scanf(“%d%d” , &x, &y);printf(“x y\n”);printf(“%4d%4d\n”, x, y );printf(“%4o%4o\n”, x, y );printf(“%4x%4x\n”, x, y );}2.main(){ float a, b, h, s;scanf(“%f%f%f”, &a, &b, &h );s=(a+b)*h/2 ;printf(“%8.1f\n”, s);}3.main(){ char c;scanf(“%c”, &c );printf(“%c, %c, %c\n”, c-1, c, c+1 ); }4.main(){ int a, a1, a2, a3;scanf(“%d”, &a );a1=a%10 ; a=a/10;a2=a%10; a=a/10;a3=a%10;printf(“%d%d%d\n”, a1, a2, a3);}5.main(){ double a, b, c, ave;scanf(“%lf%lf%lf”, &a, &b, &c );ave=(a+b+c)/3 ;printf(“%8.2f\n”, ave);}6.main(){ int a, b, c, t;scanf(“%d%d%d”, &a, &b, &c );t=a; a=c; c=b ; b=t ;printf(“a=%d, b=%d, c=%d\n”, a, b, c );}习题44-1单选题1~5 BADAC 6~10 DDACD 11~15 BBBAB4-2填空题1.12.if(x>0) y=1else if (x==0) y=0else y=-13.(1) a>0 || b>0 (2) x>0 && x<=10(3) a==1.5 && b==1.5 && c==1.5 (4)p<a || p<b || p<c4.(1)0 (2)1 (3)1 (4)0 (5)05. max=(max=a>b?a:b)>c?max:c6.-47.18.5,0,39.85 belongs to B10. (1) (a==0) (2) (b==0) (3) (disc<0)4-3 编程题1.#include “math.h”main(){ float a,b,c,s,area;scanf(“%f,%f,%f”,&a,&b,&c);if (a+b>c && a+c>b && b+c>a){ s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c));printf(“%f”,area);}}2.main(){ int x,y;scanf(“%d,%d”,&x,&y);if (x*x+y*y>1000)printf(“%d\n”,(x*x+y*y)/100); elseprintf(“%d\n”,x+y);}3.main(){ int x,scanf(“%d”,&x);if(x%3==0 && x%5==0 && x%7==0) printf(“yes\n”);elseprintf(“no\n”);}4.#include “math.h”main(){ float x,y;scanf(“%f”,&x);if(x<-2) y=x*x-sin(x);else if (x<=2) y=pow(2,x)+1;else y=sqrt(x*x+x+1);printf(“%f\n”,y);}5. main( )long a,b,c,d,e,x;scanf("%ld",&x);a=x/10000;/*分解出万位*/b=x%10000/1000;/*分解出千位*/c=x%1000/100;/*分解出百位*/d=x%100/10;/*分解出十位*/e=x%10;/*分解出个位*/if (a!=0) printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a); else if (b!=0) printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b);else if (c!=0) printf(" there are 3,%ld %ld %ld\n",e,d,c);else if (d!=0) printf("there are 2, %ld %ld\n",e,d);else if (e!=0) printf(" there are 1,%ld\n",e);}6.1.程序分析:main( ){long ge,shi,qian,wan,x;scanf("%ld",&x);wan=x/10000;qian=x%10000/1000;shi=x%100/10;ge=x%10;if (ge==wan&&shi==qian)/*个位等于万位并且十位等于千位*/ printf("this number is a huiwen\n");elseprintf("this number is not a huiwen\n");}7. main(){float p,w,s,d,f;scanf(“%f,%,%f”,p,s,w);if (s>3000) d=0.15else if( s>=2000) d=0.1;else if(s>=1000) d=0.08;else if(s>=500) d=0.05;else if(s>=250) d=0.02;else d=0f=p*w*s*(1-d);printf(“%f”,f);}习题55-1单选题1~5 CDACB 6~10 DCAAB 11~16 DBDBCB5-2填空题1.2 02.k=5,s=43.3334.How Are You5.2#18#6.(1) i<10 (2) j%3!=07.(1) flag*(float)k/(k+1) (2) flag=-flag8.(1) s=0,t=1; (2) t=t*x/i*pow(-1,i+1); (3) printf(“%f”,s); 9.(1) max=x (2) x!=-1 (3) scanf("%d", &x)10. (1) n=1 (2) s5-3 编程题1. (1) main(){ int i; lont s=0;for(i=1;i<=100;i++)s+=i*i;printf(“%ld”,s);}(2)main(){ int i; lont p=1,s=0;for( i=1;i<=10;i++){p=p*i;s+=p;}printf(“%ld”,s);}(3) main(){ int i=1; lont p=1;float s=0;do{s+=1.0/p;p*=++i;}while(1.0/p>1e-6)printf(“%f”,s);}(4)main(){ int i=1, flag=1,pi=0;do{s+=flag*1.0/(2*i-1);i++;}while(1.0/(2*i-1)>1e-6);pri ntf(”%f”,s);}2.main(){int m,n,t,a,b;scanf(“%d,%d” ,&m,&n) ;if (m<n){ t=m ;m=n ;n=t ;}a=m;b=n;t=m%n ;while(t){ m=n ; n=t ;t=m%n;}printf(”%d,%d”,n,a*b/n);}3. main(){int x,y,s=1;scanf(”%d,%d”,&x,&y) ;for( ;y>0 ;y--)s*=x ;printf(”%d,%d,%d\n ”,s%10,s/10%10,s/100%10); }4.main(){int i=1, k=2, sum=0;do{sum+=k;k=k*2;i++;}while(sum<=100);printf(”total=%f\n”, 0.4*sum/(i-1));}5. main(){ int x,y,z;for( x=1 ; x<20 ;x++)for( y=1 ;y<33 ;y++){ z=100-x-y ;if (z>=0 && (5*x+3*y+z/3)-100<1e-5)printf(”x=%d,y=%d,z=%d\n”,x,y,z) ;}}6. main(){int j,k ;for( j=1 ;j<=4 ;j++){for(k=1;k<=4-j;k++;)printf(” ”);for(k=1 ;k<=2*j-1 ;k++)printf(”*”) ;printf(”\n”) ;}for( j=3;j>=1;j--){ for(k=1;k<=4-j;k++;)printf(””);for(k=1;k<=2*j-1;k++)printf(”*”);printf(”\n”);}}7.分析:其实此问题的解法非常简单。
C语言程序设计课后习题答案

C语言程序设计(第2版)课后习题答案第一章1.请参照本章例题,编写一个C程序,输出以下信息:**************************V ery good!**************************解:#include<stdio.h>void main(){printf(“**************************”);printf(“\n”);printf(“V ery 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个字母代替原来的字母。
例如,字母“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‟,并输出。
c语言程序设计第2版习题答案

c语言程序设计第2版习题答案1. 基础语法练习题- 题目:编写一个程序,计算并输出1到10的和。
- 答案:使用一个循环结构,累加1到10的数值。
```c#include <stdio.h>int main() {int sum = 0;for (int i = 1; i <= 10; ++i) {sum += i;}printf("The sum from 1 to 10 is: %d\n", sum);return 0;}```2. 数组和字符串练习题- 题目:编写一个程序,找出一个字符串中出现次数最多的字符,并输出它。
- 答案:使用数组来统计每个字符的出现次数,然后找出最大值。
```c#include <stdio.h>#include <string.h>int main() {char str[] = "example string";int maxCount = 0;char maxChar = str[0];int charCount[256] = {0};for (int i = 0; str[i] != '\0'; ++i) {charCount[(int)str[i]]++;if (charCount[(int)str[i]] > maxCount) { maxCount = charCount[(int)str[i]];maxChar = str[i];}}printf("The most frequent character is: %c\n", maxChar);return 0;}```3. 函数和指针练习题- 题目:编写一个函数,交换两个整数的值。
- 答案:使用指针来实现参数的传递。
```c#include <stdio.h>void swap(int *x, int *y) {int temp = *x;*x = *y;*y = temp;}int main() {int a = 5, b = 10;printf("Before swap: a = %d, b = %d\n", a, b);swap(&a, &b);printf("After swap: a = %d, b = %d\n", a, b);return 0;}```4. 结构体和文件操作练习题- 题目:定义一个结构体,包含学生姓名、学号和成绩,编写程序,将学生信息存储到文件中。
C语言程序的设计习题参考答案(第二版_杜友福)

C 语言程序设计习题答案习题一 C 语言程序设计概述一、名词解释(1)程序P1 (2)程序设计P1 (3)机器语言P1 (4)汇编程序P2(5)高级语言P2 (6)编译程序P3 (7)解释程序P3 (8)算法P4(9)结构化的程序设计P9二、简述题1. 设计程序时应遵循哪些基本原则?P4答:正确性、可靠性、简明性、有效性、可维护性、可移植性。
2. 算法的要素是什么?算法具有哪些特点?答:算法的要素是:操作与控制结构;算法的特点有:有穷性、确定性、有效性、有零个或多个输入、有一个或多个输出。
3. 算法的表示形式有哪几种?答:算法的表示形式有:自然语言、传统流程图、伪代码、结构化的流程图(N_S 流程图,盒图)。
4. 有哪三种基本结构?答:三种基本结构是:顺序结构、选择结构和循环结构。
5. 传统流程图与N-S 流程图最大的区别是什么?答:N-S 流程图去掉了在传统流程图中常用的流程线,使得程序的结构显得更加清晰、简单。
三、用传统流程图、N-S 图分别表示求解以下问题的算法。
1. 有3个数a ,b ,c ,要求按由大到小的顺序把它们输出。
2. 依次将10个数输入,求出其中最大的数 和最小的数并输出。
3. 求1+2+3+…+100的值。
4. 求1×2×3×…×10的值。
5. 求下列分段函数的值。
6. 求100~200之间的所有素数。
7. 求一元二次方程ax 2+bx+c=0的根。
分别考虑d=b 2-4ac 大于0、等于0和小于0三种情况。
四、注释下面C 程序的各个组成部分。
main() /*主函数 */{ /*程序开始 */int a,k,m; /*定义三个用来存放整数的变量 */a=10; /*将整数10赋值给变量a */k=2; /*将整数2赋值给变量k */m=1; /*将整数1赋值给变量1 */a=(k+m)*k/(k-m); /*先求出算术表达式的值,并将其赋值给变量a */printf("%d\n",a); /*在屏幕上打印出变量a 的值 */} /*程序结束 */习题二 数据类型、运算符与表达式一、选择题1~10:BCDCB DDBCA11~20: ADDAA DBADC21~28: DABAD CDDY= 3X (X<1) 4X-1 (X=1) 5(X-1)+6 (1<X<5) 6-3X (X ≥5) 输入一个数给x X<=1 Yes no X<1 x<5 Yes no yes no Y=3x y=4x-1 y=5x+1 y=6-3x 输出s 的值 i =100 当i<=200时 n=2; flag=1; 当n< i 时i 能否被n 整除?yes no flag=0 n = n+1 flag=1?yes no输出i 的值i = i+1二、填空题1.字母L 或字母l2.字符或%c 、整数或%d3.在程序运行过程中,其值可以在一定的围变化的量。
c语言程序设计教程(第2版)课后题及模拟题参考答案

c语⾔程序设计教程(第2版)课后题及模拟题参考答案c语⾔程序设计教程(第2版)课后题及模拟题参考答案习题1 (4)1-1 填空题 (4)1-2 思考题 (4)1-3 编程题 (5)习题2 (6)2-1 单选题 (6)2-2 思考题 (6)习题3 (7)3-1 选择题 (7)3-2 填空题 (7)3-3 编程题 (8)习题4 (11)4-1单选题 (11)4-2填空题 (11)4-3 编程题 (11)习题5 (16)5-1单选题 (16)5-2填空题 (16)5-3 编程题 (16)习题6 (22)6-1单选题 (22)6-2填空题 (22)习题7 (25)7-1单选题 (25)7-2填空题 (25)7-3 编程题 (25)习题8 (26)8-1单选题 (26)8-2填空题 (26)8-3 编程题 (26)习题9 (30)9-1单选题 (30)9-2填空题 (30)9-3 编程题 (30)习题10 (38)10-1单选题 (38)10-2填空题 (38)10-3 编程题 (38)习题11 (41)11-1单选题 (41)11-2填空题 (41)习题12 (42)12-1单选题 (42)12-2 填空题 (42)实验1 熟悉Visual C++6.0可视化集成开发环境 (43)实验2 顺序结构程序设计 (43)实验3 选择结构程序设计 (43)实验4 循环结构程序设计 (44)实验5 函数 (47)实验6 数组 (54)实验7 指针 (58)实验8 结构体和共⽤体 (61)实验9 ⽂件 (63)实验10 综合编程 (64)模拟试卷(⼀)参考答案 (65)模拟试卷(⼆)参考答案 (67)习题11-1 填空题1. 函数2. 主函数main(),主函数main()3. 主函数main()4. 函数⾸部,函数体5. {, }6. /*, */7. 顺序结构,选择结构,循环结构8. .c, .obj, .exe1-2 思考题1. 结构化程序设计是指:为使程序具有⼀个合理的结构以保证程序正确性⽽规定的⼀套如何进⾏程序设计的原则。
C语言程序设计(第2版)-- 课后题答案.

《C语言程序设计能力教程(第二版)》课后作业及实训题参考答案第1章进入C语言程序世界二、1. I love China!printf("we are students.\n")2. 6项目实训题参考答案1.编写一个C程序,输出以下信息:* * * * * * * * * * * * * * * * * * * *I am a student!* * * * * * * * * * * * * * * * * * * *main(){ printf("********************\n");printf(" I am a student!\n ");printf("********************\n");}2.已知立方体的长、宽、高分别是10cm、20cm、15cm,编写程序,求立方体体积。
解:main(){int a,b,c,v;a=10;b=20;c=15;v=a*b*c;printf("v=%d",v);}本程序运行结果为:v=3000第2章编制C程序的基础知识一选择题C B A B A C C二操作题,2,-8,23.000000,2.500000,-8.0000002. ABC DEFGHwhy is 21+35 equal 523.3 14 32 31 24. aa bb cc abcA N项目实训题1.定义一个符号常量M为5和一个变量n值为2,把它们的乘积输出。
#define M 5main(){ int n,c;n=2; c=M*n;printf("%d\n",c); }2.编程求下面算术表达式的值。
(1)x+a%3*(int)(x+y)%2/4,设x=2.5,a=7,y=4.7;(2)(float)(a+b)/2+(int)x%(int)y,设a=2,b=3,x=3.5,y=2.5。
c语言程序设计教程第二版课后答案

c语言程序设计教程第二版课后答案【篇一:c语言程序设计(第2版)-- 课后题答案】p> 参考答案第1章进入c语言程序世界二、1.i love china!printf(we are students.\n)2.6项目实训题参考答案1.编写一个c程序,输出以下信息:* * * * * * * * * * * * * * * * * * * *i am a student!* * * * * * * * * * * * * * * * * * * *main(){ printf(********************\n);printf( i am a student!\n);printf(********************\n);}2.已知立方体的长、宽、高分别是10cm、20cm、15cm,编写程序,求立方体体积。
解:main(){int a,b,c,v;a=10;b=20;c=15;v=a*b*c;printf(v=%d,v);}本程序运行结果为:v=3000第2章编制c程序的基础知识一选择题c b a b a c c二操作题,2,-8,23.000000,2.500000,-8.0000002. abc defghwhy is21+35equal 523.34214. aaa项目实训题1.定义一个符号常量m为5和一个变量n值为2,把它们的乘积输出。
#define m 5main(){ int n,c;n=2; c=m*n;printf(%d\n,c);}2.编程求下面算术表达式的值。
(1)x+a%3*(int)(x+y)%2/4,设x=2.5,a=7,y=4.7;(2)(float)(a+b)/2+(int)x%(int)y,设a=2,b=3,x=3.5,y=2.5。
(1)main(){ int a=7;float x=2.5,y=4.7;printf(%f\n,x+a%3*(int)(x+y)%2/4);}(2)main(){ int a=2,b=3;float x=3.5,y=2.5;printf(%f\n,(float)(a+b)/2+(int)x%(int)y);}第三章顺序结构程序设计一选择题a c d c c二操作题1. x=3,a=2,b=32. z=12.7000002 13 3 2 bb cc abc n3. 1 2 1a2 1 2三.编程题编程题解:#include stdio.hmain(){float sj,gz,yfgz;printf(time,salary:);scanf(%f,%f,sj,gz);yfgz=sj*gz*0.9;printf(total salary:%f\n,yfgz);}本程序运行结果为:time,salary:4,3crtotal salary:10.8000002.编写一个程序求出任意一个输入字符的ascii码解:#include stdio.hmain(){char c;printf(input a string:);scanf(%c,c);printf(%c ascii is %d\n,c,c);}本程序运行结果为:input a string:acra ascii is 973、编写一个程序用于水果店售货员算帐:已知苹果每斤2.50元,鸭梨每斤1.80元,香蕉每斤2元,橘子每斤1.6元,要求输入各类水果的重量,打印出应付3解:main(){float p,y,x,j,ys,g,fk;printf(apple,pear,banana,orange(weight)=);scanf(%f,%f,%f,%f,p,y,x,j);ys=2.5*p+1.8*y+2*x+1.6*j;printf(fu kuan=);scanf(%f,g);fk=g-ys;printf(result:\n);printf(fukuan=%6.2fyuan\nshoukuan=%6.2fyuan\nzhaohui=%6. 2fyuan\n,g,ys,fk);}本程序运行结果为:apple,pear,banana,orange(weight)=1,2,3,4fu kuan=100result:fukuan=100.00yuanshoukuan= 18.50yuanzhaohui= 81.50yuan项目实训1.假设银行定期存款的年利率rate为2.25%,并已知存款期为n 年,存款本金为capital元,试编程计算n年后可得到本利之和deposit。
C语言程序设计课后习题答案

5.设圆半径r=1.5,圆柱高h=3,求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积。用scanf输入数据,输出计算结果,输出时要求文字说明,取小数点后2位数字。请编程序。
解:
#include<stdio.h>
voidmain()
{
float r,h,C1,Sa,Sb,Va,Vb;
c2=getchar();
printf(“c1=%d c2=%d\n”,c1,c2);
printf(“c1=%c c2=%c\n”,c1,c2);
}
第四章
3.写出下面各逻辑表达式的值。设a=3,b=4,c=5。
(1) a+b>c&&b==c
(2) a||b+c&&b-c
(3) !(a>b)&&!c||1
break;
case 2: printf(“%d,%d”,ten,indiv);
printf(“\n反序数字为:”);
printf(“%d%d\n”,indiv,ten);
break;
case 1: printf(“%d”,indiv);
printf(“\n反序数字为:”);
printf(“%d\n”,indiv);
printf(“%d%d%d%d\n”,indiv,ten,hundred,thousand);
break;
case 3: printf(“%d,%d,%d”,hundred,ten,indiv);
printf(“\n反序数字为:”);
printf(“%d%d%d\n”,indiv,ten,hundred);
}
6.输入一个华氏温度,要求输出摄氏温度。公式为c=5(F-32)/9,输出要求有文字说明,取位2小数。
C语言程序设计教程(第2版)课后习题答案(完美版)

C语言程序设计教程(第二版)课后习题答案第一章C语言程序设计概述-习题答案1算法的描述有哪些基本方法?答1、自然语言2、专用工具2 C语言程序的基本结构是怎样的?举一个例子说明。
答1、C语言程序由函数构成;2、“/*”与“*/”之间的内容构成C语言程序的注释部分;3、用预处理命令#include、#define可以包含有关文件或预定义信息;4、大小写字母在C语言中是有区别的;5、除main()函数和标准库函数外,用户也可以自己编写函数,应用程序一般由多个函数组成,这些函数指定实际所需要做的工作。
3 C语言有什么特点?答1、具有结构语言的特点,程序之间很容易实现段的共享;2、主要结构成分为函数,函数可以在程序中被定义完成独立的任务,独立地编译代码,以实现程序的模块化;3、运算符丰富,包含的范围很广;4、数据类型丰富;5、允许直接访问物理地址,即可直接对硬件进行损伤,实现汇编语言的大部分功能;6、限制不太严格,程序设计自由度大,这样使C语言能够减少对程序员的束缚;7、生成的目标代码质量,程序执行效率高,同时C语言编写的程序的可移植性好。
4★指出合法与不合法的标识符命名。
答AB12--√leed_3-- a*b2--× 8stu--× D.K.Jon--× EF3_3--√ PAS--√ if--× XYZ43K2 --√ AVE#XY--× _762--√ #_DT5--× C.D--×5说明下列Turbo C热键的功能。
答F2:源文件存盘 F10:调用主菜单 F4:程序运行到光标所在行(用于调试程序) Ctrl+F9:编译并链接成可执行文件 Alt+F5:将窗口切换到 DOS 下,查看程序运行结果。
6说明下列Turbo C方式下输入并运行下列程序,记录下运行结果。
①main(){printf("********************\n");printf(" welcome you \n");printf(" very good \n);printf("********************\n");}②main()printf("please input three numbers;");scanf("%d,%d,%d",&a,&b,&c); /*教材S是错误的*/t=max(max(a,b),c);printf("max number is:%d\n",t);}int max(int x, int y){ int z;if(x>y)z=x;else z=y;return(z);}答运行结果:********************welcome youvery good********************运行结果:please input three numbers;3,1,4 /*左侧下划线内容为键盘输入*/max number is:47一个C程序是由若干个函数构成的,其中有且只能有一个___函数。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 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的关键字。