C++.Primer.Plus第五版第五章编程练习答案
2015-07-16 14:47
369 查看
// 1 #include <iostream> using namespace std; int main() { cout << "This progress need tow int numbers:\n"; int int1,int2; cout << "Please input the little number: \n"; cin >> int1; cout << "Please input the big number: "; cin >> int2; int total = 0; for(int comp = int1;comp <= int2; comp++) total += comp; cout << "total = " << total << endl; return 0; } // 2 #include <iostream> using namespace std; int main() { int num,num_total= 0; cin >> num; while(num != 0) { num_total+= num; cout << "num_total = " << num_total << endl; cin >> num; } cout << "num_total = " << num_total << endl; /*while(cin >> i && i) sum+= i,cout << sum<< endl;*/ return 0; } // 3 #include <iostream> using namespace std; int main() { int Daphne = 100,Cleo = 100,year = 0; while(Cleo <= Daphne) { Daphne += 100 * 10 / 100; Cleo += Cleo * 5 / 100; year ++; } cout << year << "years latter :\n"; cout << "Daphne = " << Daphne << endl; cout << "Cleo = " << Cleo << endl; return 0; } // 4 #include <iostream> #define MONTH_TOTAL 12 using namespace std; int main() { const char * month[MONTH_TOTAL] = { "January", "February", "March", "April", "Mary", "June", "July", "August", "September", "October", "November", "December" }; int salo[MONTH_TOTAL] = {},sum = 0; for(int i = 0; i < MONTH_TOTAL; i++) { cout << month[i] << ":\t\t\t "; cin >> salo[i]; sum += salo[i]; } cout << "total = " << sum << endl; return 0; } // 5 #include <iostream> #define MONTH_TOTAL 12 #define YEAR_TOTAL 3 using namespace std; int main() { const char * month[MONTH_TOTAL] = { "January", "February", "March", "April", "Mary", "June", "July", "August", "September", "October", "November", "December" }; int salo[YEAR_TOTAL][MONTH_TOTAL] = {},sum = 0,year = 0,i = 0; while(year < YEAR_TOTAL) { if(i == MONTH_TOTAL) { year ++; i = 0; if(year == YEAR_TOTAL) break; } cout << year << "\t" << month[i] << ":\t\t\t "; cin >> salo[year][i]; sum += salo[year][i++]; } cout << "total = " << sum << endl; return 0; } // 6 注意cin.get()所在位置及作用 当输入多个字符串时! #include <iostream> #include <string> using namespace std; struct car { //string maker; char maker[100]; int year; }; int main() { cout << "How many cars do you wish to catalog: "; int car_number; //cin.get(); cin >> car_number; car * car_info = new car [car_number]; for(int i = 0; i < car_number; i++) { cout << "Car\t#" << i + 1 << ":\n" ; cout << "Please enter the maker: "; //cin >> car_info[i].maker; cin.get(); cin.getline(car_info[i].maker,100); //cout << "maker:" << car_info[i].maker << endl; cout << "Please enter the year made: "; cin >> car_info[i].year; } cout << "Here is your collection:\n"; for(int i = 0; i < car_number; i++) cout << car_info[i].year << "\t" << car_info[i].maker << endl; delete [] car_info; return 0; } // 7 #include <iostream> #include <cstring> using namespace std; int main() { cout << "Enter words (to stop, type the word done):\n"; int count = 0; while(1) { char string[100]; cin >> string; if(strcmp(string,"done") == 0) break; else count++; } cout << "You entered a total of " << count << " words.\n"; return 0; } // 8 #include <iostream> #include <string> using namespace std; int main() { cout << "Enter words (to stop, type the word done):\n"; int count; while(1) { string str; cin >> str; if(str == "done") break; else count++; } cout << "You entered a total of " << count << " words.\n"; return 0; } // 9 #include <iostream> using namespace std; int main() { int line; cout << "Please input a number: "; cin >> line; for(int i = 0; i < line; i++) { for(int dian = 0; dian < line - i - 1; dian++) cout << "."; for(int xing = 0;xing <= i; xing++) cout << "*"; cout << endl; } return 0; }
相关文章推荐
- c语言有符号数和无符号类型计算
- C++ 文件操作
- C++链表冒泡,归并,插入排序(纯指针)
- C++指针(我所了解的)
- C/C++字符串函数之复制函数
- C++设计模式之桥接模式(Bridge)
- C++风格的强制性类型转换
- C++风格的强制性类型转换
- C++设计模式之组合模式(Composite)
- eclipse CDT写c++使用文件作为输入源(输入重定向)
- c++ 函数返回引用
- C++设计模式之装饰者模式(Decorator)
- C++虚基类
- C语言之指针篇
- C++间接继承
- C++ 把文件路径中的单斜杠“\”换成双斜杠“\\”
- C++设计模式之外观模式(Facade)
- C++学习笔记之容器:顺序容器
- C++设计模式之享元模式(FlyWeight)
- C++虚基类