ch07C控制语句-分支和跳转_第1页
ch07C控制语句-分支和跳转_第2页
ch07C控制语句-分支和跳转_第3页
ch07C控制语句-分支和跳转_第4页
ch07C控制语句-分支和跳转_第5页
已阅读5页,还剩104页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、v/*colddays.c - finds percentage of days below freezing*/v#include vint main(void)vv const int FREEZING = 0;v float temperature;v int cold_days = 0;v int all_days = 0; v printf(Enter the list of daily low temperatures.n);v printf(Use Celsius, and enter q to quit.n);v while (scanf(%f, &temperature) =

2、 1)v v all_days+;v if (temperature FREEZING)v cold_days+;v v if (all_days != 0)v printf(%d days total: %.1f% were below freezing.n,v all_days, 100.0 * (float) cold_days / all_days);v if (all_days = 0)v printf(No data entered!n);v v return 0;vvexpression:任何表达式,常被称为测试任何表达式,常被称为测试表达式。只有该表达式成立时,才执行语表达式。

3、只有该表达式成立时,才执行语句,且仅执行一次句,且仅执行一次vstatement:可以是单条语句可以是单条语句(以(以;结束)结束)v 可以是复合语句可以是复合语句(用(用包围)包围)vif (all_days!= 0)v printf(%d days total: %.1f% were below freezing.n,v all_days, 100.0 * (float) cold_days / all_days);velsev printf(No data entered!n);v/* electric.c - calculates electric bill */v#include v

4、#define RATE1 0.12589 /* rate for first 360 kwh */v#define RATE2 0.17901 /* rate for next 320 kwh */v#define RATE3 0.20971 /* rate for over 680 kwh */v#define BREAK1 360.0 /* first breakpoint for rates */v#define BREAK2 680.0 /* second breakpoint for rates */v#define BASE1 (RATE1 * BREAK1)v /* cost

5、for 360 kwh */v#define BASE2 (BASE1 + (RATE2 * (BREAK2 - BREAK1)v /* cost for 680 kwh */vint main(void)vv double kwh; /kilowatt-hours usedv double bill; /chargesv printf(Please enter the kwh used.n);v scanf(%lf, &kwh); / %lf - type doublev if (kwh = BREAK1)v bill = RATE1 * kwh;v else if (kwh 6)v if

6、(number 12)v printf(Youre close!n);velsev printf(Sorry, you lose a turn!n);例例 输入输入1个整数,判断该数是奇数个整数,判断该数是奇数 还是偶数。还是偶数。number % 2 = 0#include int main(void) int number; printf(Enter a number: ); scanf(%d, &number); if(number % 2 = 0) printf(Tne number is even. n); else printf(Tne number is odd. n); ret

7、urn 0;Enter a number: 329 Tne number is odd.Enter a number: 1028Tne number is even.例例 输入输入1个整数,输出它的绝对值个整数,输出它的绝对值 。当当number = 0时,时,?#include int main(void) int number; printf(Enter a number: ); scanf(%d, &number); if(number 0) number = -number; printf(The absolute value is %d.n, number); return 0;En

8、ter a number: 10 The absolute value is 10.Enter a number: -300 The absolute value is 300.v给定一个整数,显示所有能整除它的给定一个整数,显示所有能整除它的约数;如果没有约数,则报告该数是约数;如果没有约数,则报告该数是个素数个素数for (div = 2; div num; div+) if (num % div = 0) printf(%d is divisible by %dn, num, div);vfor (div = 2; (div * div) = num; div+)v if (num %

9、div = 0)v printf(%d is divisible by %d and %d.n,num, div, num / div);vfor (div = 2; (div * div) = num; div+)vv if (num % div = 0)v v if (div * div != num)v printf(%d is divisible by %d and %d.n,num, div, num / div);v elsev printf(%d is divisible by %d.n, num, div);v vv是素数,能否进是素数,能否进if语句语句vif (num %

10、div = 0)v设标志设标志(flag)v/* divisors.c - nested ifs display divisors of a number */v#include v#include vint main(void)vv unsigned long num; / number to be checked v unsigned long div; / potential divisors v bool isPrime; / prime flagv printf(Please enter an integer for analysis; );v printf(Enter q to q

11、uit.n);vwhile (scanf(%lu, &num) = 1)vv for (div = 2, isPrime= true; (div * div) = num; div+)v v if (num % div = 0)v v if (div * div) != num)v printf(%lu is divisible by %lu and %lu.n,v num, div, num / div);v elsev printf(%lu is divisible by %lu.n,v num, div);v isPrime= false; / number is not prime v

12、 v v if (isPrime)v printf(%lu is prime.n, num);v printf(Please enter another integer for analysis; );v printf(Enter q to quit.n);v /whilev printf(Bye.n);v v return 0;v /main()v将将1判断为素数判断为素数v引入逻辑运算符(先介绍字符输入输出)引入逻辑运算符(先介绍字符输入输出)v字符输入输出的函数字符输入输出的函数,参考附录,参考附录stdio.h(P598)vgetchar():返回来自输入设备的下一返回来自输入设备的下

13、一个字符个字符v函数原型:函数原型:int getchar();v 例子:例子:ch = getchar();v相当于:相当于:scanf(“%c”,&ch);vputchar():把指定的字符写到标准输把指定的字符写到标准输出中出中v函数原型:函数原型:int putchar(int);v例子:例子:putchar(ch);v相当于:相当于:printf(“%c”,ch);v与与scanf()和和printf()比较:只处理字比较:只处理字符,速度更快符,速度更快v程序功能:再现输入的一行。如果字程序功能:再现输入的一行。如果字符是空格,则打印之;否则打印它在符是空格,则打印之;否则打印它在

14、ASCII序列中的下一个字符序列中的下一个字符v/* cypher1.c - alters input, preserving spaces */v#include v#define SPACE / its quote-space-quote vint main(void)vv char ch;v ch = getchar(); / read a characterv while (ch != n) / while not end of line v v if (ch = SPACE) /leave the space v putchar(ch); / character unchanged

15、v elsev putchar(ch + 1); / change the characterv ch = getchar(); /get next character v v putchar(ch); / print the newlinev v return 0;vv循环开始前,循环结束前都有getchar()v详细介绍执行过程v修改上述程序,只改变字母,保留所修改上述程序,只改变字母,保留所有的非字母(空格、句点等)有的非字母(空格、句点等)v int isalpha(int c); 如果如果c是字母则返是字母则返回真,否则返回假回真,否则返回假v/* cypher2.c - alter

16、s input, preserving non-letters*/v#include v#include / for isalpha()vint main(void)vv char ch;v while (ch = getchar() != n)v v if (isalpha(ch) / if a letter,v putchar(ch + 1); / change itv else / otherwise,v putchar(ch); / print as isv v putchar(ch); / print the newlinev v return 0;vv引入目的:多个条件的组合引入目

17、的:多个条件的组合v示例:下页示例:下页v/*chcount.c - use the logical AND operator*/v/计算除单引号和双引号外的字符的个数计算除单引号和双引号外的字符的个数v#include v#define PERIOD .vint main(void)vv int ch;v int charcount = 0;v while (ch = getchar() != PERIOD)v v if (ch != & ch != )v charcount+;v v printf(There are %d non-quote v characters.n, charcou

18、nt);v return 0;v表达式值30 12 & 3 530 12 | 3 5!(3 5)-!0 * 3!3 50 (false)1 (true)0 (false)1 (true)-3注意:短路问题v, : 逗号左侧子表达式的副作用发生在逗逗号左侧子表达式的副作用发生在逗号右侧的子表达式被计算之前号右侧的子表达式被计算之前vint weight = 4;vconst int UNIT_PRICE = 2;vweight+, cost = weight * UNIT_PRICE;vcost = ?cost的值为10v& | :前一个操作数(即表达式)的副前一个操作数(即表达式)的副作用发生

19、在后一个操作数(即表达式)被作用发生在后一个操作数(即表达式)被计算之前计算之前v一旦确定整个表达式的值时,就停止计算一旦确定整个表达式的值时,就停止计算(短路)(短路)v示例:见下页示例:见下页vwhile (c = getchar() != & c != n)vif (number != 0 & 12/number = 2)v printf(“The number is 5 or 6.n”);vwhile (x+ 10 & x+y 20)v统计输入的字符数、行数与单词数统计输入的字符数、行数与单词数v实现过程:逐个读取字符,并能停止读取;实现过程:逐个读取字符,并能停止读取;能够识别字符、

20、行、单词能够识别字符、行、单词vwhile (ch = getchar() != STOP)v /STOP 取字符取字符|v .vv统计字符:统计字符:字符计数器,每个循环周字符计数器,每个循环周期递增期递增v统计行数:统计行数:行计数器,遇到换行符时行计数器,遇到换行符时递增(约定:在某行中遇到字符递增(约定:在某行中遇到字符|时,将该行看作不完整行。即:在时,将该行看作不完整行。即:在STOP前读入的最后一个字符不是换前读入的最后一个字符不是换行符,则将该行看做不完整行)行符,则将该行看做不完整行)v统计单词数:统计单词数:单词计数器,不包含空单词计数器,不包含空白字符的一系列字符,即:一

21、个单词白字符的一系列字符,即:一个单词以首次遇到非空白字符开始,在下一以首次遇到非空白字符开始,在下一个空白字符出现时结束个空白字符出现时结束v问题:问题:v1、如何判断非空白字符与空白字、如何判断非空白字符与空白字符?符?v2、如何标记一个字符是否在某个单、如何标记一个字符是否在某个单词里?词里?vc != & c != n & c != t /* true if c is not whitespace */vc = | c = n | c = t /* true if c is whitespace */ vP586vint isspace(int c);如果如果c是空白字符,则是空白字符

22、,则返回真,否则返回假返回真,否则返回假v标志标志inword:读入单词的首字符时将:读入单词的首字符时将inword置置为为1,在此处递增单词数。当,在此处递增单词数。当inword保持为保持为1(或真)(或真)时,后续的非空白字符就不标记为一个时,后续的非空白字符就不标记为一个单词的开始。直到出现一个空白字符时,将标志单词的开始。直到出现一个空白字符时,将标志置为置为0(或假)(或假)vif (inword) -推荐使用推荐使用vif (inword = true)vif (!inword) -推荐使用推荐使用vif (inword = false)v/*wordcnt.c - count

23、s characters, words, lines*/v#include v#include / for isspace() v#include /* for bool, true, false*/v#define STOP |v vint main(void)vv char c; / read in character v char prev; / previous character readv long n_chars = 0L; / number of characters v int n_lines = 0; / number of lines v int n_words = 0;

24、 / number of words v int p_lines = 0; / number of partial lines v bool inword = false; / = true if c is in a word v printf(Enter text to be analyzed (| to v terminate):n);v prev = n; / used to identify complete linesv while (c = getchar() != STOP)v v n_chars+; / count charactersv if (c = n)v n_lines

25、+; / count linesv . /见下页v v if (!isspace(c) & !inword)v v inword = true; / starting a new wordv n_words+; / count wordv v if (isspace(c) & inword)v inword = false; / reached end of wordv prev = c; / save character value v if (prev != n)v p_lines = 1;v printf(characters = %ld, words = %d, v lines = %

26、d, ,v n_chars, n_words, n_lines);v printf(partial lines = %dn, p_lines);v v return 0;v /main()v一般形式:v示例:v用于跳过循环的一部分或者终止循环用于跳过循环的一部分或者终止循环u用于用于for、while、do-while循环循环ubreak,退出循环退出循环ucontinue,中断此次循环体的执行,开,中断此次循环体的执行,开始下一次始下一次ubreak和和continue用于嵌套循环的内层用于嵌套循环的内层循环时,只影响到最内层循环循环时,只影响到最内层循环vwhile与与do while循环

27、中,循环中,continue语句的下一条语句是语句的下一条语句是循环测试条件循环测试条件vfor循环中,循环中,continue语句的下一条语句的下一条语句是语句是循环更新表达式循环更新表达式读取10个字符,不包括换行符,并输出除换行符外的所有字符读取10个字符,包括换行符,并输出除换行符外的所有字符vint p, q;vscanf(%d, &p);vwhile ( p 0)vv printf(%dn, p);v scanf(%d, &q);v while( q 0)v v printf(%dn,p*q);v if (q 100)v break; / break from inner loop

28、v scanf(%d, &q);v v if (q 100)v break; / break from outer loopv scanf(%d, &p); vv/* animals.c 输入一个小写字母,输出某种动物 */v#include v#include vint main(void)vv char ch;v printf(Give me a letter of the alphabet, v and I will give );v printf(an animal namenbeginning with v that letter.n);v printf(Please type in

29、 a letter; type # to v end my act.n);v while (ch = getchar() != #)v v if(n = ch)v continue;v if (islower(ch) / lowercase onlyv switch (ch)v v case a :v printf(argali, a wild sheep of Asian”); break;v case b :v printf(babirusa, a wild pig of Malayn”); break;v case c :v printf(coati, racoonlike mammal

30、n”); break;v v case d :v printf(desman, aquatic, molelike v crittern);v break;v case e :v printf(echidna, the spiny v anteatern);v break;v case f :v printf(fisher, brownish martenn);v break;v default :v printf(Thats a stumper!n);v /* end of switch */v elsev printf(I recognize only lowercase v letter

31、s.n);v while (getchar() != n)v continue; /* skip rest of input line */v printf(Please type another letter or a #.n);v /* while loop end */v printf(Bye!n);v v return 0;v /main()v圆括号中的圆括号中的integer expression:该表该表达式必须具有整数值(包含达式必须具有整数值(包含char)vcase标签:标签:必须是整型(包含必须是整型(包含char)常量常量或者整数或者整数常量常量表达式(仅包含整表达式(仅

32、包含整数常量的表达式)。数常量的表达式)。不能是变量!不能是变量!v/* vowels.c :计算元音字母的个数*/v#include vint main(void)vv char ch;v int a_ct, e_ct, i_ct, o_ct, u_ct;v a_ct = e_ct = i_ct = o_ct = u_ct = 0;v printf(Enter some text; enter # to quit.n);v while (ch = getchar() != #)v v switch (ch)v v case a :v case A : a_ct+;v break;v case

33、 e :v case E : e_ct+;v break;v case i :v case I : i_ct+;v break; v case o :v case O : o_ct+;v break;v case u :v case U : u_ct+;v break;v default : break;v /* end of switch */v /* while loop end */v printf(number of vowels: A E I v O Un);v printf( %4d %4d %4d %4d v %4dn,v a_ct, e_ct, i_ct, o_ct, u_ct

34、);v v return 0;v /main()例例 输入一个形式如输入一个形式如“操作数操作数 运算符运算符 操作数操作数”的四则运算表达式,输出运算结果。的四则运算表达式,输出运算结果。例如:例如:输入:输入:3.1+4.8 输出:输出:7.9# include int main(void) char operator; double value1, value2; printf(Type in an expression: ); scanf(%lf%c%lf, &value1, &operator, &value2); switch(operator) case +: printf(=%

35、.2fn, value1 + value2); break; case -: printf(=%.2fn, value1 - value2); break; case *: printf(=%.2fn, value1 * value2); break; case /: printf(=%.2fn, value1 / value2); break; default: printf(Unknown operatorn); break; return 0;Type in an expression: 3.1+4.8 =7.9如果除数为如果除数为0?# include int main(void) char operator; double value1, value2; printf(Type i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论