5.8 复习题
1.入口条件循环,是先判断,为真进入循环,为假退出循环。
出口条件循环,先循环一次,末尾进行判断,为真再次进行循环,为假退出循环。
2.输出:01234
3.输出:0369
12
4.输出:6
8
5.输出: k = 8
6.for (int i = 1; i < 100; i *= 2)
cout << i << endl;
7.加大括号
8.x=20 原因:小括号优先级高,计算内部运算值赋给x,“024”是八进制代表十进制的“20”
y=1 原因:赋值运算优先级高于“,”,优先进行赋值运算,将“1”赋给y
9."cin>>ch"会忽略后面的所有空白符;"cin.get(ch)"和"ch=cin.get()"不会忽略,会将读取到的都保存在变量里面。
5.9编程练习
1.
#include<iostream>
using namespace std;
int main(){
//1.
int f_num, l_num;
cout << "Please enter the first number: ";
cin >> f_num;
cout << "Please enter the last number: ";
cin >> l_num;
int sum = 0;
if (f_num > l_num) {
return false;
}
else {
for (f_num; f_num <= l_num; f_num++) {
sum += f_num;
}
}
cout << "The sum: " << sum << endl;
system("pause");
return 0;
}
2.
#include<iostream>
#include<array>
using namespace std;
int main() {
//2.
array<long double, 100> factorials;
factorials[1] = factorials[0] = 1;
for (int i = 2; i < 100; i++) {
factorials[i] = i * factorials[i - 1];
}
//cout << "100! = " << factorials[100] << endl;
for (int i = 0; i < 100; i++) {
cout << i << "!= " << factorials[i] << endl;
}
system("pause");
return 0;
}
3.
#include<iostream>
using namespace std;
int main() {
//3.
int sum = 0;
do {
int num;
cout << "Please enter a number: ";
cin >> num;
if (num == 0) break;
sum += num;
cout << "now, the sum = " << sum << endl;
} while (true);
system("pause");
return 0;
}
4.
#include<iostream>
using namespace std;
struct value {
double money;
double rate;
};
int main() {
//4.
value Daphne, Cleo;
Daphne.money = Cleo.money = 100;
Daphne.rate = 0.1;
Cleo.rate = 0.05;
double D_investval = Daphne.money;
double C_investval = Cleo.money;
int flag = 0;
while (D_investval >= C_investval) {
D_investval += Daphne.money * Daphne.rate;
C_investval = C_investval * (1 + Cleo.rate);
flag++;
}
cout << flag << " years passed, Cleo's investment value is more than Daphne's investment value. " << endl;
cout << "The final investment value of Daphne is:" << D_investval << endl;
cout << "The final investment value of Cleo is:" << C_investval << endl;
system("pause");
return 0;
}
5.
#include<iostream>
using namespace std;
int main() {
//5.
string month[12] = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
int sales[12];
int sum = 0;
for (int i = 0; i < 12; i++) {
cout << month[i] << "'s sales:";
cin >> sales[i];
}
for (int i = 0; i < 12; i++) {
sum += sales[i];
}
cout << "The sum is: " << sum << endl;
system("pause");
return 0;
}
6.
#include<iostream>
using namespace std;
int main() {
//6.
string month[12] = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
int sales[3][12];
int sum[3] = { 0 };
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 12; i++) {
cout << "Year " << j+1 << ": ";
cout << month[i] << "'s sales:";
cin >> sales[j][i];
}
cout << endl;
}
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 12; i++) {
sum[j] += sales[j][i];
}
}
for (int j = 0; j < 3; j++) {
cout << "The year " << j + 1 << "'s sum is : " << sum[j] << endl;
}
system("pause");
return 0;
}
7.
#include<iostream>
using namespace std;
struct Car {
char company[30];
int year;
};
int main() {
//7.
int num = 0;
cout << "How many cars do you wish to catalog? ";
cin >> num;
cin.ignore();
Car* car = new Car[num];
for (int i = 0; i < num; i++) {
cout << "Car #" << i + 1 << ": " << endl;
cout << "Please enter the make: ";
cin.getline(car[i].company, 30);
//cin.ignore();
cout << "Please enter the year made: ";
cin >> car[i].year;
cin.ignore();
}
cout << "Here is your collection:" << endl;
for (int i = 0; i < num; i++) {
cout << car[i].year << " " << car[i].company << endl;
}
system("pause");
return 0;
}
10.
#include<iostream>
using namespace std;
int main() {
//10.
int line = 0;
cout << "Enter number of rows: ";
cin >> line;
for (int i = 0; i < line; i++) {
for (int j = 0; j < line - i - 1; j++) {
cout << ". ";
}
for (int j = 0; j < i+1; j++) {
cout << "* ";
}
cout << endl;
}
system("pause");
return 0;
}