该题使用一个for循环就可以解决,代码如下:
// ex1.cpp -- calculate the sum of between two integers' all integer #include<iostream> int main() { using namespace std; int i,j; int sum = 0; cout << "Enter two integers: "; cin >> i >> j; for(;i<=j;i++) sum += i; cout << "The sum of between two integers' all integers is "; cout << sum << endl; return 0; }
运行结果如下:
该题要求使用array对象,数据类型为long double,
array<long double,100> factories
代码如下:
// ex2.cpp -- calculate 100! #include<iostream> #include<array> const int arsize = 101; int main() { using namespace std; array<long double,arsize> factories; factories[0] = 1; factories[1] = 1; for(int i = 2; i < arsize; ++i) factories[i] = factories[i-1] * i; cout << "100! = " << factories[100] << endl; return 0; }
运行结果如下:
本题不知道循环次数,显然应该用while循环。
代码如下:
// ex3.cpp -- calculate sum #include<iostream> int main() { using namespace std; double i; double sum = 0; cout << "Enter a figure, input 0 will end.\n"; cin >> i; while(i) { sum += i; cout << "sum = " << sum << endl; cin >> i; } return 0; }
运行结果如下:
// ex4.cpp -- calculate profit #include<iostream> const int money_init = 100; int main() { using namespace std; double profit_c = money_init; int profit_d = money_init; int year = 0; while(profit_c<=profit_d) { ++ year; profit_d += 10; profit_c += 0.05 * profit_c; } cout << "After " << year << " years, "; cout << "Cleo over Daphne.\n"; cout << "Cleo's profit: " << profit_c << endl; cout << "Daphne's profit: " << profit_d << endl; return 0; }
运行结果如下:
本题使用string对象数组构建十二个月的字符,然后用for循环来进行输入和数据处理,代码如下:
// ex5.cpp --calculate a year sales of book #include<iostream> #include<string> const int Months = 12; int main() { using namespace std; string month[Months]={"January", "February","March","April","May", "June","July","August","September","October","November","December"}; // const char *month[Months] = {"January", "February","March","April","May", // "June","July","August","September","October","November","December"}; long sum = 0; int sale[Months]; for(int i = 0; i < Months; ++i) { cout << "Enter the amount of sale in " << month[i] << ": "; cin >> sale[i]; sum += sale[i]; } cout << "The sale volume of this year is " << sum << endl; return 0; }
注:使用char * 定义数组时初始化时,应该加const.
运行结果如下:
本题涉及到二维数组的定义和循环遍历。
代码如下:
// ex6.cpp --calculate three year sales of book #include<iostream> #include<string> const int Months = 12; const int Years = 3; int main() { using namespace std; string month[Months]={"January", "February","March","April","May", "June","July","August","September","October","November","December"}; const char* year[Years] = {"2019", "2020", "2021"}; // const char *month[Months] = {"January", "February","March","April","May", // "June","July","August","September","October","November","December"}; long sum_year = 0; long sum = 0; int sale[Months][Years]; for(int i = 0; i < Years; ++i) { for(int j = 0; j < Months; ++j) { cout << "Enter the amount of sale in " << year[i] << ","; cout << month[j] << ": "; cin >> sale[i][j]; sum_year += sale[i][j]; } cout << "The sales in " << year[i] << ": " << sum_year << endl; sum += sum_year; sum_year = 0; } cout << "The sales of three years: " << sum << endl; return 0; }
运行结果如下:
Enter the amount of sale in 2019,January: 1000 Enter the amount of sale in 2019,February: 1001 Enter the amount of sale in 2019,March: 1002 Enter the amount of sale in 2019,April: 1003 Enter the amount of sale in 2019,May: 1004 Enter the amount of sale in 2019,June: 1005 Enter the amount of sale in 2019,July: 1006 Enter the amount of sale in 2019,August: 1007 Enter the amount of sale in 2019,September: 1008 Enter the amount of sale in 2019,October: 1009 Enter the amount of sale in 2019,November: 1010 Enter the amount of sale in 2019,December: 1011 The sales in 2019: 12066 Enter the amount of sale in 2020,January: 2001 Enter the amount of sale in 2020,February: 2002 Enter the amount of sale in 2020,March: 2003 Enter the amount of sale in 2020,April: 1004 Enter the amount of sale in 2020,May: 2005 Enter the amount of sale in 2020,June: 2006 Enter the amount of sale in 2020,July: 2007 Enter the amount of sale in 2020,August: 2008 Enter the amount of sale in 2020,September: 2009 Enter the amount of sale in 2020,October: 2010 Enter the amount of sale in 2020,November: 2011 Enter the amount of sale in 2020,December: 2012 The sales in 2020: 23078 Enter the amount of sale in 2021,January: 3001 Enter the amount of sale in 2021,February: 3002 Enter the amount of sale in 2021,March: 3003 Enter the amount of sale in 2021,April: 3004 Enter the amount of sale in 2021,May: 3005 Enter the amount of sale in 2021,June: 2006 Enter the amount of sale in 2021,July: 3007 Enter the amount of sale in 2021,August: 3008 Enter the amount of sale in 2021,September: 3009 Enter the amount of sale in 2021,October: 3010 Enter the amount of sale in 2021,November: 3011 Enter the amount of sale in 2021,December: 3012 The sales in 2021: 35078 The sales of three years: 70222
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
本题首先定义一个car结构,使用new创建一个动态数组,然后按照题目要求进行输入和输出。
代码如下:
// ex7.cpp -- struct,new,for #include<iostream> #include<string> #include<cstring> struct car { std::string make; int year; }; int main() { using namespace std; char temp[20]; int temp_y; int n; cout << "How many cars do you wish to catalog? "; (cin >> n).get(); car* Car = new car[n]; for(int i = 0; i < n; ++i) { cout << "Car #" << (i+1) << ":\n"; cout << "Please enter the make: "; getline(cin,Car[i].make); cout << "Please enter the year made: "; (cin >> Car[i].year).get(); } for(int i = 0; i < n; ++i) cout << (Car+i)->year << " " << Car[i].make << endl; return 0; }
运行结果如下:
Enter words(to stop, typer the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
您应在程序中包含头文件cstring,并使用函数strcmp来进行比较测试。
本题每次读取一个单词的意思,笔者刚开始误解了,以为要自己写一个while循环,按一个字符一个字符输入,遇到空格 ' '
停止,重新开始读取下一个单词,存在许多问题,如临时存储数组的更新等等,导致程序一直调试错误,这里挖个坑吧,等把C++这本书学会了,再来用这次的理解做一下这道题。目前使用cin>>直接按照单词读取,实际上题目也是这么要求的,代码如下:
// ex8.cpp -- get the number of enter words #include<iostream> #include<cstring> int main() { using namespace std; char temp[20]; int count = 0; int i = 0; cout << "Enter words(to stop, type the word done): \n"; cin >> temp; while(strcmp(temp,"done")) // or while(strcmp(temp,"done")!=0) { /* use while or other to get cin >> temp*/ // cin >> temp[i]; // while(temp[i] != ' ') // { // i++; // cin >> temp[i]; // } // i = 0; cin >> temp; count++; } cout << "You entered a total of " << count << " words.\n"; return 0; }
注:准确的分析题目比编程更重要。
运行结果如下:
本题与第八题类似,需要修改的代码就是单词声明,与while的test-conditon, 代码如下:
// ex9.cpp -- get the number of enter words by using string #include<iostream> #include<string> int main() { using namespace std; string word; int count = 0; int i = 0; cout << "Enter words(to stop, type the word done): \n"; cin >> word; while(word != "done") { cin >> word; count++; } cout << "You entered a total of " << count << " words.\n"; return 0; }
运行结果如下:
Enter number of rows: 5
....*
...**
..***
.****
*****
根据图形分析,首先需要一个for循环输出row行字符,在上述for循环里嵌套for循环输入row-当前行数的.
,接着输出当前行数个的*
。代码如下:
// ex10.cpp -- draw figure #include<iostream> int main() { using namespace std; int rows; cout << "Enter number of rows: "; cin >> rows; for(int i = 1; i <= rows; ++i) { for(int j = rows -i; j > 0; --j) { cout <<"."; } for(int k = 1; k <=i; ++k) cout <<"*"; cout << endl; } return 0; }
运行结果如下: