//复习题
/*1.入口条件循坏和出口条件循环之间的区别是什么?
各种C++循环分别属于其中的哪一种?
入口:先判断,再运行 for while
出口:先执行,再判断 do - while
2.如果下面的代码片段是有效程序的组成部分,它将打印什么内容?
int i;
for(i = 0;i<5;i++ ){
cout<<i;
cout<<endl;
}
打印结果 : 0
1
2
3
4
3.下面的代码的打印结果
int j;
for(j=0;j<11;j+=3)
cout<<j;
cout<<endl<<j<<endl;
打印: 0369
12
4.int j = 5
while(++j<9)
cout << j++ <<endl;
//j++使用当前j的值再加1 ++j先加1再使用值
6 8
5.
int k = 8;
do
cout<<"k="<<k<<endl;
while(k++<5);
k=8
6.编写一个打印1,2,4,6,8,16,32的for循环 每轮循环将计数变量乘以2
int number = 1;
for (int i = 0; i < 6; i++) {
if (i == 0) {
cout << number << ",";
continue;
}
number = number * 2;
cout << number << ",";
}
7.如何在循环体中包含多条语句?
{}里面写多条语句呗
8.下面语句是否有效,否原因,是结果
int x = {1,024};
无效,花括号用于初始化结构体,数组,类
int y;
y = 1,024;
无效,但是y被赋值1
9.在查看输入方面 cin>>ch 和 cin.get(ch) ch=cin.get() 的不同
cin >> ch
这是一个格式化输入操作,它跳过空白字符(如空格、制表符、换行符等)直到找到非空白字符。
它通常用于读取一个完整的单词或数字,而不是单个字符,但在这里它被用来读取一个字符。
如果在读取字符后紧跟着有空白字符(如空格或换行符),它们将被留在输入流中,等待下一次读取。
cin.get(ch)
这是一个非格式化输入操作,它读取输入流中的下一个字符,不论是否是空白字符。
如果流中有字符,它将被读取并存储在变量 ch 中。
它不会跳过任何空白字符。
如果在读取字符后遇到文件结束标记(EOF)或输入错误,cin.get(ch) 会将 cin 设置为失败状态(failbit),并且不会将任何值赋给 ch(在C++11及更高版本中,如果 char 类型的变量被用作参数,它将保持未修改)。
ch = cin.get()
这与 cin.get(ch) 功能上几乎相同,但它是函数调用的形式,而不是成员函数调用的形式。
它也是非格式化输入,读取输入流中的下一个字符。
返回读取的字符,并将其赋值给变量 ch。
如果遇到EOF或输入错误,cin 将被设置为失败状态,但 ch 仍将被赋值为 EOF
*/
//编程题
/*5.9.1
编写一个要求用户输入两个整数的程序.该程序将计算并输出这两个整数之间(包括这两个整数)
所有的整数和,这里假设先输入较小的数. 2 和 9 答案为44
*/
//#include <iostream>
//
//using namespace std;
//
//
//int main() {
//
// int Min,Max;
// int sum = 0;
//
// cout << "输入较小的整数" << endl;
// cin >> Min;
// cout << "输入较大的整数" << endl;
// cin >> Max;
//
// for (int i = Min; i < Max+ 1; i++){
// sum = sum + i;
// }
//
// cout << sum;
//}
/*使用array对象和long double重新编写5.4计算100!的值
*/
// //源代码 循环计算0-15的阶乘
//#include<iostream>
//#include<array>
//using namespace std;
//
//const int ArSize = 16;
//
//int main() {
// long long factorials[ArSize];
// //array
// array<long double, 101> x;
//
// factorials[1] = factorials[0] = 1L;
// x[1] = x[0] = 1LL;
//
// for (int i = 2; i < x.size(); i++){
// x[i] = i * x[i - 1];
//}
// cout << "100!=" << x[100] << endl;
//
// 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;
// }
//
//
// return 0;
//}
/*3.编写一个用户输入数字的程序,每次输入后打印累计和,和为0时结束*/
//#include<iostream>
//
//using namespace std;
//
//int main() {
//
// int sum = 0;
// while(1){
// int x;
// cout << "输入数字:" << endl;
// cin >> x;
// sum = x + sum;
// cout << "到此为止,和为:" << sum << endl;
//
// if (sum == 0) {
// cout << "和为0,程序结束" << endl;
// break;
// }
//
// }
//
// return 0;
//}
/*4.D以%10的单利投资了100美元,也就是每年的利润为投资额的%10,即10美元
利息 = 0.10 * 原始存款
而C以5%的复利投资了100美元 利息是当前存款的%5
c第一年 105 ,下一年的利润 105 * 5%
写程序判断那一年C的投资价值>D,并且显示两人当前的投资价值
*/
//#include<iostream>
//
//using namespace std;
//
//const double benjin = 100.0;
//
//int main() {
//
// double d_sum = 0.0, c_sum=0.0;
// int year = 1;
//
// while (d_sum >= c_sum) {
// d_sum = benjin + benjin * 0.1*year;
//
// if (year == 1) {
// c_sum = 105.0;
// }
// else {
// c_sum = c_sum * (1 + 0.05);
// }
//
// cout << "当前c的价值" << c_sum << endl;
// cout << "当前d的价值" << d_sum << endl;
// cout << year<<endl;
//
// year++;
// }
//
//
//
// return 0;
//}
/*假设要销售C++forFools一书,写个程序
输入全年中每个月的销售量,(图书数量,不是销售额)
程序通过循环,使用初始化为月份字符串的char*数组或者string对象数组
逐月进行提示 输入且计算总额,最后显示总数量
*/
//#include<iostream>
//
//using namespace std;
//const int ArSize = 12;
//int main() {
// const string months[ArSize] = {
// "January",
// "February",
// "March",
// "April",
// "May",
// "June",
// "July",
// "August",
// "September",
// "October",
// "November",
// "December",
// };
// int sum = 0, sales_volume[ArSize];
//
// for (int i = 0; i < ArSize; i++)
// {
// cout << "Please enter number of books sold (";
// cout << months[i] << "): ";
// cin >> sales_volume[i];
// }
// for (int i = 0; i < ArSize; i++)
// {
// sum += sales_volume[i];
// }
// cout << "A total of " << sum << " <<C++ For Fools>> books were sold in a year." << endl;
//
// return 0;
//
//
// return 0;
//}
//
/*将第5题换成二维数组,要求三年*/
//#include <iostream>
//#include <string>
//using namespace std;
//
//const int NUM = 3;
//const int ArSize = 12;
//
//int show_result(int(*x)[ArSize], int n);
//
//int main()
//{
// const string months[ArSize] = {
// "January",
// "February",
// "March",
// "April",
// "May",
// "June",
// "July",
// "August",
// "September",
// "October",
// "November",
// "December",
// };
// int sum, total, sales_volume[NUM][ArSize];
//
// for (int i = 0; i < NUM; i++)
// {
// cout << "Year " << i + 1 << ": " << endl;
// for (int j = 0; j < ArSize; j++)
// {
// cout << "Please enter number of books sold (";
// cout << months[j] << "): ";
// cin >> sales_volume[i][j];
// }
// cout << endl;
// }
//
// sum = total = show_result(sales_volume, 0);
// cout << "A total of " << sum << " <<C++ For Fools>> books were sold in the first year." << endl;
// total += sum = show_result(sales_volume, 1);
// cout << "A total of " << sum << " <<C++ For Fools>> books were sold in the second year." << endl;
// total += sum = show_result(sales_volume, 2);
// cout << "A total of " << sum << " <<C++ For Fools>> books were sold in the third year." << endl;
// cout << "A total of " << total << " <<C++ For Fools>> books were sold in three years." << endl;
//
// return 0;
//}
//
//int show_result(int(*x)[ArSize], int n)
//{
// int sum = 0;
//
// for (int i = 0; i < ArSize; i++)
// {
// sum += x[n][i];
// }
// return sum;
//}
//
/*7.设计一个名为car的结构,用它存储下述有关汽车的信息:生产商(存储在字符数组或string对象中的字符串)、生产年份(整数)。
编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。
接下来,程序提示用户输入每辆车的生产商(可能由多个单词组成)和年份信息。
请注意,这需要特别小心,因为它将交替读取数值和字符串(参见第4章)。最后,程序将显示每个结构的内容。该程序的运行情况如下:
How many cars do you wish to catalog?2
Car #1:
Please enter the make: Hudson Hornet
Please enter the year made: 1952Car #2:
Please enter the make: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser
*/
//#include<iostream>
//#include<sstream>
//using namespace std;
//struct car {
// string brand;//多个单词使用getline
// int year;
//};
//
//int main() {
//
//
// int num = 0;
//
// cout << "How many cars do you wish to catalog?" << endl;
// cin >> num;
// car * x = new car[num];
// for (int i = 0; i < num; i++)
// {
// cout << "Car #" << i + 1 << ':' << endl;
// cout << "Please enter the make: ";
// getline(cin, x[i].brand);
// cout << "Please enter the year made: ";
// (cin >> x[i].year).get();
// }
//
// cout << "Here is your collection:" << endl;
// for (int i = 0; i < num; i++)
// {
// cout << x[i].year;
// cout << ' ' << x[i].brand << endl;
// }
// delete[] x;
// return 0;
//}
/*
8.编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。
随后,文该程序指出用户输入了多少个单词(不包括done在内)。下面是该程序的运行情况:
Enter words (to stop, type the word done) :
anteater birthday category dumpster
envy finagle geometry done for sureYou entered a total of 7 words.
*/
//#include <iostream>
//#include <cstring>
//using namespace std;
//
//const int ArSize = 256;
//
//int main()
//{
// char str[ArSize];
// unsigned int count = 0;
//
// cout << "Enter words (to stop, type the word done):" << endl;
// while (1)
// {
// cin >> str;
// if (!strcmp("done", str)) {
// break;
// }
// else {
// cout << "ddd" << endl;
// count++;
// }
//
// }
// cout << "You entered a total of " << count << " words." << endl;
//
// return 0;
//}
//
/*9.编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。*/
//#include <iostream>
//#include <cstring>
//using namespace std;
//
//const int ArSize = 256;
//
//int main()
//{
// string str;
// unsigned int count = 0;
//
// cout << "Enter words (to stop, type the word done):" << endl;
// while (1)
// {
// cin >> str;
// if (str == "done") {
// break;
// }
// else {
// cout << "ddd" << endl;
// count++;
// }
//
// }
// cout << "You entered a total of " << count << " words." << endl;
//
// return 0;
//}
/*
10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下:
Enter number of rows: 5
...**
..***
.****
*****
*/
#include <iostream>
using namespace std;
int main() {
int num = 0,x=0;
cout << "Enter number of rows:";
cin >> num;
while (x<=num) {
for (int i = 0; i < x; i++) {
cout << "*";
}
cout << endl;
x++;
}
return 0;
}