当前位置: 首页 > news >正文

C++学习day--10 条件判断、分支

1、if语句

if 语句的三种形态
形态1:如果。。。那么。。。
#include <iostream>
using namespace std;
int main( void ) {
int salary;
cout << " 你月薪多少 ?" ;
cin >> salary;

if (salary < 20000) {

cout << " 你是一个好人 , 我还不想谈恋爱 ." << endl;
}
system( "pause" );
return 0;
}

 形态2:如果。。。那么。。。,否则。。

#include <iostream>
using namespace std;
int main( void ) {
int salary;
cout << " 你月薪多少 ?" << endl ;
cin >> salary;
if (salary < 20000) {
cout << " 你是一个好人 , 我还不想谈恋爱 ." << endl;
} else {
cout << " 我没有什么要求 , 只要你对我好就行 ." << endl;
}
system( "pause" );
return 0;
}
形态 3
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main( void )
{
int salary;
string houseRet; // 是否有房
string carRet; // 是否有车
cout << " 你的月薪是多少 ?" << endl;
cin >> salary;
cout << " 你有房吗? " << endl;
cin >> houseRet;
cout << " 你有车吗? " << endl;
cin >> carRet;
if (salary < 20000) {
cout << " 你是一个好人, 我现在还不想谈恋爱 ." << endl;
} else if (houseRet == " " ) {
cout << " 我其实没有什么要求,只要你对我好 ." << endl;
} else if (carRet == " " ) {
cout << " 还不错哦, 以后再联系 ." << endl;
} else {
cout << " 有缘再见 ." << endl;
}
system( "pause" );
return 0;
}

if 语句的嵌套 

实例:
3 个数的最大值。
#include <iostream>
#include <windows.h>
using namespace std;
int main( void ) {
int x, y, z;
cout << " 请输入 3 个整数 : " << endl;
cin >> x >> y >> z;
if (x > y) {
if (x > z) {
cout << " 最大值是: " << x << endl;
} else {
cout << " 最大值是: " << z << endl;
}
} else {
if (y > z) {
cout << " 最大值是: " << y << endl;
} else {
cout << " 最大值是: " << z << endl;
}
}
system( "pause" );
return 0;
}
嵌套的常见错误(配对错误)
与前面最近的,而且还没有配对的 if 匹配

 

错误避免方法:
1 )严格使用 { }
2 )先写{}再写里面的内容
3 )保持良好的“缩进”

 2、switch 语句

switch 的基本使用
语法:
switch (x) {
case 表达式 1

语句 1
break;
case 表达式 2
语句 2
break;
case 表达式 3
语句 3
break;
default 表达式 1
语句 1
break;
}

 流程图:

 demo

#include <iostream>
#include <windows.h>
using namespace std;
int main(void) {
int num;
cout << " 请输入一个数字 : ";
cin >> num;
switch (num) {
case 1:
cout << " 星期一:包子 " << endl;
break;
case 2:
cout << " 星期二:馒头 " << endl;

break;
case 3:
cout << " 星期三:稀饭 " << endl;
break;
case 4:
cout << " 星期四:白菜 " << endl;
break;
case 5:
cout << " 星期五:土豆 " << endl;
break;
case 6:
case 7:
cout << " 周末:休息 " << endl;
break;
default:
cout << " 请输入 1-7" << endl;
break;
}
system("pause");
return 0;
}
switch if 的选择
switch : 用于 int/char/long/long long 类型的变量,和多个特定常量的判断处理。 (float 和 double 类型不可以), 注意: case 表达式,这个表达式只 能取整数,整数表达式,不能取 其它类型的表达式!!
if: 适用于各种逻辑判断

 -常见错误总结

switch 错误

以下错误,在 vs/vc 中有提示,但是仍可以通过编译。
gcc 编译器中,不能通过编译。
#include <stdio.h>
int main(void) {
int c;
scanf("%d", &c);
switch(c) {
case 1:
int x = 0; //错误!
printf("c=1");
break;
case 2:
printf("c=2");
break;

default:
printf("other");
break;
}
return 0;
}

应该修改为: 

#include <stdio.h>
int main(void) {
int c;
scanf("%d", &c);
switch(c) {
case 1:
{
int x = 0; //错误!
printf("c=1");
}
break;
case 2:
printf("c=2");
break;
default:
printf("other");
break;
}
return 0;
}
不安全函数( scanf 等)
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main( void )
{
int num;

scanf( "%d" , &num);
system( "pause" );
return 0;
}
vs 中不能直接使用 scanf C 标准库函数
因为 vs 使用更安全的 c11 标准 , 认为这类函数不安全。
注明,这类函数正常使用时,是没有任何问题的
但是,部分黑客可能会利用其中的缺陷,开发恶意软件,对系统造成影响
解决方案:
1. 方法 1 :使用修改项目的属性,直接使用这些“不安全”的函数。
添加: /D _CRT_SECURE_NO_WARNINGS

 

2. 方法 2 :使用 c11 标准中的“更安全”的函数 scanf_s
gets 不能使用,使用 gets_s
gets 是老标准 C 语言函数, vs 使用更安全的 c11 标准 , 使用对应的 gets_s
char line[32];
gets_s(line, sizeof (line));
scanf 不能使用
原因同上,改用 scanf_s
int x;
scanf_s("%d", &x); // 不需要使用第 3 个参数,用法和 scanf 相同
float f;
scanf_s("%f", &f); // 不需要使用第 3 个参数 , 用法和 scanf 相同
char c;
scanf_s("%c", &c, sizeof(c)); // 需要使用第 3 个参数 , 否则有告警
char name[16];
scanf_s("%s", name, sizeof(name)); // 需要使用第 3 个参数
int age;
char name[16];
scanf_s("%d%s", &age, name, sizeof(name));

cin >> 的返回值 

#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
int main( void ) {
string word;
int count = 0;
int length = 0;
cout << " 请输入任意多个单词: " ;
while (1) {
if ((cin >> word) == 0) { // vs 中不能通过编译
break ;
}
count++;
length += word.length();
}
cout << " 一共有 " << count << " 单词 " << endl;
cout << " 总长度: " << length << endl;
system( "pause" );
return 0;
}
if ((cin >> word) == 0) 修改为: if ((bool)(std::cin >> word) == 0) {
或者修改为: if (!(cin >> word)) {
getline 的返回值
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
int main( void ) {
string line;
int lineCount = 0;
int length = 0;
cout << " 请输入任意多行: " ;
while (1) {
// 遇到文件结束符时, 返回 NULL 0
if (getline(cin, line) == 0) {
break ;
}
lineCount++;
length += line.length();
}
cout << " 一共有 " << lineCount << " " << endl;
cout << " 总长度 : " << length << endl;
system( "pause" );
return 0;
}

测试代码: 

string line;
int length = 0;
getline(cin, line) >> length;
cout << "line=" << line << endl;
cout << "length=" << length << endl;
 
修改:
if (getline(cin, line) == 0) {
修改为:
if ( (bool) g etline(cin, line) == 0) {
或者修改为:
if ( ! getline(cin, line)) {

 if 语句后面误加分号

int age;
cout << "请输入年龄: " ;
cin >> age;
if (age > 40); {
cout << "大叔" << endl;
}
严格遵循代码规范,做到零警告。
以上代码在 VS 中编译时,会有警告 warning

 

3、计算机英语加油站

bool
布尔
逻辑类型
if
如果
else
否则
switch
开关
case
情况
default
默认

4、职场修炼:怎样优雅地避免加班

996 工作制?
9-6-5
制定合理的项目计划
提高工作效率(要有一定的技术积累)
养成良好的工作习惯【不加班】
每天有目标的学习 1 小时以上

 5、练习

习题 1. 让用户输入一个字符, 然后进行转换:
如果是大写字母,就转换为小写字母
如果是小写字母,就转换为大写字母
如果是其它字符,不做任何转换
#include <iostream>
#include <string>
#include <Windows.h>

using namespace std;
int main( void ) {
char c;
cout << " 请输入一个字符 : " << endl;
cin >> c;
if (c >= 'a' && c <= 'z' ) {
c = c - 'a' + 'A' ;
} else if (c >= 'A' && c <= 'Z' ) {
c = c - 'A' + 'a' ;
}
cout << c << endl;
system( "pause" );
return 0;
}
习题 2. 让用户输入一个数字(
0-9 ),然后输出对应的大写汉字。
注: 零 壹 贰 叁 肆 伍 陆 柒 捌 玖
例如,用户输入 3 , 输出“叁”
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
// 零 壹 贰 叁 肆 伍 陆 柒 捌 玖
int main( void ) {
int num;
string ret;
cout << " 请输入一个数字 [0-9]: " ;
cin >> num;
switch (num) {
case 0:
cout << " " ;
break ;
case 1:
cout << " " ;
break ;
case 2:

cout << " " ;
break ;
case 3:
cout << " " ;
break ;
case 4:
cout << " " ;
break ;
case 5:
cout << " " ;
break ;
case 6:
cout << " " ;
break ;
case 7:
cout << " " ;
break ;
case 8:
cout << " " ;
break ;
case 9:
cout << " " ;
break ;
default :
break ;
}
cout << endl;
system( "pause" );
return 0;
}
方法 2
用空间换速度。
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
int main( void ) {
int num;
string ret[10] = { " " , " " , " " , " " , " " , " " , " " , " " , " " ,
" " };
cout << " 请输入一个数字 [0-9]: " ;
cin >> num;
if (num >=0 && num <=9) {
cout << ret[num] << endl;
}
system( "pause" );
return 0;
}
习题 3. 让用户输入年份和月份,然后输出这个月有多少天。
说明:
闰年的 2 月份有 29
普通闰年: 能被 4 整除但不能被 100 整除的年份为
世纪闰年: 能被 400 整除
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
/*
闰年的 2 月份有 29
普通闰年 : 能被 4 整除但不能被 100 整除的年份为
世纪闰年 : 能被 400 整除
*/
int main( void ) {
int year;
int month;
bool flag = false ;
int days;
cout << " 请输入年份: " ;
cin >> year;
cout << " 请输入月份: " ;
cin >> month;
if (year % 400 == 0) {
flag = true ;
} else if (year % 4 == 0 && year % 100 != 0) {
flag = true ;
} else {
flag = false ;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:

case 12:

days = 31;
break ;
case 2:
days = flag ? 29 : 28;
break ;
case 4:
case 6:
case 9:
case 11:
days = 30;
break ;
default :
std::cout << " 无效月份 " << std::endl;
break ;
}
cout << year << " " << month << " 月一共有: " << days << " " << endl;
system( "pause" );
return 0;
}
http://www.lryc.cn/news/66748.html

相关文章:

  • 和月薪5W的聊过后,才发现自己一直在打杂···
  • SSM框架学习-AOP通知类型
  • 微信小程序原生开发功能合集十四:登录健权及注册功能实现
  • 【Java零基础入门篇】第 ⑤ 期 - 抽象类和接口(二)
  • Halcon 集合运算(差集difference、交集intersection、并集union2、打散connection与 合集 union1)
  • Allegro约束规则设计
  • PyQt5桌面应用开发(11):摸鱼也要讲基本法之桌面精灵
  • Talk预告 | 大连理工大学IIAU Lab在读博士生严彬:走向通用实例感知
  • 2023-05-04 LeetCode每日一题(摘水果)
  • [工具]Pytorch-lightning的使用
  • 互联网摸鱼日报(2023-05-09)
  • MySQL常见的存储引擎
  • 迅为i.MX6ULL开发板生成 KEY 文件,并安装
  • 常见舆情监测系统的分类和特点
  • 联合群美叶彦文:坚持,只要有一口气,能坚持多久,就坚持多久
  • 动态规划的学习
  • 计算机网络:HTTPS
  • 数据库系列-什么是 JDBC?它的作用是什么?
  • C++学习day--08 数组和字符串
  • 系统分析师之系统测试与维护(十六)
  • 板材激光切割机切割穿孔时注意的几个问题
  • 奶爸式Swagger教学
  • 入门级的家用洗地机怎么样?入门级洗地机推荐
  • 【面试】Java 反射机制(常见面试题)
  • JavaScript最佳实践
  • 景23转债,海能转债上市价格预测
  • TDengine 部署与使用----时序数据库
  • ShardingSphere系列四(Sharding-JDBC内核原理及核心源码解析)
  • 【2023】华为OD机试真题全语言-题目0234-字符串重新排列
  • Springboot +Flowable,三种常见网关的使用(排他、并行、包容网关)(一)