n个分支需要n-1次比较
ch=getchar() 相当于从缓冲区scanf一个字符赋值给ch
Putchar(ch) 输出ch的值
输入8个字符,然后将这些字符输出,输出时在字符之间加一个减号,第一个字符的前面和最后一个字符的后面都没有减号。运行结果:
AMEHYST
A-M-E-T-H-Y-S-T
char ch;
int first = 1, k;
printf("Enter 8 characters: ");
for(k = 1; k <= 8; k++){
ch = getchar();
if ( first == 1 ){
putchar(ch);
first = 0;
}else{
putchar('-');
putchar(ch);
}
}
判断ch 是否为小写英文字母 (ch >= 'a') && (ch <= 'z') ch >= 'a' && ch <= 'z'
判断ch 是否为英文字母 (ch >= 'a' && ch <= 'z') || ( ch >= 'A' && ch <= 'Z')
判断ch是否为数字 ch >= '0' && ch <= '9'
Switch
Switch(表达式){
Case常量表达式:语句段;break;
同上
Default:语句段;break;
switch (choice) {
case 1: price = 3.0; break; /*choice=1时*/
case 2: price = 2.5; break;
case 3: price = 4.0; break;
case 4: price = 3.5; break;
default: price = 0.0; break; /*choice!=1,2,3,4时*/
}
switch (op){
case '+': printf ("=%d\n", value1 + value2); break; /*op=‘+’时*/
case '-': printf ("=%d\n", value1 - value2); break;
case '*': printf ("=%d\n", value1 * value2); break;
case '/':
if (value2 != 0) printf ("=%d\n", value1 / value2);
else printf ("Divisor can not be 0!\n");
break;
case '%':
if (value2 != 0) printf ("=%d\n", value1 % value2);
else printf ("Divisor can not be 0!\n");
break;
default: printf ("Unknown operator\n"); break;
}