復(fù)習(xí)題
- 入口條件循環(huán)和出口條件循環(huán)之間的區(qū)別是什么硕淑?各種
C++
循環(huán)分別屬于其中的哪一種彬坏?
答:
入口條件:先判斷再執(zhí)行。for童太、while
出口條件:先執(zhí)行再判斷米辐。do while
- 如果下面的代碼片段是有效程序的組成部分胸完,它將打印什么內(nèi)容?
int i;
for ( i = 0; i < 5; i++ )
cout << i;
cout << endl;
答:
0
1
2
3
4
- 如果下面的代碼片段是有效程序的組成部分翘贮,它將打印什么內(nèi)容赊窥?
int j;
for ( j = 0; j < 11; j += 3 )
cout << j;
cout << endl << j << endl;
答:
0369
12
- 如果下面的代碼片段是有效程序的組成部分,它將打印什么內(nèi)容狸页?
int j = 5;
while ( ++j < 9 )
cout << j++ << endl;
答:
6
8
- 如果下面的代碼片段是有效程序的組成部分锨能,它將打印什么內(nèi)容?
int k = 8;
do
cout << " k = " << k << endl;
while( k++ < 5 );
答:
k = 8
- 編寫一個打印
1芍耘、2址遇、4、8斋竞、 16倔约、 32、 64
的for
循環(huán)坝初,每輪循環(huán)都將計數(shù)變量的值乘以2
浸剩。
答:
for ( int i = 1; i < 128; i *= 2 )
{
cout << i << " ";
}
- 如何在循環(huán)體中包含多條語句?
答:
使用大括號將多條語句括起來脖卖。 - 下面的語句是否有效乒省?如果無效,原因是什么畦木?如果有效袖扛,它將完成什么工作?
int x = (1, 024);
答:有效十籍,x=1蛆封。
答:表達(dá)式由兩個表達(dá)式組成——1和024,用逗號運算符連接勾栗。值為右側(cè)表達(dá)式的值惨篱,即024(八進(jìn)制),即20围俘。因此砸讳,該聲明將值20賦給x。
下面的語句如何呢界牡?
int y;
y = 1, 024;
答:無效簿寂。
答:有效。但是運算符優(yōu)先級將導(dǎo)致它被判定成這樣:
(y=1), 024
也就是說宿亡,左側(cè)表達(dá)式將y設(shè)置為1常遂,整個表達(dá)式的值(沒有使用)為024(八進(jìn)制)或20(十進(jìn)制)。 - 在查看輸入方面挽荠,
cin >> ch
同cin.get(ch)
和ch=cin.get()
有什么不同克胳?
答:母雞啊平绩。
答:cin>>ch
將跳過空格、換行符和制表符漠另,其他兩種格式將讀取這些字符捏雌。
編程練習(xí)
- 編寫一個要求用戶輸入兩個整數(shù)的程序。該程序?qū)⒂嬎悴⑤敵鲞@兩個整數(shù)之間(包括這兩個整數(shù))所有整數(shù)的和酗钞。這里假設(shè)先輸入較小的整數(shù)腹忽。例如,如果用戶輸入的是2和9砚作,則程序?qū)⒅赋?~9之間所有整數(shù)的和為44窘奏。
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
int min, max, sum;
cout << "Enter the min and max number: ";
cin >> min >> max;
sum = 0;
for (int i = min; i <= max; i++)
{
sum += i;
}
cout << " The sum is: " << sum << endl;
return 0;
}
- 使用
array
對象(而不是數(shù)組)和long double
(而不是long long
)重新編寫程序清單5.4,并計算100!的值葫录。
// 后面補上
- 編寫一個要求用戶輸入數(shù)字的程序着裹。每次輸入后,程序都將報告到目前為止米同,所有輸入的累計和骇扇。當(dāng)用戶輸入0時,程序結(jié)束面粮。
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
int num, sum;
sum = 0;
while (true)
{
cout << "Enter the min and max number: ";
cin >> num;
if (0 == num)
break;
sum += num;
cout << "sum is : " << sum << endl;
}
return 0;
}
- Daphne以10%的単例投資了100美元少孝。也就是說,每一年的利潤都是投資額的10%熬苍,即每年10美元:
利息 = 0.10 * 原始存款
而Cleo以5%的復(fù)利投資了100美元稍走。也就是說,利息是當(dāng)前存款(包括獲得利息)的5%:
利息 = 0.05 * 當(dāng)前存款
Cleo在第一年投資100美元的盈利是5%——得到了105美元柴底。下一年的盈利是105美元的5%——即5.25美元婿脸,以此類推。請編寫一個程序柄驻,計算多少年后狐树,Cleo的投資價值才能超過Daphne的投資價值,并顯示此時兩個人的投資價值鸿脓。
#include <iostream>
#include <string>
using namespace std;
const int money = 100;
int main(void)
{
float Daphne, Cleo;
int years = 0;
Daphne = float(money);
Cleo = float(money);
while (true)
{
years++;
Daphne += float(money) * 0.1;
Cleo += Cleo * 0.05;
if (Cleo > Daphne)
break;
}
cout << "years: " << years << " Daphne: " << Daphne << " Cleo: " << Cleo << endl;
return 0;
}
- 假設(shè)要銷售
《C++ For Fools》
一書抑钟。請編寫一個程序,輸入全年中每個月的銷售量(圖書數(shù)量野哭,而不是銷售額)味赃。程序通過循環(huán),使用初始化為月份字符串的char*
數(shù)組(或者string
對象數(shù)組)逐月進(jìn)行提示虐拓,并將輸入的數(shù)據(jù)存儲在一個int
數(shù)組中。然后傲武,程序計算數(shù)組中各元素的總數(shù)蓉驹,并報告這一年的銷售情況城榛。
#include <iostream>
#include <string>
using namespace std;
const int Month = 12;
int main(void)
{
string monthArray[Month] = { "Jan.","Feb.","Mar.", "Apr.", "May.","Jun.","Jul.","Aug.","Sept.","Oct.","Nov.", "Dec." };
int numArray[Month] = { 0 };
int total = 0;
for (int i = 0; i < Month; i++)
{
cout << "Enter " << monthArray[i] << " number: ";
cin >> numArray[i];
total += numArray[i];
}
cout << "Total : " << total << endl;
for (int i = 0; i < Month; i++)
{
cout << monthArray[i] << " " << numArray[i] << endl;
}
return 0;
}
- 完成編程練習(xí)5,但這一次要使用一個二維數(shù)組來存儲輸入——3年中每個月的銷售量态兴。程序?qū)蟾婷磕赇N售量以及3年的總銷售量狠持。
#include <iostream>
#include <string>
using namespace std;
const int Year = 3;
const int Month = 12;
int main(void)
{
string monthArray[Year][Month] = { { "Jan.","Feb.","Mar.", "Apr.", "May.","Jun.","Jul.","Aug.","Sept.","Oct.","Nov.", "Dec." }\
,{ "Jan.","Feb.","Mar.", "Apr.", "May.","Jun.","Jul.","Aug.","Sept.","Oct.","Nov.", "Dec." } \
,{ "Jan.","Feb.","Mar.", "Apr.", "May.","Jun.","Jul.","Aug.","Sept.","Oct.","Nov.", "Dec." } };
int numArray[Year][Month] = { 0 };
int total = 0;
for (int i = 0; i < Year; i++)
{
for (int j = 0; j < Month; j++)
{
cout << "Enter the " << i+1 << " year " << monthArray[i][j] << " number: ";
cin >> numArray[i][j];
total += numArray[i][j];
}
}
cout << "Total : " << total << endl;
for (int i = 0; i < Year; i++)
{
for (int j = 0; j < Month; j++)
{
cout << monthArray[i][j] << " " << numArray[i][j] << endl;
}
}
return 0;
}
- 設(shè)計一個名為
car
的結(jié)構(gòu),用它存儲下述有關(guān)汽車的信息:生產(chǎn)商(存儲在字符數(shù)組或string
對象中的字符串)瞻润、生產(chǎn)年份(整數(shù))喘垂。編寫一個程序,向用戶詢問有多少輛汽車绍撞。隨后正勒,程序使用new
來創(chuàng)建一個由相應(yīng)數(shù)量的car
結(jié)構(gòu)組成的動態(tài)數(shù)組。接下來傻铣,程序提示用戶輸入每輛車的生產(chǎn)商(可能由多個單詞組成)和年份信息章贞。請注意,這需要特別小心非洲,因為它將交替讀取數(shù)值和字符串(參見第4章)鸭限。最后,程序?qū)@示每個結(jié)構(gòu)的內(nèi)容两踏。該程序的運行情況如下:
How many cars do you wish to catalog? 2
Car#1:
Please enter the make: Hudson Hornet
Please enter the year made: 1952
Car#2:
Please enter the make: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser
#include <iostream>
#include <string>
using namespace std;
struct car {
string make;
int year;
};
int main(void)
{
int number;
car* pcar = NULL;
char ch;
cout << "How many cars do you wish to catalog?: ";
cin >> number;
pcar = new car[number];
for (int i = 0; i < number; i++)
{
ch = cin.get(); // 加上這句的目的是將緩沖區(qū)中殘留的字符拿走败京,這里是"\n"
cout << "Car#" << i+1 << ":" << endl;
cout << "Please enter the make: ";
getline(cin, pcar[i].make);
cout << "Please enter the year made: ";
cin >> pcar[i].year;
NULL;
NULL;
}
cout << "Here is your collection:" << endl;
for (int i = 0; i < number; i++)
{
cout << pcar[i].year << " " << pcar[i].make << endl;
}
return 0;
}
- 編寫一個程序,它使用一個
char
數(shù)組和循環(huán)來每次讀取一個單詞梦染,直到用戶輸入done為止赡麦。隨后,改程序指出用戶輸入了多少個單詞(不包括done在內(nèi))弓坞。下面是該程序的運行情況:
Enter words (to stop, type the word done):
anteater brithday category dumpster envy finagle geometry done for sure
You entered a total of 7 words.
您應(yīng)在程序中包含頭文件cstring
隧甚,并使用函數(shù)strcmp()
來進(jìn)行比較測試。
#include <iostream>
#include <cstring>
using namespace std;
int main(void)
{
char str[100] = { 0 };
int count = 0;
while (true)
{
cout << "Enter words (to stop, type the word done):";
cin >> str;
if (0 == strcmp(str, "done"))
{
break;
}
count++;
}
cout << "You entered a total of " << count << " words." << endl;
return 0;
}
- 編寫一個滿足前一個練習(xí)中描述的程序渡冻,但是用
string
對象而不是字符數(shù)組戚扳。請在程序中包含頭文件string
,并使用關(guān)系運算符來進(jìn)行比較測試族吻。
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string str;
int count = 0;
while (true)
{
cout << "Enter words (to stop, type the word done):";
cin >> str;
if (str == "done")
{
break;
}
count++;
}
cout << "You entered a total of " << count << " words." << endl;
return 0;
}
- 編寫一個使用嵌套循環(huán)的程序帽借,要求用戶輸入一個值,指出要顯示多少行超歌。然后砍艾,程序?qū)@示相應(yīng)行數(shù)的星號,其中第一行包括一個星號巍举,第二行包括兩個星號脆荷,依此類推。每一行包含的字符數(shù)等于用戶指定的行數(shù),在星號不夠的情況下蜓谋,在星號簽名加上句點梦皮。該程序的運行情況如下:
Enter number of rows: 5
....*
...**
..***
.****
*****
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
int number;
cout << "Enter number of rows: ";
cin >> number;
for (int i = number-1; i >= 0; i--)
{
for (int j = 0; j < i; j++)
{
cout << ".";
}
for (int k = 0; k < number - i; k++)
{
cout << "*";
}
cout << endl;
}
return 0;
}