C++ 学习笔记 - 《C++ Primer Plus》习题 2.7
显示姓名和地址
#include <iostream> using namespace std; int main() { //char name[5] = {'J', 'e', 'f', 'f', '\0'}; //char address[9] = {'S', 'h', 'e', 'n', 'z', 'h', 'e', 'n', '\0'}; char name[] = "Jeff"; char address[] = "Shenzhen"; cout << "My name is " << name << "." << endl; cout << "I live in " << address << "." << endl; return 0; }
单位转换(1 long = 220 码)
#include <iostream> using namespace std; int main() { int m = 0; cout << "请输入一个long型数:"; cin >> m; cout << m << " long = " << m * 220 << " 码" << endl; return 0; }
函数的应用1
#include <iostream> using namespace std; void func1(); void func2(); int main() { func1(); func1(); func2(); func2(); return 0; } void func1() { cout << "Three blind mice." << endl; } void func2() { cout << "See how they run." << endl; }
函数的应用2
#include <iostream> using namespace std; int func1(int); int main() { int age; cout << "Enter your age: "; cin >> age; cout << "该年龄包含" << func1(age) << "个月。" << endl; return 0; } int func1(int age) { return age * 12;
函数的应用3
#include <iostream> using namespace std; float func1(float); int main() { float lightYears; cout << "Please enter the number of light years: "; cin >> lightYears; cout << lightYears << " light years = " << func1(lightYears) << " astronomical units." << endl; return 0; } float func1(float lightYears) { return lightYears * 63240.0; }
函数的应用4
#include <iostream> using namespace std; void func1(int hours, int minutes); int main() { int hours, minutes; cout << "Enter the number of hours: "; cin >> hours; cout << "Enter the number of minutes: "; cin >> minutes; func1(hours, minutes); return 0; } void func1(int hours, int minutes) { cout << "Time: " << hours << ":" << minutes << endl;; }