C++primer plus 第五章程式設計練習
阿新 • • 發佈:2019-01-13
本人用code::block 編寫,如需參考,善用Ctrl+shift+C 和 Ctrl + shift + X 快捷鍵
如有任何錯誤或疑問,歡迎留言
//5.1 //#include <iostream> //using namespace std; //int main() //{ // int start, end; // int sum = 0; // cout << "Enter two integers, small one first: \n"; // cin >> start >> end; // for (start; start<=end; ++start) // sum += start; // cout << "sum = " << sum << endl; // return 0; //} //5.2 //#include <iostream> //#include <array> //using namespace std; //int main() //{ // array<long long, 100> factorials; // factorials[0] = factorials[1] = 1; // for (int i=2; i<=100; i++) // { // factorials[i] = factorials[i-1] * i; // } // cout << "Enter a integers x, I will give you the answer x! \n"; // int num; // cin >> num; // cout << num << "! = " << factorials[num] << endl; // return 0; //} //5.3 //#include <iostream> //#include <climits> //using namespace std; //int main() //{ // int sum = 0; // int ent; // cout << "Enter a seriors of numbers, I will sum them, and I will be shut down when I get a zero.\n"; // cin >> ent; // while (ent) // { // sum += ent; // cout << "So far, the sum = " << sum << endl; // cin >> ent; // } // return 0; //} //5.4 //#include <iostream> //using namespace std; //const int StartMoney = 100; //int main() //{ // double Daphne = StartMoney; // double Cleo = StartMoney; // int years = 0; // do // { // Daphne=0.1*StartMoney+Daphne; // Cleo=Cleo*1.05; // ++years; // }while (Cleo <= Daphne); // // cout << years << " later, Cleo's money is " << Cleo << endl; // cout << "and Daphone's money is " << Daphne << endl; // // return 0; //} //5.5 //#include <iostream> //using namespace std; //int main() //{ // const char* month[12] = {"January", "February", "March", "April", // +const ╬м©иртак // "May", "June", "July", "August", "September", "October", "November", "December"}; // int sales[12] = {0}; // int sum = 0; // for(int i = 1; i<=12; i++) // { // cout << "Enter the sales for " << month[i-1] << ": "; // cin >> sales[i-1]; // sum += sales[i-1]; // } // cout << "The annual sales are " << sum; // return 0; //} //5.6 //#include <iostream> //using namespace std; //const int Years = 3; //const int Months = 12; //int main() //{ // const char* months[12] = {"January", "February", "March", "April", // "May", "June", "July", "August", "September", "October", "November", "December"}; // int sales[Years][Months] = {0}; // int yearsales[Years] = {0}; // // for(int year=1; year <= Years; ++year) // { // cout << "Enter the sales for " << year << " year." << endl;; // for (int month=1; month <=12; ++month) // { // cout << months[month-1] << ": "; // cin >> sales[year-1][month-1]; // yearsales[year-1] += sales[year-1][month-1]; // } // } // for (int i =0; i<3; i++) // { // cout << "The annual sales for year " << i+1 << ": " << yearsales[i] << endl; // } // cout << "The sales for the three years is " << yearsales[0]+yearsales[1]+yearsales[2] << endl; // return 0; //} //5.7 //#include <iostream> //#include <string> //using namespace std; //struct car //{ // string producer; // int year; //}; //int main() //{ // int num; // cout << "How many cars do you wish to catalog? "; // cin >> num; // cin.get(); // car * carlog = new car[num]; // int i; // for (i=1; i<=num; i++) // { // cout << "Car #" << i << endl; // cout << "Please enter the make: "; // getline(cin, (carlog+i-1)->producer); // cout << "Please enter the year made: " ; // (cin >> (carlog+i-1)->year).get(); // } // cout << "Here is your collection: \n"; // for (i=0; i<num; i++) // cout << (carlog+i)->year <<" " << (carlog+i)->producer << endl; // return 0; //} //5.8 //#include <iostream> //#include <cstring> // //using namespace std; //int main() //{ // char words[50]; // cout << "Enter words (to stop, type the word done)\n"; // cin >> words; // int count = 0; // while(strcmp(words, "done")) // { // count += 1; // cin >> words; // } // cout << "You entered a total of " << count << " words."; // return 0; //} //5.9 //#include <iostream> //#include <string> //using namespace std; //int main() //{ // string words; // cout << "Enter words (to stop, type the word done)\n"; // cin >> words; // int count = 0; // while(!(words == "done")) // { // count += 1; // cin >> words; // } // cout << "You entered a total of " << count << " words."; // return 0; //} //5.10 #include <iostream> using namespace std; int main () { int rows = 0; cout << "Enter number of rows: "; cin >> rows; int L, point, xx; for (L=1; L<=rows; L++) { for(point=1; point<=(rows-L); ++point) cout << "."; for(xx=1; xx<=L; ++xx) cout << "*"; cout << endl; } return 0; }