编程环境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 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 |
// Chapter11.cpp #include <iostream> #include <cstring> #include <string> #include <cctype> #include <cstdlib> #include <ctime> #include <fstream> #include "vector.h" #include "mytime1.h" #include "Stonewt1.h" #include "Stonewt2.h" #include "complex0.h" using namespace std; // practice 1 // 第六版的校对做得真的有点差,随机漫步应该是程序清单11.15才对 // 对应的vector.h和vector.cpp可以用书本中原有的,也可以用practice 2中修改过的 void p11_1(void) { using VECTOR::Vector; string filename = "randwalk.txt"; ofstream outFile; outFile.open(filename.c_str(), ios_base::out); srand(time(0)); double direction; Vector step; Vector result(0.0, 0.0); unsigned long steps = 0; double target; double dstep; cout << "Enter target distance (q to quit): "; while (cin >> target) { cout << "Enter step length: "; if (!(cin >> dstep)) break; outFile << "Target Distance: " << target << ", " << "Step Size: " << dstep << endl; while (result.magval() < target) { direction = rand() % 360; step.reset(dstep, direction, Vector::POL); result = result + step; steps++; step.rect_mode(); outFile << result << endl; } outFile << "After " << steps << " steps, the subject " "has the following locations:\n"; outFile << result << endl; result.polar_mode(); outFile << " or\n" << result << endl; outFile << "Average outward distance per step = " << result.magval() / steps << endl; steps = 0; result.reset(0.0, 0.0); cout << "Enter target distance (q to quit): "; } cout << "Bye!\n"; cin.clear(); while (cin.get() != '\n') continue; return ; } // practice 2 void p11_2(void) { using VECTOR::Vector; srand(time(0)); double direction; Vector step; Vector result(0.0, 0.0); unsigned long steps = 0; double target; double dstep; cout << "Enter target distance (q to quit): "; while (cin >> target) { cout << "Enter step length: "; if (!(cin >> dstep)) break; cout << "Target Distance: " << target << ", " << "Step Size: " << dstep << endl; while (result.magval() < target) { direction = rand() % 360; step.reset(dstep, direction, Vector::POL); result = result + step; steps++; step.rect_mode(); cout << result << endl; } cout << "After " << steps << " steps, the subject " "has the following locations:\n"; cout << result << endl; result.polar_mode(); cout << " or\n" << result << endl; cout << "Average outward distance per step = " << result.magval() / steps << endl; steps = 0; result.reset(0.0, 0.0); cout << "Enter target distance (q to quit): "; } cout << "Bye!\n"; cin.clear(); while (cin.get() != '\n') continue; return; } // practice 3 void p11_3(void) { using VECTOR::Vector; srand(time(0)); double direction; Vector step; Vector result(0.0, 0.0); unsigned long steps = 0; double target; double dstep; unsigned long max_steps = 0; unsigned long min_steps = 0 - 1; unsigned long total_steps = 0; double average_steps = 0.0; int test_times = 0; cout << "Enter target distance (q to quit): "; cin >> target; cout << "Enter step length: "; cin >> dstep; cout << "Enter test times: "; cin >> test_times; cin.get(); for (int i = 0; i < test_times; i++) { cout << "Target Distance: " << target << ", " << "Step Size: " << dstep << endl; while (result.magval() < target) { direction = rand() % 360; step.reset(dstep, direction, Vector::POL); result = result + step; steps++; step.rect_mode(); cout << result << endl; } cout << "After " << steps << " steps, the subject " "has the following locations:\n"; cout << result << endl; result.polar_mode(); cout << " or\n" << result << endl; cout << "Average outward distance per step = " << result.magval() / steps << endl; total_steps += steps; if (steps > max_steps) { max_steps = steps; } else if (steps < min_steps) { min_steps = steps; } steps = 0; result.reset(0.0, 0.0); cout << endl; } average_steps = (double)total_steps / test_times; cout << endl; cout << "Test " << test_times << " times, max steps is " << max_steps << ", min steps is " << min_steps << ", average steps is " << average_steps << endl; cout << "Bye!\n"; cin.clear(); while (cin.get() != '\n') continue; return; } // practice 4 void p11_4(void) { Time aida(3, 35); Time tosca(2, 48); Time temp; cout << "Aida and Tosca:\n"; cout << aida << "; " << tosca << endl; temp = aida + tosca; cout << "Aida + Tosca: " << temp << endl; temp = aida * 1.17; cout << "Aida * 1.17: " << temp << endl; cout << "10 * Tosca: " << 10 * tosca << endl; return; } // practice 5 void p11_5(void) { Stonewt1 st1(120); Stonewt1 st2(12.3, Stonewt1::IPOUND); Stonewt1 st3(123.3, Stonewt1::DPOUND); Stonewt1 st4 = 13 + st1; Stonewt1 st5 = 13 * st2; Stonewt1 st6 = 13 - st2; Stonewt1 st7 = st1 + 10; Stonewt1 st8 = st1 - 10; Stonewt1 st9 = st1 * 10; cout << st1 << endl; cout << st2 << endl; cout << st3 << endl; cout << st4 << endl; cout << st5 << endl; cout << st6 << endl; cout << st7 << endl; cout << st8 << endl; cout << st9 << endl; } // practice 6 void p11_6(void) { Stonewt2 st[6] = { Stonewt2(10.0, 0), Stonewt2(11.1, 0), Stonewt2(22.2, 0) }; Stonewt2 max = st[0]; Stonewt2 min = st[0]; Stonewt2 ele(11, 0); int num = 0; for (int i = 0; i < 3; i++) { double pounds = 0; cout << "Enter the pounds: "; cin >> pounds; Stonewt2 tmp(pounds); st[3 + i] = tmp; } for (int i = 0; i < 6; i++) { if (max < st[i]) { max = st[i]; } else if (min > st[i]) { min = st[i]; } if (st[i] >= ele) { num++; } } cout << "Max element: "; max.show_stn(); cout << "Min element: "; min.show_stn(); cout << "There are " << num << " element large than 11 stones." << endl; return; } // practice 7 char *mystrncpy(char *s1, char *s2, int n) { int s1_length = strlen(s1); int s2_length = strlen(s2); int copy_length = (s2_length >= n ? n : s2_length); for (size_t i = 0; i < copy_length; i++) { *++s1 = *++s2; } s1[copy_length - 1] = '\0'; return s1; } void p11_7(void) { char string1[256]; char string2[256]; int string1_length = 0; int string2_length = 0; while (1) // 一直循环输入吧,也可以添加一个退出条件 { printf("Enter the first string:"); fgets(string1, 256, stdin); printf("Enter the second string:"); fgets(string2, 256, stdin); string1_length = strlen(string1); string2_length = strlen(string2); printf("The first original string: "); printf("%s", string1); mystrncpy(string1, string2, 256); printf("After mystrncpy, ths first string: "); printf("%s", string2); printf("\n"); } return; } int main(int argc, char ** argv) { p11_7(); while (cin.get()); } // complex0.h #pragma once #ifndef COMPLEX0_H_ #define COMPLEX0_H_ #include <iostream> class complex { public: complex(); complex(double real, double imaginary); complex operator+(const complex & cx) const; complex operator-(const complex & cx) const; complex operator*(const complex & cx) const; friend complex operator~(const complex & cx); friend complex operator*(int x, const complex & cx); friend std::istream & operator>>(std::istream & is, complex & cx); friend std::ostream & operator<<(std::ostream & os, const complex & cx); private: double real; double imaginary; }; #endif // !COMPLEX0_H_ // complex0.cpp #include <iostream> using namespace std; #include "complex0.h" complex::complex() { real = imaginary = 0; } complex::complex(double real, double imaginary) { this->real = real; this->imaginary = imaginary; } complex complex::operator+(const complex & cx) const { return complex(real + cx.real, imaginary + cx.imaginary); } complex complex::operator-(const complex & cx) const { return complex(real - cx.real, imaginary - cx.imaginary); } complex complex::operator*(const complex & cx) const { return complex(real * cx.real - imaginary * cx.imaginary, real * cx.imaginary + imaginary * cx.real); } complex operator~(const complex & cx) { return complex(cx.real, -cx.imaginary); } complex operator*(int x, const complex & cx) { return complex(x*cx.real, x*cx.imaginary); } std::istream & operator>>(std::istream & is, complex & cx) { cout << "real: "; is >> cx.real; if (!is) { return is; } cout << "imaginary: "; is >> cx.imaginary; cin.get(); return is; } std::ostream & operator<<(std::ostream & os, const complex & cx) { os << "(" << cx.real << ", " << cx.imaginary << "i)"; return os; } // mytime1.h #pragma once #ifndef MYTIME1_H_ #define MYTIME1_H_ #include <iostream> class Time { private: int hours; int minutes; public: Time(); Time(int h, int m = 0); void AddMin(int m); void AddHr(int h); void Reset(int h = 0, int m = 0); friend Time operator+(const Time & t1, const Time & t2); friend Time operator-(const Time & t1, const Time & t2); friend Time operator*(double n, const Time & t); friend Time operator*(const Time & t, double n); friend std::ostream & operator<<(std::ostream & os, const Time & t); }; #endif // MYTIME1_H_ // mytime1.cpp #include "mytime1.h" using namespace std; Time::Time() { } Time::Time(int h, int m) { hours = h; minutes = m; } void Time::AddHr(int h) { hours += h; } void Time::AddMin(int m) { minutes += m; } void Time::Reset(int h, int m) { hours = h; minutes = m; } Time operator+(const Time & t1, const Time & t2) { Time sum; sum.minutes = t1.minutes + t2.minutes; sum.hours = t1.hours + t2.hours + sum.minutes / 60; sum.minutes %= 60; return sum; } Time operator-(const Time & t1, const Time & t2) { Time diff; int tot1, tot2; tot1 = t1.minutes + 60 * t1.hours; tot2 = t2.minutes + 60 * t2.hours; diff.minutes = (tot1 - tot2) % 60; diff.hours = (tot1 - tot2) / 60; return diff; } Time operator*(double n, const Time & t) { Time result; long totalminutes = t.hours * n * 60 + t.minutes * n; result.hours = totalminutes / 60; result.minutes = totalminutes % 60; return result; } Time operator*(const Time & t, double n) { Time result; long totalminutes = t.hours * n * 60 + t.minutes * n; result.hours = totalminutes / 60; result.minutes = totalminutes % 60; return result; } std::ostream & operator<<(std::ostream & os, const Time & t) { os << t.hours << " hours, " << t.minutes << " minutes"; return os; } // Stonewt1.h #pragma once #ifndef STONEWT1_H_ #define STONEWT1_H_ #include <iostream> class Stonewt1 { public: enum format { STONES, IPOUND, DPOUND }; private: enum {Lbs_per_stn = 14}; format ft; double stone; int ipounds; double dpounds; // 有三种格式的数据,成员函数先对total进行操作 // 然后调用update更新所有的成员变量 // 免得每个成员函数里面都用if...else if的判断来更新 double total; void update(); public: Stonewt1(); Stonewt1(double n, format f = STONES); Stonewt1 operator+(double n) const; Stonewt1 operator-(double n) const; Stonewt1 operator*(double n) const; friend Stonewt1 operator+(double n, const Stonewt1 & st); friend Stonewt1 operator-(double n, const Stonewt1 & st); friend Stonewt1 operator*(double n, const Stonewt1 & st); friend std::ostream & operator<<(std::ostream & os, const Stonewt1 & st); }; #endif // !STONEWT1_H_ // Stonewt1.cpp #include "Stonewt1.h" using namespace std; void Stonewt1::update() { if (ft == STONES) { stone = total; ipounds = (int)stone * Lbs_per_stn; dpounds = stone * Lbs_per_stn; } else if (ft == IPOUND) { ipounds = total; stone = ipounds / Lbs_per_stn; dpounds = ipounds; } else if (ft == DPOUND) { dpounds = total; stone = dpounds / Lbs_per_stn; ipounds = (int)dpounds; } } Stonewt1::Stonewt1() { total = 0; ft = STONES; update(); } Stonewt1::Stonewt1(double n, format f) { total = n; ft = f; update(); } Stonewt1 Stonewt1::operator+(double n) const { Stonewt1 st(total + n, ft); return st; } Stonewt1 Stonewt1::operator-(double n) const { Stonewt1 st(total - n, ft); return st; } Stonewt1 Stonewt1::operator*(double n) const { Stonewt1 st(total * n, ft); return st; } Stonewt1 operator+(double n, const Stonewt1 & st) { return (st + n); } Stonewt1 operator-(double n, const Stonewt1 & st) { return st - n; } Stonewt1 operator*(double n, const Stonewt1 & st) { return st * n; } std::ostream & operator<<(std::ostream & os, const Stonewt1 & st) { if (st.ft == st.STONES) { cout << "Stone: " << st.stone ; } else if (st.ft == st.IPOUND) { cout << "pounds in int: " << st.ipounds ; } else if (st.ft == st.DPOUND) { cout << "pounds in double: " << st.dpounds; } return os; } // Stonewt2.h #pragma once #ifndef MYTIME2_H_ #define MYTIME2_H_ class Stonewt2 { private: enum { Lbs_per_stn = 14 }; int stone; double pds_left; double pounds; public: Stonewt2(double lbs); Stonewt2(int stn, double lbs); Stonewt2(); ~Stonewt2(); void show_lbs() const; void show_stn() const; bool operator==(const Stonewt2 & st) const; bool operator!=(const Stonewt2 & st) const; bool operator<(const Stonewt2 & st) const; bool operator>(const Stonewt2 & st) const; bool operator<=(const Stonewt2 & st) const; bool operator>=(const Stonewt2 & st) const; }; #endif // !MYTIME2_H_ // Stone2.cpp #include "Stonewt2.h" #include <iostream> using namespace std; Stonewt2::Stonewt2(double lbs) { stone = int(lbs) / Lbs_per_stn; pds_left = int(lbs) % Lbs_per_stn + lbs - (int)lbs; pounds = lbs; } Stonewt2::Stonewt2(int stn, double lbs) { stone = stn; pds_left = lbs; pounds = stn * Lbs_per_stn + lbs; } Stonewt2::Stonewt2() { stone = pds_left = pounds = 0; } Stonewt2::~Stonewt2() { } void Stonewt2::show_stn() const { cout << stone << " stone, " << pds_left << " pounds\n"; } void Stonewt2::show_lbs() const { cout << pounds << " pounds\n"; } bool Stonewt2::operator<(const Stonewt2 & st) const { return pounds < st.pounds; } bool Stonewt2::operator>(const Stonewt2 & st) const { return pounds > st.pounds; } bool Stonewt2::operator<=(const Stonewt2 & st) const { return pounds <= st.pounds; } bool Stonewt2::operator>=(const Stonewt2 & st) const { return pounds >= st.pounds; } bool Stonewt2::operator==(const Stonewt2 & st) const { return pounds == st.pounds; } bool Stonewt2::operator!=(const Stonewt2 & st) const { return pounds != st.pounds; } // vector.h #ifndef VECTOR_H_ #define VECTOR_H_ #pragma once #include <iostream> namespace VECTOR { class Vector { public: enum Mode {RECT, POL}; private: double x; double y; Mode mode; void set_x(double, double); void set_y(double, double); public: Vector(); Vector(double n1, double n2, Mode form = RECT); void reset(double n1, double n2, Mode form = RECT); ~Vector(); double xval() const { return x; } double yval() const { return y; } double magval() const; double angval() const; void polar_mode(); void rect_mode(); Vector operator+(const Vector & b) const; Vector operator-(const Vector & b) const; Vector operator-() const; Vector operator*(double n) const; friend Vector operator*(double n, const Vector & a); friend std::ostream & operator<<(std::ostream & os, const Vector & v); }; } #endif // !VECTOR_H_ // vector.cpp #include <cmath> #include "vector.h" using std::sqrt; using std::sin; using std::cos; using std::atan; using std::atan2; using std::cout; namespace VECTOR { const double Rad_to_deg = 45.0 / atan(1.0); void Vector::set_x(double mag, double ang) { x = mag * cos(ang); } void Vector::set_y(double mag, double ang) { y = mag * sin(ang); } Vector::Vector() { x = y = 0.0; mode = RECT; } Vector::Vector(double n1, double n2, Mode form) { mode = form; if (form == RECT) { x = n1; y = n2; } else if (form == POL) { set_x(n1, n2); set_y(n1, n2); } else { cout << "Incorrect 3rd argument to Vector() -- "; cout << "vector set to 0\n"; x = y = 0.0; mode = RECT; } } void Vector::reset(double n1, double n2, Mode form) { mode = form; if (form == RECT) { x = n1; y = n2; } else if (form == POL) { set_x(n1, n2); set_y(n1, n2); } else { cout << "Incorrect 3rd argument to Vector() -- "; cout << "vector set to 0\n"; x = y = 0.0; mode = RECT; } } Vector::~Vector() { } void Vector::polar_mode() { mode = POL; } void Vector::rect_mode() { mode = RECT; } double Vector::magval() const { return sqrt(x * x + y * y); } double Vector::angval() const { if(x == 0.0 && y == 0.0) { return 0.0; } else return atan2(y, x); } Vector Vector::operator+(const Vector & b) const { return Vector(x + b.x, y + b.y); } Vector Vector::operator-(const Vector & b) const { return Vector(x - b.x, y - b.y); } Vector Vector::operator-() const { return Vector(-x, -y); } Vector Vector::operator*(double n) const { return Vector(n*x, n*y); } Vector operator*(double n, const Vector & a) { return a * n; } std::ostream & operator<<(std::ostream & os, const Vector & v) { if (v.mode == Vector::RECT) { os << "(x,y) = (" << v.x << ", " << v.y << ")"; } else if (v.mode == Vector::POL) { os << "(m,a) = (" << v.magval() << ", " << v.angval() * Rad_to_deg << ")"; } else os << "Vector object mode is invalid"; return os; } } |
此文为博主原创文章,转载请注明出处