C++ 语法基础课2 —— printf 语句与判断结构

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

1. printf 输出格式(#include)

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

#include<iostream>
#include<cstdio>
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<iostream>
#include<cstdio>
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<iostream>
#include<cstdio>
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<iostream>
#include<cstdio>
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<iostream>
#include<cstdio>
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<iostream>
#include<cstdio>
using namespace std;

int main()
{
	float b = 3.12345678;
	double c = 3.12345678;

	printf("%.4f\n",b);
	printf("%.3lf\n",c);
	return 0;
}
  • 最小数字宽度
  • %8.3f, 表示这个浮点数的最小宽度为 8,保留 3 位小数,当宽度不足时在前面补空格
#include<iostream>
#include<cstdio>
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;
}
  • %-8.3f,表示最小宽度为 8,保留 3 位小数,当宽度不足时在后面补上空格
#include<iostream>
#include<cstdio>
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<iostream>
#include<cstdio>
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<iostream>
#include<cstdio>
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<iostream>
#include<cstdio>
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<iostream>
#include<cstdio>
using namespace std;

int main()
{
	int a = 3;
	cin >> a;

	if(a > 5)
		printf("%d is big!\n",a);
	else
		printf("%d is small!\n",a);
	return 0;
}

2.1.1 练习1

  • 输入一个整数,输出这个数的绝对值
#include<iostream>
#include<cstdio>
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<iostream>
#include<cstdio>
using namespace std;

int main()
{
	int a, b;
	cin >> a >> b;

	if(a > b)
		cout << a << endl;
	else
		cout << b <<endl;
	return 0;
}

2.1.3 练习3

  • If-else 语句内部也可以是 if-else 语句
  • 输入三个整数,输出三个数中最大的那个
#include<iostream>
#include<cstdio>
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<iostream>
#include<cstdio>
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<iostream>
#include<cstdio>
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<iostream>
#include<cstdio>
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<iostream>
#include<cstdio>
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;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2021 Nqq

你的鼓励是我学习的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值