C++基础知识《一》

《书本以及习题第二章部分》

函数调用格式:

如果子函数在main函数之后,要在main函数中先对子函数声明,才能调用
在被调用函数的首部加分号即可

(三个数字中的最小值)

#include <iostream>
using namespace std;

int main() {
	int f(int x, int y, int z);
	//如果子函数在main函数之后,要在main函数中先对子函数声明,才能调用
	//在被调用函数的首部加分号即可
	int x, y, z, min;
	cout << "enter three numbers:" << endl;
	cin >> x >> y >> z;
	min=f(x, y, z);
	cout << min;
	return 0}
int f(int x, int y, int c) {
	int m;
	if (x < y) m = x;
	else m = y;
	if (y < m) m = y;
	return(m);
}

三个数字从小到大排序

注意子函数是void形式,而且注意main函数不要忘记写return 0;


#include <iostream>
using namespace std;
int main() {
	void f(int x, int y, int z);
	int x, y, z;
	cout << "enter three numbers:" << endl;
	cin >> x >> y >> z;
	f(x, y, z);
	return 0;
}

void f(int x, int y, int z) {
	int temp;
	if (x > y) {
		temp = x;x = y;y = temp;
	}
	if (z > y) cout << x <<" ," <<y << ", " << z;
	else if(z>x) cout << x << " ," << z << ", " << x;
	else cout << z << " ," << x << ", " << y;
	
}

\b:相当于前移一位

\t:跳到下一个tab位置

#include <iostream>
using namespace std;
int main() {
	int a, b;
	cout << "***\t___\b***" << endl;
	cout << "***\t___***";
	return 0}

在这里插入图片描述
\b使得*符号前移了一位

转义符号

cout << "c\"" << endl;
//正确形式,会输出c"
cout << "c"" << endl;
//错误形式

章节2.8

char在内存是以ASCII码的二进制存储的


#include <iostream>
using namespace std;
int main() {
	char c1 = 'C', c2 = 'h', c3 = 'i', c4 = 'n', c5 = 'a';

	cout << c1 + 4 << c2 + 4 << c3 + 4 << c4 + 4 << c5 + 4 << endl;
	//结果是int型!!!char在内存是以ASCII码的二进制存储的

	char r1=c1 + 4, r2=c2 + 4, r3= c3 + 4, r4= c4+ 4, r5= c5+ 4;
	cout << r1 << r2 << r3 << r4 << r5 << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值