1.编写一个要求用户输入两个整数的程序,将程序将计算并输出这两个整数之间(包括这两个整数)所有的整数的和。这里假设先输入较小的整数,例如如果用户输入的是2和9,则程序将指出2-9之间所有整数的和为44.
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 int a, b; 7 int cnt = 0; 8 cin >> a >> b; 9 for (int i = a; i <= b; i++) 10 { 11 cnt = cnt + i; 12 } 13 cout << cnt; 14 while (1); 15 return 0; 16 }
2.使用array对象(而不是数组)和long double(而非long long)重新编写程序清单5.4,并计算100!的值。
1 #include<iostream> 2 #include<array> 3 using namespace std; 4 5 const int ArSize = 101; 6 7 int main() 8 { 9 array <long double, ArSize> cnt; 10 cnt[1] = cnt[0] = 1L; //long double型用L做后缀 11 for (int i = 2; i < ArSize; i++) 12 { 13 cnt[i] = i*cnt[i - 1]; 14 } 15 for (int i = 0; i < ArSize; i++) 16 { 17 cout << i << "! = " << cnt[i] << endl; 18 } 19 while (1); 20 return 0; 21 }
3.编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累积和,输入0时,程序结束
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 int n, cnt = 0; 7 cin >> n; 8 while (n!=0) 9 { 10 cnt += n; 11 cout << "截至目前为止,输入累计和为:" << cnt << endl; 12 cin >> n; 13 } 14 if (n == 0) 15 { 16 cout << "程序结束"; 17 } 18 while (1); 19 return 0; 20 }
4、 Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%(利息=0.10*原始存款)。而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息)的5%(利息=0.05*当前存款)。请计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两人的投资价值。
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 double dap = 100, celo = 100; 7 int year=0; 8 while (celo<=dap) 9 { 10 dap += (100 * 0.10); 11 celo += celo*0.05; 12 year++; 13 } 14 cout << "After " << year << " years, Celo pass Daphne." << endl; 15 cout << "Daphne: " << dap << endl; 16 cout << "Celo: " << celo << endl; 17 system("pause"); 18 return 0; 19 }
5、 假设要销售《C++ For Fools》一书,请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char * 数组(或string对象数组)逐月进行提示,并将输入的数据存储在一个int数组中。然后,程序计算数组中各元素的总和,并报告这一年的销售情况。
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int main() 5 { 6 int sales[12], cnt = 0; 7 string month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; //可替换为char *month[12] 8 for (int i = 0; i < 12; i++) 9 { 10 cout << month[i] << ": "; 11 cin >> sales[i]; 12 } 13 for (int i = 0; i < 12; i++) 14 { 15 cnt += sales[i]; 16 } 17 cout << "The total sales of this year is " << cnt << endl; 18 system("pause"); 19 return 0; 20 }
6、 完成编程练习5,但这一次使用一个二维数组来存储输入——3年中每个月的销售量,程序将报告每年销售量以及三年的总销售量。
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int main() 5 { 6 int sales[3][12], cnt[3] = {}, total = 0; 7 string month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; 8 for (int i = 0; i < 3; i++) 9 { 10 cout << "Enter number " << i + 1 << " year‘s sales. " << endl; 11 for (int j = 0; j < 12; j++) 12 { 13 cout << month[j] << ": "; 14 cin >> sales[i][j]; 15 cnt[i] += sales[i][j]; 16 } 17 cout << endl; 18 } 19 for (int i = 0; i < 3; i++) 20 { 21 cout << "The total sales of number " << i + 1 << " year is " << cnt[i] <<" ."<< endl; 22 total += cnt[i]; 23 } 24 cout << "The total sales of three years is " << total <<" ."<< endl; 25 system("pause"); 26 return 0; 27 }
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
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 struct car 6 { 7 string name; 8 int year; 9 }; 10 11 int main() 12 { 13 int n; 14 cout << "How many cars do you wish to catalog ? "; 15 cin >> n; 16 car *pr = new car[n]; 17 for (int i = 0; i < n; i++) 18 { 19 cout << "Car #" << i+1 << ":" << endl; 20 cout << "Please enter the make: "; 21 cin.get(); //前面cin>>n导致输入队列中留下了换行符,cin.getline()看到换行符后,将认为是一个空行,并将一个空字符串赋给pr[i].name 22 //此处cin.get()用来跳过前面cin>>n和每次循环cin>>pr[i].year留下的换行符 23 getline(cin, pr[i].name); 24 cout << "Please enter the year made: "; 25 cin >> pr[i].year; 26 } 27 cout << "Here is your collection: " << endl; 28 for (int i = 0; i < n; i++) 29 { 30 cout << pr[i].year << " " << pr[i].name << endl; 31 } 32 delete[]pr; //记得进行delete 33 system("pause"); 34 return 0; 35 }
8、编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入了多少单词(不包括done在内)。下面是该程序的运行情况:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You enter a total of 7words.
你应在程序中包含头文件cstring,并使用strcmp( )来进行比较测试。
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 int main() 6 { 7 char word[20]; 8 int number = 0; 9 cout << "Enter words (to stop, type the word done):" << endl; 10 do 11 { 12 cin >> word; 13 number++; 14 } while (strcmp(word,"done")!=0); 15 cout << "You entered a total of " << number-1 << " words."; 16 //方法二 17 /* 18 cin >> word; 19 while (strcmp(word, "done") != 0) 20 { 21 if (bool(cin >> word) == true) 22 { 23 number++; 24 } 25 } 26 cout << "You entered a total of " << number << " words."; 27 */ 28 system("pause"); 29 return 0; 30 }
9、编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 int main() 6 { 7 string word; 8 int number = 0; 9 cout << "Enter words (to stop, type the word done):" << endl; 10 do 11 { 12 cin >> word; 13 number++; 14 } while (word!="done"); 15 cout << "You entered a total of " << number-1 << " words."; 16 //方法二 17 /* 18 cin >> word; 19 while (word!="done") 20 { 21 if (bool(cin >> word) == true) 22 { 23 number++; 24 } 25 } 26 cout << "You entered a total of " << number << " words."; 27 */ 28 system("pause"); 29 return 0; 30 }
10、编写一个使用循环嵌套的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包含一个星号,第二行包含两个星号,依次类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下:
Enter number of rows: 5
....*
...**
..***
.****
*****
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 int n; 7 cout << "Enter number of rows: "; 8 cin >> n; 9 for (int i = 1; i <= n; i++) 10 { 11 for (int j = n - i; j >0; j--) 12 { 13 cout << ‘.‘; 14 } 15 for (int k = 1; k <= i; k++) 16 { 17 cout << ‘*‘; 18 } 19 cout << endl; 20 } 21 system("pause"); 22 return 0; 23 }
原文地址:https://www.cnblogs.com/Fionaaa/p/12283463.html