编程环境Visual Studio 2017
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
#include <iostream> #include <string> #include <array> using std::cin; using std::cout; using std::endl; using std::array; using std::string; using std::getline; // practice 1 void p5_1(void) { int number1 = 0; int number2 = 0; int sum = 0; cout << "Enter the first number: "; cin >> number1; cout << "Enter the second number: "; cin >> number2; for (int i = number1; i <= number2; i++) { sum += i; } cout << "The sum of number " << number1 << " to " << number2 << ", sum = " << sum << endl; } // practice 2 const int ArSize = 100; void p5_2(void) { array<long double, ArSize+1> factorials; factorials[0] = factorials[1] = 1.0; 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; } // practice 3 void p5_3(void) { int sum = 0; int num = 0; while (1) { cout << "Enter a number( 0 to exit): "; cin >> num; if (num == 0) { break; } sum += num; cout << "Until now, the sum of the number you inputed is " << sum << endl; } cout << "Done." << endl; return; } // practice 4 void p5_4(void) { double Daphne_account = 100; double Cleo_account = 100; int years = 0; while (Daphne_account >= Cleo_account) { years++; Cleo_account = Cleo_account + Cleo_account * 0.05; Daphne_account += 10; } cout << "After " << years << " years. " << "Cleo account which is " << Cleo_account << " will more than Daphne account which is " << Daphne_account << "." << endl; } // practie 5 void p5_5(void) { string month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "Septempber", "October", "November", "December" }; int sell[12]; int total_sales = 0; cout << "Enter the sales of book <C++ for Fools> each month: " << endl; for (int i = 0; i < 12; i++) { cout << month[i] << ": "; cin >> sell[i]; total_sales += sell[i]; } cout << "The total sales is " << total_sales << endl; for (int i = 0; i < 12; i++) { cout << month[i] << ": " << sell[i] << endl; } return; } // practice 6 void p5_6(void) { string month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "Septempber", "October", "November", "December" }; unsigned int sales[3][12] = { 0 }; unsigned int total_sales[3] = { 0 }; for (int i = 0; i < 3; i++) { cout << "Enter " << i + 1 << " year(s) sales of book <C++ for Fools> each month: " << endl; for (int j = 0; j < 12; j++) { cout << month[j] << ": "; cin >> sales[i][j]; total_sales[i] += sales[i][j]; } } for (int i = 0; i < 3; i++) { cout << i + 1 << " year(s) total sales is " << total_sales[i] << endl; } cout << "Three years total sales is " << total_sales[0] + total_sales[1] + total_sales[2] << endl; } //practice7 struct car { string company; int pro_year; }; void p5_7(void) { unsigned int size = 0; cout << "How many cars do you wish to catalog? "; cin >> size; cin.get(); struct car *pcar = new struct car[size]; for (unsigned int i = 0; i < size; i++) { cout << "Car #" << i + 1 << ":" << endl; cout << "Please enter the make: "; getline(cin, pcar[i].company); cout << "Please enter the year make: "; cin >> pcar[i].pro_year; cin.get(); } cout << "Here is your collection:" << endl; for (unsigned int i = 0; i < size; i++) { cout << pcar[i].pro_year << " " << pcar[i].company << endl; } } // practice 8 void p5_8(void) { unsigned int n_word = 0; char input[128]; cout << "Enter words (to stop, type the word done):" << endl; while (cin >> input) { if (strcmp(input, "done")) { n_word++; } else break; } cout << "You entered a total of " << n_word << " words." << endl; } // practice 9 void p5_9(void) { unsigned int n_word = 0; string input; cout << "Enter words (to stop, type the word done):" << endl; while (cin >> input) { if (input != "done") { n_word++; } else break; } cout << "You entered a total of " << n_word << " words." << endl; } // practice 10 void p5_10(void) { unsigned int row = 0; cout << "Enter number of rows: "; cin >> row; for (unsigned int i = 1; i <= row; i++) { for (unsigned int j = i; j < row; j++) { cout << "."; } for (unsigned int j = 0; j < i; j++) { cout << "*"; } cout << endl; } } int main(int argc, char **argv) { p5_10(); while (getchar()); return 0; } |
此文为博主原创文章,转载请注明出处
5.2 array factorials;为什么要用Arsize+1,还有用long long类型为什么不可以啊
不太记得题目意思是否是要打印到100了,如果是的话0-100是101个数;如果使用long long的话100!会超过long long上上限导致溢出,而使用long double可以通过科学记数法表示。就不怕数值溢出