C控制语句
C控制语句
default
、goto
- 更复杂的测试表达式中用逻辑运算符组合关系表达式
C
条件表达式switch
语句continue
、goto
语句I/O
函数getchar()
和putchar()
ctype.h
宏文件字符分析函数
if分支语句
示例代码:
/**
* @Author: Lucifer
* @Date: 4/29/2023, 10:38:10 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/29/2023, 10:38:10 PM
* Description: if branching statement
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# 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) // if 复合语句
{
cold_days++;
}
if (all_days != 0)
{
printf("%d days total: %.lf%% were below freezing.\n",
all_days, 100.0 * (float) cold_days / all_days);
}
if (all_days == 0)
printf("No data entered!\n"); // if简单语句
getchar();
return 0;
}
}
if...else
示例代码:
/**
* @Author: Lucifer
* @Date: 4/29/2023, 10:51:26 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/29/2023, 10:51:26 PM
* Description: if...else branching statements
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
int main(void)
{
const int FREEZING = 10;
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++;
} else {
printf("!!!");
}
getchar();
return 0;
}
}
getchar()和Putchar()字符处理函数
getchar()
主要作用:
从输入队列中返回下一个字符.--->ch = getchar();
等效于scanf("%c", &ch);
putchar()
主要作用:
打印参数--->putchar(ch);
等效于printf("%c", ch);
示例代码:
/**
* @Author: Lucifer
* @Date: 4/29/2023, 10:59:44 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/29/2023, 10:59:44 PM
* Description: getchar()函数和putchar()函数的结合使用
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
# define SPACE ' '
int main(void)
{
char ch;
ch = getchar();
while (ch != '\n')
{
if (ch == SPACE)
putchar(ch);
else
putchar(ch + 1);
/**
* ch被转为int类型
* int类型的计算结果被传递给接受一个int类型参数的putchar()
* 根据最后一个字节确定显示哪个字符
*/
ch = getchar();
}
putchar(ch);
return 0;
}
ctype.h
系列字符函数
示例代码:
/**
* @Author: Lucifer
* @Date: 4/29/2023, 11:07:43 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/29/2023, 11:07:43 PM
* Description: ctype.h宏文件函数用法
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
# include<ctype.h>
int main(void)
{
char ch;
while ((ch = getchar()) != '\n')
{
if (isalpha(ch))
putchar(ch + 1);
else
putchar(ch);
}
putchar(ch);
getchar();
return 0;
}
ctype.h
宏文件当中的测试函数:
isxdigit()
---> 十六进制字数字符tolower()
---> 如果参数是大写字符,返回小写字符,否则,返回原始参数toupper()
---> 如果参数是小写字符,返回大写字符,否则,返回原始参数
else...if
示例代码:
/**
* @Author: Lucifer
* @Date: 4/29/2023, 11:30:09 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/29/2023, 11:30:09 PM
* Description: 电费经典题目
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
# define RATE1 0.13230
# define RATE2 0.15040
# define RATE3 0.30025
# define RATE4 0.34025
# define BREAK1 360.0
# define BREAK2 468.0
# define BREAK3 720.0
# define BASE1 (RATE1 * BREAK1)
# define BASE2 (BASE1 + (RATE2 * (BREAK2 - BREAK1)))
# define BASE3 (BASE2 + (RATE3 * (BREAK3 - BREAK2)))
int main(void)
{
double kwh;
double bill;
printf("请输入使用的电量(kwh):\n");
scanf("%lf", &kwh);
if (kwh <= BREAK1)
bill = RATE1 * kwh;
else if (kwh < BREAK2)
bill = BASE1 + (RATE2 * (kwh - BREAK1)); // 360 - 480 kwh
else if (kwh < BREAK3)
bill = BASE2 + (RATE3 * (kwh - BREAK2)); // 468 - 720 kwh
else
bill = BREAK3 + (RATE4 * (kwh - BREAK3)); // 超过720 kwh
printf("用电: %.lf kwh, 电费为: $%1.2f. \n", kwh, bill);
getchar();
getchar();
return 0;
}
需要注意,简单的if
语句当中else
与哪个if
匹配:
示例代码:
# include<stdio.h>
int main(void)
{
int number;
scanf("%d", &number);
if (number > 6)
if (number < 12)
printf("关闭!");
else
/**
* 在简单的if语句当中,else与最近的if匹配.
* 除非是用{}括起来的语句才会有层级关系,类似
* if()
* {
* if()
* {
*
* }
* }
* else
* {
*
* }
* 这个当中的else与最外层的if匹配
*/
printf("继续!\n");
getchar();
getchar();
return 0;
}
变式:
/**
* @Author: Lucifer
* @Date: 4/29/2023, 11:43:48 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/29/2023, 11:43:48 PM
* Description: else匹配的if变式
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
int main(void)
{
int number;
scanf("%d", &number);
if (number > 6)
{
if (number < 12)
printf("关闭!");
}
else // 这个else匹配的if是结构体({})外的if
printf("继续!");
getchar();
getchar();
return 0;
}
求约数:
示例代码:
/**
* @Author: Lucifer
* @Date: 4/30/2023, 11:24:29 AM
* @LastEditors: Lucifer
* @LastEditTime: 4/30/2023, 11:24:29 AM
* Description: 约数算法
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
int main(void)
{
int num;
int div;
printf("输入一个数:\n");
scanf("%d", &num);
for ( div = 2; div < num; div++)
if (num % div == 0)
printf("%d is divisible by %d\n", num, div);
/**
* 算法缺陷:
* 如果144%2得0,说明2是144的约数.
* 144/2得72,说明72也是144约数
* 所以num%div可以获得两个约数
* 那么实际上不需要循环到143,只需要循环12次即可停止循环
*/
getchar();
getchar();
return 0;
}
约束算法优化2:
/**
* @Author: Lucifer
* @Date: 4/30/2023, 11:39:10 AM
* @LastEditors: Lucifer
* @LastEditTime: 4/30/2023, 11:39:10 AM
* Description: 约数算法优化
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
int main(void)
{
// 测试数只需要到num的平方即可
int num;
int div;
printf("输入一个数:\n");
scanf("%d", &num);
for ( div = 2; (div * div) <= num; div++) // 使用等于是因为要注意完全平方数
/**
* 使用平方而不是平方根原因
* 整数乘法比平方根快
*/
{
if (num % div == 0)
printf("%d 是 %d 和 %d 的积.\n", num, div, num / div); // num / div需要注意的是如果num是完全平方数(一个数等于两个相同整数之积)
}
getchar();
getchar();
return 0;
}
约束算法优化3:
/**
* @Author: Lucifer
* @Date: 4/30/2023, 11:39:27 AM
* @LastEditors: Lucifer
* @LastEditTime: 4/30/2023, 11:39:27 AM
* Description: 约束算法优化2
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
int main(void)
{
int num;
int div;
printf("请输入一个数:\n");
scanf("%d", &num);
for ( div = 2; (div * div) <= num; div++)
{
if (num % div == 0)
{
if (div * div != num)
printf("%d 是 %d 和 %d 的积.\n", num, div, num / div);
else
printf("%d 是 %d 的完全平方数.\n", num, div);
}
else
printf("%d 不是 2 的约数\n", num);
}
getchar();
getchar();
return 0;
}
素数算法:
/**
* @Author: Lucifer
* @Date: 4/30/2023, 12:08:21 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/30/2023, 12:08:21 PM
* Description: 素数约数算法
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
# include<stdbool.h>
int main(void)
{
unsigned long num; // 待测的数
unsigned long div; // 可能的约数
bool isPrime; // 素数标记
printf("输入一个整数来分析:\n");
printf("输入q或者quit退出\n");
while (scanf("%lu", &num) == 1)
{
for (div = 2, isPrime = true; (div * div) <= num; div++)
{
if (num % div == 0)
{
if ((div * div) != num) // 非完全平方数
printf("%lu 是 %lu 和 %lu 的约数.\n", num, div, num / div);
else
printf("%lu 是 %lu 的完全平方数.\n", num, div);
isPrime = false;
}
else
{
printf("该数不是2的约数.\n");
break;
}
if (isPrime)
printf("%lu 是素数.\n", num);
printf("请输入另一个整数来分析:\n");
printf("输入q或者quit退出\n");
}
printf("退出!");
}
getchar();
return 0;
}
条件表达式: ? :
运行逻辑:
判断表达式的true
或false
.如果为true
输出? :
之间的内容,如果为false
输出:
后的内容
示例代码:
/**
* @Author: Lucifer
* @Date: 4/30/2023, 2:26:36 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/30/2023, 2:26:36 PM
* Description: 条件运算符在实际中的运用
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
# define COVERAGE 350
int main(void)
{
int sq_feet;
int cans;
printf("Enter number of square feet to be painted: \n");
while (scanf("%d", &sq_feet) == 1)
{
cans += sq_feet / COVERAGE;
cans += (sq_feet % COVERAGE == 0) ? 0 : 1; // 先判断sq_feet % COVERAGE的true或false
printf("You need %d %s of paint.\n", cans, cans == 1 ? "can" : "cans");
printf("Enter next value (q or quit to quit):\n");
}
getchar();
return 0;
}
辅助循环: Continue 和 Break
continue
语句:
作用,跳过本次迭代剩余部分,进入下一轮迭代:
示例代码:
/**
* @Author: Lucifer
* @Date: 4/30/2023, 2:50:08 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/30/2023, 2:50:08 PM
* Description: while循环当中的continue语句,当执行到这以后执行的是测试条件
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
int main(void)
{
const float MIN = 0.0f;
const float MAX = 100.0f;
float score;
float total = 0.0f;
int n = 0;
float min = MAX;
float max = MIN;
printf("Enter the first score (q to quit):");
while (scanf("%f", &score) == 1)
{
if (score < MIN || score > MAX)
{
printf("%0.1f is an invalid value. Try again:", score);
continue; // 回到while循环的测试条件当中
}
printf("Accepting %0.1f:\n", score);
min = (score < min) ? score : min;
max = (score > max) ? score : max;
total += score;
n++;
printf("Enter next score (q to quit):");
}
if (n > 0)
{
printf("Average of %d scores is %0.1f.\n", n, total / n);
printf("Low = %0.1f, high = %0.1f\n", min, max);
}
else
printf("No valid scores were entered.\n");
getchar();
return 0;
}
在for
循环当中执行到continue
以后下一个执行的是更新表达式求值,然后在是测试条件:
示例代码:
/**
* @Author: Lucifer
* @Date: 4/30/2023, 3:09:02 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/30/2023, 3:09:02 PM
* Description: for循环当中的continue使用
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
int main(void)
{
int count;
char ch;
for (count = 0; count < 10; count++)
{
ch = getchar();
if (ch == '\n')
continue;
putchar(ch);
}
getchar();
return 0;
}
break
语句:
作用:跳出循环,执行循环体后续的内容
示例代码:
/**
* @Author: Lucifer
* @Date: 4/30/2023, 3:19:24 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/30/2023, 3:19:24 PM
* Description: break在循环当中的使用,这是一个计算面积的程序
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
int main(void)
{
float length, width;
printf("Enter the length of the rectangle:\n");
while (scanf("%f", &length) == 1)
{
printf("长度 = %0.2f: \n", length);
printf("输入宽度:\n");
if (scanf("%f", &width) != 1)
break;
printf("宽度: %0.2f: \n", width);
printf("Area = %0.2f: \n", length * width);
printf("Enter the length of the rectangle:\n");
}
printf("Done.\n");
getchar();
return 0;
}
上诉代码的控制语句可以改写成:
while(scanf("%f %f", &length, &width) == 2)
{
}
嵌套循环的break
只会跳出当前层的循环:
示例代码:
/**
* @Author: Lucifer
* @Date: 4/30/2023, 3:29:37 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/30/2023, 3:29:37 PM
* Description: break在嵌套循环当中的使用
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
int main(void)
{
int p, q;
scanf("%d", &p);
while (p > 0)
{
printf("%d\n", p);
scanf("%d", &q);
while (q > 0)
{
printf("%d\n", p * q);
if (q > 100)
break; // 跳出内层循环
scanf("%d", &q);
}
if (q > 100)
break; // 跳出外层循环
scanf("%d", &p);
}
getchar();
return 0;
}
多重选择:Switch
switch
语句扫描的是标签,该标签可以是单个标签也可以是多个标签:
单标签示例代码:
/**
* @Author: Lucifer
* @Date: 4/30/2023, 4:14:23 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/30/2023, 4:14:23 PM
* Description: switch在循环当中的时候,单标签形式
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
# include<ctype.h>
int main(void)
{
char ch;
printf("Give me a letter of the alphabet, and I will give ");
printf("an animal name\n beginning with that letter.\n");
printf("Please type in a letter; type # to end my act. \n");
while ((ch = getchar()) != '#')
{
if ('\n' == ch) // 尤达表达式
continue;
if (islower(ch))
switch (ch)
{
case 'a':
printf("A \n");
break;
case 'b':
printf("B \n");
break;
case 'c':
printf("C \n");
break;
case 'd':
printf("D \n");
break;
case 'e':
printf("E \n");
break;
case 'f':
printf("F \n");
break;
default:
printf("结束!");
}
else
printf("I recognize only lowercase letters.\n");
while (getchar() != '\n')
continue; // 跳过输入行剩余部分
printf("Please type another letter or a #.\n"); // 循环结束
}
printf("Bye!\n");
getchar();
return 0;
}
多重标签的使用:
/**
* @Author: Lucifer
* @Date: 4/30/2023, 4:31:26 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/30/2023, 4:31:26 PM
* Description: switch多重标签 ---> 统计一个句子当中的字符出现的次数
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
# include<ctype.h>
int main(void)
{
char ch;
int a_ct, e_ct, i_ct, o_ct, u_ct;
a_ct = e_ct = i_ct = o_ct = u_ct = 0;
printf("Enter some text; enter # to quuit.\n");
// ch = isupper(ch); ---> 转成大写判断,就可以变成单标签
while ((ch = getchar()) != '#')
{
switch (ch)
{
case 'a':
case 'A': a_ct++;
break;
case 'e':
case 'E': e_ct++;
break;
case 'i':
case 'I': i_ct++;
break;
case 'o':
case 'O': o_ct++;
break;
case 'u':
case 'U': u_ct++;
break;
default: break;
} // switch结束
} // while循环结束
printf("number of vowels: A E I O U\n");
printf(" %4d %4d %4d %4d %4d\n", a_ct, e_ct, i_ct, o_ct, u_ct);
getchar();
getchar();
return 0;
}
It's a lonely road!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!