《书本以及习题第二章部分》
函数调用格式:
如果子函数在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;
}