6.完成編程練習5,但這一次使用一個二維數組來存儲輸入一3年中每個月的銷售量。程序將報告每年銷售量以及三年的總銷售量
#include<iostream>
using namespace std;
int main() {
// 月份,二維數組存儲三年的銷售量
const string months[12] = {"1月","2月","3月","4月","5月","6月",
"7月","8月","9月","10月","11月","12月"};
int numbers[3][12]; // 存放三年的圖書數量
// 2.循環提示用户輸入
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 12; ++j) {
cout << "請輸入第" << (i + 1) << "年,第" << months[j] << "的圖書數量";
cin >> numbers[i][j];
}
}
// 3. 報告每年銷售量以及三年的總銷售量
int sum = 0;
for (int i = 0; i < 3; ++i) {
int year_sum = 0;
for (int j = 0; j < 12; ++j) {
sum += numbers[i][j];
year_sum += numbers[i][j];
}
cout << "第" << (i + 1) <<"年的銷售量為:" << year_sum << endl;
}
cout << "這三年的總銷售量為:" << sum << endl;
return 0;
}
xaye@orange:~/code/dev/18$ ./a.out
請輸入第1年,第1月的圖書數量1
...
請輸入第1年,第12月的圖書數量1
請輸入第2年,第1月的圖書數量2
...
請輸入第2年,第12月的圖書數量2
請輸入第3年,第1月的圖書數量3
...
請輸入第3年,第12月的圖書數量3
第1年的銷售量為:12
第2年的銷售量為:24
第3年的銷售量為:36
這三年的總銷售量為:72
7.設計一個名為car的結構,用它存儲下述有關汽車的信息:生產商(存儲在字符數組或string對象中的字符串)、生產年份(整數)。編寫一個程
序,向用户詢問有多少輛汽車。隨後,程序使用new來創建一個由相應數量的car結構組成的動態數組。接下來,程序提示用户輸入每輛汽車的生產商
(可能由多個單詞組成)和年份信息。請注意,這需要特別小心,因為它將交替讀取數值和字符串。最後,程序將顯示每個結構的內容。
該程序的運行情況如下:
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>
using namespace std;
// 1. 定義結構體
struct car {
char maker[20];
int year;
};
int main() {
// 2. 提示用户輸入,創建數組
cout << "How many cars do you wish to catalog?" << endl;
int number;
cin >> number;
car* cars = new car[number];
for (int i = 0; i < number; ++i) {
// cin.get() // 如果輸入的有空格 則必須加這個
cout << "Car #" << (i + 1) << endl;
cout << "Please enter the make:";
cin >> cars[i].maker;
cout << "Please enter the year made:";
cin >> cars[i].year;
}
cout << "Here is your collection:" << endl;
// 3. 循環輸出用户信息
for (int i = 0; i < number; ++i) {
cout << cars[i].year << " " << cars[i].maker << endl;
}
// 4. 釋放內存
delete[] cars;
return 0;
}
xaye@orange:~/code/dev/18$ ./a.out
How many cars do you wish to catalog?
2
Car #1
Please enter the make:Hudson
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
1951 Kaiser
8.編寫一個程序,它使用一個char數組和循環來讀取一個單詞,直到用户輸入done為止。隨後,該程序指出用户輸入了多少單詞(不包括done在內)。
下面是該程序的運行情況:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
#include<iostream>
using namespace std;
#include <cstring> // +
int main() {
// 1. 提示用户輸入
cout << "Enter words (to stop, type the word done):";
// 2. 不斷循環單詞進行比較判斷
char word[20];
cin >> word;
int sum = 0;
while (strcmp(word, "done") != 0) {
++sum;
cin >> word;
}
// 3. 輸出
cout << "You entered a total of "<< sum <<" words." << endl;
return 0;
}
xaye@orange:~/code/dev/18$ ./a.out
Enter words (to stop, type the word done):anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
9.編寫一個滿足前一個練習中描述的程序,但使用string對象而不是字符數組。請在程序中包含string,並使用關係運算符來進行比較測試。
#include<iostream>
using namespace std;
#include <cstring>
int main() {
//1.提示用户輸入
cout << " Enter words (to stop, type the word done):";
//2.不斷循環單詞進行比較判斷
string word;
cin >> word;//讀到空格截止
int sum = 0;
while (word != "done") {
++sum;
cin >> word;
}
// 3. 輸出
cout << "You entered a total of "<< sum <<" words." << endl;
return 0;
}
10.編寫一個使用嵌套循環的程序,要求用户輸入一個值,指出要顯示多少行。然後,程序將顯示相應行數的星號,其中第一行包括一個星號,第二行包
括兩個星號,以此類推。每一行包含的字符數等於用户指定的行數,在星號不夠的情況下,在星號前面加上句點。
....*
...**
..***
.****
*****
典中典 ,想起來在網吧用記事本敲雙重for循環了 哈哈哈哈哈。
#include<iostream>
using namespace std;
int main() {
// 1. 接收用户的輸出
cout << "請輸入一個數值:";
int number;
cin >> number;
// 2. 循環嵌套輸出
for (int i = 0; i < number; ++i) {
// 2.1 用來輸出 .
for (int j = 0; j < (number - i - 1); ++j) {
cout << ".";
}
// 2.2 用來輸出 *
for (int j = 0; j < (i + 1); ++j) {
cout << "*";
}
cout << endl;
}
return 0;
}
xaye@orange:~/code/dev/18$ ./a.out
請輸入一個數值:8
.......*
......**
.....***
....****
...*****
..******
.*******
********