编程环境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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
#include <iostream> #include <string> #include <cctype> using namespace std; // practice 1 void p7_1(void) { double x, y; double avg = 0; cout << "Enter two numbers: "; cin >> x >> y; while (x != 0 && y != 0) { avg = 2.0 * x * y / (x + y); cout << "The average of " << x << " and " << y << " is " << avg << endl; cout << "Enter the next two numbers: "; cin >> x >> y; } } // practice 2 void GetInput(double *grade, unsigned int *number) { cout << "You can enter up to 10 grades( -1 to quit): " << endl; while (cin >> grade[(*number)++]) { if (grade[*number - 1] == -1) { break; } } (*number)--; } void PrintArray(const double *grage, const unsigned int number) { cout << "The grade is: " << endl; for (unsigned int i = 0; i < number; i++) { cout << grage[i] << " "; } cout << endl; } void CalAvg(const double *grade, const unsigned int number) { double total = 0.0; cout << "The average is :"; for (unsigned int i = 0; i < number; i++) { total += grade[i]; } cout << total / number << endl; } void p7_2(void) { double grade[10]; unsigned int enter = 0; GetInput(grade, &enter); PrintArray(grade, enter); CalAvg(grade, enter); return; } // practice 3 struct box { char maker[40]; float height; float width; float length; float volume; }; void PrintBox(struct box mbox) { cout << "Box maker: " << mbox.maker << endl; cout << "Box height: " << mbox.height << endl; cout << "Box width: " << mbox.width << endl; cout << "Box length: " << mbox.length << endl; cout << "Box volume: " << mbox.volume << endl; } void CalBoxVolume(struct box *pbox) { pbox->volume = pbox->height * pbox->width * pbox->length; } void p7_3(void) { struct box mbox = { "Jimmy Chen", 0.25, 4.0, 1.0, 0.0 }; PrintBox(mbox); CalBoxVolume(&mbox); PrintBox(mbox); } // practice 4 long double probability(unsigned number, unsigned picks) { long double result = 1.0; long double n = 0; unsigned p; for (n = number, p = picks; p > 0; n--, p--) { result = result * n / p; } return result; } void p7_4(void) { unsigned field1 = 47; unsigned field2 = 27; cout << "You have on chance in "; cout << probability(47, 5) * probability(27, 1); cout << " of winning.\n" << endl; } // practice 5 long long factorial(unsigned int number) { if (number == 0 || number == 1) { return 1; } else { return number * factorial(number - 1); } } void p7_5(void) { long long result = 0; unsigned int input = 0; cout << "Please enter the number: "; cin >> input; while (1) { result = factorial(input); cout << "The result of " << input << "! is " << result << "." << endl; cout << "Please enter the next number: "; cin >> input; } } // practice 6 unsigned int Fill_array(double double_array[], unsigned int length) { unsigned int n_input = 0; cout << "Enter double numbers (non-digital to quit): " << endl; for (size_t i = 0; i < length; i++) { if (cin >> double_array[i]) { n_input++; } else { break; } } return n_input; } void Show_array(double double_array[], unsigned int length) { cout << "The double array is :" << endl; for (size_t i = 0; i < length; i++) { cout << double_array[i] << " "; if ((i + 1) % 5 == 0) { cout << endl; } } if (length % 5 != 0) { cout << endl; } } void Reverse_array(double double_array[], unsigned int length) { cout << "Revert the array" << endl; for (size_t i = 0; i < length / 2; i++) { double tmp = double_array[i]; double_array[i] = double_array[length - 1 - i]; double_array[length - 1 - i] = tmp; } } void p7_6(void) { double double_array[10]; unsigned int n_input; n_input = Fill_array(double_array, 10); Show_array(double_array, n_input); Reverse_array(double_array, n_input); Show_array(double_array, n_input); } // practice 7 int fill_array(double *ar_begin, double *ar_end) { double temp = 0.0; int i = 0; double *ar_tmp = nullptr; for (ar_tmp = ar_begin; ar_tmp < ar_end; ar_tmp++) { cout << "Enter value #" << (i + 1) << ": "; cin >> temp; if (!cin) { cin.clear(); while (cin.get() != '\n') continue; cout << "Bad input; input preocess terminated.\n"; break; } else if (temp < 0) { break; } *ar_tmp = temp; i++; } return i; } void show_array(const double *ar_begin, const double *ar_end) { const double *ar_tmp = nullptr; unsigned int i = 0; for (ar_tmp = ar_begin; ar_tmp < ar_end; ar_tmp++) { cout << "Property #" << (i + 1) << ": $"; cout << *ar_tmp << endl; i++; } } void revalue(double *ar_begin, double *ar_end, double r) { for (double * ar = ar_begin; ar < ar_end; ar++) { *ar *= r; } } void p7_7(void) { double properties[10]; int size = fill_array(properties, properties + 10); show_array(properties, properties + size); if (size > 0) { cout << "Enter revaluation factor: "; double factor; while (!(cin >> factor)) { cin.clear(); while (cin.get() != '\n') continue; cout << "Bad input; Please enter a number: "; } revalue(properties, properties + size, factor); show_array(properties, properties + size); } cout << "Done.\n"; return; } // practice 8 const int Seasons = 4; const char *Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" }; struct cost { double expenses[Seasons]; }; void fill(double *pa) { for (int i = 0; i < Seasons; i++) { cout << "Enter " << Snames[i] << " expenses: "; cin >> pa[i]; } } void show(double *pa) { double total = 0.0; cout << "\nEXPENSES\n"; for (int i = 0; i < Seasons; i++) { cout << Snames[i] << ": $" << pa[i] << endl; total += pa[i]; } cout << "Total Expenses: $" << total << endl; } void fill(struct cost *pCost) { for (int i = 0; i < Seasons; i++) { cout << "Enter " << Snames[i] << " expenses: "; cin >> pCost->expenses[i]; } } void show(struct cost *pCost) { double total = 0.0; cout << "\nEXPENSE\n"; for (int i = 0; i < Seasons; i++) { cout << Snames[i] << ": $" << pCost->expenses[i] << endl; total += pCost->expenses[i]; } cout << "Total Expense: $" << total << endl; } void p7_8(void) { // situation a cout << "Situation a: " << endl; double pa[Seasons] = { 0 }; fill(pa); show(pa); // situation b cout << endl << endl; cout << "Situation b: " << endl; struct cost *pCost = new struct cost; fill(pCost); show(pCost); delete pCost; } // practice 9 const int SLEN = 30; struct student { char fullname[SLEN]; char hobby[SLEN]; int ooplevel; }; int getinfo(student pa[], int n) { int enter = 0; for (int i = 0; i < n; i++) { cout << "Enter the infomation of student #" << i+1 << endl; cout << "Enter full name (blank line to quit): "; cin.getline(pa[i].fullname, SLEN); cout << "Enter hobby: "; cin.getline(pa[i].hobby, SLEN); cout << "Enter ooplevel: "; cin >> pa[i].ooplevel; while (cin.get() != '\n') continue; enter++; } return enter; } void display1(student st) { cout << "Using display1(student st): " << endl; cout << "Full name: " << st.fullname << endl; cout << "Hobby: " << st.hobby << endl; cout << "Ooplevel: " << st.ooplevel << endl; } void display2(const student *st) { cout << "Using display2(const student *st)" << endl; cout << "Full name: " << st->fullname << endl; cout << "Hobby: " << st->hobby << endl; cout << "Ooplevel: " << st->ooplevel << endl; } void display3(const student pa[], int n) { cout << "Using display3(const student pa[], int n)" << endl;; for (int i = 0; i < n; i++) { cout << "Infomation of student #" << i + 1 << ": " << endl; cout << "Full name: " << pa[i].fullname << endl; cout << "Hobby: " << pa[i].hobby << endl; cout << "Ooplevel: " << pa[i].ooplevel << endl; } } void p7_9(void) { cout << "Enter class size: "; int class_size; cin >> class_size; while (cin.get() != '\n') continue; student *ptr_stu = new student[class_size]; int entered = getinfo(ptr_stu, class_size); for (int i = 0; i < entered; i++) { display1(ptr_stu[i]); display2(&ptr_stu[i]); } display3(ptr_stu, entered); delete[] ptr_stu; cout << "Done.\n"; return; } // practice 10 double add(double x, double y) { return x + y; } double mul(double x, double y) { return x * y; } double calculate(double x, double y, double(*fun)(double, double)) { return fun(x, y); } void p7_10(void) { double x = 0.0; double y = 0.0; cout << "Enter two double number: "; while (cin >> x >> y) { cout << "Call add, the result of " << x << " and " << y << " is " << calculate(x, y, add) << endl; cout << "Call mul, the result of " << x << " abd " << y << " is " << calculate(x, y, mul) << endl; cout << "Enter next two double number: "; } } int main(int argc, char **argv) { p7_10(); while (getchar()); return 0; } |
此文为博主原创文章,转载请注明出处
第二题练习input那里改成这样更好吧
while (cin >> grade[(*number)++], *number < 10)
{
if (grade[*number - 1] == -1)
{
(*number)--;
break;
}
好像比较少看到有这种用法啊。另外这里提出问题与你探究下:在while的条件判断语句里面使用逗号表达式,貌似是以最后一个逗号表达式的值作为while判断语句的值吧,那如果cin执行输入有误的话,用逗号表达式的while判断好像没有办法判断出来吧!
啊,我是自己刚开始看书自学的,后来研究了一下while那样用好像是不好,下面是我自己写的第二题代码,感觉bug会少点
#include
using namespace std;
void input( double *x ,int &enter);
void show(double *x, int enter);
void avg(double *x, int enter);
int main()
{
double grade[10];
int enter;
input(grade,enter);
show(grade, enter);
avg(grade, enter);
return 0;
}
void input(double *x, int &enter)
{
cout << "你可以最多输入10个成绩,输入-1提前结束" << endl;
for (enter = 0; enter > x[enter];
if (x[enter] ==(-1))
break;
}
return;
}
void show(double *x, int enter)
{
for (int i = 0; i < enter; i++)
{
cout << x[i] << " ";
}
return;
}
void avg(double *x, int enter)
{
double sum, avg;
for (int i = 0; i < enter; i++)
{
sum += x[i];
}
avg = sum / enter;
cout << endl << "平均成绩是" << avg << endl;
return;
}
自学中,谢谢楼主大大的答案参考。
第二题输入数组数据的修改,楼主大大可以看下。
void input(int arr[],int *n)
{
for (int i = 0; i < 10; i++)
{
cout << "input grade" << i+1 <> arr[i];
if (arr[i] == -1)
{
(*n) = i;
break;
}
}
}
7-7fill_array函数要求返回值是一个指向结尾的指针不是数字