part 1
一、if 语句
1 、if 语句的格式:
if ( expression )
statement
2 、if -else 语句的格式
if (expression)
statement1
else
statement2
二、对于字符的输入和输出函数
1、getchar () 函数不带任何参数,它从输入队列中返回下一个字符。
e.g.ch=getchar() 等价于 scanf("%c",&ch)
2、putchar ()函数打印它的参数。
e.g. putchar(ch) 等价于 printf("%c",ch)
注:这两个函数通常定义在stdio.h的头文件中,是预处理宏。
3、ctype.h 头文件中的一些函数
函数名 | 若是下列参数时,返回值为真 |
---|---|
isalnum() | 字母数字 |
isalpha() | 字母 |
isblank | 标准的空白字符 |
iscntrl () | 控制字符,如 Ctrl +B |
isdigit () | 数字 |
isgraph () | 除空格之外的任意可打印字符 |
islower () | 小写字母 |
isprint () | 可打印字符 |
ispunct () | 标点符号(除空格或字母数字字符以外的任何可打印字符) |
isspace () | 空白字符 |
isupper () | 大写字母 |
isxdigit () | 十六进制数字符 |
函数名 | 行为 |
---|---|
tolower() | 如果参数是大写字符,该函数返回小写字符;否则,返回原始参数 |
toupper() | 如果参数是小写字符,该函数返回大写字符;否则,返回原始参数 |
三、多重选择(else if)
例子:计算电费小程序
// electric.c -- calculates electric bill
#include <stdio.h>
#define RATE1 0.13230 // rate for first 360 kwh
#define RATE2 0.15040 // rate for next 108 kwh
#define RATE3 0.30025 // rate for next 252 kwh
#define RATE4 0.34025 // rate for over 720 kwh
#define BREAK1 360.0 // first breakpoint for rates
#define BREAK2 468.0 // second breakpoint for rates
#define BREAK3 720.0 // third breakpoint for rates
#define BASE1 (RATE1 * BREAK1)
// cost for 360 kwh
#define BASE2 (BASE1 + (RATE2 * (BREAK2 - BREAK1)))
// cost for 468 kwh
#define BASE3 (BASE1 + BASE2 + (RATE3 *(BREAK3 - BREAK2)))
//cost for 720 kwh
int main(void)
{
double kwh; // kilowatt-hours used
double bill; // charges
printf("Please enter the kwh used.\n");
scanf("%lf", &kwh); // %lf for type double
if (kwh <= BREAK1)
bill = RATE1 * kwh;
else if (kwh <= BREAK2) // kwh between 360 and 468
bill = BASE1 + (RATE2 * (kwh - BREAK1));
else if (kwh <= BREAK3) // kwh betweent 468 and 720
bill = BASE2 + (RATE3 * (kwh - BREAK2));
else // kwh above 680
bill = BASE3 + (RATE4 * (kwh - BREAK3));
printf("The charge for %.1f kwh is $%1.2f.\n", kwh, bill);
return 0;
}
注:else与最近的if 匹配。
四、逻辑运算符
逻辑运算符 | 含义 |
---|---|
&& | 与 |
|| | 或 |
! | 非 |
注:逻辑运算符两侧的条件必须都为真,整个表达式才为真。
逻辑运算符的优先级比关系运算符的优先级低,所以不必在子表达式两侧加圆括号。
e.g.
#include <stdio.h>
#define PERIOD '.'
int main(void)
{
char ch;
int charcount = 0;
while ((ch = getchar()) != PERIOD)
{
if (ch != '"' && ch != '\'')
charcount++;
}
printf("There are %d non-quote characters.\n", charcount);
return 0;
}
其他形式:用iso646.h头文件
只要有 #include <iso646.h> ,就可以用 and 代替 &&;用or 代替 ||;用not 代替 !。
注意:
C语言 if (range>=90 && range<=100);
不能用Python里的 if(90<=range<=100)
五、条件运算符:?
e.g. x = (y<0) ? -y:y; 等价于 if (y<0) x=-y; else x = y;
条件表达式的通用形式如下:
(expression1) ? expression2 : expression3
典例:max = ( a>b)? a : b ;
解释:如果a>b,则把max设置为a,否则,将max设置为b。
例子:买防水浆
#include <stdio.h>
#define SQCOVER 300 //一桶防水浆可以涂的面积
int main(void)
{
int cans;
int sqreal;
while (scanf("%d",&sqreal) == 1)
{
cans=sqreal/SQCOVER;
cans+ (sqreal%SQCOVER==0) ? 0:1;
printf("You need %d %s of paint.\n",cans,cans ==1 ? "can" : "cans");
printf("Enter next value (q to quit):\n");
}
return 0;
}
六、多重选择(switch-case语句 和 break语句)
首先对switch 语句后面的圆括号求值。然后寻找对应的case,跳转到该case语句。
如果没有与之匹配的case语句,就跳转至default语句,否则,程序继续执行在switch后面的语句。
一般case后要有break。
注:switch在圆括号中的测试表达式是整数(包括char类型)。case标签必须是整数类型(包括char类型)的常量或整型常量表达式(即:表达式中只包含整型常量)。不能用变量作为case标签。switch的构造如下:
switch (整型表达式)
{
case 常量1:
语句
case 常量2:
语句
default:
语句
}