#include<iostream> using namespace std; void mice(); void howrun(); int main() { mice(); mice(); howrun(); howrun(); } void mice() { cout << "Three Blind mice" << endl; } void howrun() { cout << "See how they run" << endl; }
输出:
Three Blind mice Three Blind mice See how they run See how they run
二、用户输入年龄,显示该年龄包含多少个月?
#include<iostream> using namespace std; int monthnumber(int); int main() { cout << "please input your age:" << endl; int age; cin >> age; int month = monthnumber(age); cout << month << "monthes" << endl; return 0; } int monthnumber(int age) { return age * 24; }
输出:
please input your age: 24 576monthes
#include<iostream> using namespace std; void showtime(int, int); int main() { int hour; int minute; cout << "Enter hour: "; cin >> hour; cout << "Enter minutes: "; cin >> minute; showtime(hour,minute); } void showtime(int hour, int minute) { cout << "Time " << hour << " : " << minute << endl; }
输出:
Enter hour: 3 Enter minutes: 15 Time 3 : 15
#include<iostream> #include<climits> using namespace std; int main() { short n_short = SHRT_MAX; int n_int = INT_MAX; long n_long = LONG_MAX; long long n_long_long = LLONG_MAX; cout << " short: " << sizeof(n_short) << " bytes." << endl; cout << " int: " << sizeof(n_int) << " bytes." << endl; cout << " long: " << sizeof(n_long) << " bytes." << endl; cout << " long long: " << sizeof(n_long_long) << " bytes." << endl; cout << "max num:" << endl; cout << "short: " << n_short << endl; cout << "int: " << n_int << endl; cout << "long: " << n_long << endl; cout << "long long: " << n_long_long << endl; cout << "min: " << INT_MIN << endl; cout << "Bits per byte: " << CHAR_BIT << endl; }
输出:
short: 2 bytes. // short int 占2个字节,每个字节8位,总计16位 int: 4 bytes. // int 占 4个字节,每个字节8位,总计32位 long: 8 bytes. // .... long long: 8 bytes. max num: short: 32767 int: 2147483647 long: 9223372036854775807 long long: 9223372036854775807 min: -2147483648 Bits per byte: 8
无符号的说白了就是增大数据可表示范围,注意::该值不能表示复数,仅仅表示正数
#include<iostream> #include<climits> #define ZERO 0 using namespace std; int main() { short sam = SHRT_MAX; unsigned short sue = sam; cout << " sam value is: " << sam << ", sue vale is: " << sue << endl; sam += 1; sue += 1; cout << " sam value is: " << sam << ", sue vale is: " << sue << endl; sam = ZERO; sue = ZERO; cout << " sam value is: " << sam << ", sue vale is: " << sue << endl; sam -= 1; sue -= 1; cout << " sam value is: " << sam << ", sue vale is: " << sue << endl; return 0; }
输出:
sam value is: 32767, sue vale is: 32767 sam value is: -32768, sue vale is: 32768 sam value is: 0, sue vale is: 0 sam value is: -1, sue vale is: 65535
#include<iostream> #include<climits> using namespace std; int main() { int chest = 42; int waist = 42; int inseam = 42; cout << "10 jinzhi: chest " << chest << endl; cout << hex; cout << "16 jinzhi: waist " << waist << endl; cout << oct; cout << "8 jinzhi: inseam " << inseam << endl; return 0; }
输出:
10 jinzhi: chest 42
16 jinzhi: waist 2a
8 jinzhi: inseam 52
#include<iostream> #include<climits> using namespace std; int main() { int chest = 42; int waist = 0x42; int inseam = 042; cout << "10 jinzhi: chest " << chest << endl; cout << "16 jinzhi: waist " << waist << endl; cout << "8 jinzhi: inseam " << inseam << endl; return 0; }
输出:
10 jinzhi: chest 42 16 jinzhi: waist 66 8 jinzhi: inseam 34
#include<iostream> #include<climits> using namespace std; int main() { char ch; cout << "please input: " << endl; cin >> ch; cout << "character is: " << ch << endl; return 0; }
输出:
please input: M character is: M
输入字符M,cin首先将M转换为77,输出时,将77转换为M;
也就是说,cout和cin的行为都是由变量类型引导的;
#include<iostream> #include<climits> using namespace std; int main() { char ch; int i; cout << "please input ch: " << endl; cin >> ch; i = ch; cout << "character: " << ch << ", code: " << i << endl; ch += 1; i = ch; cout << "character: " << ch << ", code: " << i << endl; cout << "displaying char in using cout.put(ch): "; cout.put(ch); cout << endl; cout << "displaying a char constant in using cout.put: "; cout.put('i'); cout << endl; return 0; }
输出:
please input ch: M character: M, code: 77 character: N, code: 78 displaying char in using cout.put(ch): N displaying a char constant in using cout.put: i
#include<iostream> #include<climits> using namespace std; int main() { bool right = true; bool wrong = false; cout << "bool: " << right << ", " << wrong << endl; int righ = true; int wron = false; cout << "int: " << righ << ", " << wron << endl; bool ri = -100; bool wr = 0; cout << "bool: " << ri << ", " << wr << endl; return 0; }
输出:
bool: 1, 0 int: 1, 0 bool: 1, 0
十一、const 限定符
与#define相比,可以限定作用域;可以适用于更复杂的数据类型;
#include<iostream> #include<climits> using namespace std; const int Year = 2016; int main() { const int Months = 12; cout << Year << " "<< Months << endl; return 0; }
输出:
2016 12
十二、