百度做的网站能优化吗,公司管理app有哪些,网站原型怎么做,福田祥菱学习笔记——《C Prime Plus》 第7章 C控制语句#xff1a;分支和跳转7.1 if 语句7.2 if else 语句7.2.1 介绍 getchar() 和 putchar()7.4 一个统计单词的程序7.1 if 语句
下程序读取一列数据#xff0c;每个数据都表示每日的最低温度#xff08;℃#xff09;#xff0c…学习笔记——《C Prime Plus》
第7章 C控制语句分支和跳转7.1 if 语句7.2 if else 语句7.2.1 介绍 getchar() 和 putchar()7.4 一个统计单词的程序7.1 if 语句
下程序读取一列数据每个数据都表示每日的最低温度℃然后打印统计的总天数和最低温度在 0℃ 以下的天数占总天数的百分比。程序中的循环通过 scanf() 读入温度值。while 循环每迭代一次就递增计数器增加天数其中的 if 语句负责判断 0℃ 以下的温度并单独统计相应的天数。
#include stdio.h
int main(void)
{const int FREEZING 0;float temperature;int cold_days 0;int all_days 0;printf(Enter the list of daily low temperatures.\n);printf(Use Celsius, and enter q to quit.\n);while(scanf(%f, temperature) 1){all_days;if(temperature FREEZING)cold_days;}if (all_days ! 0)printf(%d days total: %.1f%% were below freezing.\n,all_days, 100.0 * (float) cold_days / all_days);if (all_days 0)printf(No data entered!\n);return 0;
}运行结果 程序分析
while 循环的测试条件利用 scanf() 的返回值来结束循环因为 scanf() 在读到非数字字符时会返回 0。temperature 的类型是 float 而不是 int 这样的程序既可以接受 -2.5 这样 的值也可以接受 8 这样的值。循环中 if 的语句如果刚读取的值temperature小于 0就把 cold_days 递增 1如果 temperature 不小于 0就跳过 cold_days; 语句while 循环继续读取下一个温度值。接着该程序又用了两次 if 语句控制程序的输出。如果有数据就打印结果如果没有数据就打印一条信息。为避免整数除法该程序实例把计算后的百分比强制转换为 float 类型。
if 语句被称为分支语句branching statement或选择语句selection statement 它相当于一个交叉点程序在两条分支中选择一条执行。对条件判断为真非 0则执行 statement否则跳过 statement。
7.2 if else 语句
简单形式的 if 语句可以让程序选择执行一条语句或者跳过这条语句。 C 还提供了 if else 形式可以在两条语句之间作选择。
if (all_days ! 0)printf(%d days total: %.1f%% were below freezing.\n,all_days, 100.0 * (float) cold_days / all_days);
elseprintf(No data entered!\n);//if else 语句的通用形式
if(expression)statement1
elsestatementf2如果 if 判断为真 非 0则执行 statement1如果 expression 为假或 0则执行 else 后面的 statement2。 如果要在 if 和 else 之间执行多条语句必须用花括号把这些语句括起来成为一个块。
7.2.1 介绍 getchar() 和 putchar()
对字符输入/输出函数getchar() 和 putchar() getchar() 函数不带任何参数它从输入队列中返回下一个字符。例如下面的语句读取下一个字符输入并把该字符的赋值给变量 ch:
ch getchar();
//等同于下面的语句
scanf(%c, ch);putchar() 函数打印它的参数。例如下面的语句把之前赋给 ch 的值作为字符打印出来
putchar(ch);
//等同于下面的语句
printf(%c, ch);由于这些函数只处理字符所以它们比更通用的 scanf() 和 printf() 函数更快、更简洁。而且注意 getchar() 和 putchar() 不需要转换说明因为它们只处理字符。这两个函数通常定义在 stdio.h 头文件。
7.4 一个统计单词的程序
编写一个统计单词数量的程序即该程序读取并报告单词的数量。该程序还可以计算字符数和行数。 首先该程序要逐个字符读取输入知道何时停止读取。然后该程序能识别并计算这些内容字符、行数和单词。 编写伪代码
读取一个字符
当有更多输入时递增字符计数如果读完一行递增行数计数如果读完一个单词递增单词计数读取下一个字符如循环输入模型
while((ch getchar()) ! STOP) //STOP表示能标识输入末尾的某个值。
{...
}#include stdio.h
#include ctype.h //为 isspace()函数提供原型
#include stdbool.h //为 bool、true、false 提供定义
#define STOP |
int main(void)
{char c; //读入字符 char prev; //读入的前一个字符long n_chars 0L; //字符数int n_lines 0; //行数 int n_words 0; //单词数 int p_lines 0; //不完整的行数 bool inword false; //如果 c 在 单词中inword 等于 trueprintf(Enter text to be analyzed (| to terminate):\n); prev \n; //用于识别完整的行while((c getchar()) ! STOP) {n_chars; //统计字符if(c \n)n_lines; //统计行if(!isspace(c) !inword) {inword true; //开始一个新的单词n_words; //统计单词 }if(isspace(c) inword)inword false; //达到单词的末尾prev c; }if (prev ! \n)p_lines 1;printf(characters %ld, words %d, lines %d,, n_chars, n_words, n_lines);printf(partial lines %d\n, p_lines);return 0;
}运行结果