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

B站C语言课程笔记3

下面是学习的网站:

【C语言】

目录

11、if-else if-else语句

12、switch语句

13、逻辑运算符

14、scanf语句

15、终端scanf的中文乱码


11、if-else if-else语句

简单if语句:

#include <stdio.h>int main(){int a = 2;int b = 8;int result;if (a > b){result = a - b;printf("a与b的差值为:%d\n",result);}else{result = b - a;printf("a与b的差值为:%d\n",result);}return 0;
}

if嵌套语句:

#include <stdio.h>int main(){int user_type;float price;//也可以用double类型float discout;float final_price;user_type = 2;price =180;if (user_type == 1){if(price > 100){discout = 0.95;}else{discout = 1;}        }else if(user_type == 2){if(price > 200){discout = 0.9;}else{discout = 0.97;}}else{printf("该用户类型无效\n");}final_price = price * discout;printf("最终支付金额为:%.2f",final_price);return 0;
}

12、switch语句

#include <stdio.h>int main(){int commodity_type = 2;double price = 299.9;switch (commodity_type){case 1:if(price < 500){printf("电子产品价格较低,无优惠。\n");}else if(price <= 1000){printf("电子产品可享受5%%优惠。\n");}else{printf("电子产品可享受10%%优惠。\n");}break;case 2:if(price < 200){printf("服装价格较低,无优惠。\n");}else if(price <= 500){printf("服装可享受8%%优惠。\n");}else{printf("服装可享受15%%优惠。\n");}break;default:printf("商品类型既不属于电子产品又不属于服装\n");break;}
}

13、逻辑运算符

主要用于多个条件判断的情况

&& ;或 || (英文输入状态下顿号可以打出竖线);非

优先级为 () > ! > && > ||

#include <stdio.h>int main(){int year = 2088;if(year % 4 == 0 && year % 100 != 0){printf("%d年是闰年\n",year);}else if(year % 400 == 0){printf("%d年是闰年\n",year);}//UP主用了一个包含逻辑运算符的条件语句就达成了以上的代码//if((year % 4 ==0) && !(year % 100 == 0) || (year % 400 == 0))else{printf("%d年不是闰年\n",year);}
}

14、scanf语句

用来接收用户输入的内容并把数据存起来。

此时不能在VSCode的问题窗口进行输入,而是要在终端进行。需要在设置里面设置在终端(Terminal)运行代码(Run Code)。

scanf语句和printf语句的格式比较类似,尤其要注意变量地址前面有一个取址符&

scanf("格式字符串",&变量地址1,&变量地址2...); 

以下是scanf的练习:

#include <stdio.h>int main(){char category;float price;printf("请输入商品类型,A代表电子产品,B代表服装:\n");scanf(" %c",&category);printf("请输入商品价格:\n");scanf(" %f",&price);if(category == 'A'){if(price < 500){printf("电子产品价格较低,无优惠\n");}else if (price >= 500 && price < 1000){printf("电子产品可享受5%%的优惠\n");}else{printf("电子产品可享受10%%的优惠\n");}}else if(category == 'B'){if(price < 200){printf("服装价格较低,无优惠\n");}else if (price >= 200 && price < 500){printf("服装可享受8%%的优惠\n");}else{printf("服装可享受15%%的优惠\n");}}else{printf("此商品既不是电子产品也不是服装\n");}//    使用switch  //    char commodity_type;//    double price;//    printf("请输入商品类型,A代表电子产品,B代表服装:\n");//    scanf("%c",&commodity_type);//    printf("请输入商品价格:\n");//    scanf(" %f",&price);//    switch (commodity_type)//    {//    case 'A'://        if(price < 500){//            printf("电子产品价格较低,无优惠。\n");//        }//        else if(price <= 1000){//            printf("电子产品可享受5%%优惠。\n");//        }//        else{//            printf("电子产品可享受10%%优惠。\n");//        }//        break;//    case 'B'://        if(price < 200){//        printf("服装价格较低,无优惠。\n");//        }//        else if(price <= 500){//        printf("服装可享受8%%优惠。\n");//        }//        else{//        printf("服装可享受15%%优惠。\n");//        }//        break;//    default://        printf("商品类型既不属于电子产品又不属于服装\n");//        break;//    } 
}

15、终端scanf的中文乱码

从“C语言scanf | 你的角色名是啥?”这一章节后就有许多人在评论区说终端有乱码问题,我的解决办法是在VSCode的setting.json文件中进行相关设置:

{// 基础界面设置"workbench.colorTheme": "Default Dark Modern","files.autoSave": "onFocusChange","explorer.confirmDelete": false,"workbench.statusBar.visible": true,"files.encoding":"utf8",// C/C++ 开发环境配置"C_Cpp.default.includePath": ["F:\\msys2\\mingw64\\include","F:\\msys2\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\14.2.0\\include"],// 中文编码相关设置 - 解决中文乱码的核心配置//"files.encoding": "gb2312","files.autoGuessEncoding": true,"[c]": {//"files.encoding": "gb2312""files.encoding":"utf8"},"[cpp]": {//"files.encoding": "gb2312""files.encoding":"utf8"},// 终端设置 - 解决控制台输出中文乱码"terminal.integrated.defaultProfile.windows": "Command Prompt","terminal.integrated.profiles.windows": {"Command Prompt": {"path": "C:\\Windows\\System32\\cmd.exe","args": []}},"terminal.integrated.shellArgs.windows": ["/k","chcp 936"],// 代码运行设置 - 解决程序运行时的中文乱码"code-runner.runInTerminal": true,"code-runner.executorMap": {"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt -fexec-charset=GBK && $dir$fileNameWithoutExt"},"code-runner.saveFileBeforeRun": true,"code-runner.clearPreviousOutput": true,
http://www.xdnf.cn/news/2022.html

相关文章:

  • 《AI大模型趣味实战》基于RAG向量数据库的知识库AI问答助手设计与实现
  • 开发小程序后端用PHP好还是Java哪个好?
  • 【N8N】Docker Desktop + WSL 安装过程(Docker Desktop - WSL update Failed解决方法)
  • 大内存生产环境tomcat-jvm配置实践
  • 同一页面下动态加载内容的两种方式:AJAX与iframe
  • Git 进阶使用指南
  • 【深度强化学习 DRL 快速实践】策略梯度算法 (PG)
  • 图论---染色法(判断是否为二分图)
  • PH热榜 | 2025-04-25
  • 【物联网】基于LORA组网的远程环境监测系统设计(ThingsCloud云平台版)
  • Feign接口调用失败降级机制
  • 力扣DAY68 | 热100 | 寻找两个正序数组的中位数
  • 【数据可视化-33】病毒式社交媒体潮流与用户参与度可视化分析
  • 入侵检测系统(IDS)与入侵防御系统(IPS):功能对比与部署实践
  • QT开发技术【QT实现桌面右下角消息】
  • 通过模仿学习实现机器人灵巧操作:综述(上)
  • 使用 AutoGen 与 Elasticsearch
  • 6.ArkUI Row的介绍和使用
  • 笔记:记一次使用EasyExcel重写convertToExcelData方法无法读取@ExcelDictFormat注解的问题(已解决)
  • 计算机视觉各类任务评价指标详解
  • 8. 深入Spring AI:自定义Advisor
  • 反爬策略应对指南:淘宝 API 商品数据采集的 IP 代理与请求伪装技术
  • OceanBase 复合索引指南
  • 项目maven版本不一致 导致无法下载
  • 人工智能与机器学习:Python从零实现性回归模型
  • 从“能耗大户”到“节能标杆”:安科瑞助力污水处理厂绿色转型
  • 告别进度失控:用燃尽图补上甘特图的监控盲区
  • Windows server:
  • [OS_8] 终端和 UNIX Shell | 会话和进程组 | sigaction | dash
  • 多模态大语言模型(MLLM)- kimi-vl technical report论文阅读