c++ primer plus 第六版程式設計練習答案第五章
阿新 • • 發佈:2018-11-17
該章節第二題程式沒問題,但不懂為什麼
5.1
#include<stdafx.h> #include <iostream> using namespace std; int main() { int max, min; long long sum=0; cout << "enter the max number: "; cin >> max; cout << "enter the min number; "; cin >> min; for (int i = max ; i >= min; i--) sum = i + sum; cout << "sum of the integer between max and min is: "<<sum<<endl; return 0; } 注意:for迴圈括號中的最後一條語句是在執行了迴圈體之後執行的
5.2
#include<stdafx.h> #include <iostream> #include<array> const int Arsize = 100; using namespace std; int main() { array<long double, Arsize+1> factorials; factorials[0] = factorials[1] = 1; for (int i = 2; i <= Arsize; i++) factorials[i]= i * factorials[i-1]; for (int i = 0; i <= Arsize; i++) cout <<i <<"!= "<<factorials[i]<<endl; //cout << "size of long double :" << sizeof(long double) << endl; //cout << "size of long long:" << sizeof(long long) << endl; return 0; }
問題1:當定義array的時候用array<long double, Arsize> factorials;會出現array subscript out of range 的錯誤;若用 array<long double, Arsize+1> factorials;則可以正確顯示結果;:
問題2:若將定義array行改為:array<long long, Arsize+1> factorials;即用long long型別的資料,計算出的階乘結果出現錯誤,用sizeof(long long)和sizeof(long double)檢視兩種資料型別所佔的位元組數都顯示的是8位,那麼也就是說不是long long資料型別長度不夠
5.3
#include<stdafx.h>
#include <iostream>
using namespace std;
int main()
{
int num;
long long sum=0;
cout << "enter a number:";
cin >> num;
while (num != 0)//不能用(num=cin.get)!=0作為測試條件,此時會將數字看做字元,將其ASCII碼存入num,且會將輸入完成的換行符作為下一個輸入
{
sum = sum + num;
cout << "now the sum of the number you enter is:" << sum << endl;
cout << "enter next number: ";
cin >> num;
}
return 0;
}
5.4
#include<stdafx.h>
#include <iostream>
const int principal = 100;
const int D_intter_per_ye = 10;//Daphne每年利息都相同
const double C_interest = 0.05;//Cleo的利率,最好將資料型別定義為相同的,否則可能造成結果錯誤
using namespace std;
int main()
{
double Daphne=principal;
double Cleo = principal;
int year = 0;
while (Daphne>=Cleo)
{
year++;
Daphne =D_intter_per_ye+ Daphne;//直接用加上每年的利率,不用乘,運算速度更快
Cleo = Cleo * C_interest + Cleo;
}
cout << year << " years later Cleo's deposit lager than Daphne's\n"
<< "now,Cloe's deposit is: " << Cleo << endl
<< "now,Daphne's deposit is: " << Daphne << endl;
return 0;
}
5.5
#include<stdafx.h>
#include <iostream>
const int monthes = 12;
using namespace std;
int main()
{
const char *month[monthes] =
{
"Jan.","Feb.","Mar.","Apr.","May.","Jun.",
"Jul.","Aug.","Sept.","Oct.","Nov.","Dec"
};
int sales[monthes],sum=0;
for (int i = 0; i < monthes; i++)
{
cout << "enter the sales of " << month[i]<<": ";
cin >> sales[i];
sum = sum + sales[i];
}
cout << "Total sales this year is: " << sum;
return 0;
}
5.6
#include<stdafx.h>
#include <iostream>
const int monthes = 12;
const int years = 3;
using namespace std;
int main()
{
const char *month[monthes] =
{
"Jan.","Feb.","Mar.","Apr.","May.","Jun.",
"Jul.","Aug.","Sept.","Oct.","Nov.","Dec"
};
int sales[years][monthes];
int sum[years] = {0};
int total = 0;
for (int i = 0; i < years; i++)
{
cout << "enter No." << i + 1 << " year's sales of <C++ For Fools> each month\n";
for (int j = 0; j < monthes; j++)
{
cout << month[j] << ": ";
cin >> sales[i][j];
sum[i] = sum[i] + sales[i][j];
total = total + sales[i][j];
}
cout << "No." << i + 1 << "year's total sales is: " << sum[i]<<endl;
}
cout << " total sales of three years is: " << total;
return 0;
}
5.7
#include<stdafx.h>
#include <iostream>
#include<string>
using namespace std;
struct car
{
string manufacturer;
int prod_year;
};
int main()
{
int number;
cout << "How many cars do you wish to catalig? ";
cin >> number;
cin.get();//用於吸收輸入數字後的換行符,否則將會將其認為下一個字元的輸入
car *c_message= new car[number];//用new建立一個包含number個元素的car結構
for (int i = 0; i < number; i++)
{
cout << "Car #" << i + 1 << ":\n";
cout << "Please enter the make: ";
getline(cin,(c_message+i)->manufacturer);
cout << "Please enter the year made: ";
cin >> (c_message+i)->prod_year;
cin.get();//用於吸收輸入數字後的換行符,否則將會將其認為下一個汽車的生產商名稱
}
cout << "Here is your collection:\n";
for (int i = 0; i < number; i++)
{
cout << (c_message + i)->prod_year << " " << (c_message)->manufacturer<<endl;
}
return 0;
}
5.8
#include<stdafx.h>
#include <iostream>
using namespace std;
int main()
{
char words[20];//用於存放每次輸入的單詞,其值為最後一次輸入的單詞
int w_coun=0;
cout << "Enter words(to stop,type the word done):\n";
cin >> words;//輸入的是單詞,不需識別空格等,用cin就可以
while (strcmp(words, "done"))
{
w_coun++;
cin >> words;
}
cout << "You entered a total of " << w_coun << " words";
return 0;
}
5.9
#include<stdafx.h>
#include<iostream>
#include<string>
int main()
{
using namespace std;
string word;
int w_coun = 0;
cout << "Enter words(to stop,type the word done):\n";
cin >> word;//輸入的是單詞,不需識別空格等,用cin就可以
while (word!="done")
{
w_coun++;
cin >> word;
}
cout << "You entered a total of " << w_coun << " words";
return 0;
}
5.10
#include<stdafx.h>
#include<iostream>
int main()
{
using namespace std;
int row;
cout << "Enter number of rows: ";
cin >> row;
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= row - i; j++)
cout << ".";
for (int n = 1; n <= i; n++)
cout << "*";
cout << endl;
}
return 0;
}