C++ Primer Plus第6版—第2章課後習題答案
阿新 • • 發佈:2019-01-12
//第1題
#include<iostream>
int main()
{
using namespace std;
cout << "Enter your name:wahaha" << endl;
cout << "Enter your address:beijing" << endl;
return 0;
}
//第2題 #include<iostream> int main() { using namespace std; cout << "輸入一個以long為單位的距離:" << endl; int m,n; cin >> n; m = n * 220; cout << "轉換結果為:" << m << endl; return 0; }
//第3題 #include<iostream> void function1(); void function2(); using namespace std; int main() { function1(); function1(); function2(); function2(); return 0; } void function1() { cout << "Three blind mice" << endl; return; } void function2() { cout << "See how they run" << endl; return; }
//第4題
#include<iostream>
int main()
{
using namespace std;
cout << "Enter your age:";
int age;
cin >> age;
cout << "包含:" << age * 12 <<"個月"<< endl;
return 0;
}
//第5題 #include<iostream> using namespace std; void function(double n); int main() { cout << "Please enter a Celsius value:"; double n; cin >> n; function(n); return 0; } void function(double n) { double m; m = 1.8*n + 32.0; cout << n << " degree Celsius is " << m << " degree Fahrenheit." << endl; return; } //如果寫成以下方式,就會出現:無法從“void”轉換為“double”的錯誤 #include<iostream> using namespace std; void function(double n); int main() { cout << "Please enter a Celsius value:"; double m,n; cin >> n; m=function(n); cout << n << "degree Celsius is" << m << "degree Fahrenheit." << endl; return 0; } void function(double n) { double a; a = 1.8*n + 32.0; return; }
//第6題
#include<iostream>
using namespace std;
void function(double n);
int main()
{
cout << "Enter the number of light years: ";
double n;
cin >> n;
function(n);
return 0;
}
void function(double n)
{
double m;
m = 63240*n;
cout << n << " light years = " << m <<" astronmical units." << endl;
return;
}
//第7題
#include<iostream>
using namespace std;
void function(int m,int n);
int main()
{
int m, n;
cout << "Enter the number of hours: ";
cin >> m;
cout << "Enter the number of minutes: ";
cin >> n;
function(m, n);
return 0;
}
void function(int m,int n)
{
cout << "time: "<< m << ":" <<n<< endl;
return;
}