C++ 语法基础课2 —— printf 语句与判断结构
创始人
2024-01-21 05:48:10

文章目录

  • 1. printf 输出格式(#include)
    • 1.1 int、float、double、char 等类型的输出格式
    • 1.2 所有输出的变量均可包含在一个字符串中
      • 1.2.1 练习1
      • 1.2.2 练习2
    • 1.3 扩展功能
  • 2. if 语句
    • 2.1 基本 if-else 语句
      • 2.1.1 练习1
      • 2.1.2 练习2
      • 2.1.3 练习4
    • 2.2 常用比较运算符
    • 2.3 if else 连写:
      • 2.3.1 练习1
      • 2.3.1 练习2
  • 3. 条件表达式
    • 3.1 练习1
    • 3.2 练习2

  • 学习语言最好的方式就是实践,每当掌握一个新功能时,就要立即将这个功能应用到实践中。

1. printf 输出格式(#include)

注意:使用 printf 时最好添加头文件 #include

#include
#include
using namespace std;int main()
{printf("HELLO WORLD!");return 0;
}

1.1 int、float、double、char 等类型的输出格式

类型格式说明
int%d整型
float%f默认保留 6 位小数
double%lf默认保留 6 位小数
char%c回车也是一个字符,用’\n’表示
#include
#include
using namespace std;int main()
{int a = 3;float b = 3.12345678;double c = 3.12345678;char d = 'y';printf("%d\n", a);printf("%f\n", b);printf("%lf\n", c);printf("%c\n", d);return 0;
}

1.2 所有输出的变量均可包含在一个字符串中

#include
#include
using namespace std;int main()
{int a = 3;float b = 3.12345678;double c = 3.12345678;char d = 'y';printf("int a = %d,float b = %f\n,double c = %lf, char d = %c\n",a, b, c, d);return 0;
}

1.2.1 练习1

  • 输入一个字符,用这个字符输出一个菱形
#include
#include
using namespace std;int main()
{char c;cin >> c;printf("  %c\n",c);printf(" %c%c%c\n",c,c,c);printf("%c%c%c%c%c\n",c,c,c,c,c);printf(" %c%c%c\n",c,c,c);printf("  %c\n",c);return 0;
}

1.2.2 练习2

  • 输入一个整数,表示时间,单位是秒;输出一个字符串,用”时:分:秒”的形式表示这个时间
#include
#include
using namespace std;int main()
{int t;cin >> t;int hours = t / 3600;int minutes = t % 3600 / 60;int seconds = t % 60;printf("%d:%d:%d\n",hours, minutes, seconds);return 0;
}

1.3 扩展功能

  • float, double 等输出保留若干位小数时用:%.4f, %3lf
#include
#include
using namespace std;int main()
{float b = 3.12345678;double c = 3.12345678;printf("%.4f\n",b);printf("%.3lf\n",c);return 0;
}
  • 最小数字宽度
  • a. %8.3f, 表示这个浮点数的最小宽度为 8,保留 3 位小数,当宽度不足时在前面补空格
#include
#include
using namespace std;int main()
{int a = 3;float b = 3.12345678;double c = 3.12345678;printf("%5d\n",a);printf("%8.4f\n",b);printf("%7.3lf\n",c);return 0;
}
  • b. %-8.3f,表示最小宽度为 8,保留 3 位小数,当宽度不足时在后面补上空格
#include
#include
using namespace std;int main()
{int a = 3;float b = 3.12345678;double c = 3.12345678;printf("%-5d\n",a);printf("%-8.4f\n",b);printf("%-7.3lf\n",c);return 0;
}
  • %08.3f, 表示最小宽度为 8,保留 3 位小数,当宽度不足时在前面补上 0
#include
#include
using namespace std;int main()
{int a = 3;float b = 3.12345678;double c = 3.12345678;printf("%05d\n",a);printf("%08.4f\n",b);printf("%07.3lf\n",c);return 0;
}

2. if 语句

2.1 基本 if-else 语句

  • 当条件成立时,执行某些语句;否则执行另一些语句
#include
#include
using namespace std;int main()
{int a = 3;cin >> a;if(a > 5){printf("%d is big!\n",a);printf("%d + 1 = %d\n",a ,a+1);}else{printf("%d is small!\n",a);printf("%d - 1 = %d\n",a ,a-1);}return 0;
}
  • else 语句可以省略
#include
#include
using namespace std;int main()
{int a = 3;cin >> a;if(a > 5){printf("%d is big!\n",a);printf("%d + 1 = %d\n",a ,a+1);}return 0;
}
  • 当只有一条语句时,大括号可以省略
#include
#include
using namespace std;int main()
{int a = 3;cin >> a;if(a > 5)printf("%d is big!\n",a);elseprintf("%d is small!\n",a);return 0;
}

2.1.1 练习1

  • 输入一个整数,输出这个数的绝对值
#include
#include
using namespace std;int main()
{int x;cin >> x;if(x > 0) cout << x << endl;else cout << -x << endl;return 0;
}

2.1.2 练习2

  • 输入两个整数,输出两个数中较大的那个
#include
#include
using namespace std;int main()
{int a, b;cin >> a >> b;if(a > b)cout << a << endl;elsecout << b <

2.1.3 练习4

  • If-else 语句内部也可以是 if-else 语句
  • 输入三个整数,输出三个数中最大的那个
#include
#include
using namespace std;int main()
{int a, b, c;cin >> a >> b >> c;if(a > b){if (a > c) cout << a << endl;else cout << c << endl;}else{if (b > c) cout << b << endl;else cout << c << endl;}return 0;
}

2.2 常用比较运算符

含义符号
大于>
小于<
大于等于>=
小于等于<=
等于==
不等于!=

2.3 if else 连写:

  • 输入一个 0 到 100 之间的分数,如果大于等于 85,输出 A;如果大于等于 70 并且小于 85,输出 B;如果大于等于 60 并且小于 70,输出 C;如果小于 60,输出 D;
#include
#include
using namespace std;int main()
{int s;cin >> s;if(s >= 85){printf("A");}else if (s >= 70){printf("B");}else if(s >= 60){printf("C");}else{printf("D");}return 0;
}

2.3.1 练习1

  • 简单计算器输入两个数,以及一个运算符+, -, *, /,输出这两个数运算后的结果。当运算符是/,且除数是 0 时,输出”Divided by zero!”; 当输入的字符不是+, -, *, /时,输出”Invalid operator!”。
    在这里插入图片描述

2.3.1 练习2

  • 判断闰年。闰年有两种情况:(1) 能被 100 整除时,必须能被 400 整除;(2) 不能被 100 整除时,被 4 整除即可。
  • 输入一个年份,如果是闰年输出 yes,否则输出 no。
#include
#include
using namespace std;int mian()
{int year;cin >> year;if(year % 100 == 0){if(year % 400 == 0) cout << "yes" << endl;else cout << "no" endl;}else{if(year % 4 == 0) cout << "yes" << endl;else cout << "no" endl;}
}
return 0;

3. 条件表达式

含义符号
&&
||

3.1 练习1

  • 输入三个数,输出三个数中的最大值
#include
#include
using namespace std;int main()
{int a, b, c;cin >> a >> b >> c;if(a > b && a > c) cout << a << endl;else if(b > a && b > c) cout << b << endl;else cout << c << endl;}return 0;
}

3.2 练习2

  • 用一条 if 语句,判断闰年
#include
#include
using namespace std;int mian()
{int year;cin >> year;if(year % 100 == 0 && year % 400 == 0 || year % 4 == 0 && year % 100 != 0) cout << "yes" << endl;else cout << "no" endl;}
}
return 0;

相关内容

热门资讯

长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
不用扬鞭自奋蹄 乘势而上千帆竞... 思明区正全力向“两个进位”目标发起冲刺,努力为建设高素质高颜值现代化国际化的厦门作出“思明贡献”。(...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
好听的民谣歌曲 2021最火的... #我的年度歌单#在2021年里听过很多民谣,但是最能触及心中温柔的还是下面的几首。不听民谣的理由有很...