c++ primer plus 第六版 書本中原始碼
阿新 • • 發佈:2019-02-06
第二章
程式清單2.1 myfirst.cpp
//myfirst.cpp -- displays a message
#include<iostream>
int main()
{
using namespace std;
cout << "Come up and C++ me some time.";
cout << endl;
cout << "You won't regret it!" << endl;
cin.get();
return 0;
}
程式清單2.2 carrots.cpp#include<iostream> int main() { using namespace std; int carrots;// carrots = 25; cout << "I hava "; cout << carrots; cout << " carrots."; cout << endl; carrots = carrots - 1; cout << "Crunch,crunch.Now I have " << carrots << " carrots." << endl; cin.get(); return 0; }
程式清單2.3 getinfo.cpp
#include<iostream> int main() { using namespace std; int carrots; cout << "How many carrots do you have ?" << endl; cin >> carrots; cout << "Here are two more. "; carrots = carrots + 2; cout << "Now you have " << carrots << " carrots." << endl; cin.get(); cin.get(); return 0; }
程式清單2.4 sqrt.cpp
#include<iostream> #include<cmath> int main() { using namespace std; double area; cout << "Enter the floor area,in square feet,of your home: "; cin >> area; double side; side = sqrt(area); cout << "That's the equivalent of a square " << side << " feet to the side." << endl; cout << "How fascinating!" << endl; cin.get(); cin.get(); return 0; }