C程序设计教程(第2版)第4章选择结构复习
- 格式:ppt
- 大小:204.50 KB
- 文档页数:7
第四章选择结构一、选择题1、当把以下四个表达式用作if语句的控制表达式时,有一个选项与其它三个选项含义不同,这个选项是A)k%2 B)k%2==1 C)(k%2)!=0 D)!k%2==12、设有定义:int k=1,m=2; float f=7;,则以下选项中错误的表达式是A)k=k>=k B)-k++ C)k%int(f) D)k>=f>=m3、设有定义:int a=2,b=3,c=4;,则以下选项中值为0的表达式是A)(!a==1)&&(!b==0) B)(a < b)&& !c||1C)a && b D)a||(b+b)&&(c-a)4、有以下程序main(){ int a,b,d=25;a=d/10%9;b=a&&(-1);printf("%d,%d\n",a,b); }程序运行后的输出结果是A)6,1 B)2,1 C)6,0 D)2,05、有以下程序main(){int i=1,j=2,k=3;if(i++==1&&(++j==3||k++==3))printf("%d %d %d\n",i,j,k); }程序运行后的输出结果是A)1 2 3 B)2 3 4 C)2 2 3 D)2 3 36、有以下程序main(){int a=3,b=4,c=5,d=2;if(a>b)if(b>c)printf("%d",d++ +1);elseprintf("%d",++d +1);printf("%d\n",d); }程序运行后的输出结果是A) 2 B) 3 C) 43 D) 447、下列条件语句中,功能与其他语句不同的是A) if(a) printf("%d\n",x); else printf("%d\n",y);B) if(a==0) printf("%d\n",y); else printf("%d\n",x);C) if (a!=0) printf("%d\n",x); else printf("%d\n",y);D) if(a==0) printf("%d\n",x); else printf("%d\n",y);8、以下4个选项中,不能看作一条语句的是A){;} B)a=0,b=0,c=0;C) if(a>0); D)if(b==0) m=1;n=2;9、以下程序段中与语句k=a>b?(b>c?1:0):0;功能等价的是A)if((a>b) &&(b>c) ) k=1; B)if((a>b) ||(b>c) ) k=1else k=0;C)if(a<=b) k=0; D) if(a>b) k=1;else if(b<=C) k=1; else if(b>c) k=1;else k=0;10、有以下程序main(){int a=1,b=2,m=0,n=0,k;k=(n=b>a)||(m=a < b);printf("%d,%d\n",k,m); }程序运行后的输出结果是A)0,0 B)0,1 C)1,0 D)1,111、有定义语句:int a=1,b=2,c=3,x; 则以下选项中各程序段执行后,x的值不为3的是A) if (c < a) x=1; B) if(a<3) x=3;else if (b < a) x=2; else if (a < 2) x=2;else x=3; else x=1;C) if (a < 3) x=3; D)if (a< b) x=b;if (a < 2) x=2; if (b< c) x=c;if (a< 1) x=1; if (c< a) x=a;12、有以下程序main(){ int i=1,j=1,k=2;if((j++‖k++)&&i++) printf("%d,%d,%d\n",i,j,k); }执行后输出结果是A)1,1,2 B)2,2,1 C)2,2,2 D)2,2,3 13、有以下程序main(){ int a=5,b=4,c=3,d=2;if(a>b>c) printf("%d\n",d);else if((c-1>=d)==1)printf("%d\n",d+1);else printf("%d\n",d+2) }执行后输出结果是A)2 B)3 C) 4 D)编译时有错,无结果14、已有定义:int x=3,y=4,z=5;,则表达式!(x+y)+z-1 && y+z/2的值是A)6 B)0 C)2 D)115、以下程序的输出结果是main(){int a=5,b=4,c=6,d;printf("%d\n",d=a>b?(a>c?a:c):(b)); }A)5 B) 4 C)6 D)不确定16、以下程序的输出结果是main(){ int a=4,b=5,c=0,d;d=!a&&!b||!c;printf("%d\n",d); }A)1 B)0 C)非0的数 D)-117、设 a、b、 c、d、m、n均为 int型变量,且 a=5、b=6、c=7、d=8、m=2、n=2,则逻辑表达式 (m=a>b)&&(n=c>d)运算后,n的值为A) 0 B) 1 C)2 D) 318、阅读以下程序:main(){int x;scanf("%d",&x);if(x--<5) printf("%d",x);else printf("%d",x++);}程序运行后,如果从键盘上输入5,则输出结果是A)3 B) 4 C) 5 D) 619、设x、y、t均为int型变量,则执行语句:x=y=3;t=++x||++y;后,y的值为A)不定值 B)4 C)3 D)120、若执行以下程序时从键盘上输入9,则输出结果是A) 11 B) 10 C) 9 D) 8main(){int n;scanf("%d",&n);if(n++<10) printf("%d\n",n);else printf("%d\n",n--);}21、有如下程序main(){float x=2.0,y;if(x<0.0) y=0.0;else if(x<10.0) y=1.0/x;else y=1.0;printf("%f\n",y);}该程序的输出结果是A)0.000000 B)0.250000C)0.500000 D)1.00000022、有如下程序main(){int a=2,b=-1,c=2;if(a< b)if(b< 0) c=0;else c++;printf("%d\n",c);}该程序的输出结果是A) 0 B)1 C) 2 D) 323、设 int x=1, y=1; 表达式(!x||y--)的值是A)0 B) 1 C) 2 D) -124、语句:printf("%d",(a=2)&&(b= -2));的输出结果是A)无输出 B)结果不确定 C)-1 D)125、有以下程序段int k=0,a=1,b=2,c=3; k=a< b ? b:a; k=k> c ? c:k;执行该程序段后,k的值是A)3 B)2 C)1 D)026、若整型变量a、b、c、d中的值依次为:1、4、3、2。
Exercise 4-1Write the function strrindex(s,t) , which returns the position of the rightmost occurrence of t in s , or -1 if there is none.#include<stdio.h>#define MAXLINE 1000int getline(char line[], int max);int find_the_situaion(char t[],char s[]);int count(char v[]);void main(){char target[MAXLINE];char line[MAXLINE];while (getline(line, MAXLINE) > 0){printf("%d", find_the_situaion(line, target));}}int getline(char s[], int lim) {int c, i;i = 0;while (--lim > 0 && (c = getchar()) != EOF && c != '\n')s[i++] = c;if (c == '\n')s[i++] = c;s[i] = '\0';return i;}int find_the_situaion(char t[], char s[]){int i, j, k;int b = count(s);int c = count(t);for (i = 0; s[i] != '\0'; i++){for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++,k++);if(k==b+1){return c - i - b;}}return -1;}int count(char v[]){int a = 0;if (v[a]!='\0'){a++;}return a;}Exercise 4-2Extend atof to handle scientific notation of the form 123.45e-6 where a floating-point number may be followed by e or E and an optionally signed exponent.Exercise 4-3Given the basic framework, it's straightforward to extend the calculator. Add the modulus ( % ) operator and provisions for negative numbers.#include<stdlib.h>#include"pch.h"#include<iostream>#define MAXOP 100#define NUMBER'0'#define TRUE 1;int Getop(char[]);int getch(void);double pop(void);void push(double f);void ungetch(int c);int main(void){int type;double op2;char s[MAXOP];int flag = TRUE;while ((type = Getop(s)) != EOF){switch (type){case'%':op2 = pop();if (op2)push(fmod(pop(), op2));elseprintf("\nError: Division by zero!");break;}}return EXIT_SUCCESS;}int Getop(char s[]){#define PERIOD'.'int i = 0;int c;int next;while ((s[0] = c = getch()) == ' ' || c == '\t') ;s[1] = '\0';if (!isdigit(c) && c != PERIOD && c != '-')return c;if (c == '-'){next = getch();if (!isdigit(next) && next != PERIOD){return c;}c = next;}else{c = getch();}while (isdigit(s[++i] = c))c = getch();if (c == PERIOD)while (isdigit(s[++i] = c = getch()));s[i] = '\0';if (c != EOF)ungetch(c);return NUMBER;}#define MAXVAL 100int sp = 0;double val[MAXVAL];double pop(void){ if (sp > 0)return val[--sp];else{printf("error: stack empty\n");return 0.0;}}void push(double f){if (sp < MAXVAL)val[sp++] = f;elseprintf("error: stack full, can't push %g\n", f);}#define BUFSIZE 100char buf[BUFSIZE];int bufp = 0;int getch(void){return (bufp > 0) ? buf[--bufp] : getchar();}void ungetch(int c){if (bufp >= BUFSIZE)printf("ungetch: too many characters\n");elsebuf[bufp++] = c;}Exercise 4-4Add commands to print the top element of the stack without popping, to duplicate it, and to swap the top two elements. Add a command to clear the stack.#include"pch.h"#include<stdlib.h>#include<stdio.h>#include<ctype.h>#include<math.h>#define MAXOP 100#define NUMBER 0#define TRUE 1#define FALSE 0int Getop(char s[]);void push(double val);double pop(void);void showTop(void);void duplicate(void);void swapItems(void);void clearStack();int main(void){int type;double op2;char s[MAXOP];int flag = TRUE;while ((type = Getop(s)) != EOF){switch (type){case NUMBER:push(atof(s));break;case'+':push(pop() + pop());break;case'*':push(pop() * pop());break;case'-':op2 = pop();push(pop() - op2);break;case'/':op2 = pop();if (op2)push(pop() / op2);elseprintf("\n出现错误!");break;case'%':op2 = pop();if (op2)push(fmod(pop(), op2));elseprintf("\n出现错误!");break;case'?':showTop();break;case'#':duplicate();break;case'~':swapItems();break;case'!':clearStack();case'\n':printf("\n\t%.8g\n", pop());break;default:printf("\nError: 不清楚的指令 %s.\n", s);break;}}return EXIT_SUCCESS;}#define MAXVAL 100int sp = 0;double val[MAXVAL];void push(double f){if (sp < MAXVAL)val[sp++] = f;elseprintf("\n出现错误 %g\n", f);}double pop(void){if (sp > 0)return val[--sp];else{printf("\n出现错误\n");return 0.0;}}void showTop(void){if (sp > 0)printf("Top of stack contains: %8g\n", val[sp - 1]);elseprintf("The stack is empty!\n");}void duplicate(void){double temp = pop();push(temp);push(temp);}void swapItems(void){double item1 = pop();double item2 = pop();push(item1);push(item2);}void clearStack(void){sp = 0;}int getch(void);void unGetch(int);int Getop(char s[]){int i = 0;int c;int next;while ((s[0] = c = getch()) == ' ' || c == '\t') ;s[1] = '\0';if (!isdigit(c) && c != '.' && c != '-')return c;if (c == '-'){next = getch();if (!isdigit(next) && next != '.'){return c;}c = next;}elsec = getch();while (isdigit(s[++i] = c))c = getch();if (c == '.')while (isdigit(s[++i] = c = getch()));s[i] = '\0';if (c != EOF)unGetch(c);return NUMBER;}#define BUFSIZE 100char buf[BUFSIZE];int bufp = 0;int getch(void){return (bufp > 0) ? buf[--bufp] : getchar();}void unGetch(int c){if (bufp >= BUFSIZE)printf("\n出现错误\n");elsebuf[bufp++] = c;}Exercise 4-5Add access to library functions like sin , exp , and pow . See <math.h> in Appendix B, Section 4.#include<stdlib.h>#include<stdio.h>#include<ctype.h>#include<math.h>#include<string.h>#define MAXOP 100#define NUMBER 0#define IDENTIFIER 1#define TRUE 1#define FALSE 0int Getop(char s[]);void push(double val);double pop(void);void showTop(void);void duplicate(void);void swapItems(void);void clearStack();void dealWithName(char s[]);int main(void){int type;double op2;char s[MAXOP];int flag = TRUE;while ((type = Getop(s)) != EOF){switch (type){case NUMBER:push(atof(s));break;case IDENTIFIER:dealWithName(s);break;case'+':push(pop() + pop());break;case'*':push(pop() * pop());break;case'-':op2 = pop();push(pop() - op2);break;case'/':op2 = pop();if (op2)push(pop() / op2);elseprintf("\nError: division by zero!");break;case'%':op2 = pop();if (op2)push(fmod(pop(), op2));elseprintf("\nError: division by zero!");break;case'?':showTop();break;case'#':duplicate();break;case'~':swapItems();break;case'!':clearStack();case'\n':printf("\n\t%.8g\n", pop());break;default:printf("\nError: unknown command %s.\n", s);break;}}return EXIT_SUCCESS;}#define MAXVAL 100int sp = 0;double val[MAXVAL];void push(double f){if (sp < MAXVAL)val[sp++] = f;elseprintf("\nError: stack full can't push %g\n", f);}double pop(void){if (sp > 0)return val[--sp];else{printf("\nError: stack empty\n");return 0.0;}}void showTop(void){if (sp > 0)printf("Top of stack contains: %8g\n", val[sp - 1]);elseprintf("The stack is empty!\n");}void duplicate(void){double temp = pop();push(temp);push(temp);}void swapItems(void)double item1 = pop();double item2 = pop();push(item1);push(item2);}void clearStack(void){sp = 0;}void dealWithName(char s[]){double op2;if (0 == strcmp(s, "sin"))push(sin(pop()));else if (0 == strcmp(s, "cos"))push(cos(pop()));else if (0 == strcmp(s, "exp"))push(exp(pop()));else if (!strcmp(s, "pow")){op2 = pop();push(pow(pop(), op2));}elseprintf("%s is not a supported function.\n", s); }int getch(void);void unGetch(int);int Getop(char s[]){int i = 0;int c;int next;while ((s[0] = c = getch()) == ' ' || c == '\t') ;s[1] = '\0';if (isalpha(c)){i = 0;while (isalpha(s[i++] = c))c = getch();s[i - 1] = '\0';if (c != EOF)unGetch(c);return IDENTIFIER;}if (!isdigit(c) && c != '.' && c != '-') return c;if (c == '-'){next = getch();if (!isdigit(next) && next != '.'){return c;}c = next;}elsec = getch();while (isdigit(s[++i] = c))c = getch();if (c == '.')while (isdigit(s[++i] = c = getch()));s[i] = '\0';if (c != EOF)unGetch(c);return NUMBER;}#define BUFSIZE 100char buf[BUFSIZE];int bufp = 0;int getch(void)return (bufp > 0) ? buf[--bufp] : getchar();}void unGetch(int c){if (bufp >= BUFSIZE)printf("\nUnGetch: too many characters\n");elsebuf[bufp++] = c;}Exercise 4-6Add commands for handling variables. (It's easy to provide twenty-six variables with single-letter names.) Add a variable for the most recently printed value.#include<stdlib.h>#include<stdio.h>#include<ctype.h>#include<math.h>#include<string.h>#define MAXOP 100#define NUMBER 0/* 4-6 these are new for this exercise*/#define IDENTIFIER 1#define ENDSTRING 2/* 4-6 end of new stuff */#define TRUE 1#define FALSE 0#define MAX_ID_LEN 32#define MAXVARS 30struct varType {char name[MAX_ID_LEN];double val;};int Getop(char s[]);void push(double val);double pop(void);void showTop(void);void duplicate(void);void swapItems(void);void clearStacks(struct varType var[]);void dealWithName(char s[], struct varType var[]);void dealWithVar(char s[], struct varType var[]);int pos = 0;struct varType last;int main(void){int type;double op2;char s[MAXOP];struct varType var[MAXVARS];clearStacks(var);while ((type = Getop(s)) != EOF){switch (type){case NUMBER:push(atof(s));break;case IDENTIFIER:dealWithName(s, var);break;case'+':push(pop() + pop());break;case'*':push(pop() * pop());break;case'-':op2 = pop();push(pop() - op2);break;case'/':op2 = pop();if (op2)push(pop() / op2);elseprintf("\nError: division by zero!");break;case'%':op2 = pop();if (op2)push(fmod(pop(), op2));elseprintf("\nError: division by zero!");break;case'?':showTop();break;case'#':duplicate();break;case'~':swapItems();break;case'!':clearStacks(var);break;case'\n':printf("\n\t%.8g\n", pop());break;/* 4-6 this is new for this program */case ENDSTRING:break;case'=':pop();var[pos].val = pop();last.val = var[pos].val;push(last.val);break;case'<':printf("The last variable used was: %s (value == %g)\n",, last.val);break;/* 4-6 End of new stuff */default:printf("\nError: unknown command %s.\n", s);break;}}return EXIT_SUCCESS;}#define MAXVAL 100int sp = 0;double val[MAXVAL];void push(double f){if (sp < MAXVAL)val[sp++] = f;elseprintf("\nError: stack full can't push %g\n", f);}double pop(void){if (sp > 0){return val[--sp];}else{printf("\nError: stack empty\n");return 0.0;}}void showTop(void){if (sp > 0)printf("Top of stack contains: %8g\n", val[sp - 1]);elseprintf("The stack is empty!\n");}void duplicate(void){double temp = pop();push(temp);push(temp);}void swapItems(void){double item1 = pop();double item2 = pop();push(item1);push(item2);}void clearStacks(struct varType var[]){int i;sp = 0;for (i = 0; i < MAXVARS; ++i){var[i].name[0] = '\0';var[i].val = 0.0;}}void dealWithName(char s[], struct varType var[]){double op2;if (!strcmp(s, "sin"))push(sin(pop()));else if (!strcmp(s, "cos"))push(cos(pop()));else if (!strcmp(s, "exp"))push(exp(pop()));else if (!strcmp(s, "pow")){op2 = pop();push(pow(pop(), op2));}else{dealWithVar(s, var);}}void dealWithVar(char s[], struct varType var[]){int i = 0;while (var[i].name[0] != '\0' && i < MAXVARS - 1) {if (!strcmp(s, var[i].name)){strcpy(, s);last.val = var[i].val;push(var[i].val);pos = i;return;}i++;}strcpy(var[i].name, s);strcpy(, s);push(var[i].val);pos = i;}int getch(void);void unGetch(int);int Getop(char s[]){int i = 0;int c;int next;while ((s[0] = c = getch()) == ' ' || c == '\t') {;}s[1] = '\0';if (isalpha(c)){i = 0;while (isalpha(s[i++] = c)){c = getch();}s[i - 1] = '\0';if (c != EOF)unGetch(c);return IDENTIFIER;}if (!isdigit(c) && c != '.' && c != '-'){if ('=' == c && '\n' == (next = getch())){unGetch('\0');return c;}if ('\0' == c)return ENDSTRING;return c;}if (c == '-'){next = getch();if (!isdigit(next) && next != '.'){return c;}c = next;}else{c = getch();}while (isdigit(s[++i] = c)){c = getch();}if (c == '.'){while (isdigit(s[++i] = c = getch()));}s[i] = '\0';if (c != EOF)unGetch(c);return NUMBER;}#define BUFSIZE 100int buf[BUFSIZE];int bufp = 0;int getch(void){return (bufp > 0) ? buf[--bufp] : getchar();}void unGetch(int c){if (bufp >= BUFSIZE)printf("\nUnGetch: too many characters\n");elsebuf[bufp++] = c;}Exercise 4-7Write a routine ungets(s) that will push back an entire string onto the input. Should ungets know about buf and bufp , or should it just use ungetch ?#include"pch.h"#include<string.h>#include<stdio.h>#define BUFSIZE 100char buf[BUFSIZE];int bufp = 0;int getch(void){return (bufp > 0) ? buf[--bufp] : getchar();}void ungetch(int c){if (bufp >= BUFSIZE)printf("ungetch: too many characters\n");elsebuf[bufp++] = c;}void ungets(const char *s){size_t i = strlen(s);while (i > 0)ungetch(s[--i]);}int main(void){char s="Hello";int c;ungets(s);while ((c = getch()) != EOF)putchar(c);return 0;}Exercise 4-8Suppose there will never be more than one character of pushback. Modify getch and ungetch accordingly.#include<stdio.h>#include"pch.h"int buf = EOF;#define EOF -1;int getch(void){int temp;if (buf != EOF){temp = buf;buf = EOF;}else {temp = getchar();}return temp;}void ungetch(int c){if (buf != EOF)printf("ungetch: too many characters\n");elsebuf = c;}int main(void){int c;while ((c = getch()) != EOF) {if (c == '/') {putchar(c);if ((c = getch()) == '*') {ungetch('!');}}putchar(c);}return 0;}Exercise 4-9Our getch and ungetch do not handle a pushed-back EOF correctly. Decide what their properties ought to be if an EOF is pushed back, and then implement your design.#include<stdio.h>#define BUFSIZE 100int buf[BUFSIZE];int bufp = 0;int getch(void){return (bufp > 0) ? buf[--bufp] : getchar();}void ungetch(int c){if (bufp >= BUFSIZE)printf("ungetch: too many characters \n");elsebuf[bufp++] = c;}Exercise 4-10#include<stdio.h>#include<ctype.h>#define MAXLINE 100#define NUMBER'0'int getline(char line[], int limit);int li = 0;char line[MAXLINE];int getop(char s[]){int c, i;if (line[li] == '\0')if (getline(line, MAXLINE) == 0)return EOF;elseli = 0;while ((s[0] = c = line[li++]) == ' ' || c == '\t');s[1] = '\0';if (!isdigit(c) && c != '.')return c;i = 0;if (isdigit(c))while (isdigit(s[++i] = c = line[li++]));if (c == '.')while (isdigit(s[++i] = c = line[li++]));s[i] = '\0';li--;return NUMBER;}Exercise 4-11Modify getop so that it doesn't need to use ungetch. Hint: use an internal static variable.int getop(char *s){int c;static int buf = EOF;if (buf == EOF || buf == ' ' || buf == '/t')while ((*s = c = getch()) == ' ' || c == '/t');else*s = c = buf;buf = EOF;*(s + 1) = '/0';if (!isdigit(c) && c != '.')return c; /* not a number */if (isdigit(c)) /* collect integer part */while (isdigit(*++s = c = getch()));if (c == '.') /* collect fraction part */while (isdigit(*++s = c = getch()));*++s = '/0';buf = c;return NUMBER;}Exercise 4-12Adapt the ideas of printd to write a recursive version of atoi ; that is, convert an integer into a string by calling a recursive routine. #include"pch.h"#include<iostream>#include<stdio.h>#include<stdlib.h>char *utoa(unsigned value, char *digits, int base){char *s, *p;s = "0123456789abcdefghijklmnopqrstuvwxyz";if (base == 0)base = 10;if (digits == NULL || base < 2 || base > 36)return NULL;if (value < (unsigned)base) {digits[0] = s[value];digits[1] = '\0';}else {for (p = utoa(value / ((unsigned)base), digits, base);*p;p++);utoa(value % ((unsigned)base), p, base);}return digits;}char *itoa(int value, char *digits, int base){char *d;unsigned u;d = digits;if (base == 0)base = 10;if (digits == NULL || base < 2 || base > 36)return NULL;if (value < 0) {*d++ = '-';u = -value;}elseu = value;utoa(u, d, base);return digits;}Exercise 4-13Write a recursive version of the function reverse(s) , which reverses the string s in place.#include"pch.h"#include<iostream>#include<stdio.h>#include<stdlib.h>static void swap(char *a, char *b, size_t n){while (n--) {*a ^= *b;*b ^= *a;*a ^= *b;a++;b++;}}void my_memrev(char *s, size_t n){switch (n) {case 0:case 1:break;case 2:case 3:swap(s, s + n - 1, 1);break;default:my_memrev(s, n / 2);my_memrev(s + ((n + 1) / 2), n / 2);swap(s, s + ((n + 1) / 2), n / 2);break;}}void reverse(char *s){char *p;for (p = s; *p; p++);my_memrev(s, (size_t)(p - s));}。
c 程序设计(第2版)课后习题答案C 程序设计(第2版)是一本经典的计算机编程教材,被广泛应用于计算机科学与技术专业的教学中。
该教材的每一章都附带了一系列的习题,这些习题在巩固学生对于 C 语言的理解和掌握方面起到了重要的作用。
然而,很多学生在自学或者课后复习的过程中常常会遇到习题解答的困扰。
因此,本文将为大家提供一些 C 程序设计(第2版)课后习题的答案,希望能够对大家的学习有所帮助。
第一章:C 程序设计概述1.1 习题解答:1) C 程序设计的特点包括高效性、可移植性、灵活性和可扩展性。
2) C 程序设计的基本结构包括预处理指令、函数定义和主函数。
3) C 程序设计的编译过程包括预处理、编译、汇编和链接四个阶段。
第二章:C 语言基础2.1 习题解答:1) C 语言中的标识符是指用于标识变量、函数、数组等各种程序实体的名称。
标识符的命名规则包括:只能由字母、数字和下划线组成,且不能以数字开头;不能使用 C 语言的关键字作为标识符;标识符区分大小写。
2) C 语言中的常量是指在程序中直接使用的固定值,包括整型常量、实型常量、字符常量和字符串常量。
3) C 语言中的变量是指用于存储数据的内存空间,包括整型变量、实型变量、字符变量和字符串变量。
第三章:运算符和表达式3.1 习题解答:1) C 语言中的算术运算符包括加法运算符(+)、减法运算符(-)、乘法运算符(*)、除法运算符(/)和取模运算符(%)等。
2) C 语言中的关系运算符包括大于运算符(>)、小于运算符(<)、等于运算符(==)、不等于运算符(!=)等。
3) C 语言中的逻辑运算符包括与运算符(&&)、或运算符(||)和非运算符(!)等。
第四章:选择结构4.1 习题解答:1) C 语言中的 if 语句用于实现单一条件的判断,格式为:if (条件表达式) {语句块}。
2) C 语言中的 if-else 语句用于实现两个条件的判断,格式为:if (条件表达式) {语句块1} else {语句块2}。
本文由xjsir2010贡献 ppt文档可能在WAP端浏览体验不佳。
建议您优先选择TXT,或下载源文件到本机查看。
第4章 选择结构程序设计 章 第4章 选择结构程序设计 章 4.1 关系运算符及其表达式 4.2 逻辑运算符及其表达式 4.3 条件运算符 4.4 单条件选择if语句 单条件选择 语句 4.5 开关分支 开关分支switch语句 语句 习题4 第4章 选择结构程序设计 章 4.1 关系运算符及其表达式 关系表达式是由关系运算符连接表达式构成的。
1. 关系运算符 1) 关系运算符 关系运算符都是双目运算符,共有如下6种: >,<,>=,<=,==,!= 分别是大于、小于、大于或等于、小于或等于、 等于和不等于。
第4章 选择结构程序设计 章 2) 运算符的优先级和结合性 前4种关系运算符的优先级别相同,后2种也相同,前4 种高于后2种。
关系运算符具有自左至右的结合性。
关系运算符、算术运算符和赋值运算符之间的优先级次序 为:算术运算符优先级最高,关系运算符次之,赋值运算 符最低。
2. 关系表达式 1) 关系表达式 由关系运算符组成的表达式称为关系表达式。
关系运算符两边的运算对象可以是C语言中任意合法 的表达式。
例如,x>y,(x=5)<=y,x==y等。
第4章 选择结构程序设计 章 2) 关系表达式的值 关系表达式的值是整数0(代表结果为逻辑假)或 1(代表结果为逻辑真),在C语言中不存在专门的“逻 辑值”,此处请读者务必清楚。
例如,关系表达式 (x=3)>(y=4)的值为0。
关系表达式常用在条件语句和循环语句中。
3) 举例 一般情况下,关系表达式用在选择结构或循环结 构的关系判断中,并不需要输出关系表达式的值,这 里我们为了了解关系表达式的内部运行机制,在例4-1 中可以看到关系表达式的值只能是0或1。
第4章 选择结构程序设计 章 【例4-1】关系表达式示例。
#include "stdio.h" main( ) { char x='m' , y='n' ; int n ; n=x<y ; printf("%d\n" , n) ; n=x==y-1 ; printf("%d\n" , n) ; n=('y'!='Y')+(5<3)+(y-x==1) ; printf("%d\n" , n) ; } 第4章 选择结构程序设计 章 运行结果: 1 1 2 通过上面的程序可以看出:关系运算的结果为 “真”时,值等于1;结果为“假”时,值等于0。
4.3 课后习题4.3.1 项目练习一.练习目的1.进一步巩固选择结构程序设计语句的使用2.进一步巩固break语句的使用方法3.提高编程和调试程序的能力二.练习内容1.接受用户输入的三种商品的价格。
如果购买的三种商品中至少有一种商品的价格大于50 或者三种商品的总额大于100,则折扣率为15%,否则折扣率为0,计算并显示用户应付的钱数。
#include "stdio.h"main(){float a,b,c,price;scanf("%f%f%f",&a,&b,&c);price=a+b+c;if((a>50)||(b>50)||(c>50)||(price>100))price=price*0.85;printf("%.2f",price);getch();}2.判断所输入的一个年份是否为闰年。
#include "stdio.h"main(){int year,flag=0;scanf("%d",&year);if((year%4==0)&&(year%100!=0)||(year%400==0))flag=1;if(flag==1)printf("%d年是闰年!",year);elseprintf("%d年不是闰年!",year);getch();}3.利用if结构编写程序,输入x值,求解以下分段函数的y值。
当x<1时,y=x+1;当1≤x<10,y=2x+5;当x≥10时,y=x2+8。
#include "stdio.h"main(){int x,y;scanf("%d",&x);if(x<1) y=x+1;else if(x>=10) y=x*x+8;else y=2*x+5;printf("%d",y);getch();}4.编写一个程序,根据用户输入的期末考试成绩,输出相应的成绩评定信息。
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。