Week 2

2.1输入输出进阶

用cin读入所有所输入的字符,包括空格回车

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
	int c;
	while ((c=cin.get())!=EOF)	//用cin读入所有所输入的字符,包括空格回车
	{
		cout << (char)c;
	}
	return 0;
}

用scanf读入所有所输入的字符,包括空格回车

#include <iostream>
#include <cstdio>	//使用printf,scanf要有这行
using namespace std;

int main()
{
	char c;
	while (scanf("%c", &c) != EOF)	//用scanf读入所有所输入的字符,包括空格回车
	{
		printf("%c", c);
	}
	return 0;
}
  • cin/cout 速度比 scanf/printf 输入输出数据量大时用后者
  • 两种写法不要同时用

2.2算术运算符和算术表达式

  • 除法运算
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
	int a = 10;
	int b = 3;
	double d = a / b;
	cout << d << endl;	//3
	d = 5 / 2;
	cout << d << endl;	//2
	d = 5 / 2.0;
	cout << d << endl;	//2.5
	d = (double)a / b;
	cout << d << endl;	//3.33333
	return 0;
}
  • ++a 将a的值加1,表达式返回的是a+1的值
  • a++ 将a的值加1,表达式返回的是a+1的值

2.3关系运算符和逻辑表达式

  • exp1 && exp2:如果已经算出表达式exp1为假,那么整个表达式的值肯定为假,于是表达式exp2就不需要再计算
  • exp1 || exp2:如果已经算出表达式exp1为真,那么整个表达式的值必定为真,于是表达式exp2也不必计算
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
	int a = 0, b = 1;
	bool n = (a++) && (b++);
	cout << n << "," << a << "," << b << endl;	//0,1,1
	n = a++ || b++;
	cout << n << "," << a << "," << b << endl;	//1,2,1
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值