有书共读22:C Primer Plus
知识点整理:
1)break语句:终止Loop或switch语句,程序流将继续执行紧接着 loop 或 switch 的下一条语句。
2)continue语句:引起循环跳过主体的剩余部分,立即重新开始测试条件。所有的循环都可以使用continue语句,但是switch不行。
3)goto语句:将控制转移到被标记的语句。但是不建议在程序中使用 goto 语句。
4)一个if语句由一个布尔表达式后跟一个或多个语句组成。
if(boolean_expression)
{
/* 如果布尔表达式为真将执行的语句 */
}
如果布尔表达式为true,则 if 语句内的代码块将被执行。如果布尔表达式为false,则 if 语句结束后的第一组代码(闭括号后)将被执行。
C 语言把任何非零和非空的值假定为true,把零或null假定为false。
5)if语句:一个if语句后可跟一个可选的else 语句,else 语句在布尔表达式为 false 时执行。
if(boolean_expression)
{
/* 如果布尔表达式为真将执行的语句 */
}
else
{
/* 如果布尔表达式为假将执行的语句 */
}
如果布尔表达式为true,则执行if块内的代码。如果布尔表达式为false,则执行else块内的代码。
6) if-else 语句:在 C 语言中,嵌套if-else 语句是合法的,这意味着您可以在一个if或else if语句内使用另一个if或else if语句。
if( boolean_expression 1)
{
/* 当布尔表达式 1 为真时执行 */
if(boolean_expression 2)
{
/* 当布尔表达式 2 为真时执行 */
}
}
注意:如果要在if和else之间执行多条语句,必须用花括号把这些语句括起来成为一个块。下面的代码结构就违反了C语法:
if(x>0)
printf(“incrementing x:\n”);
x++;
else
printf(“x<=0\n”);
编译器会把printf()语句视为if语句的一部分,而把X++;看做一条单独语句,他不是if语句的一部分。
7) switch语句:一个switch语句允许测试一个变量等于多个值时的情况。每个值称为一个 case,且被测试的变量会对每个switch case进行检查。
switch(expression){
case constant-expression :
statement(s);
break; /* 可选的 */
case constant-expression :
statement(s);
break; /* 可选的 */
/* 您可以有任意数量的 case 语句 */
default : /* 可选的 */
statement(s);
}
switch语句必须遵循下面的规则:
· switch语句中的expression必须是一个整型或枚举类型,或者是一个 class 类型,其中 class 有一个单一的转换函数将其转换为整型或枚举类型。
· 在一个 switch 中可以有任意数量的 case 语句。每个 case 后跟一个要比较的值和一个冒号。
· case 的constant-expression必须与 switch 中的变量具有相同的数据类型,且必须是一个常量或字面量。
· 当被测试的变量等于 case 中的常量时,case 后跟的语句将被执行,直到遇到break语句为止。
· 当遇到break语句时,switch 终止,控制流将跳转到 switch 语句后的下一行。
· 不是每一个 case 都需要包含break。如果 case 语句不包含break,控制流将会继续后续的 case,直到遇到 break 为止。
· 一个switch语句可以有一个可选的default case,出现在 switch 的结尾。default case 可用于在上面所有 case 都不为真时执行一个任务。default case 中的break语句不是必需的。
8)嵌套switch语句:可以把一个switch作为一个外部switch的语句序列的一部分,即可以在一个switch语句内使用另一个switch语句。即使内部和外部 switch 的 case 常量包含共同的值,也没有矛盾。
switch(ch1) {
case 'A':
printf("这个 A 是外部 switch 的一部分" );
switch(ch2) {
case 'A':
printf("这个 A 是内部 switch 的一部分" );
break;
case 'B': /* 内部 B case 代码 */
}
break;
case 'B': /* 外部 B case 代码 */
}
8)字符输入/输出函数:getchar()和putchar(),不需要转换说明。
9)如果程序中包含iso646.h头文件,可适使用and代替&&、or代替||、not代替!(逻辑运算符的优先级比关系运算符低)。
10)检测一个单词是否结,使用ctype.h头文件中的函数isspace()更简单。
11)switch和if else语句的选择:如果是根据浮点类型的变量或表达式来选择,就无法使用switch。
编程练习:
1)7-8
#include<stdio.h> #define RATE1 0.001 #define RATE2 0.002 #define RATE3 0.003 #define TAX "total_income * RATE(X)" #include <ctype.h> int main(void) { int chooice = 0; double work_hours = 0; double total_income = 0; double tax = 0; double net_income = 0; double hourly_wage = 0; while (1) { printf("Enter the number corresponding to the desired pay rate to action:\n"); printf("%-20s%-20s\n%-20s%-20s\n%-20s\n", "1) $8.75/hr", "2) $9.33/hr", "3) $10.00/hr", "4) $11.20/hr", "5) quit"); scanf("%d", &chooice); switch (chooice) { case 1: hourly_wage = 8.75; break; case 2: hourly_wage = 9.33; break; case 3: hourly_wage = 10.00; break; case 4: hourly_wage = 11.20; break; case 5: printf("quit\n"); break; default: printf("Please enter the choice between 1 to 5\n"); continue; } printf("How long have you worked:"); scanf("%lf", &work_hours); if (work_hours > 40) { work_hours = (work_hours - 40) * 1.5 + 40; } total_income = work_hours * hourly_wage; if (total_income <= 300) { tax = total_income * RATE1; net_income = total_income - tax; } else if (300 < total_income && total_income <= 450) { tax = 300 * RATE1 + (total_income - 300) * RATE2; net_income = total_income - tax; } else { tax = 300 * RATE1 + 150 * RATE2 + (total_income - 450) * RATE3; net_income = total_income - tax; } printf("total income = %.2lf, tax = %.2lf, net income = %.2lf\n", total_income, tax, net_income); } return 0; }
2)7-11
#include<stdio.h> #include <ctype.h> int main(void) { double n_artichoke = 0.0; double n_beet = 0.0; double n_carrot = 0.0; double freight = 0.0; double n_pound = 0.0; char choice = 0; double total_cost = 0.0; double discount = 0.0; double total_weight = 0.0; while ('q' != choice) { printf("Please choose the item you want to buy:\n"); printf("%-20s%-20s\n%-20s%-20s\n", "a) Artichoke", "b) Beet", "c) Carrot", "q) Quit"); printf("Now enter you choose:"); choice = getchar(); switch (choice) { case 'a': printf("How many pounds of Artichoke do you want to buy:"); scanf("%lf", &n_pound); n_artichoke += n_pound; break; case 'b': printf("How many pounds of Beet do you want to buy:"); scanf("%lf", &n_pound); n_beet += n_pound; break; case 'c': printf("How many pounds of Carrot do you want to buy:"); scanf("%lf", &n_pound); n_carrot += n_pound; break; case 'q': continue; default: printf("Your choice is invalid! Please choose again.\n"); break; } while (getchar() != '\n'); } printf("%-20s%-20s%-20s%-20s\n", "Category", "Price", "Pounds", "Total price"); printf("%-20s%-20s%-20.2lf%-20.2lf\n", "Artichoke", "$2.05/pound", n_artichoke, (n_artichoke * 2.05)); printf("%-20s%-20s%-20.2lf%-20.2lf\n", "Beet", "$1.15/pound", n_beet, (n_beet * 1.15)); printf("%-20s%-20s%-20.2lf%-20.2lf\n", "Carrot", "$1.09/pound", n_carrot, (n_carrot * 1.09)); total_cost = n_artichoke * 2.05 + n_beet * 1.15 + n_carrot * 1.09; printf("Total_cost:%.2lf", total_cost); if (total_cost > 100) { discount = total_cost * 0.05; printf(" Discount:%,2lf", discount); total_cost -= discount; } total_weight = n_artichoke + n_beet + n_carrot; if (0 < total_weight && total_weight <= 5.0) { freight = 6.5; } else if (5.0 < total_weight && total_weight <= 20) { freight = 14.0; } else if (20 < total_weight ) { freight = 14 + (total_weight - 20) * 0.5; } printf(" Total_freight:%.2lf", freight); printf(" Final cost:%.2lf", (total_cost + freight)); return 0; }