From cea0578a9dac6d78bb861a24351aa8437bccacf9 Mon Sep 17 00:00:00 2001 From: fyang Date: Mon, 30 May 2022 21:54:04 +0800 Subject: [PATCH 1/2] TODO --- 10#/10-9.cpp | 28 + 10#/p1/main.cpp | 20 + 10#/p1/p1.cpp | 27 + 10#/p1/p1.h | 20 + 10#/p2/main.cpp | 23 + 10#/p2/p2.cpp | 23 + 10#/p2/p2.h | 22 + 10#/p3/golf.cpp | 28 + 10#/p3/golf.h | 35 + 10#/p3/main.cpp | 15 + 10#/p4/main.cpp | 19 + 10#/p4/p4.cpp | 74 + 10#/p4/p4.h | 39 + 10#/p5/stack.cpp | 38 + 10#/p5/stack.h | 26 + 10#/p5/stacker.cpp | 55 + 10#/p6/main.cpp | 24 + 10#/p6/p6.cpp | 28 + 10#/p6/p6.h | 20 + 10#/p7/main.cpp | 13 + 10#/p7/p7.cpp | 17 + 10#/p7/p7.h | 22 + 10#/p8/list.cpp | 38 + 10#/p8/list.h | 25 + 10#/p8/main.cpp | 22 + 10#/r5.h | 12 + 10#/r7.cpp | 19 + 10#/r9.h | 30 + 10#/stack.cpp | 38 + 10#/stack.h | 20 + 10#/stacker.cpp | 49 + 10#/stock00.cpp | 64 + 10#/stock00.h | 22 + 10#/stock10.cpp | 77 + 10#/stock10.h | 24 + 10#/stock20.cpp | 85 + 10#/stock20.h | 25 + 10#/usestock1.cpp | 24 + 10#/usestock2.cpp | 30 + 10#/usestock20.cpp | 19 + 11#/demo0.cpp | 50 + 11#/demo1.cpp | 53 + 11#/demo2.cpp | 41 + 11#/mytime0.cpp | 45 + 11#/mytime0.h | 23 + 11#/mytime1.cpp | 45 + 11#/mytime1.h | 23 + 11#/mytime2.cpp | 69 + 11#/mytime2.h | 25 + 11#/mytime3.cpp | 85 + 11#/mytime3.h | 26 + 11#/mytime4.cpp | 92 + 11#/mytime4.h | 27 + 11#/p1/main.cpp | 66 + 11#/p1/randwalk.txt | 1643 ++++++++++++++++++ 11#/p1/vector.cpp | 134 ++ 11#/p1/vector.h | 44 + 11#/p2/main.cpp | 51 + 11#/p2/vector.cpp | 139 ++ 11#/p2/vector.h | 44 + 11#/p3/main.cpp | 69 + 11#/p3/vector.cpp | 134 ++ 11#/p3/vector.h | 44 + 11#/p4/mytime4.cpp | 92 + 11#/p4/mytime4.h | 31 + 11#/p4/usetime4.cpp | 35 + 11#/p5/stone.cpp | 32 + 11#/p5/stonewt.cpp | 54 + 11#/p5/stonewt.h | 27 + 11#/p6/stone.cpp | 39 + 11#/p6/stonewt.cpp | 60 + 11#/p6/stonewt.h | 30 + 11#/p7/complex0.cpp | 52 + 11#/p7/complex0.h | 25 + 11#/p7/main.cpp | 26 + 11#/r1/stone.cpp | 16 + 11#/r1/stonewt.cpp | 35 + 11#/r1/stonewt.h | 23 + 11#/r4/stone.cpp | 16 + 11#/r4/stonewt.cpp | 35 + 11#/r4/stonewt.h | 23 + 11#/r7/vector.h | 46 + 11#/stone/stone.cpp | 24 + 11#/stone/stonewt.cpp | 30 + 11#/stone/stonewt.h | 23 + 11#/stone1/stone1.cpp | 19 + 11#/stone1/stonewt1.cpp | 40 + 11#/stone1/stonewt1.h | 24 + 11#/usetime0.cpp | 21 + 11#/usetime1.cpp | 26 + 11#/usetime2.cpp | 29 + 11#/usetime3.cpp | 32 + 11#/usetime4.cpp | 35 + 11#/vector/main.cpp | 51 + 11#/vector/vector.cpp | 134 ++ 11#/vector/vector.h | 44 + 4-compound_types/1.cpp | 24 + 4-compound_types/10.cpp | 27 + 4-compound_types/11.cpp | 32 + 4-compound_types/12.cpp | 30 + 4-compound_types/13.cpp | 26 + 4-compound_types/14.cpp | 23 + 4-compound_types/15.cpp | 14 + 4-compound_types/16.cpp | 20 + 4-compound_types/17.cpp | 27 + 4-compound_types/18.cpp | 25 + 4-compound_types/19.cpp | 30 + 4-compound_types/2.cpp | 19 + 4-compound_types/20.cpp | 17 + 4-compound_types/21.cpp | 35 + 4-compound_types/22.cpp | 27 + 4-compound_types/23.cpp | 32 + 4-compound_types/24.cpp | 38 + 4-compound_types/25.cpp | 32 + 4-compound_types/3.cpp | 18 + 4-compound_types/4.cpp | 21 + 4-compound_types/6.cpp | 20 + 4-compound_types/7.cpp | 24 + 4-compound_types/8.cpp | 26 + 4-compound_types/9.cpp | 28 + 4-compound_types/p1.cpp | 26 + 4-compound_types/p10.cpp | 26 + 4-compound_types/p2.cpp | 18 + 4-compound_types/p3.cpp | 25 + 4-compound_types/p4.cpp | 19 + 4-compound_types/p5.cpp | 21 + 4-compound_types/p6.cpp | 36 + 4-compound_types/p7.cpp | 28 + 4-compound_types/p8.cpp | 30 + 4-compound_types/p9.cpp | 52 + 5-loops_and_relational_expressions/1.cpp | 15 + 5-loops_and_relational_expressions/10.cpp | 24 + 5-loops_and_relational_expressions/11.cpp | 19 + 5-loops_and_relational_expressions/12.cpp | 19 + 5-loops_and_relational_expressions/13.cpp | 19 + 5-loops_and_relational_expressions/14.cpp | 23 + 5-loops_and_relational_expressions/15.cpp | 20 + 5-loops_and_relational_expressions/16.cpp | 26 + 5-loops_and_relational_expressions/17.cpp | 24 + 5-loops_and_relational_expressions/18.cpp | 24 + 5-loops_and_relational_expressions/19.cpp | 24 + 5-loops_and_relational_expressions/1_1.cpp | 16 + 5-loops_and_relational_expressions/2.cpp | 19 + 5-loops_and_relational_expressions/20.cpp | 39 + 5-loops_and_relational_expressions/3.cpp | 22 + 5-loops_and_relational_expressions/4.cpp | 17 + 5-loops_and_relational_expressions/5.cpp | 19 + 5-loops_and_relational_expressions/6.cpp | 17 + 5-loops_and_relational_expressions/7.cpp | 20 + 5-loops_and_relational_expressions/8.cpp | 19 + 5-loops_and_relational_expressions/9.cpp | 23 + 5-loops_and_relational_expressions/p1.cpp | 22 + 5-loops_and_relational_expressions/p10.cpp | 22 + 5-loops_and_relational_expressions/p2.cpp | 18 + 5-loops_and_relational_expressions/p3.cpp | 19 + 5-loops_and_relational_expressions/p4.cpp | 25 + 5-loops_and_relational_expressions/p5.cpp | 29 + 5-loops_and_relational_expressions/p6.cpp | 36 + 5-loops_and_relational_expressions/p7.cpp | 37 + 5-loops_and_relational_expressions/p8.cpp | 24 + 5-loops_and_relational_expressions/p9.cpp | 24 + 5-loops_and_relational_expressions/review.md | 25 + 6#/1.cpp | 24 + 6#/10-1.cpp | 58 + 6#/10.cpp | 58 + 6#/11.cpp | 51 + 6#/12.cpp | 32 + 6#/13.cpp | 21 + 6#/14.cpp | 34 + 6#/15.cpp | 33 + 6#/16.cpp | 45 + 6#/17.cpp | 56 + 6#/2.cpp | 24 + 6#/3.cpp | 24 + 6#/4.cpp | 22 + 6#/5.cpp | 49 + 6#/6.cpp | 36 + 6#/7.cpp | 34 + 6#/8.cpp | 42 + 6#/9.cpp | 18 + 6#/carinfo.txt | 4 + 6#/p1.cpp | 31 + 6#/p2.cpp | 41 + 6#/p3.cpp | 37 + 6#/p4.cpp | 113 ++ 6#/p5.cpp | 26 + 6#/p6.cpp | 60 + 6#/p7.cpp | 47 + 6#/p8.cpp | 35 + 6#/p8.txt | 3 + 6#/p9.cpp | 79 + 6#/p9.txt | 9 + 6#/scores.txt | 3 + 7#/1.cpp | 18 + 7#/10.cpp | 32 + 7#/11.cpp | 45 + 7#/12.cpp | 54 + 7#/13.cpp | 51 + 7#/14.cpp | 31 + 7#/15.cpp | 45 + 7#/16.cpp | 20 + 7#/17.cpp | 42 + 7#/18.cpp | 36 + 7#/19.cpp | 65 + 7#/1_1.cpp | 18 + 7#/2.cpp | 34 + 7#/3.cpp | 35 + 7#/4.cpp | 30 + 7#/5.cpp | 28 + 7#/6.cpp | 37 + 7#/7.cpp | 74 + 7#/8.cpp | 37 + 7#/8_1.cpp | 37 + 7#/9.cpp | 33 + 7#/const.cpp | 24 + 7#/const_1.cpp | 24 + 7#/p1.cpp | 28 + 7#/p10.cpp | 33 + 7#/p2.cpp | 72 + 7#/p3.cpp | 39 + 7#/p4.cpp | 26 + 7#/p5.cpp | 29 + 7#/p6.cpp | 67 + 7#/p7.cpp | 77 + 7#/p8-a.cpp | 45 + 7#/p8-b.cpp | 50 + 7#/p9.cpp | 92 + 8#/1.cpp | 22 + 8#/10.cpp | 67 + 8#/11.cpp | 33 + 8#/12.cpp | 67 + 8#/13.cpp | 74 + 8#/13_1.cpp | 86 + 8#/14.cpp | 64 + 8#/15.cpp | 50 + 8#/2.cpp | 22 + 8#/3.cpp | 29 + 8#/4.cpp | 61 + 8#/5.cpp | 30 + 8#/5_1.cpp | 28 + 8#/6.cpp | 71 + 8#/7.cpp | 63 + 8#/8.cpp | 51 + 8#/9.cpp | 60 + 8#/ep-data.txt | 7 + 8#/p1.cpp | 28 + 8#/p2.cpp | 40 + 8#/p3.cpp | 32 + 8#/p4.cpp | 51 + 8#/p5.cpp | 31 + 8#/p6.cpp | 51 + 8#/p7.cpp | 94 + 8#/r5.cpp | 45 + 8#/review.txt | 78 + 8#/test.cpp | 27 + 9#/1.cpp | 9 + 9#/10.cpp | 62 + 9#/2.cpp | 2 + 9#/4.cpp | 47 + 9#/9.cpp | 41 + 9#/coordin.h | 19 + 9#/external.cpp | 19 + 9#/file1.cpp | 22 + 9#/file2.cpp | 22 + 9#/namesp.cpp | 48 + 9#/namesp.h | 27 + 9#/namessp.cpp | 13 + 9#/p1/golf.cpp | 43 + 9#/p1/golf.h | 12 + 9#/p1/main.cpp | 34 + 9#/p2.cpp | 36 + 9#/p3.cpp | 53 + 9#/p4/main.cpp | 18 + 9#/p4/p4.cpp | 74 + 9#/p4/p4.h | 23 + 9#/support.cpp | 18 + 9#/support.h | 11 + 9#/twofile1.cpp | 19 + 9#/twofile2.cpp | 13 + 279 files changed, 11669 insertions(+) create mode 100644 10#/10-9.cpp create mode 100644 10#/p1/main.cpp create mode 100644 10#/p1/p1.cpp create mode 100644 10#/p1/p1.h create mode 100644 10#/p2/main.cpp create mode 100644 10#/p2/p2.cpp create mode 100644 10#/p2/p2.h create mode 100644 10#/p3/golf.cpp create mode 100644 10#/p3/golf.h create mode 100644 10#/p3/main.cpp create mode 100644 10#/p4/main.cpp create mode 100644 10#/p4/p4.cpp create mode 100644 10#/p4/p4.h create mode 100644 10#/p5/stack.cpp create mode 100644 10#/p5/stack.h create mode 100644 10#/p5/stacker.cpp create mode 100644 10#/p6/main.cpp create mode 100644 10#/p6/p6.cpp create mode 100644 10#/p6/p6.h create mode 100644 10#/p7/main.cpp create mode 100644 10#/p7/p7.cpp create mode 100644 10#/p7/p7.h create mode 100644 10#/p8/list.cpp create mode 100644 10#/p8/list.h create mode 100644 10#/p8/main.cpp create mode 100644 10#/r5.h create mode 100644 10#/r7.cpp create mode 100644 10#/r9.h create mode 100644 10#/stack.cpp create mode 100644 10#/stack.h create mode 100644 10#/stacker.cpp create mode 100644 10#/stock00.cpp create mode 100644 10#/stock00.h create mode 100644 10#/stock10.cpp create mode 100644 10#/stock10.h create mode 100644 10#/stock20.cpp create mode 100644 10#/stock20.h create mode 100644 10#/usestock1.cpp create mode 100644 10#/usestock2.cpp create mode 100644 10#/usestock20.cpp create mode 100644 11#/demo0.cpp create mode 100644 11#/demo1.cpp create mode 100644 11#/demo2.cpp create mode 100644 11#/mytime0.cpp create mode 100644 11#/mytime0.h create mode 100644 11#/mytime1.cpp create mode 100644 11#/mytime1.h create mode 100644 11#/mytime2.cpp create mode 100644 11#/mytime2.h create mode 100644 11#/mytime3.cpp create mode 100644 11#/mytime3.h create mode 100644 11#/mytime4.cpp create mode 100644 11#/mytime4.h create mode 100644 11#/p1/main.cpp create mode 100644 11#/p1/randwalk.txt create mode 100644 11#/p1/vector.cpp create mode 100644 11#/p1/vector.h create mode 100644 11#/p2/main.cpp create mode 100644 11#/p2/vector.cpp create mode 100644 11#/p2/vector.h create mode 100644 11#/p3/main.cpp create mode 100644 11#/p3/vector.cpp create mode 100644 11#/p3/vector.h create mode 100644 11#/p4/mytime4.cpp create mode 100644 11#/p4/mytime4.h create mode 100644 11#/p4/usetime4.cpp create mode 100644 11#/p5/stone.cpp create mode 100644 11#/p5/stonewt.cpp create mode 100644 11#/p5/stonewt.h create mode 100644 11#/p6/stone.cpp create mode 100644 11#/p6/stonewt.cpp create mode 100644 11#/p6/stonewt.h create mode 100644 11#/p7/complex0.cpp create mode 100644 11#/p7/complex0.h create mode 100644 11#/p7/main.cpp create mode 100644 11#/r1/stone.cpp create mode 100644 11#/r1/stonewt.cpp create mode 100644 11#/r1/stonewt.h create mode 100644 11#/r4/stone.cpp create mode 100644 11#/r4/stonewt.cpp create mode 100644 11#/r4/stonewt.h create mode 100644 11#/r7/vector.h create mode 100644 11#/stone/stone.cpp create mode 100644 11#/stone/stonewt.cpp create mode 100644 11#/stone/stonewt.h create mode 100644 11#/stone1/stone1.cpp create mode 100644 11#/stone1/stonewt1.cpp create mode 100644 11#/stone1/stonewt1.h create mode 100644 11#/usetime0.cpp create mode 100644 11#/usetime1.cpp create mode 100644 11#/usetime2.cpp create mode 100644 11#/usetime3.cpp create mode 100644 11#/usetime4.cpp create mode 100644 11#/vector/main.cpp create mode 100644 11#/vector/vector.cpp create mode 100644 11#/vector/vector.h create mode 100644 4-compound_types/1.cpp create mode 100644 4-compound_types/10.cpp create mode 100644 4-compound_types/11.cpp create mode 100644 4-compound_types/12.cpp create mode 100644 4-compound_types/13.cpp create mode 100644 4-compound_types/14.cpp create mode 100644 4-compound_types/15.cpp create mode 100644 4-compound_types/16.cpp create mode 100644 4-compound_types/17.cpp create mode 100644 4-compound_types/18.cpp create mode 100644 4-compound_types/19.cpp create mode 100644 4-compound_types/2.cpp create mode 100644 4-compound_types/20.cpp create mode 100644 4-compound_types/21.cpp create mode 100644 4-compound_types/22.cpp create mode 100644 4-compound_types/23.cpp create mode 100644 4-compound_types/24.cpp create mode 100644 4-compound_types/25.cpp create mode 100644 4-compound_types/3.cpp create mode 100644 4-compound_types/4.cpp create mode 100644 4-compound_types/6.cpp create mode 100644 4-compound_types/7.cpp create mode 100644 4-compound_types/8.cpp create mode 100644 4-compound_types/9.cpp create mode 100644 4-compound_types/p1.cpp create mode 100644 4-compound_types/p10.cpp create mode 100644 4-compound_types/p2.cpp create mode 100644 4-compound_types/p3.cpp create mode 100644 4-compound_types/p4.cpp create mode 100644 4-compound_types/p5.cpp create mode 100644 4-compound_types/p6.cpp create mode 100644 4-compound_types/p7.cpp create mode 100644 4-compound_types/p8.cpp create mode 100644 4-compound_types/p9.cpp create mode 100644 5-loops_and_relational_expressions/1.cpp create mode 100644 5-loops_and_relational_expressions/10.cpp create mode 100644 5-loops_and_relational_expressions/11.cpp create mode 100644 5-loops_and_relational_expressions/12.cpp create mode 100644 5-loops_and_relational_expressions/13.cpp create mode 100644 5-loops_and_relational_expressions/14.cpp create mode 100644 5-loops_and_relational_expressions/15.cpp create mode 100644 5-loops_and_relational_expressions/16.cpp create mode 100644 5-loops_and_relational_expressions/17.cpp create mode 100644 5-loops_and_relational_expressions/18.cpp create mode 100644 5-loops_and_relational_expressions/19.cpp create mode 100644 5-loops_and_relational_expressions/1_1.cpp create mode 100644 5-loops_and_relational_expressions/2.cpp create mode 100644 5-loops_and_relational_expressions/20.cpp create mode 100644 5-loops_and_relational_expressions/3.cpp create mode 100644 5-loops_and_relational_expressions/4.cpp create mode 100644 5-loops_and_relational_expressions/5.cpp create mode 100644 5-loops_and_relational_expressions/6.cpp create mode 100644 5-loops_and_relational_expressions/7.cpp create mode 100644 5-loops_and_relational_expressions/8.cpp create mode 100644 5-loops_and_relational_expressions/9.cpp create mode 100644 5-loops_and_relational_expressions/p1.cpp create mode 100644 5-loops_and_relational_expressions/p10.cpp create mode 100644 5-loops_and_relational_expressions/p2.cpp create mode 100644 5-loops_and_relational_expressions/p3.cpp create mode 100644 5-loops_and_relational_expressions/p4.cpp create mode 100644 5-loops_and_relational_expressions/p5.cpp create mode 100644 5-loops_and_relational_expressions/p6.cpp create mode 100644 5-loops_and_relational_expressions/p7.cpp create mode 100644 5-loops_and_relational_expressions/p8.cpp create mode 100644 5-loops_and_relational_expressions/p9.cpp create mode 100644 5-loops_and_relational_expressions/review.md create mode 100644 6#/1.cpp create mode 100644 6#/10-1.cpp create mode 100644 6#/10.cpp create mode 100644 6#/11.cpp create mode 100644 6#/12.cpp create mode 100644 6#/13.cpp create mode 100644 6#/14.cpp create mode 100644 6#/15.cpp create mode 100644 6#/16.cpp create mode 100644 6#/17.cpp create mode 100644 6#/2.cpp create mode 100644 6#/3.cpp create mode 100644 6#/4.cpp create mode 100644 6#/5.cpp create mode 100644 6#/6.cpp create mode 100644 6#/7.cpp create mode 100644 6#/8.cpp create mode 100644 6#/9.cpp create mode 100644 6#/carinfo.txt create mode 100644 6#/p1.cpp create mode 100644 6#/p2.cpp create mode 100644 6#/p3.cpp create mode 100644 6#/p4.cpp create mode 100644 6#/p5.cpp create mode 100644 6#/p6.cpp create mode 100644 6#/p7.cpp create mode 100644 6#/p8.cpp create mode 100644 6#/p8.txt create mode 100644 6#/p9.cpp create mode 100644 6#/p9.txt create mode 100644 6#/scores.txt create mode 100644 7#/1.cpp create mode 100644 7#/10.cpp create mode 100644 7#/11.cpp create mode 100644 7#/12.cpp create mode 100644 7#/13.cpp create mode 100644 7#/14.cpp create mode 100644 7#/15.cpp create mode 100644 7#/16.cpp create mode 100644 7#/17.cpp create mode 100644 7#/18.cpp create mode 100644 7#/19.cpp create mode 100644 7#/1_1.cpp create mode 100644 7#/2.cpp create mode 100644 7#/3.cpp create mode 100644 7#/4.cpp create mode 100644 7#/5.cpp create mode 100644 7#/6.cpp create mode 100644 7#/7.cpp create mode 100644 7#/8.cpp create mode 100644 7#/8_1.cpp create mode 100644 7#/9.cpp create mode 100644 7#/const.cpp create mode 100644 7#/const_1.cpp create mode 100644 7#/p1.cpp create mode 100644 7#/p10.cpp create mode 100644 7#/p2.cpp create mode 100644 7#/p3.cpp create mode 100644 7#/p4.cpp create mode 100644 7#/p5.cpp create mode 100644 7#/p6.cpp create mode 100644 7#/p7.cpp create mode 100644 7#/p8-a.cpp create mode 100644 7#/p8-b.cpp create mode 100644 7#/p9.cpp create mode 100644 8#/1.cpp create mode 100644 8#/10.cpp create mode 100644 8#/11.cpp create mode 100644 8#/12.cpp create mode 100644 8#/13.cpp create mode 100644 8#/13_1.cpp create mode 100644 8#/14.cpp create mode 100644 8#/15.cpp create mode 100644 8#/2.cpp create mode 100644 8#/3.cpp create mode 100644 8#/4.cpp create mode 100644 8#/5.cpp create mode 100644 8#/5_1.cpp create mode 100644 8#/6.cpp create mode 100644 8#/7.cpp create mode 100644 8#/8.cpp create mode 100644 8#/9.cpp create mode 100644 8#/ep-data.txt create mode 100644 8#/p1.cpp create mode 100644 8#/p2.cpp create mode 100644 8#/p3.cpp create mode 100644 8#/p4.cpp create mode 100644 8#/p5.cpp create mode 100644 8#/p6.cpp create mode 100644 8#/p7.cpp create mode 100644 8#/r5.cpp create mode 100644 8#/review.txt create mode 100644 8#/test.cpp create mode 100644 9#/1.cpp create mode 100644 9#/10.cpp create mode 100644 9#/2.cpp create mode 100644 9#/4.cpp create mode 100644 9#/9.cpp create mode 100644 9#/coordin.h create mode 100644 9#/external.cpp create mode 100644 9#/file1.cpp create mode 100644 9#/file2.cpp create mode 100644 9#/namesp.cpp create mode 100644 9#/namesp.h create mode 100644 9#/namessp.cpp create mode 100644 9#/p1/golf.cpp create mode 100644 9#/p1/golf.h create mode 100644 9#/p1/main.cpp create mode 100644 9#/p2.cpp create mode 100644 9#/p3.cpp create mode 100644 9#/p4/main.cpp create mode 100644 9#/p4/p4.cpp create mode 100644 9#/p4/p4.h create mode 100644 9#/support.cpp create mode 100644 9#/support.h create mode 100644 9#/twofile1.cpp create mode 100644 9#/twofile2.cpp diff --git a/10#/10-9.cpp b/10#/10-9.cpp new file mode 100644 index 0000000..5bbffce --- /dev/null +++ b/10#/10-9.cpp @@ -0,0 +1,28 @@ +#include +#include "stock20.h" + +const int STKS = 4; + +int main() +{ + Stock stocks[STKS] = { + Stock("NanoSmart", 12, 20.0), + Stock("Boffo Objects", 200, 2.0), + Stock("Mono", 130, 3.25), + Stock("Fleep", 60, 6.5) + }; + + std::cout << "Stock holding:\n"; + int st; + for (st = 0; st < STKS; st++) + stocks[st].show(); + + const Stock *top = &stocks[0]; + for (st = 1; st < STKS; st++) + top = &top->topval(stocks[st]); + + std::cout << "\nMost valuable holding:\n"; + top->show(); + + return 0; +} \ No newline at end of file diff --git a/10#/p1/main.cpp b/10#/p1/main.cpp new file mode 100644 index 0000000..418e6c6 --- /dev/null +++ b/10#/p1/main.cpp @@ -0,0 +1,20 @@ +#include +#include "p1.h" + +int main() +{ + BankAccount n1("Rick", "0001", 1000); + n1.show(); + + cout << endl; + n1.deposit(500); + cout << "After deposit 500: " << endl; + n1.show(); + + cout << endl; + n1.withdraw(800); + cout << "After withdraw 800: " << endl; + n1.show(); + + return 0; +} \ No newline at end of file diff --git a/10#/p1/p1.cpp b/10#/p1/p1.cpp new file mode 100644 index 0000000..d13b52f --- /dev/null +++ b/10#/p1/p1.cpp @@ -0,0 +1,27 @@ +#include "p1.h" +#include + +BankAccount::BankAccount(string client, string num, double bal) +{ + name = client; + accountnum = num; + balance = bal; +} + +void BankAccount::show(void) const +{ + std::cout << "The Account information: " << std::endl; + std::cout << "Name: " << name << std::endl; + std::cout << "Account: " << accountnum << std::endl; + std::cout << "Balance: " << balance << std::endl; +} + +void BankAccount::deposit(double cash) +{ + balance += cash; +} + +void BankAccount::withdraw(double cash) +{ + balance -= cash; +} \ No newline at end of file diff --git a/10#/p1/p1.h b/10#/p1/p1.h new file mode 100644 index 0000000..5dea88f --- /dev/null +++ b/10#/p1/p1.h @@ -0,0 +1,20 @@ +#ifndef __P1_H__ +#define __P1_H__ + +#include +using namespace std; + +class BankAccount +{ +private: + string name; + string accountnum; + double balance; +public: + BankAccount(string client, string num, double bal = 0.0); + void show(void) const; + void deposit(double cash); + void withdraw(double cash); +}; + +#endif \ No newline at end of file diff --git a/10#/p2/main.cpp b/10#/p2/main.cpp new file mode 100644 index 0000000..8bc007a --- /dev/null +++ b/10#/p2/main.cpp @@ -0,0 +1,23 @@ +#include "p2.h" +#include + +int main() +{ + Person one; + Person two("Rick"); + Person three("Micheal", "Jascke"); + + cout << endl; + one.Show(); + one.FormalShow(); + + cout << endl; + two.Show(); + two.FormalShow(); + + cout << endl; + three.Show(); + three.FormalShow(); + + return 0; +} \ No newline at end of file diff --git a/10#/p2/p2.cpp b/10#/p2/p2.cpp new file mode 100644 index 0000000..1bfa02f --- /dev/null +++ b/10#/p2/p2.cpp @@ -0,0 +1,23 @@ +#include "p2.h" + +Person::Person(const string &ln, const char *fn) +{ + lname = ln; + strcpy(fname, fn); +} + +void Person::Show() const +{ + if (lname == "" && fname[0] == '\0') + cout << "No name: " << endl; + else + cout << "Person name: " << fname << " " << lname << endl; +} + +void Person::FormalShow() const +{ + if (lname == "" && fname[0] == '\0') + cout << "No name: " << endl; + else + cout << "Person name: " << lname << "," << fname << endl; +} \ No newline at end of file diff --git a/10#/p2/p2.h b/10#/p2/p2.h new file mode 100644 index 0000000..9685031 --- /dev/null +++ b/10#/p2/p2.h @@ -0,0 +1,22 @@ +#ifndef __P2_H__ +#define __P2_H__ + +#include +#include + +using namespace std; + +class Person +{ +private: + static const int LIMIT = 25; + string lname; + char fname[LIMIT]; +public: + Person() {lname = "", fname[0] = '\0';} + Person(const string &ln, const char *fn = "Heyyou"); + void Show() const; + void FormalShow() const; +}; + +#endif \ No newline at end of file diff --git a/10#/p3/golf.cpp b/10#/p3/golf.cpp new file mode 100644 index 0000000..cccd20d --- /dev/null +++ b/10#/p3/golf.cpp @@ -0,0 +1,28 @@ +#include "golf.h" + +golf::golf(const char *name, int hc) +{ + strcpy(fullname, name); + handicap = hc; +} + +golf::golf() +{ + cout << "The fullname is: "; + cin.getline(fullname, Len); + + cout << "The handicap is: "; + cin >> handicap; + cin.get(); +} + +void golf::sethandicap(int hc) +{ + handicap = hc; +} + +void golf::showgolf() const +{ + cout << "The fullname is: " << fullname << endl; + cout << "The handicap is: " << handicap << endl; +} \ No newline at end of file diff --git a/10#/p3/golf.h b/10#/p3/golf.h new file mode 100644 index 0000000..8fa6c68 --- /dev/null +++ b/10#/p3/golf.h @@ -0,0 +1,35 @@ +#ifndef __GOLF_H__ +#define __GOLF_H__ + +#include +#include + +using namespace std; + +const int Len = 40; + +/* +struct golf +{ + char fullname[Len]; + int handicap; +}; + +void setgolf(golf &g, const char *name, int hc); +int setgolf(golf &g); +void handicap(golf &g, int hc); +void showgolf(const golf &g); +*/ + +class golf +{ +private: + char fullname[Len]; + int handicap; +public: + golf(const char *name, int hc); + golf(); + void sethandicap(int hc); + void showgolf() const; +}; +#endif \ No newline at end of file diff --git a/10#/p3/main.cpp b/10#/p3/main.cpp new file mode 100644 index 0000000..3b6b54d --- /dev/null +++ b/10#/p3/main.cpp @@ -0,0 +1,15 @@ + +#include "golf.h" + +int main() +{ + golf p1("Rick", 100); + golf p2; + + cout << endl; + cout << "Now show two Person: " << endl; + p1.showgolf(); + p2.showgolf(); + + return 0; +} \ No newline at end of file diff --git a/10#/p4/main.cpp b/10#/p4/main.cpp new file mode 100644 index 0000000..560ed9b --- /dev/null +++ b/10#/p4/main.cpp @@ -0,0 +1,19 @@ +#include "p4.h" + +using namespace std; + +int main() +{ + using namespace SALES; + + double ar[4] = {11.1, 22.2, 33.3, 44.4}; + Sales s1(ar, QUARTERS); + Sales s2; + + cout << endl; + cout << "Now show two info: " << endl; + s1.showSales(); + s2.showSales(); + + return 0; +} \ No newline at end of file diff --git a/10#/p4/p4.cpp b/10#/p4/p4.cpp new file mode 100644 index 0000000..79c31f4 --- /dev/null +++ b/10#/p4/p4.cpp @@ -0,0 +1,74 @@ +#include "p4.h" + +using namespace std; + +namespace SALES +{ + Sales::Sales(const double ar[], int n) + { + double total; + int i; + + for (i = 0; i < n && i < 4; i++) + { + sales[i] = ar[i]; + total += sales[i]; + } + + average = total / i; + max = sales[0]; + min = sales[0]; + + for (size_t k = 1; k < i; k++) + { + max = (max > sales[k]) ? max : sales[k]; + min = (min < sales[k]) ? min : sales[k]; + } + + if (n < 4) + { + for (size_t k = n; k < 4; k++) + { + sales[k] = 0; + } + } + } + + Sales::Sales() + { + double total = 0.0; + + cout << "Enter 4 sales quarters: " << endl; + for (int i = 0; i < QUARTERS; i++) + { + cout << "#" << i + 1 << ": "; + cin >> sales[i]; + total += sales[i]; + + if (i == 0) + { + max = sales[i]; + min = sales[i]; + } + else + { + max = (max > sales[i]) ? max : sales[i]; + min = (min < sales[i]) ? min : sales[i]; + } + } + + average = total / QUARTERS; + } + + void Sales::showSales() const + { + cout << "Sales of 4 quarters: " << endl; + for (size_t i = 0; i < QUARTERS; i++) + cout << sales[i] << " "; + + cout << endl; + cout << "Average: " << average << endl;; + cout << "MAx = " << max << endl; + cout << "Min = " << min << endl; + } +} diff --git a/10#/p4/p4.h b/10#/p4/p4.h new file mode 100644 index 0000000..f50e300 --- /dev/null +++ b/10#/p4/p4.h @@ -0,0 +1,39 @@ +#ifndef __P4_H__ +#define __P4_H__ + +#include + +namespace SALES +{ +const int QUARTERS = 4; + +/* +struct Sales +{ + double sales[QUARTERS]; + double average; + double max; + double min; +}; + +void setSales(Sales &s, const double ar[], int n); +void setSales(Sales &s); +void showSales(const Sales &s); +*/ + +class Sales +{ +private: + double sales[QUARTERS]; + double average; + double max; + double min; +public: + Sales(const double ar[], int); + Sales(); + void showSales() const; +}; + +} + +#endif \ No newline at end of file diff --git a/10#/p5/stack.cpp b/10#/p5/stack.cpp new file mode 100644 index 0000000..1e03f99 --- /dev/null +++ b/10#/p5/stack.cpp @@ -0,0 +1,38 @@ +#include "stack.h" + +Stack::Stack() +{ + top = 0; +} + +bool Stack::isempty() const +{ + return top == 0; +} + +bool Stack::isfull() const +{ + return top == MAX; +} + +bool Stack::push(Item &item) +{ + if (top < MAX) + { + items[top++] = item; + return true; + } + else + return false; +} + +bool Stack::pop(Item &item) +{ + if (top > 0) + { + item = items[--top]; + return true; + } + else + return false; +} diff --git a/10#/p5/stack.h b/10#/p5/stack.h new file mode 100644 index 0000000..a0327c7 --- /dev/null +++ b/10#/p5/stack.h @@ -0,0 +1,26 @@ +#ifndef __STACK_H__ +#define __STACK_H__ + +struct customer +{ + char fullname[35]; + double payment; +}; + +typedef customer Item; + +class Stack +{ +private: + enum {MAX = 10}; + Item items[MAX]; + int top; +public: + Stack(); + bool isempty() const; + bool isfull() const; + bool push(Item &item); + bool pop(Item &item); +}; + +#endif \ No newline at end of file diff --git a/10#/p5/stacker.cpp b/10#/p5/stacker.cpp new file mode 100644 index 0000000..6adaac7 --- /dev/null +++ b/10#/p5/stacker.cpp @@ -0,0 +1,55 @@ +#include +#include "stack.h" +#include + +int main() +{ + char ch; + customer cust; + Stack st; + double sum = 0.0; + + std::cout << "Please enter A to push to stack, \n" + << "P to pop from stack, Q to quit. \n"; + + while (std::cin >> ch && toupper(ch) != 'Q') + { + while (std::cin.get() != '\n') + continue; + + switch (ch) + { + case 'A': + case 'a': + std::cout << "Enter a customer's fullname to push to stack.\n"; + std::cin >> cust.fullname; + std::cout << "Enter a customer payment: \n"; + std::cin >> cust.payment; + if (st.isfull()) + std::cout << "Stack already full" << std::endl; + else + st.push(cust); + break; + case 'P': + case 'p': + if (st.isempty()) + std::cout << "Stack is empty" << std::endl; + else + { + st.pop(cust); + sum += cust.payment; + std::cout << cust.fullname << " is poped" << std::endl; + std::cout << cust.payment << " is poped" << std::endl; + std::cout << "sum payment: " << sum << std::endl; + } + break; + default: + break; + } + + std::cout << "Please enter A to push to stack, \n" + << "P to pop from stack, Q to quit. \n"; + } + + return 0; +} \ No newline at end of file diff --git a/10#/p6/main.cpp b/10#/p6/main.cpp new file mode 100644 index 0000000..f08047c --- /dev/null +++ b/10#/p6/main.cpp @@ -0,0 +1,24 @@ +#include +#include "p6.h" + +int main() +{ + double x, y; + Move m1(11.1, 22.2); + Move m2(10.0, 20.0); + Move m3; + m1.showmove(); + m2.showmove(); + + cout << "Enter new x and y: "; + cin >> x >> y; + cout << "Reset m1: " << endl; + m1.reset(x, y); + m1.showmove(); + + m3 = m1.add(m2); + cout << "Show m3: " << endl; + m3.showmove(); + + return 0; +} diff --git a/10#/p6/p6.cpp b/10#/p6/p6.cpp new file mode 100644 index 0000000..424263e --- /dev/null +++ b/10#/p6/p6.cpp @@ -0,0 +1,28 @@ +#include "p6.h" + +Move::Move(double a, double b) +{ + x = a; + y = b; +} + +void Move::showmove() const +{ + cout << "x = " << x << ", y = " << y << endl; +} + +Move Move::add(const Move &m) +{ + Move temp; + + temp.x = x + m.x; + temp.y = y + m.y; + + return temp; +} + +void Move::reset(double a, double b) +{ + x = a; + y = b; +} \ No newline at end of file diff --git a/10#/p6/p6.h b/10#/p6/p6.h new file mode 100644 index 0000000..17ca87a --- /dev/null +++ b/10#/p6/p6.h @@ -0,0 +1,20 @@ +#ifndef __P6_H__ +#define __P6_H__ + +#include + +using namespace std; + +class Move +{ +private: + double x; + double y; +public: + Move(double a = 0, double b = 0); + void showmove() const; + Move add(const Move &m); + void reset(double a = 0, double b = 0); +}; + +#endif \ No newline at end of file diff --git a/10#/p7/main.cpp b/10#/p7/main.cpp new file mode 100644 index 0000000..6c61550 --- /dev/null +++ b/10#/p7/main.cpp @@ -0,0 +1,13 @@ +#include +#include "p7.h" + +int main() +{ + plorg p1; + p1.show(); + + p1.reset(100); + p1.show(); + + return 0; +} \ No newline at end of file diff --git a/10#/p7/p7.cpp b/10#/p7/p7.cpp new file mode 100644 index 0000000..d8bdbea --- /dev/null +++ b/10#/p7/p7.cpp @@ -0,0 +1,17 @@ +#include "p7.h" + +plorg::plorg(const char str[], int ci) +{ + strcpy(name, str); + CI = ci; +} + +void plorg::reset(int ci) +{ + CI = ci; +} + +void plorg::show() const +{ + cout << "Name = " << name << ", CI = " << CI << endl; +} diff --git a/10#/p7/p7.h b/10#/p7/p7.h new file mode 100644 index 0000000..bf7e4e3 --- /dev/null +++ b/10#/p7/p7.h @@ -0,0 +1,22 @@ +#ifndef __P7_H__ +#define __P7_H__ + +#include +#include + +using namespace std; + +const int SIZE = 19; + +class plorg +{ +private: + char name[SIZE]; + int CI; +public: + plorg(const char str[] = "Plorg", int ci = 50); + void reset(int ci); + void show() const; +}; + +#endif \ No newline at end of file diff --git a/10#/p8/list.cpp b/10#/p8/list.cpp new file mode 100644 index 0000000..74ff6af --- /dev/null +++ b/10#/p8/list.cpp @@ -0,0 +1,38 @@ +#include "list.h" + +List::List() +{ + top = 0; +} + +bool List::isempty() const +{ + return top == 0; +} + +bool List::isfull() const +{ + return top == MAX; +} + +bool List::add(const Item &item) +{ + if (top < MAX) + { + items[top++] = item; + return true; + } + else + return false; +} + +void List::visit(void (*pf)(Item &)) +{ + for (int i = 0; i < top; i++) + pf(items[i]); +} + +void visit_item(Item &item) +{ + std::cout << "Item = " << item << std::endl; +} \ No newline at end of file diff --git a/10#/p8/list.h b/10#/p8/list.h new file mode 100644 index 0000000..dfdde44 --- /dev/null +++ b/10#/p8/list.h @@ -0,0 +1,25 @@ +#ifndef __LIST_H__ +#define __LIST_H__ + +#include + +typedef unsigned long Item; + +void visit_item(Item &item); + +class List +{ +private: + enum {MAX = 10}; + Item items[MAX]; + int top; +public: + List(); + bool isempty() const; + bool isfull() const; + bool add(const Item &item); + void visit(void (*pf)(Item &)); +}; + + +#endif \ No newline at end of file diff --git a/10#/p8/main.cpp b/10#/p8/main.cpp new file mode 100644 index 0000000..4d740d5 --- /dev/null +++ b/10#/p8/main.cpp @@ -0,0 +1,22 @@ +#include +#include "list.h" + +using namespace std; + +int main() +{ + List list; + Item num; + + for (size_t i = 0; i < 5; i++) + { + cout << "Please enter a number: "<< endl; + cin >> num; + list.add(num); + // list.visit(visit_item); + } + + list.visit(visit_item); + + return 0; +} \ No newline at end of file diff --git a/10#/r5.h b/10#/r5.h new file mode 100644 index 0000000..7105489 --- /dev/null +++ b/10#/r5.h @@ -0,0 +1,12 @@ +class BankAccount +{ +private: + char name[50]; + char accountnum[50]; + double balabnce; +public: + BankAccount(const char *client, const char *num, double bal = 0.0); + void show(void) const; + void deposit(double cash); + void withdraw(double cash); +}; \ No newline at end of file diff --git a/10#/r7.cpp b/10#/r7.cpp new file mode 100644 index 0000000..eacb845 --- /dev/null +++ b/10#/r7.cpp @@ -0,0 +1,19 @@ +class BankAccount +{ +private: + char name[50]; + char accountnum[50]; + double balabnce; +public: + BankAccount(const char *client, const char *num, double bal = 0.0); + void show(void) const; + void deposit(double cash); + void withdraw(double cash); +}; + +BankAccount::BankAccount(const char *client, const char *num, double bal) +{ + name = client; + accountnum = num; + balabnce = bal; +} \ No newline at end of file diff --git a/10#/r9.h b/10#/r9.h new file mode 100644 index 0000000..ae6c043 --- /dev/null +++ b/10#/r9.h @@ -0,0 +1,30 @@ +#ifndef __STOCK20_H__ +#define __STOCK20_H__ + +#include + +class Stock +{ +private: + std::string company; + long shares; + double share_val; + double total_val; + void set_total() {total_val = shares * share_val;} +public: + Stock(); + Stock(const std::string &co, long n, double pr); + ~Stock(); + void buy(long num, double price); + void sell(long num, double price); + void update(double price); + void show() const; + const Stock &topval(const Stock &s) const; + + const std::string &company_name() const {return company;} + long num_shares() const {return shares;} + double shareval() const {return share_val;} + double totalval() const {return total_val;} +}; + +#endif \ No newline at end of file diff --git a/10#/stack.cpp b/10#/stack.cpp new file mode 100644 index 0000000..1e03f99 --- /dev/null +++ b/10#/stack.cpp @@ -0,0 +1,38 @@ +#include "stack.h" + +Stack::Stack() +{ + top = 0; +} + +bool Stack::isempty() const +{ + return top == 0; +} + +bool Stack::isfull() const +{ + return top == MAX; +} + +bool Stack::push(Item &item) +{ + if (top < MAX) + { + items[top++] = item; + return true; + } + else + return false; +} + +bool Stack::pop(Item &item) +{ + if (top > 0) + { + item = items[--top]; + return true; + } + else + return false; +} diff --git a/10#/stack.h b/10#/stack.h new file mode 100644 index 0000000..4b902e9 --- /dev/null +++ b/10#/stack.h @@ -0,0 +1,20 @@ +#ifndef __STACK_H__ +#define __STACK_H__ + +typedef unsigned long Item; + +class Stack +{ +private: + enum {MAX = 10}; + Item items[MAX]; + int top; +public: + Stack(); + bool isempty() const; + bool isfull() const; + bool push(Item &item); + bool pop(Item &item); +}; + +#endif \ No newline at end of file diff --git a/10#/stacker.cpp b/10#/stacker.cpp new file mode 100644 index 0000000..14f5c8a --- /dev/null +++ b/10#/stacker.cpp @@ -0,0 +1,49 @@ +#include +#include "stack.h" +#include + +int main() +{ + char ch; + unsigned long po; + Stack st; + + std::cout << "Please enter A to push to stack, \n" + << "P to pop from stack, Q to quit. \n"; + + while (std::cin >> ch && toupper(ch) != 'Q') + { + while (std::cin.get() != '\n') + continue; + + switch (ch) + { + case 'A': + case 'a': + std::cout << "Enter a number you want to push to stack.\n"; + std::cin >> po; + if (st.isfull()) + std::cout << "Stack already full" << std::endl; + else + st.push(po); + break; + case 'P': + case 'p': + if (st.isempty()) + std::cout << "Stack is empty" << std::endl; + else + { + st.pop(po); + std::cout << po << " is poped" << std::endl; + } + break; + default: + break; + } + + std::cout << "Please enter A to push to stack, \n" + << "P to pop from stack, Q to quit. \n"; + } + + return 0; +} \ No newline at end of file diff --git a/10#/stock00.cpp b/10#/stock00.cpp new file mode 100644 index 0000000..8ab0616 --- /dev/null +++ b/10#/stock00.cpp @@ -0,0 +1,64 @@ +#include +#include "stock00.h" + +void Stock::acquire(const std::string &co, long n, double pr) +{ + company = co; + if (n < 0) + { + std::cout << "Number of shares can't be negative; " << company << " shares set to be 0." << std::endl; + shares = 0; + } + else + shares = n; + + share_val = pr; + set_total(); +} + +void Stock::buy(long num, double price) +{ + if (num < 0) + { + std::cout << "Number of shares can't be negative; Transaction is aborted" << std::endl; + } + else + { + shares += num; + share_val = price; + set_total(); + } +} + +void Stock::sell(long num, double price) +{ + if (num < 0) + { + std::cout << "Number of shares can't be negative; Transaction is aborted" << std::endl; + } + else if (num > shares) + { + std::cout << "You can't sell more than you have! Transcation is aborted" << std::endl; + } + else + { + shares -= num; + share_val = price; + set_total(); + } + +} + +void Stock::update(double price) +{ + share_val = price; + set_total(); +} + +void Stock::show() +{ + std::cout << "Company: " << company << std::endl; + std::cout << "Shares: " << shares << std::endl; + std::cout << "Share price: " << share_val << std::endl; + std::cout << "Total worth: " << total_val << std::endl; +} \ No newline at end of file diff --git a/10#/stock00.h b/10#/stock00.h new file mode 100644 index 0000000..5b89a19 --- /dev/null +++ b/10#/stock00.h @@ -0,0 +1,22 @@ +#ifndef __STOCK00_H__ +#define __STOCK00_H__ + +#include + +class Stock +{ +private: + std::string company; + long shares; + double share_val; + double total_val; + void set_total() {total_val = shares * share_val;} +public: + void acquire(const std::string &co, long n, double pr); + void buy(long num, double price); + void sell(long num, double price); + void update(double price); + void show(); +}; + +#endif \ No newline at end of file diff --git a/10#/stock10.cpp b/10#/stock10.cpp new file mode 100644 index 0000000..9db46a1 --- /dev/null +++ b/10#/stock10.cpp @@ -0,0 +1,77 @@ +#include +#include "stock10.h" + +Stock::Stock() +{ + company = "no name"; + shares = 0; + share_val = 0.0; + total_val = 0.0; +} + +Stock::~Stock() +{ + std::cout << "Bye " << company << std::endl; +} + +Stock::Stock(const std::string &co, long n, double pr) +{ + company = co; + if (n < 0) + { + std::cout << "Number of shares can't be negative; " << company << " shares set to be 0." << std::endl; + shares = 0; + } + else + shares = n; + + share_val = pr; + set_total(); +} + +void Stock::buy(long num, double price) +{ + if (num < 0) + { + std::cout << "Number of shares can't be negative; Transaction is aborted" << std::endl; + } + else + { + shares += num; + share_val = price; + set_total(); + } +} + +void Stock::sell(long num, double price) +{ + if (num < 0) + { + std::cout << "Number of shares can't be negative; Transaction is aborted" << std::endl; + } + else if (num > shares) + { + std::cout << "You can't sell more than you have! Transcation is aborted" << std::endl; + } + else + { + shares -= num; + share_val = price; + set_total(); + } + +} + +void Stock::update(double price) +{ + share_val = price; + set_total(); +} + +void Stock::show() const +{ + std::cout << "Company: " << company << std::endl; + std::cout << "Shares: " << shares << std::endl; + std::cout << "Share price: " << share_val << std::endl; + std::cout << "Total worth: " << total_val << std::endl; +} \ No newline at end of file diff --git a/10#/stock10.h b/10#/stock10.h new file mode 100644 index 0000000..009d68e --- /dev/null +++ b/10#/stock10.h @@ -0,0 +1,24 @@ +#ifndef __STOCK10_H__ +#define __STOCK10_H__ + +#include + +class Stock +{ +private: + std::string company; + long shares; + double share_val; + double total_val; + void set_total() {total_val = shares * share_val;} +public: + Stock(); + Stock(const std::string &co, long n, double pr); + ~Stock(); + void buy(long num, double price); + void sell(long num, double price); + void update(double price); + void show() const; +}; + +#endif \ No newline at end of file diff --git a/10#/stock20.cpp b/10#/stock20.cpp new file mode 100644 index 0000000..1f8e0e9 --- /dev/null +++ b/10#/stock20.cpp @@ -0,0 +1,85 @@ +#include +#include "stock20.h" + +Stock::Stock() +{ + company = "no name"; + shares = 0; + share_val = 0.0; + total_val = 0.0; +} + +Stock::~Stock() +{ + // std::cout << "Bye " << company << std::endl; +} + +Stock::Stock(const std::string &co, long n, double pr) +{ + company = co; + if (n < 0) + { + std::cout << "Number of shares can't be negative; " << company << " shares set to be 0." << std::endl; + shares = 0; + } + else + shares = n; + + share_val = pr; + set_total(); +} + +void Stock::buy(long num, double price) +{ + if (num < 0) + { + std::cout << "Number of shares can't be negative; Transaction is aborted" << std::endl; + } + else + { + shares += num; + share_val = price; + set_total(); + } +} + +void Stock::sell(long num, double price) +{ + if (num < 0) + { + std::cout << "Number of shares can't be negative; Transaction is aborted" << std::endl; + } + else if (num > shares) + { + std::cout << "You can't sell more than you have! Transcation is aborted" << std::endl; + } + else + { + shares -= num; + share_val = price; + set_total(); + } + +} + +void Stock::update(double price) +{ + share_val = price; + set_total(); +} + +void Stock::show() const +{ + std::cout << "Company: " << company << std::endl; + std::cout << "Shares: " << shares << std::endl; + std::cout << "Share price: " << share_val << std::endl; + std::cout << "Total worth: " << total_val << std::endl; +} + +const Stock &Stock::topval(const Stock &s) const +{ + if (s.total_val > total_val) // total_val == this->total_val; + return s; + else + return *this; +} \ No newline at end of file diff --git a/10#/stock20.h b/10#/stock20.h new file mode 100644 index 0000000..ee00ca6 --- /dev/null +++ b/10#/stock20.h @@ -0,0 +1,25 @@ +#ifndef __STOCK20_H__ +#define __STOCK20_H__ + +#include + +class Stock +{ +private: + std::string company; + long shares; + double share_val; + double total_val; + void set_total() {total_val = shares * share_val;} +public: + Stock(); + Stock(const std::string &co, long n, double pr); + ~Stock(); + void buy(long num, double price); + void sell(long num, double price); + void update(double price); + void show() const; + const Stock &topval(const Stock &s) const; +}; + +#endif \ No newline at end of file diff --git a/10#/usestock1.cpp b/10#/usestock1.cpp new file mode 100644 index 0000000..9b1f58a --- /dev/null +++ b/10#/usestock1.cpp @@ -0,0 +1,24 @@ +#include +#include "stock00.h" + +int main() +{ + Stock fluffy_the_cat; + fluffy_the_cat.acquire("NanoSmart", 20, 12.50); + fluffy_the_cat.show(); + + fluffy_the_cat.buy(15, 18.125); + fluffy_the_cat.show(); + + fluffy_the_cat.sell(400, 20); + fluffy_the_cat.show(); + + fluffy_the_cat.buy(300000, 40.125); + fluffy_the_cat.update(15.50); + fluffy_the_cat.show(); + + fluffy_the_cat.sell(300000, 0.125); + fluffy_the_cat.show(); + + return 0; +} \ No newline at end of file diff --git a/10#/usestock2.cpp b/10#/usestock2.cpp new file mode 100644 index 0000000..ac51243 --- /dev/null +++ b/10#/usestock2.cpp @@ -0,0 +1,30 @@ +#include +#include "stock10.h" + +using namespace std; + +int main() +{ + Stock stock1("Nanosmart", 12, 20.0); + stock1.show(); + + Stock stock2 = Stock("Boffo", 2, 2); + stock2.show(); + + cout << "Assigning stock1 to stock2: \n"; + stock2 = stock1; + cout << "Listing stock1 and stock2: \n"; + stock1.show(); + stock2.show(); + + cout << "Using a constructor to reset an object: \n"; + stock1 = Stock("Nifty Foods", 10, 50.0); + cout << "Revised stock1: \n"; + stock1.show(); + stock2.show(); + + const Stock land = Stock("Klu proper", 3, 3.0); + land.show(); + + return 0; +} \ No newline at end of file diff --git a/10#/usestock20.cpp b/10#/usestock20.cpp new file mode 100644 index 0000000..a75bed8 --- /dev/null +++ b/10#/usestock20.cpp @@ -0,0 +1,19 @@ +#include +#include "stock20.h" + +using namespace std; + +int main() +{ + Stock stock1("Nanosmart", 12, 20.0); + stock1.show(); + + Stock stock2 = Stock("Boffo", 2, 2); + stock2.show(); + + Stock top = stock1.topval(stock2); + cout << "\nNow show the top val: \n"; + top.show(); + + return 0; +} \ No newline at end of file diff --git a/11#/demo0.cpp b/11#/demo0.cpp new file mode 100644 index 0000000..88f2d48 --- /dev/null +++ b/11#/demo0.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +class Num +{ +public: + int n; +public: + Num(); + Num(int m); + void Show() const; +}; + +Num::Num() +{ + n = 0; +} + +Num::Num(int m) +{ + n = m; +} + +void Num::Show() const +{ + cout << "n = " << n << endl; +} + +Num operator+(const Num &m, const Num &l) +{ + Num temp; + temp.n = m.n + l.n; + return temp; +} + +int main() +{ + Num a(10); + a.Show(); + + Num b(20); + b.Show(); + + Num c; + c = a + b; + c.Show(); + + return 0; +} \ No newline at end of file diff --git a/11#/demo1.cpp b/11#/demo1.cpp new file mode 100644 index 0000000..788bd5c --- /dev/null +++ b/11#/demo1.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +class Num +{ +private: + int n; +public: + Num(); + Num(int m); + Num operator+ (const Num &t) const; + void Show() const; +}; + +Num::Num() +{ + n = 0; +} + +Num::Num(int m) +{ + n = m; +} + +Num Num::operator+(const Num &t) const +{ + Num temp; + + temp.n = n + t.n; + + return temp; +} + +void Num::Show() const +{ + cout << "n = " << n << endl; +} + +int main() +{ + Num a(10); + a.Show(); + + Num b(20); + b.Show(); + + Num c; + c = a + b; + c.Show(); + + return 0; +} \ No newline at end of file diff --git a/11#/demo2.cpp b/11#/demo2.cpp new file mode 100644 index 0000000..77e7569 --- /dev/null +++ b/11#/demo2.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +class Num +{ +private: + int n; +public: + Num(); + Num(int m); + void operator=(int l); +}; + +Num::Num() +{ + n = 0; + cout << "Hello world" << endl; +} + +Num::Num(int m) +{ + n = m; + cout << "n = " << n << endl; + cout << "Good moring" << endl; +} + +void Num::operator=(int l) +{ + n = l; + cout << "n = " << n << endl; + cout << "I love you Rick!" << endl; +} + +int main() +{ + Num a; + a = 10; // a(10); + + return 0; +} \ No newline at end of file diff --git a/11#/mytime0.cpp b/11#/mytime0.cpp new file mode 100644 index 0000000..41ec3aa --- /dev/null +++ b/11#/mytime0.cpp @@ -0,0 +1,45 @@ +#include "mytime0.h" + +Time::Time() +{ + hours = minutes = 0; +} + +Time::Time(int h, int m) +{ + hours = h; + minutes = m; +} + +void Time::AddMin(int m) +{ + minutes += m; + hours += minutes / 60; + minutes %= 60; +} + +void Time::AddHr(int h) +{ + hours += h; +} + +void Time::Reset(int h, int m) +{ + hours = h; + minutes = m; +} + +Time Time::Sum(const Time &t) const +{ + Time sum; + sum.minutes = minutes + t.minutes; + sum.hours = hours + t.hours + sum.minutes / 60; + sum.minutes %= 60; + + return sum; +} + +void Time::Show() const +{ + cout << hours << " hours, " << minutes << " minutes." << endl; +} \ No newline at end of file diff --git a/11#/mytime0.h b/11#/mytime0.h new file mode 100644 index 0000000..f0cc0dc --- /dev/null +++ b/11#/mytime0.h @@ -0,0 +1,23 @@ +#ifndef __MYTIME0_H__ +#define __MYTIME0_H__ + +#include + +using namespace std; + +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); + Time Sum(const Time &t) const; + void Show() const; +}; + +#endif \ No newline at end of file diff --git a/11#/mytime1.cpp b/11#/mytime1.cpp new file mode 100644 index 0000000..bf1eb27 --- /dev/null +++ b/11#/mytime1.cpp @@ -0,0 +1,45 @@ +#include "mytime1.h" + +Time::Time() +{ + hours = minutes = 0; +} + +Time::Time(int h, int m) +{ + hours = h; + minutes = m; +} + +void Time::AddMin(int m) +{ + minutes += m; + hours += minutes / 60; + minutes %= 60; +} + +void Time::AddHr(int h) +{ + hours += h; +} + +void Time::Reset(int h, int m) +{ + hours = h; + minutes = m; +} + +Time Time::operator+(const Time &t) const +{ + Time sum; + sum.minutes = minutes + t.minutes; + sum.hours = hours + t.hours + sum.minutes / 60; + sum.minutes %= 60; + + return sum; +} + +void Time::Show() const +{ + cout << hours << " hours, " << minutes << " minutes." << endl; +} \ No newline at end of file diff --git a/11#/mytime1.h b/11#/mytime1.h new file mode 100644 index 0000000..b74cb19 --- /dev/null +++ b/11#/mytime1.h @@ -0,0 +1,23 @@ +#ifndef __MYTIME1_H__ +#define __MYTIME1_H__ + +#include + +using namespace std; + +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); + Time operator +(const Time &t) const; + void Show() const; +}; + +#endif \ No newline at end of file diff --git a/11#/mytime2.cpp b/11#/mytime2.cpp new file mode 100644 index 0000000..1970c1a --- /dev/null +++ b/11#/mytime2.cpp @@ -0,0 +1,69 @@ +#include "mytime2.h" + +Time::Time() +{ + hours = minutes = 0; +} + +Time::Time(int h, int m) +{ + hours = h; + minutes = m; +} + +void Time::AddMin(int m) +{ + minutes += m; + hours += minutes / 60; + minutes %= 60; +} + +void Time::AddHr(int h) +{ + hours += h; +} + +void Time::Reset(int h, int m) +{ + hours = h; + minutes = m; +} + +Time Time::operator+(const Time &t) const +{ + Time sum; + sum.minutes = minutes + t.minutes; + sum.hours = hours + t.hours + sum.minutes / 60; + sum.minutes %= 60; + + return sum; +} + +Time Time::operator-(const Time &t) const +{ + Time diff; + + int total1, total2; + total1 = hours * 60 + minutes; + total2 = t.hours * 60 + t.minutes; + diff.hours = (total1 - total2) / 60; + diff.minutes = (total1 - total2) % 60; + + return diff; +} + +Time Time::operator*(double mul) const +{ + Time result; + + long totalminutes = hours * mul * 60 + minutes * mul; + result.hours = totalminutes / 60; + result.minutes = totalminutes % 60; + + return result; +} + +void Time::Show() const +{ + cout << hours << " hours, " << minutes << " minutes." << endl; +} \ No newline at end of file diff --git a/11#/mytime2.h b/11#/mytime2.h new file mode 100644 index 0000000..00bc56a --- /dev/null +++ b/11#/mytime2.h @@ -0,0 +1,25 @@ +#ifndef __MYTIME1_H__ +#define __MYTIME1_H__ + +#include + +using namespace std; + +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); + Time operator +(const Time &t) const; + Time operator -(const Time &t) const; + Time operator *(double mult) const; + void Show() const; +}; + +#endif \ No newline at end of file diff --git a/11#/mytime3.cpp b/11#/mytime3.cpp new file mode 100644 index 0000000..18b955f --- /dev/null +++ b/11#/mytime3.cpp @@ -0,0 +1,85 @@ +#include "mytime3.h" + +Time::Time() +{ + hours = minutes = 0; +} + +Time::Time(int h, int m) +{ + hours = h; + minutes = m; +} + +void Time::AddMin(int m) +{ + minutes += m; + hours += minutes / 60; + minutes %= 60; +} + +void Time::AddHr(int h) +{ + hours += h; +} + +void Time::Reset(int h, int m) +{ + hours = h; + minutes = m; +} + +Time Time::operator+(const Time &t) const +{ + Time sum; + sum.minutes = minutes + t.minutes; + sum.hours = hours + t.hours + sum.minutes / 60; + sum.minutes %= 60; + + return sum; +} + +Time Time::operator-(const Time &t) const +{ + Time diff; + + int total1, total2; + total1 = hours * 60 + minutes; + total2 = t.hours * 60 + t.minutes; + diff.hours = (total1 - total2) / 60; + diff.minutes = (total1 - total2) % 60; + + return diff; +} + +Time Time::operator*(double mul) const +{ + Time result; + + long totalminutes = hours * mul * 60 + minutes * mul; + result.hours = totalminutes / 60; + result.minutes = totalminutes % 60; + + return result; +} + +Time operator*(double m, const Time &t) +{ + Time result; + + long totalminutes = t.hours * m * 60 + t.minutes * m; + result.hours = totalminutes / 60; + result.minutes = totalminutes % 60; + + return result; +} + +// Time operator*(double m, const Time &t) +// { +// return t * m; +// } + +void Time::Show() const +{ + cout << hours << " hours, " << minutes << " minutes." << endl; +} \ No newline at end of file diff --git a/11#/mytime3.h b/11#/mytime3.h new file mode 100644 index 0000000..ba70b64 --- /dev/null +++ b/11#/mytime3.h @@ -0,0 +1,26 @@ +#ifndef __MYTIME1_H__ +#define __MYTIME1_H__ + +#include + +using namespace std; + +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); + Time operator +(const Time &t) const; + Time operator -(const Time &t) const; + Time operator *(double mult) const; + friend Time operator*(double m, const Time &t); + void Show() const; +}; + +#endif \ No newline at end of file diff --git a/11#/mytime4.cpp b/11#/mytime4.cpp new file mode 100644 index 0000000..717859f --- /dev/null +++ b/11#/mytime4.cpp @@ -0,0 +1,92 @@ +#include "mytime4.h" + +Time::Time() +{ + hours = minutes = 0; +} + +Time::Time(int h, int m) +{ + hours = h; + minutes = m; +} + +void Time::AddMin(int m) +{ + minutes += m; + hours += minutes / 60; + minutes %= 60; +} + +void Time::AddHr(int h) +{ + hours += h; +} + +void Time::Reset(int h, int m) +{ + hours = h; + minutes = m; +} + +Time Time::operator+(const Time &t) const +{ + Time sum; + sum.minutes = minutes + t.minutes; + sum.hours = hours + t.hours + sum.minutes / 60; + sum.minutes %= 60; + + return sum; +} + +Time Time::operator-(const Time &t) const +{ + Time diff; + + int total1, total2; + total1 = hours * 60 + minutes; + total2 = t.hours * 60 + t.minutes; + diff.hours = (total1 - total2) / 60; + diff.minutes = (total1 - total2) % 60; + + return diff; +} + +Time Time::operator*(double mul) const +{ + Time result; + + long totalminutes = hours * mul * 60 + minutes * mul; + result.hours = totalminutes / 60; + result.minutes = totalminutes % 60; + + return result; +} + +Time operator*(double m, const Time &t) +{ + Time result; + + long totalminutes = t.hours * m * 60 + t.minutes * m; + result.hours = totalminutes / 60; + result.minutes = totalminutes % 60; + + return result; +} + +ostream &operator<<(ostream &os, const Time &t) +{ + os << t.hours << " hours, " << t.minutes << " minutes." << endl; + + return os; +} + +// Time operator*(double m, const Time &t) +// { +// return t * m; +// } + +// void Time::Show() const +// { +// cout << hours << " hours, " << minutes << " minutes." << endl; +// } \ No newline at end of file diff --git a/11#/mytime4.h b/11#/mytime4.h new file mode 100644 index 0000000..ffd618b --- /dev/null +++ b/11#/mytime4.h @@ -0,0 +1,27 @@ +#ifndef __MYTIME1_H__ +#define __MYTIME1_H__ + +#include + +using namespace std; + +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); + Time operator +(const Time &t) const; + Time operator -(const Time &t) const; + Time operator *(double mult) const; + friend Time operator*(double m, const Time &t); + friend ostream &operator<<(ostream &os, const Time &t); + // void Show() const; +}; + +#endif \ No newline at end of file diff --git a/11#/p1/main.cpp b/11#/p1/main.cpp new file mode 100644 index 0000000..feb04ea --- /dev/null +++ b/11#/p1/main.cpp @@ -0,0 +1,66 @@ +#include +#include "vector.h" +#include +#include +#include + +using namespace std; +using namespace VECTOR; + +int main() +{ + double target; + double dstep; + Vector result(0.0, 0.0); + Vector step; + double direction; + unsigned long steps = 0; + + ofstream fout; + fout.open("randwalk.txt"); + + srand(time(NULL)); + + cout << "Enter target distance(q to quit): " << endl; + + while(cin >> target) + { + cout << "Enter the step length: "; + + if(!(cin >> dstep)) + break; + + fout << "Target distance: " << target << ", step size: " << dstep << endl; + + while(result.magval() < target) + { + fout << steps << ": (x, y) = " << result << endl; + direction = rand() % 360; + step.reset(dstep, direction, Vector::POL); + result = result + step; + steps++; + } + + cout << "After " << steps << " steps, the subject has the following location." << endl; + cout << result; + + fout << "After " << steps << " steps, the subject has the following location:\n"; + fout << result; + + result.polar_mode(); + cout << result; + fout << result; + + fout << "Average outward distance per step = " << result.magval() / steps << endl; + + cout << endl; + + steps = 0; + result.reset(0.0, 0.0); + + cout << "Enter target distance(q to quit): " << endl; + } + cout << "Bye." << endl; + + return 0; +} \ No newline at end of file diff --git a/11#/p1/randwalk.txt b/11#/p1/randwalk.txt new file mode 100644 index 0000000..1b09465 --- /dev/null +++ b/11#/p1/randwalk.txt @@ -0,0 +1,1643 @@ +Target distance: 50, step size: 2 +0: (x, y) = x, y = 0, 0 + +1: (x, y) = x, y = -1.90211, 0.618034 + +2: (x, y) = x, y = -1.48629, 2.57433 + +3: (x, y) = x, y = -0.254967, 0.998308 + +4: (x, y) = x, y = -2.08206, 0.184834 + +5: (x, y) = x, y = -2.25637, 2.17722 + +6: (x, y) = x, y = -1.50716, 0.322856 + +7: (x, y) = x, y = -3.1845, 1.41213 + +8: (x, y) = x, y = -4.24434, 3.10823 + +9: (x, y) = x, y = -5.94043, 2.04839 + +10: (x, y) = x, y = -7.00027, 3.74449 + +11: (x, y) = x, y = -7.78173, 1.90348 + +12: (x, y) = x, y = -7.43444, 3.87309 + +13: (x, y) = x, y = -5.94815, 5.21136 + +14: (x, y) = x, y = -4.80099, 6.84966 + +15: (x, y) = x, y = -5.45213, 8.7407 + +16: (x, y) = x, y = -6.79039, 7.25441 + +17: (x, y) = x, y = -8.27668, 8.59267 + +18: (x, y) = x, y = -10.131, 7.84345 + +19: (x, y) = x, y = -12.0221, 7.19232 + +20: (x, y) = x, y = -10.2401, 6.28434 + +21: (x, y) = x, y = -12.2154, 5.97147 + +22: (x, y) = x, y = -13.9813, 6.91041 + +23: (x, y) = x, y = -14.9813, 8.64246 + +24: (x, y) = x, y = -13.0008, 8.92081 + +25: (x, y) = x, y = -11.7152, 10.4529 + +26: (x, y) = x, y = -10.5116, 12.0502 + +27: (x, y) = x, y = -8.5116, 12.0502 + +28: (x, y) = x, y = -8.16431, 14.0198 + +29: (x, y) = x, y = -9.19438, 15.7341 + +30: (x, y) = x, y = -7.85612, 14.2478 + +31: (x, y) = x, y = -5.88651, 13.9005 + +32: (x, y) = x, y = -5.4366, 11.9518 + +33: (x, y) = x, y = -3.70455, 12.9518 + +34: (x, y) = x, y = -2.76561, 11.1859 + +35: (x, y) = x, y = -3.96924, 12.7832 + +36: (x, y) = x, y = -5.11639, 14.4215 + +37: (x, y) = x, y = -5.53222, 16.3778 + +38: (x, y) = x, y = -7.19029, 17.4962 + +39: (x, y) = x, y = -8.19029, 19.2282 + +40: (x, y) = x, y = -6.77608, 17.814 + +41: (x, y) = x, y = -6.81098, 15.8143 + +42: (x, y) = x, y = -7.84106, 14.1 + +43: (x, y) = x, y = -6.20275, 15.2471 + +44: (x, y) = x, y = -8.20001, 15.1424 + +45: (x, y) = x, y = -7.38654, 13.3154 + +46: (x, y) = x, y = -5.47393, 13.9001 + +47: (x, y) = x, y = -4.21529, 15.4544 + +48: (x, y) = x, y = -3.06814, 13.8161 + +49: (x, y) = x, y = -4.62243, 12.5574 + +50: (x, y) = x, y = -5.68227, 14.2535 + +51: (x, y) = x, y = -5.40392, 12.273 + +52: (x, y) = x, y = -4.06566, 13.7593 + +53: (x, y) = x, y = -3.89135, 11.7669 + +54: (x, y) = x, y = -3.85644, 9.76721 + +55: (x, y) = x, y = -3.20531, 7.87617 + +56: (x, y) = x, y = -1.567, 6.72902 + +57: (x, y) = x, y = -3.48953, 6.17774 + +58: (x, y) = x, y = -5.38056, 6.82888 + +59: (x, y) = x, y = -5.10222, 4.84834 + +60: (x, y) = x, y = -3.61593, 3.51008 + +61: (x, y) = x, y = -4.4294, 5.33717 + +62: (x, y) = x, y = -4.4992, 3.33839 + +63: (x, y) = x, y = -5.46882, 1.58915 + +64: (x, y) = x, y = -7.08685, 2.76472 + +65: (x, y) = x, y = -8.88444, 1.88798 + +66: (x, y) = x, y = -9.85406, 0.138739 + +67: (x, y) = x, y = -11.4301, 1.37006 + +68: (x, y) = x, y = -10.1988, -0.20596 + +69: (x, y) = x, y = -10.5804, -2.16921 + +70: (x, y) = x, y = -11.9697, -0.730534 + +71: (x, y) = x, y = -12.2134, -2.71563 + +72: (x, y) = x, y = -10.6813, -1.43005 + +73: (x, y) = x, y = -8.7326, -0.980149 + +74: (x, y) = x, y = -10.708, -0.66728 + +75: (x, y) = x, y = -12.1467, -2.0566 + +76: (x, y) = x, y = -14.1467, -2.0566 + +77: (x, y) = x, y = -16.1272, -2.33494 + +78: (x, y) = x, y = -17.8045, -1.24567 + +79: (x, y) = x, y = -18.9517, 0.392639 + +80: (x, y) = x, y = -17.5877, 1.85535 + +81: (x, y) = x, y = -19.1637, 3.08667 + +82: (x, y) = x, y = -18.9894, 1.09428 + +83: (x, y) = x, y = -17.2933, 0.0344412 + +84: (x, y) = x, y = -16.204, 1.71178 + +85: (x, y) = x, y = -18.1931, 1.50273 + +86: (x, y) = x, y = -19.4517, 3.05702 + +87: (x, y) = x, y = -20.003, 1.13449 + +88: (x, y) = x, y = -20.5206, -0.797358 + +89: (x, y) = x, y = -19.8366, -2.67674 + +90: (x, y) = x, y = -18.3979, -1.28743 + +91: (x, y) = x, y = -20.0752, -2.3767 + +92: (x, y) = x, y = -18.6366, -0.987388 + +93: (x, y) = x, y = -17.2983, -2.47368 + +94: (x, y) = x, y = -15.6402, -3.59206 + +95: (x, y) = x, y = -14.6706, -1.84282 + +96: (x, y) = x, y = -12.9386, -0.842824 + +97: (x, y) = x, y = -12.5569, 1.12043 + +98: (x, y) = x, y = -12.0393, 3.05228 + +99: (x, y) = x, y = -10.1721, 2.33555 + +100: (x, y) = x, y = -8.30498, 1.61881 + +101: (x, y) = x, y = -10.0542, 0.649191 + +102: (x, y) = x, y = -10.8677, 2.47628 + +103: (x, y) = x, y = -8.97666, 3.12742 + +104: (x, y) = x, y = -10.4629, 1.78916 + +105: (x, y) = x, y = -9.94531, -0.142694 + +106: (x, y) = x, y = -10.7905, 1.66992 + +107: (x, y) = x, y = -9.30426, 3.00818 + +108: (x, y) = x, y = -10.0857, 4.84919 + +109: (x, y) = x, y = -10.3295, 2.8641 + +110: (x, y) = x, y = -8.77517, 4.12274 + +111: (x, y) = x, y = -7.09783, 5.21202 + +112: (x, y) = x, y = -5.68361, 6.62623 + +113: (x, y) = x, y = -4.2694, 5.21202 + +114: (x, y) = x, y = -5.47303, 3.61475 + +115: (x, y) = x, y = -3.67544, 2.73801 + +116: (x, y) = x, y = -5.22973, 3.99665 + +117: (x, y) = x, y = -3.23095, 4.06645 + +118: (x, y) = x, y = -3.57825, 6.03606 + +119: (x, y) = x, y = -3.64805, 4.03728 + +120: (x, y) = x, y = -5.57057, 4.58855 + +121: (x, y) = x, y = -7.20887, 5.73571 + +122: (x, y) = x, y = -8.23895, 7.45004 + +123: (x, y) = x, y = -7.92608, 9.42542 + +124: (x, y) = x, y = -6.21175, 8.39534 + +125: (x, y) = x, y = -4.67966, 7.10977 + +126: (x, y) = x, y = -6.25568, 8.34109 + +127: (x, y) = x, y = -7.487, 6.76507 + +128: (x, y) = x, y = -5.93271, 8.02371 + +129: (x, y) = x, y = -5.89781, 10.0234 + +130: (x, y) = x, y = -7.61214, 8.99333 + +131: (x, y) = x, y = -8.29618, 7.11394 + +132: (x, y) = x, y = -7.35724, 5.34805 + +133: (x, y) = x, y = -9.26985, 4.7633 + +134: (x, y) = x, y = -10.8459, 5.99463 + +135: (x, y) = x, y = -9.14977, 7.05447 + +136: (x, y) = x, y = -10.2391, 8.73181 + +137: (x, y) = x, y = -10.689, 6.78307 + +138: (x, y) = x, y = -10.1713, 8.71492 + +139: (x, y) = x, y = -10.7893, 10.617 + +140: (x, y) = x, y = -12.6687, 11.3011 + +141: (x, y) = x, y = -10.7281, 10.8172 + +142: (x, y) = x, y = -12.2824, 12.0759 + +143: (x, y) = x, y = -11.8325, 14.0246 + +144: (x, y) = x, y = -10.8025, 12.3103 + +145: (x, y) = x, y = -11.6159, 10.4832 + +146: (x, y) = x, y = -11.166, 8.53444 + +147: (x, y) = x, y = -9.75181, 7.12023 + +148: (x, y) = x, y = -10.501, 5.26586 + +149: (x, y) = x, y = -8.67394, 6.07934 + +150: (x, y) = x, y = -10.1366, 4.71534 + +151: (x, y) = x, y = -8.93301, 6.31261 + +152: (x, y) = x, y = -7.42359, 7.62473 + +153: (x, y) = x, y = -9.02087, 6.4211 + +154: (x, y) = x, y = -10.4102, 7.85978 + +155: (x, y) = x, y = -11.3491, 6.09388 + +156: (x, y) = x, y = -13.3464, 6.19855 + +157: (x, y) = x, y = -15.0425, 7.25839 + +158: (x, y) = x, y = -15.856, 9.08548 + +159: (x, y) = x, y = -15.7862, 11.0843 + +160: (x, y) = x, y = -16.8754, 9.40692 + +161: (x, y) = x, y = -18.788, 8.82218 + +162: (x, y) = x, y = -16.9209, 8.10544 + +163: (x, y) = x, y = -15.5569, 9.56815 + +164: (x, y) = x, y = -13.9186, 10.7153 + +165: (x, y) = x, y = -14.9487, 12.4296 + +166: (x, y) = x, y = -14.567, 14.3929 + +167: (x, y) = x, y = -12.835, 15.3929 + +168: (x, y) = x, y = -13.519, 13.5135 + +169: (x, y) = x, y = -11.7214, 14.3903 + +170: (x, y) = x, y = -9.75182, 14.7375 + +171: (x, y) = x, y = -8.49318, 16.2918 + +172: (x, y) = x, y = -6.54444, 16.7417 + +173: (x, y) = x, y = -4.71735, 17.5552 + +174: (x, y) = x, y = -2.71735, 17.5552 + +175: (x, y) = x, y = -1.32804, 16.1165 + +176: (x, y) = x, y = -1.32804, 14.1165 + +177: (x, y) = x, y = -0.844192, 12.1759 + +178: (x, y) = x, y = -2.39848, 13.4346 + +179: (x, y) = x, y = -4.26564, 14.1513 + +180: (x, y) = x, y = -6.26564, 14.1513 + +181: (x, y) = x, y = -4.27326, 14.3256 + +182: (x, y) = x, y = -4.0642, 16.3147 + +183: (x, y) = x, y = -4.16887, 18.3119 + +184: (x, y) = x, y = -5.65516, 16.9737 + +185: (x, y) = x, y = -5.65516, 18.9737 + +186: (x, y) = x, y = -3.84254, 18.1284 + +187: (x, y) = x, y = -3.80764, 16.1287 + +188: (x, y) = x, y = -4.49168, 18.0081 + +189: (x, y) = x, y = -2.91566, 16.7768 + +190: (x, y) = x, y = -0.915963, 16.7419 + +191: (x, y) = x, y = -2.47026, 15.4833 + +192: (x, y) = x, y = -4.33742, 16.2 + +193: (x, y) = x, y = -6.06947, 15.2 + +194: (x, y) = x, y = -6.20898, 13.2049 + +195: (x, y) = x, y = -5.39551, 15.032 + +196: (x, y) = x, y = -7.23652, 14.2505 + +197: (x, y) = x, y = -8.83379, 13.0469 + +198: (x, y) = x, y = -9.04284, 15.0359 + +199: (x, y) = x, y = -9.18236, 17.031 + +200: (x, y) = x, y = -10.0591, 15.2334 + +201: (x, y) = x, y = -8.19194, 14.5167 + +202: (x, y) = x, y = -6.27933, 15.1015 + +203: (x, y) = x, y = -6.72923, 13.1527 + +204: (x, y) = x, y = -4.73197, 13.2574 + +205: (x, y) = x, y = -2.81936, 13.8421 + +206: (x, y) = x, y = -2.57562, 15.8272 + +207: (x, y) = x, y = -2.47095, 13.83 + +208: (x, y) = x, y = -4.45604, 14.0737 + +209: (x, y) = x, y = -5.87026, 12.6595 + +210: (x, y) = x, y = -4.75187, 11.0014 + +211: (x, y) = x, y = -2.76678, 10.7577 + +212: (x, y) = x, y = -4.05235, 9.22559 + +213: (x, y) = x, y = -4.39965, 7.25597 + +214: (x, y) = x, y = -4.26014, 9.2511 + +215: (x, y) = x, y = -2.6421, 8.07553 + +216: (x, y) = x, y = -4.12839, 6.73727 + +217: (x, y) = x, y = -3.31492, 4.91018 + +218: (x, y) = x, y = -4.46207, 3.27187 + +219: (x, y) = x, y = -2.69618, 2.33293 + +220: (x, y) = x, y = -3.18002, 0.392338 + +221: (x, y) = x, y = -3.7313, 2.31486 + +222: (x, y) = x, y = -2.82332, 4.09687 + +223: (x, y) = x, y = -1.67616, 5.73518 + +224: (x, y) = x, y = -0.213456, 4.37118 + +225: (x, y) = x, y = 1.50088, 3.34111 + +226: (x, y) = x, y = 3.48992, 3.13205 + +227: (x, y) = x, y = 1.61054, 3.81609 + +228: (x, y) = x, y = -0.374555, 3.57235 + +229: (x, y) = x, y = -0.374555, 5.57235 + +230: (x, y) = x, y = -1.55013, 7.19038 + +231: (x, y) = x, y = -0.898989, 9.08142 + +232: (x, y) = x, y = 0.131087, 7.36709 + +233: (x, y) = x, y = -1.46618, 8.57072 + +234: (x, y) = x, y = -3.34557, 7.88668 + +235: (x, y) = x, y = -2.22718, 6.2286 + +236: (x, y) = x, y = -4.11822, 6.87974 + +237: (x, y) = x, y = -3.66832, 4.931 + +238: (x, y) = x, y = -3.66832, 6.931 + +239: (x, y) = x, y = -1.87073, 6.05426 + +240: (x, y) = x, y = -3.56683, 7.11409 + +241: (x, y) = x, y = -3.70634, 5.11897 + +242: (x, y) = x, y = -5.58572, 4.43493 + +243: (x, y) = x, y = -6.26976, 2.55554 + +244: (x, y) = x, y = -4.71547, 3.81418 + +245: (x, y) = x, y = -4.1642, 1.89166 + +246: (x, y) = x, y = -5.82227, 0.773272 + +247: (x, y) = x, y = -6.13514, -1.20211 + +248: (x, y) = x, y = -7.66723, -2.48768 + +249: (x, y) = x, y = -6.11294, -3.74632 + +250: (x, y) = x, y = -6.66421, -1.8238 + +251: (x, y) = x, y = -4.70792, -2.23962 + +252: (x, y) = x, y = -3.95871, -0.385253 + +253: (x, y) = x, y = -5.92196, -0.00363532 + +254: (x, y) = x, y = -5.7129, -1.99268 + +255: (x, y) = x, y = -6.16281, -3.94142 + +256: (x, y) = x, y = -4.19955, -3.5598 + +257: (x, y) = x, y = -4.19955, -5.5598 + +258: (x, y) = x, y = -3.54841, -7.45084 + +259: (x, y) = x, y = -5.45053, -8.06887 + +260: (x, y) = x, y = -6.68185, -9.64489 + +261: (x, y) = x, y = -5.56346, -7.98682 + +262: (x, y) = x, y = -7.11776, -6.72818 + +263: (x, y) = x, y = -9.1068, -6.51912 + +264: (x, y) = x, y = -10.9978, -7.17026 + +265: (x, y) = x, y = -12.6752, -8.25954 + +266: (x, y) = x, y = -10.7056, -8.60683 + +267: (x, y) = x, y = -11.9369, -10.1829 + +268: (x, y) = x, y = -9.95635, -9.90451 + +269: (x, y) = x, y = -9.60905, -7.93489 + +270: (x, y) = x, y = -9.22744, -5.97164 + +271: (x, y) = x, y = -7.4782, -5.00202 + +272: (x, y) = x, y = -9.03249, -6.26066 + +273: (x, y) = x, y = -8.71962, -8.23604 + +274: (x, y) = x, y = -8.40675, -10.2114 + +275: (x, y) = x, y = -8.82257, -12.1677 + +276: (x, y) = x, y = -10.6352, -13.0129 + +277: (x, y) = x, y = -8.64615, -12.8039 + +278: (x, y) = x, y = -6.93181, -11.7738 + +279: (x, y) = x, y = -5.99287, -13.5397 + +280: (x, y) = x, y = -7.8839, -14.1908 + +281: (x, y) = x, y = -9.34661, -12.8268 + +282: (x, y) = x, y = -11.201, -12.0776 + +283: (x, y) = x, y = -11.3057, -14.0749 + +284: (x, y) = x, y = -13.3053, -14.04 + +285: (x, y) = x, y = -12.7541, -12.1175 + +286: (x, y) = x, y = -11.136, -10.9419 + +287: (x, y) = x, y = -13.1057, -11.2892 + +288: (x, y) = x, y = -11.1831, -11.8405 + +289: (x, y) = x, y = -9.52506, -12.9588 + +290: (x, y) = x, y = -9.87235, -14.9285 + +291: (x, y) = x, y = -11.4696, -16.1321 + +292: (x, y) = x, y = -12.0209, -18.0546 + +293: (x, y) = x, y = -11.9162, -20.0519 + +294: (x, y) = x, y = -10.2019, -19.0218 + +295: (x, y) = x, y = -10.3066, -17.0245 + +296: (x, y) = x, y = -11.4821, -15.4065 + +297: (x, y) = x, y = -9.61497, -14.6898 + +298: (x, y) = x, y = -11.5171, -15.3078 + +299: (x, y) = x, y = -12.9064, -13.8691 + +300: (x, y) = x, y = -11.1088, -14.7459 + +301: (x, y) = x, y = -10.8651, -16.731 + +302: (x, y) = x, y = -9.22677, -15.5838 + +303: (x, y) = x, y = -8.38154, -13.7712 + +304: (x, y) = x, y = -10.3041, -14.3225 + +305: (x, y) = x, y = -8.88984, -15.7367 + +306: (x, y) = x, y = -10.8217, -16.2543 + +307: (x, y) = x, y = -10.717, -18.2516 + +308: (x, y) = x, y = -12.6658, -18.7015 + +309: (x, y) = x, y = -14.0551, -20.1402 + +310: (x, y) = x, y = -13.8113, -18.1551 + +311: (x, y) = x, y = -12.1152, -17.0952 + +312: (x, y) = x, y = -13.2336, -18.7533 + +313: (x, y) = x, y = -14.1726, -20.5192 + +314: (x, y) = x, y = -16.1531, -20.2409 + +315: (x, y) = x, y = -15.006, -18.6025 + +316: (x, y) = x, y = -16.9865, -18.8809 + +317: (x, y) = x, y = -16.6392, -16.9113 + +318: (x, y) = x, y = -17.4844, -18.7239 + +319: (x, y) = x, y = -19.4695, -18.4802 + +320: (x, y) = x, y = -18.0802, -17.0415 + +321: (x, y) = x, y = -16.1676, -16.4567 + +322: (x, y) = x, y = -14.4356, -15.4567 + +323: (x, y) = x, y = -13.4356, -17.1888 + +324: (x, y) = x, y = -12.7515, -19.0682 + +325: (x, y) = x, y = -11.97, -20.9092 + +326: (x, y) = x, y = -12.1096, -22.9043 + +327: (x, y) = x, y = -11.4915, -24.8064 + +328: (x, y) = x, y = -13.4806, -25.0155 + +329: (x, y) = x, y = -11.6535, -25.829 + +330: (x, y) = x, y = -10.84, -27.656 + +331: (x, y) = x, y = -9.14391, -26.5962 + +332: (x, y) = x, y = -9.72865, -28.5088 + +333: (x, y) = x, y = -7.9966, -29.5088 + +334: (x, y) = x, y = -8.24034, -27.5237 + +335: (x, y) = x, y = -6.68605, -28.7824 + +336: (x, y) = x, y = -8.52706, -28.0009 + +337: (x, y) = x, y = -7.77785, -29.8553 + +338: (x, y) = x, y = -8.32912, -27.9327 + +339: (x, y) = x, y = -6.35951, -28.28 + +340: (x, y) = x, y = -4.64517, -27.25 + +341: (x, y) = x, y = -3.70623, -25.4841 + +342: (x, y) = x, y = -4.2575, -27.4066 + +343: (x, y) = x, y = -4.36217, -29.4039 + +344: (x, y) = x, y = -3.24379, -31.0619 + +345: (x, y) = x, y = -2.30485, -32.8278 + +346: (x, y) = x, y = -4.24544, -33.3117 + +347: (x, y) = x, y = -5.5837, -31.8254 + +348: (x, y) = x, y = -3.69266, -31.1742 + +349: (x, y) = x, y = -2.38054, -32.6837 + +350: (x, y) = x, y = -4.3778, -32.579 + +351: (x, y) = x, y = -3.53257, -30.7664 + +352: (x, y) = x, y = -1.83647, -31.8262 + +353: (x, y) = x, y = -1.45485, -29.863 + +354: (x, y) = x, y = -2.51469, -31.5591 + +355: (x, y) = x, y = -2.93051, -33.5153 + +356: (x, y) = x, y = -0.974218, -33.0995 + +357: (x, y) = x, y = 0.948306, -32.5482 + +358: (x, y) = x, y = 2.00814, -30.8522 + +359: (x, y) = x, y = 2.65928, -28.9611 + +360: (x, y) = x, y = 1.2206, -30.3504 + +361: (x, y) = x, y = 3.20114, -30.6288 + +362: (x, y) = x, y = 2.75123, -28.68 + +363: (x, y) = x, y = 4.71449, -29.0617 + +364: (x, y) = x, y = 6.29051, -30.293 + +365: (x, y) = x, y = 6.87525, -28.3804 + +366: (x, y) = x, y = 8.16083, -29.9125 + +367: (x, y) = x, y = 7.19121, -28.1632 + +368: (x, y) = x, y = 6.10193, -29.8406 + +369: (x, y) = x, y = 6.71997, -31.7427 + +370: (x, y) = x, y = 8.611, -32.3938 + +371: (x, y) = x, y = 8.5412, -34.3926 + +372: (x, y) = x, y = 10.5108, -34.7399 + +373: (x, y) = x, y = 9.89279, -32.8378 + +374: (x, y) = x, y = 11.2568, -31.3751 + +375: (x, y) = x, y = 9.31619, -30.8912 + +376: (x, y) = x, y = 10.7055, -29.4525 + +377: (x, y) = x, y = 9.73589, -31.2018 + +378: (x, y) = x, y = 10.7055, -32.951 + +379: (x, y) = x, y = 11.5507, -31.1384 + +380: (x, y) = x, y = 9.59445, -31.5542 + +381: (x, y) = x, y = 11.5507, -31.9701 + +382: (x, y) = x, y = 13.548, -31.8654 + +383: (x, y) = x, y = 11.6686, -31.1813 + +384: (x, y) = x, y = 12.4178, -29.327 + +385: (x, y) = x, y = 11.6364, -27.486 + +386: (x, y) = x, y = 12.3204, -29.3653 + +387: (x, y) = x, y = 13.6587, -30.8516 + +388: (x, y) = x, y = 15.6584, -30.8167 + +389: (x, y) = x, y = 17.6147, -31.2326 + +390: (x, y) = x, y = 19.4557, -30.4511 + +391: (x, y) = x, y = 18.0415, -31.8653 + +392: (x, y) = x, y = 16.3641, -32.9546 + +393: (x, y) = x, y = 18.3638, -32.9895 + +394: (x, y) = x, y = 18.5033, -30.9944 + +395: (x, y) = x, y = 16.5109, -30.8201 + +396: (x, y) = x, y = 17.6293, -32.4781 + +397: (x, y) = x, y = 17.5595, -34.4769 + +398: (x, y) = x, y = 15.6108, -34.027 + +399: (x, y) = x, y = 13.6111, -34.0619 + +400: (x, y) = x, y = 11.6886, -34.6132 + +401: (x, y) = x, y = 13.5679, -33.9291 + +402: (x, y) = x, y = 15.144, -35.1605 + +403: (x, y) = x, y = 13.1589, -35.4042 + +404: (x, y) = x, y = 15.1589, -35.4042 + +405: (x, y) = x, y = 17.1343, -35.0913 + +406: (x, y) = x, y = 19.1193, -35.3351 + +407: (x, y) = x, y = 20.4833, -33.8724 + +408: (x, y) = x, y = 19.3941, -32.195 + +409: (x, y) = x, y = 17.4715, -31.6438 + +410: (x, y) = x, y = 19.3941, -32.195 + +411: (x, y) = x, y = 20.8083, -33.6092 + +412: (x, y) = x, y = 19.0263, -32.7013 + +413: (x, y) = x, y = 17.472, -33.9599 + +414: (x, y) = x, y = 19.4283, -34.3757 + +415: (x, y) = x, y = 19.3236, -32.3785 + +416: (x, y) = x, y = 17.4965, -33.1919 + +417: (x, y) = x, y = 15.7645, -32.1919 + +418: (x, y) = x, y = 17.1787, -30.7777 + +419: (x, y) = x, y = 15.3661, -31.623 + +420: (x, y) = x, y = 17.3293, -31.2413 + +421: (x, y) = x, y = 18.3594, -32.9557 + +422: (x, y) = x, y = 19.4192, -34.6518 + +423: (x, y) = x, y = 17.9805, -36.0411 + +424: (x, y) = x, y = 16, -35.7627 + +425: (x, y) = x, y = 17.7659, -34.8238 + +426: (x, y) = x, y = 17.8008, -36.8235 + +427: (x, y) = x, y = 16.9241, -35.0259 + +428: (x, y) = x, y = 17.8008, -36.8235 + +429: (x, y) = x, y = 19.0864, -38.3556 + +430: (x, y) = x, y = 17.1231, -37.974 + +431: (x, y) = x, y = 19.1155, -37.7997 + +432: (x, y) = x, y = 17.2745, -38.5811 + +433: (x, y) = x, y = 15.9624, -37.0717 + +434: (x, y) = x, y = 14.0061, -37.4875 + +435: (x, y) = x, y = 12.1651, -38.269 + +436: (x, y) = x, y = 10.6108, -37.0103 + +437: (x, y) = x, y = 11.7001, -35.333 + +438: (x, y) = x, y = 10.6402, -33.6369 + +439: (x, y) = x, y = 10.4312, -35.626 + +440: (x, y) = x, y = 8.94489, -36.9642 + +441: (x, y) = x, y = 8.36014, -35.0516 + +442: (x, y) = x, y = 8.32524, -33.0519 + +443: (x, y) = x, y = 7.54377, -34.8929 + +444: (x, y) = x, y = 9.20185, -36.0113 + +445: (x, y) = x, y = 10.514, -37.5207 + +446: (x, y) = x, y = 8.81787, -38.5806 + +447: (x, y) = x, y = 6.8425, -38.2677 + +448: (x, y) = x, y = 6.42667, -36.3114 + +449: (x, y) = x, y = 5.97677, -38.2601 + +450: (x, y) = x, y = 5.22756, -40.1145 + +451: (x, y) = x, y = 7.15941, -40.6321 + +452: (x, y) = x, y = 7.84345, -38.7528 + +453: (x, y) = x, y = 8.75143, -36.9707 + +454: (x, y) = x, y = 7.00219, -37.9404 + +455: (x, y) = x, y = 6.06325, -39.7063 + +456: (x, y) = x, y = 7.66052, -40.9099 + +457: (x, y) = x, y = 9.64105, -41.1882 + +458: (x, y) = x, y = 8.35548, -42.7203 + +459: (x, y) = x, y = 8.00818, -44.6899 + +460: (x, y) = x, y = 6.01579, -44.5156 + +461: (x, y) = x, y = 7.54788, -43.2301 + +462: (x, y) = x, y = 6.08517, -41.8661 + +463: (x, y) = x, y = 7.98729, -41.248 + +464: (x, y) = x, y = 6.01191, -40.9352 + +465: (x, y) = x, y = 7.72625, -41.9652 + +466: (x, y) = x, y = 6.21683, -43.2773 + +467: (x, y) = x, y = 4.2217, -43.4169 + +468: (x, y) = x, y = 3.80587, -45.3732 + +469: (x, y) = x, y = 5.7284, -45.9244 + +470: (x, y) = x, y = 7.26049, -47.21 + +471: (x, y) = x, y = 6.26049, -45.478 + +472: (x, y) = x, y = 7.85776, -46.6816 + +473: (x, y) = x, y = 9.08908, -45.1056 + +474: (x, y) = x, y = 8.059, -46.8199 + +475: (x, y) = x, y = 6.16797, -46.1688 + +476: (x, y) = x, y = 5.02081, -47.8071 + +477: (x, y) = x, y = 4.98591, -45.8074 + +478: (x, y) = x, y = 3.15882, -46.6208 + +479: (x, y) = x, y = 1.40958, -47.5905 + +480: (x, y) = x, y = -0.0998395, -48.9026 + +481: (x, y) = x, y = -0.204511, -46.9053 + +482: (x, y) = x, y = -2.1451, -47.3892 + +483: (x, y) = x, y = -4.05771, -46.8044 + +484: (x, y) = x, y = -5.8848, -45.9909 + +485: (x, y) = x, y = -5.71049, -47.9833 + +486: (x, y) = x, y = -4.32118, -46.5447 + +487: (x, y) = x, y = -5.29079, -44.7954 + +488: (x, y) = x, y = -5.36059, -46.7942 + +489: (x, y) = x, y = -7.05669, -45.7344 + +490: (x, y) = x, y = -7.70783, -47.6254 + +491: (x, y) = x, y = -7.46409, -45.6403 + +492: (x, y) = x, y = -9.44462, -45.9187 + +493: (x, y) = x, y = -7.67873, -44.9797 + +494: (x, y) = x, y = -8.06035, -43.0165 + +495: (x, y) = x, y = -6.06035, -43.0165 + +496: (x, y) = x, y = -7.37246, -44.5259 + +497: (x, y) = x, y = -9.31306, -45.0097 + +498: (x, y) = x, y = -8.19467, -43.3516 + +499: (x, y) = x, y = -9.42599, -41.7756 + +500: (x, y) = x, y = -8.14042, -40.2435 + +501: (x, y) = x, y = -6.22781, -39.6588 + +502: (x, y) = x, y = -7.22781, -37.9267 + +503: (x, y) = x, y = -5.27907, -37.4768 + +504: (x, y) = x, y = -4.24899, -39.1912 + +505: (x, y) = x, y = -5.48031, -40.7672 + +506: (x, y) = x, y = -4.11632, -39.3045 + +507: (x, y) = x, y = -6.11145, -39.165 + +508: (x, y) = x, y = -8.0433, -39.6826 + +509: (x, y) = x, y = -8.69443, -41.5736 + +510: (x, y) = x, y = -6.89685, -40.6969 + +511: (x, y) = x, y = -5.77846, -39.0388 + +512: (x, y) = x, y = -3.85594, -39.5901 + +513: (x, y) = x, y = -5.38803, -40.8757 + +514: (x, y) = x, y = -5.45782, -38.8769 + +515: (x, y) = x, y = -3.50153, -38.4611 + +516: (x, y) = x, y = -4.94021, -37.0718 + +517: (x, y) = x, y = -6.4265, -35.7335 + +518: (x, y) = x, y = -4.43137, -35.594 + +519: (x, y) = x, y = -4.22231, -37.583 + +520: (x, y) = x, y = -3.67104, -39.5056 + +521: (x, y) = x, y = -4.87467, -37.9083 + +522: (x, y) = x, y = -6.13331, -39.4626 + +523: (x, y) = x, y = -4.18457, -39.9125 + +524: (x, y) = x, y = -2.41867, -38.9735 + +525: (x, y) = x, y = -4.24577, -39.787 + +526: (x, y) = x, y = -5.80006, -41.0456 + +527: (x, y) = x, y = -7.0587, -39.4914 + +528: (x, y) = x, y = -5.48268, -38.26 + +529: (x, y) = x, y = -5.30837, -40.2524 + +530: (x, y) = x, y = -6.56701, -38.6981 + +531: (x, y) = x, y = -5.01271, -39.9568 + +532: (x, y) = x, y = -6.93524, -39.4055 + +533: (x, y) = x, y = -7.99508, -37.7094 + +534: (x, y) = x, y = -9.79266, -38.5861 + +535: (x, y) = x, y = -10.3103, -36.6543 + +536: (x, y) = x, y = -8.32126, -36.8633 + +537: (x, y) = x, y = -8.906, -38.776 + +538: (x, y) = x, y = -8.49018, -40.7323 + +539: (x, y) = x, y = -10.4656, -41.0451 + +540: (x, y) = x, y = -12.1039, -39.898 + +541: (x, y) = x, y = -14.1039, -39.898 + +542: (x, y) = x, y = -15.0428, -38.1321 + +543: (x, y) = x, y = -13.0431, -38.0972 + +544: (x, y) = x, y = -11.511, -36.8116 + +545: (x, y) = x, y = -13.2431, -37.8116 + +546: (x, y) = x, y = -14.0565, -39.6387 + +547: (x, y) = x, y = -12.7183, -38.1524 + +548: (x, y) = x, y = -14.2504, -39.438 + +549: (x, y) = x, y = -15.3102, -37.7419 + +550: (x, y) = x, y = -17.2327, -38.2931 + +551: (x, y) = x, y = -15.3306, -37.6751 + +552: (x, y) = x, y = -16.3607, -35.9608 + +553: (x, y) = x, y = -15.5792, -34.1198 + +554: (x, y) = x, y = -13.5868, -33.9455 + +555: (x, y) = x, y = -15.3012, -32.9154 + +556: (x, y) = x, y = -15.2663, -34.9151 + +557: (x, y) = x, y = -15.4753, -32.926 + +558: (x, y) = x, y = -17.2246, -31.9564 + +559: (x, y) = x, y = -19.2234, -32.0262 + +560: (x, y) = x, y = -17.2671, -31.6104 + +561: (x, y) = x, y = -16.9198, -33.58 + +562: (x, y) = x, y = -18.517, -32.3764 + +563: (x, y) = x, y = -20.5122, -32.2369 + +564: (x, y) = x, y = -19.0259, -30.8986 + +565: (x, y) = x, y = -20.5353, -32.2107 + +566: (x, y) = x, y = -22.3329, -31.334 + +567: (x, y) = x, y = -22.4027, -33.3328 + +568: (x, y) = x, y = -23.2479, -31.5201 + +569: (x, y) = x, y = -22.7641, -29.5796 + +570: (x, y) = x, y = -23.0424, -27.599 + +571: (x, y) = x, y = -24.3807, -26.1127 + +572: (x, y) = x, y = -24.8645, -28.0533 + +573: (x, y) = x, y = -24.7947, -26.0545 + +574: (x, y) = x, y = -22.8023, -25.8802 + +575: (x, y) = x, y = -24.7586, -25.4644 + +576: (x, y) = x, y = -26.7282, -25.1171 + +577: (x, y) = x, y = -27.3794, -27.0081 + +578: (x, y) = x, y = -27.3445, -25.0084 + +579: (x, y) = x, y = -27.6228, -26.989 + +580: (x, y) = x, y = -25.6741, -26.5391 + +581: (x, y) = x, y = -25.1564, -28.4709 + +582: (x, y) = x, y = -26.9223, -27.532 + +583: (x, y) = x, y = -28.3365, -26.1178 + +584: (x, y) = x, y = -26.3515, -26.3615 + +585: (x, y) = x, y = -24.6194, -25.3615 + +586: (x, y) = x, y = -26.4465, -24.548 + +587: (x, y) = x, y = -27.8852, -23.1587 + +588: (x, y) = x, y = -29.0607, -21.5407 + +589: (x, y) = x, y = -27.8571, -23.138 + +590: (x, y) = x, y = -26.3708, -24.4762 + +591: (x, y) = x, y = -24.4012, -24.1289 + +592: (x, y) = x, y = -24.7485, -22.1593 + +593: (x, y) = x, y = -22.9665, -23.0673 + +594: (x, y) = x, y = -21.9364, -21.353 + +595: (x, y) = x, y = -23.5124, -20.1216 + +596: (x, y) = x, y = -25.4443, -20.6393 + +597: (x, y) = x, y = -27.1216, -21.7285 + +598: (x, y) = x, y = -25.863, -20.1743 + +599: (x, y) = x, y = -27.8618, -20.2441 + +600: (x, y) = x, y = -29.7528, -19.5929 + +601: (x, y) = x, y = -27.8857, -18.8762 + +602: (x, y) = x, y = -26.4963, -20.3149 + +603: (x, y) = x, y = -25.4071, -18.6375 + +604: (x, y) = x, y = -23.516, -19.2887 + +605: (x, y) = x, y = -22.0066, -20.6008 + +606: (x, y) = x, y = -22.2503, -18.6157 + +607: (x, y) = x, y = -20.4093, -17.8342 + +608: (x, y) = x, y = -20.1656, -15.8491 + +609: (x, y) = x, y = -22.1546, -16.0582 + +610: (x, y) = x, y = -23.9956, -15.2767 + +611: (x, y) = x, y = -24.2047, -13.2877 + +612: (x, y) = x, y = -24.9539, -11.4333 + +613: (x, y) = x, y = -23.6953, -12.9876 + +614: (x, y) = x, y = -24.1452, -11.0389 + +615: (x, y) = x, y = -22.3042, -11.8203 + +616: (x, y) = x, y = -20.437, -12.5371 + +617: (x, y) = x, y = -21.7753, -11.0508 + +618: (x, y) = x, y = -19.8434, -11.5684 + +619: (x, y) = x, y = -20.3947, -9.64589 + +620: (x, y) = x, y = -20.0131, -7.68263 + +621: (x, y) = x, y = -18.8947, -9.34071 + +622: (x, y) = x, y = -16.9384, -9.75653 + +623: (x, y) = x, y = -15.6797, -8.20224 + +624: (x, y) = x, y = -17.3941, -7.17216 + +625: (x, y) = x, y = -16.0301, -5.70946 + +626: (x, y) = x, y = -17.9707, -6.1933 + +627: (x, y) = x, y = -19.5887, -7.36887 + +628: (x, y) = x, y = -19.936, -9.33849 + +629: (x, y) = x, y = -20.844, -7.55647 + +630: (x, y) = x, y = -19.268, -6.32515 + +631: (x, y) = x, y = -18.2983, -4.57591 + +632: (x, y) = x, y = -16.3287, -4.22861 + +633: (x, y) = x, y = -17.7674, -5.61793 + +634: (x, y) = x, y = -15.7978, -5.96523 + +635: (x, y) = x, y = -13.8224, -6.2781 + +636: (x, y) = x, y = -12.704, -4.62002 + +637: (x, y) = x, y = -12.6342, -2.62124 + +638: (x, y) = x, y = -13.4477, -4.44833 + +639: (x, y) = x, y = -14.9571, -3.13621 + +640: (x, y) = x, y = -16.6152, -2.01783 + +641: (x, y) = x, y = -15.1525, -0.65383 + +642: (x, y) = x, y = -15.7372, 1.25878 + +643: (x, y) = x, y = -17.0228, 2.79087 + +644: (x, y) = x, y = -15.0423, 2.51252 + +645: (x, y) = x, y = -16.7566, 1.48245 + +646: (x, y) = x, y = -17.3414, 3.39506 + +647: (x, y) = x, y = -19.1684, 2.58158 + +648: (x, y) = x, y = -20.8068, 1.43443 + +649: (x, y) = x, y = -19.1684, 0.287277 + +650: (x, y) = x, y = -21.1672, 0.217478 + +651: (x, y) = x, y = -23.1078, 0.701321 + +652: (x, y) = x, y = -23.1427, -1.29837 + +653: (x, y) = x, y = -25.1123, -0.951078 + +654: (x, y) = x, y = -26.6884, -2.1824 + +655: (x, y) = x, y = -26.0043, -4.06179 + +656: (x, y) = x, y = -27.5803, -5.29311 + +657: (x, y) = x, y = -27.0965, -7.2337 + +658: (x, y) = x, y = -28.7738, -6.14442 + +659: (x, y) = x, y = -26.8042, -6.49172 + +660: (x, y) = x, y = -25.2948, -5.1796 + +661: (x, y) = x, y = -24.2055, -3.50226 + +662: (x, y) = x, y = -23.7217, -5.44285 + +663: (x, y) = x, y = -22.463, -3.88856 + +664: (x, y) = x, y = -22.0472, -5.84485 + +665: (x, y) = x, y = -21.9077, -7.83998 + +666: (x, y) = x, y = -21.7682, -9.83511 + +667: (x, y) = x, y = -19.9138, -10.5843 + +668: (x, y) = x, y = -21.0894, -12.2024 + +669: (x, y) = x, y = -22.9688, -11.5183 + +670: (x, y) = x, y = -23.8768, -9.7363 + +671: (x, y) = x, y = -22.0096, -9.01957 + +672: (x, y) = x, y = -21.492, -7.08772 + +673: (x, y) = x, y = -22.9306, -8.47703 + +674: (x, y) = x, y = -21.0896, -7.69557 + +675: (x, y) = x, y = -20.3082, -5.85456 + +676: (x, y) = x, y = -19.1045, -7.45183 + +677: (x, y) = x, y = -17.1073, -7.34716 + +678: (x, y) = x, y = -18.7046, -6.14353 + +679: (x, y) = x, y = -20.6067, -5.5255 + +680: (x, y) = x, y = -18.9106, -6.58533 + +681: (x, y) = x, y = -17.2723, -7.73249 + +682: (x, y) = x, y = -19.1394, -8.44922 + +683: (x, y) = x, y = -20.2287, -10.1266 + +684: (x, y) = x, y = -21.6674, -8.73725 + +685: (x, y) = x, y = -20.6373, -7.02291 + +686: (x, y) = x, y = -19.083, -5.76427 + +687: (x, y) = x, y = -20.4723, -7.20295 + +688: (x, y) = x, y = -18.5597, -7.7877 + +689: (x, y) = x, y = -20.5488, -7.99675 + +690: (x, y) = x, y = -22.1668, -9.17232 + +691: (x, y) = x, y = -20.3397, -8.35885 + +692: (x, y) = x, y = -21.2165, -10.1564 + +693: (x, y) = x, y = -22.7485, -8.87086 + +694: (x, y) = x, y = -21.5449, -10.4681 + +695: (x, y) = x, y = -19.9689, -9.23681 + +696: (x, y) = x, y = -19.3841, -11.1494 + +697: (x, y) = x, y = -19.4888, -9.15216 + +698: (x, y) = x, y = -17.5663, -8.60089 + +699: (x, y) = x, y = -15.889, -7.51161 + +700: (x, y) = x, y = -17.0361, -5.8733 + +701: (x, y) = x, y = -15.0437, -5.69899 + +702: (x, y) = x, y = -14.4924, -7.62152 + +703: (x, y) = x, y = -16.3718, -8.30556 + +704: (x, y) = x, y = -16.7534, -6.3423 + +705: (x, y) = x, y = -15.4679, -7.87439 + +706: (x, y) = x, y = -16.9773, -6.56227 + +707: (x, y) = x, y = -15.6652, -5.05285 + +708: (x, y) = x, y = -14.3012, -6.51556 + +709: (x, y) = x, y = -13.8513, -8.4643 + +710: (x, y) = x, y = -13.6075, -6.47921 + +711: (x, y) = x, y = -13.2259, -8.44246 + +712: (x, y) = x, y = -13.9751, -10.2968 + +713: (x, y) = x, y = -13.4913, -12.2374 + +714: (x, y) = x, y = -11.7592, -11.2374 + +715: (x, y) = x, y = -9.93214, -10.4239 + +716: (x, y) = x, y = -11.5704, -11.5711 + +717: (x, y) = x, y = -10.8537, -9.70394 + +718: (x, y) = x, y = -11.5378, -11.5833 + +719: (x, y) = x, y = -13.4399, -12.2014 + +720: (x, y) = x, y = -14.6712, -10.6253 + +721: (x, y) = x, y = -12.804, -11.3421 + +722: (x, y) = x, y = -12.9435, -9.34695 + +723: (x, y) = x, y = -13.8203, -11.1445 + +724: (x, y) = x, y = -11.8449, -11.4574 + +725: (x, y) = x, y = -10.3355, -10.1453 + +726: (x, y) = x, y = -9.91966, -12.1016 + +727: (x, y) = x, y = -9.64132, -14.0821 + +728: (x, y) = x, y = -11.0306, -12.6434 + +729: (x, y) = x, y = -10.0917, -14.4093 + +730: (x, y) = x, y = -12.0868, -14.5488 + +731: (x, y) = x, y = -13.3989, -13.0394 + +732: (x, y) = x, y = -15.3913, -12.8651 + +733: (x, y) = x, y = -16.0754, -10.9857 + +734: (x, y) = x, y = -14.3261, -11.9553 + +735: (x, y) = x, y = -14.1171, -13.9444 + +736: (x, y) = x, y = -13.1171, -12.2123 + +737: (x, y) = x, y = -13.8338, -10.3452 + +738: (x, y) = x, y = -15.7464, -10.9299 + +739: (x, y) = x, y = -13.7977, -10.48 + +740: (x, y) = x, y = -12.1396, -9.36164 + +741: (x, y) = x, y = -11.758, -11.3249 + +742: (x, y) = x, y = -10.9445, -13.152 + +743: (x, y) = x, y = -10.5287, -15.1083 + +744: (x, y) = x, y = -10.0788, -13.1595 + +745: (x, y) = x, y = -12.0013, -13.7108 + +746: (x, y) = x, y = -14.001, -13.6759 + +747: (x, y) = x, y = -13.2195, -15.5169 + +748: (x, y) = x, y = -13.3591, -17.512 + +749: (x, y) = x, y = -12.329, -19.2264 + +750: (x, y) = x, y = -10.3317, -19.1217 + +751: (x, y) = x, y = -10.4712, -21.1168 + +752: (x, y) = x, y = -10.1239, -23.0865 + +753: (x, y) = x, y = -10.4023, -21.1059 + +754: (x, y) = x, y = -12.1343, -20.1059 + +755: (x, y) = x, y = -13.7524, -18.9303 + +756: (x, y) = x, y = -11.7891, -18.5487 + +757: (x, y) = x, y = -11.1711, -16.6466 + +758: (x, y) = x, y = -10.3576, -18.4737 + +759: (x, y) = x, y = -8.45549, -17.8557 + +760: (x, y) = x, y = -9.39444, -19.6216 + +761: (x, y) = x, y = -10.3334, -17.8557 + +762: (x, y) = x, y = -12.0477, -16.8256 + +763: (x, y) = x, y = -11.3966, -18.7166 + +764: (x, y) = x, y = -12.7087, -20.2261 + +765: (x, y) = x, y = -13.7388, -18.5117 + +766: (x, y) = x, y = -15.5514, -19.357 + +767: (x, y) = x, y = -14.9003, -21.248 + +768: (x, y) = x, y = -16.1316, -22.824 + +769: (x, y) = x, y = -14.6689, -21.46 + +770: (x, y) = x, y = -16.6613, -21.2857 + +771: (x, y) = x, y = -18.1475, -19.9474 + +772: (x, y) = x, y = -20.0701, -20.4987 + +773: (x, y) = x, y = -18.179, -21.1499 + +774: (x, y) = x, y = -17.4298, -23.0042 + +775: (x, y) = x, y = -19.0681, -21.8571 + +776: (x, y) = x, y = -21.0669, -21.7873 + +777: (x, y) = x, y = -19.1543, -22.372 + +778: (x, y) = x, y = -17.8687, -23.9041 + +779: (x, y) = x, y = -16.43, -25.2934 + +780: (x, y) = x, y = -15.9124, -27.2253 + +781: (x, y) = x, y = -14.5231, -28.6639 + +782: (x, y) = x, y = -16.0094, -30.0022 + +783: (x, y) = x, y = -15.5595, -28.0535 + +784: (x, y) = x, y = -14.3281, -26.4774 + +785: (x, y) = x, y = -15.2049, -28.275 + +786: (x, y) = x, y = -16.7143, -29.5872 + +787: (x, y) = x, y = -15.8691, -31.3998 + +788: (x, y) = x, y = -17.8496, -31.6781 + +789: (x, y) = x, y = -19.6767, -30.8646 + +790: (x, y) = x, y = -18.3127, -32.3274 + +791: (x, y) = x, y = -17.2529, -34.0234 + +792: (x, y) = x, y = -18.9109, -32.9051 + +793: (x, y) = x, y = -19.3608, -30.9563 + +794: (x, y) = x, y = -20.6195, -32.5106 + +795: (x, y) = x, y = -18.6876, -31.993 + +796: (x, y) = x, y = -20.1263, -33.3823 + +797: (x, y) = x, y = -19.6425, -31.4417 + +798: (x, y) = x, y = -21.6397, -31.337 + +799: (x, y) = x, y = -19.9436, -32.3969 + +800: (x, y) = x, y = -17.9683, -32.7097 + +801: (x, y) = x, y = -19.4069, -34.0991 + +802: (x, y) = x, y = -17.5526, -34.8483 + +803: (x, y) = x, y = -16.5225, -33.1339 + +804: (x, y) = x, y = -18.3351, -33.9792 + +805: (x, y) = x, y = -18.3351, -35.9792 + +806: (x, y) = x, y = -20.1477, -36.8244 + +807: (x, y) = x, y = -20.6654, -38.7563 + +808: (x, y) = x, y = -21.8409, -37.1382 + +809: (x, y) = x, y = -23.6535, -36.293 + +810: (x, y) = x, y = -25.3679, -37.3231 + +811: (x, y) = x, y = -24.1092, -35.7688 + +812: (x, y) = x, y = -22.2182, -36.4199 + +813: (x, y) = x, y = -21.071, -38.0582 + +814: (x, y) = x, y = -19.071, -38.0582 + +815: (x, y) = x, y = -19.2454, -40.0506 + +816: (x, y) = x, y = -21.2207, -40.3635 + +817: (x, y) = x, y = -22.707, -41.7017 + +818: (x, y) = x, y = -24.5742, -42.4185 + +After 819 steps, the subject has the following location: +x, y = -25.2909, -44.2856 +mag, ang = 50.9985, -2.08968 +Average outward distance per step = 0.0622692 diff --git a/11#/p1/vector.cpp b/11#/p1/vector.cpp new file mode 100644 index 0000000..47b2a2a --- /dev/null +++ b/11#/p1/vector.cpp @@ -0,0 +1,134 @@ +#include "vector.h" +#include + +namespace VECTOR +{ +const double Rad_to_reg = 45.0 / atan(1.0); + +void Vector::set_mag() +{ + mag = sqrt(x * x + y * y); +} + +void Vector::set_ang() +{ + if(x == 0.0 && y == 0.0) + ang = 0.0; + else + ang = atan2(y, x); +} + +void Vector::set_x() +{ + x = mag * cos(ang); +} + +void Vector::set_y() +{ + y = mag * sin(ang); +} + +Vector::Vector() +{ + x = y = mag = ang = 0.0; + mode = RECT; +} + +Vector::Vector(double n1, double n2, Mode form) +{ + mode = form; + if (form == RECT) + { + x = n1; + y = n2; + set_ang(); + set_mag(); + } + else if (form == POL) + { + mag = n1; + ang = n2 / Rad_to_reg; + set_x(); + set_y(); + } + else + { + std::cout << "Error" << std::endl; + x = y = mag = ang = 0.0; + mode = RECT; + } +} + +void Vector::reset(double n1, double n2, Mode form) +{ + mode = form; + if (form == RECT) + { + x = n1; + y = n2; + set_ang(); + set_mag(); + } + else if (form == POL) + { + mag = n1; + ang = n2 / Rad_to_reg; + set_x(); + set_y(); + } + else + { + std::cout << "Error" << std::endl; + x = y = mag = ang = 0.0; + mode = RECT; + } +} + +void Vector::polar_mode() +{ + mode = POL; +} + +void Vector::rect_mode() +{ + mode = RECT; +} + +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 << std::endl; + else if (v.mode == Vector::POL) + os << "mag, ang = " << v.mag << ", " << v.ang << std::endl; + else + os << "Invalid mode" << std::endl; + + return os; +} + +} \ No newline at end of file diff --git a/11#/p1/vector.h b/11#/p1/vector.h new file mode 100644 index 0000000..9689e30 --- /dev/null +++ b/11#/p1/vector.h @@ -0,0 +1,44 @@ +#ifndef __VECTOR_H__ +#define __VECTOR_H__ + +#include + +namespace VECTOR +{ +class Vector +{ + public: + enum Mode{RECT, POL}; + private: + double x; + double y; + double mag; + double ang; + Mode mode; + void set_mag(); + void set_ang(); + void set_x(); + void set_y(); + public: + Vector(); + Vector(double n1, double n2, Mode from = RECT); + void reset(double n1, double n2, Mode form = RECT); + double xval() const {return x;} + double yval() const {return y;} + double angval() const {return ang;} + double magval() const {return mag;} + 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 \ No newline at end of file diff --git a/11#/p2/main.cpp b/11#/p2/main.cpp new file mode 100644 index 0000000..14153c1 --- /dev/null +++ b/11#/p2/main.cpp @@ -0,0 +1,51 @@ +#include +#include "vector.h" +#include +#include + +using namespace std; +using namespace VECTOR; + +int main() +{ + double target; + double dstep; + Vector result(0.0, 0.0); + Vector step; + double direction; + unsigned long steps = 0; + + srand(time(NULL)); + + cout << "Enter target distance(q to quit): " << endl; + + while(cin >> target) + { + cout << "Enter the step length: "; + + if(!(cin >> dstep)) + break; + while(result.magval() < target) + { + direction = rand() % 360; + step.reset(dstep, direction, Vector::POL); + result = result + step; + steps++; + } + + cout << "After " << steps << " steps, the subject has the following location." << endl; + cout << result; + result.polar_mode(); + cout << result; + + cout << endl; + + steps = 0; + result.reset(0.0, 0.0); + + cout << "Enter target distance(q to quit): " << endl; + } + cout << "Bye." << endl; + + return 0; +} \ No newline at end of file diff --git a/11#/p2/vector.cpp b/11#/p2/vector.cpp new file mode 100644 index 0000000..b7c9f5c --- /dev/null +++ b/11#/p2/vector.cpp @@ -0,0 +1,139 @@ +#include "vector.h" +#include + +namespace VECTOR +{ +const double Rad_to_reg = 45.0 / atan(1.0); + +// void Vector::set_mag() +// { +// mag = sqrt(x * x + y * y); +// } + +// void Vector::set_ang() +// { +// if(x == 0.0 && y == 0.0) +// ang = 0.0; +// else +// ang = atan2(y, x); +// } + +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 / Rad_to_reg); + set_y(n1, n2 / Rad_to_reg); + } + else + { + std::cout << "Error" << std::endl; + 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 / Rad_to_reg); + set_y(n1, n2 / Rad_to_reg); + } + else + { + std::cout << "Error" << std::endl; + x = y = 0.0; + mode = RECT; + } +} + +double Vector::angval() const +{ + if (x == 0.0 && y == 0.0) + return 0.0; + else + return atan2(y, x); +} + +double Vector::magval() const +{ + return sqrt(x * x + y * y); +} + +void Vector::polar_mode() +{ + mode = POL; +} + +void Vector::rect_mode() +{ + mode = RECT; +} + +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 << std::endl; + else if (v.mode == Vector::POL) + os << "mag, ang = " << v.magval() << ", " << v.angval() << std::endl; + else + os << "Invalid mode" << std::endl; + + return os; +} + +} \ No newline at end of file diff --git a/11#/p2/vector.h b/11#/p2/vector.h new file mode 100644 index 0000000..34cbcd1 --- /dev/null +++ b/11#/p2/vector.h @@ -0,0 +1,44 @@ +#ifndef __VECTOR_H__ +#define __VECTOR_H__ + +#include + +namespace VECTOR +{ +class Vector +{ + public: + enum Mode{RECT, POL}; + private: + double x; + double y; + // double mag; + // double ang; + Mode mode; + // void set_mag(); + // void set_ang(); + void set_x(double mag, double ang); + void set_y(double mag, double ang); + public: + Vector(); + Vector(double n1, double n2, Mode from = RECT); + void reset(double n1, double n2, Mode form = RECT); + double xval() const {return x;} + double yval() const {return y;} + double angval() const; + double magval() 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 \ No newline at end of file diff --git a/11#/p3/main.cpp b/11#/p3/main.cpp new file mode 100644 index 0000000..46f25a9 --- /dev/null +++ b/11#/p3/main.cpp @@ -0,0 +1,69 @@ +#include +#include "vector.h" +#include +#include + +using namespace std; +using namespace VECTOR; + +int main() +{ + double target; + double dstep; + Vector result(0.0, 0.0); + Vector step; + double direction; + unsigned long steps = 0; + unsigned int N; + unsigned int max = 0; + unsigned int min = 0; + unsigned int sum = 0; + + srand(time(NULL)); + + cout << "How many times dou you want to try: " << endl; + cin >> N; + cout << "Enter target distance: "; + cin >> target; + cout << "Enter the step lenght: "; + cin >> dstep; + + for (size_t i = 0; i < N; i++) + { + while (result.magval() < target) + { + direction = rand() % 360; + step.reset(dstep, direction, Vector::POL); + result = result + step; + steps++; + } +/* + cout << "After " << steps << " steps, the subject has the following location." << endl; + cout << result; + result.polar_mode(); + cout << result; + + cout << endl; + + steps = 0; + result.reset(0.0, 0.0); +*/ + max = (max > steps) ? max : steps; + if (min == 0) + min = max; + else + min = (min < steps) ? min : steps; + + sum += steps; + + steps = 0; + result.reset(0.0, 0.0); + } + + cout << "The max steps is: " << max << endl; + cout << "The min steps is: " << min << endl; + cout << "The average steps is: " << (double)sum / N << endl; + cout << "Bye." << endl; + + return 0; +} \ No newline at end of file diff --git a/11#/p3/vector.cpp b/11#/p3/vector.cpp new file mode 100644 index 0000000..47b2a2a --- /dev/null +++ b/11#/p3/vector.cpp @@ -0,0 +1,134 @@ +#include "vector.h" +#include + +namespace VECTOR +{ +const double Rad_to_reg = 45.0 / atan(1.0); + +void Vector::set_mag() +{ + mag = sqrt(x * x + y * y); +} + +void Vector::set_ang() +{ + if(x == 0.0 && y == 0.0) + ang = 0.0; + else + ang = atan2(y, x); +} + +void Vector::set_x() +{ + x = mag * cos(ang); +} + +void Vector::set_y() +{ + y = mag * sin(ang); +} + +Vector::Vector() +{ + x = y = mag = ang = 0.0; + mode = RECT; +} + +Vector::Vector(double n1, double n2, Mode form) +{ + mode = form; + if (form == RECT) + { + x = n1; + y = n2; + set_ang(); + set_mag(); + } + else if (form == POL) + { + mag = n1; + ang = n2 / Rad_to_reg; + set_x(); + set_y(); + } + else + { + std::cout << "Error" << std::endl; + x = y = mag = ang = 0.0; + mode = RECT; + } +} + +void Vector::reset(double n1, double n2, Mode form) +{ + mode = form; + if (form == RECT) + { + x = n1; + y = n2; + set_ang(); + set_mag(); + } + else if (form == POL) + { + mag = n1; + ang = n2 / Rad_to_reg; + set_x(); + set_y(); + } + else + { + std::cout << "Error" << std::endl; + x = y = mag = ang = 0.0; + mode = RECT; + } +} + +void Vector::polar_mode() +{ + mode = POL; +} + +void Vector::rect_mode() +{ + mode = RECT; +} + +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 << std::endl; + else if (v.mode == Vector::POL) + os << "mag, ang = " << v.mag << ", " << v.ang << std::endl; + else + os << "Invalid mode" << std::endl; + + return os; +} + +} \ No newline at end of file diff --git a/11#/p3/vector.h b/11#/p3/vector.h new file mode 100644 index 0000000..9689e30 --- /dev/null +++ b/11#/p3/vector.h @@ -0,0 +1,44 @@ +#ifndef __VECTOR_H__ +#define __VECTOR_H__ + +#include + +namespace VECTOR +{ +class Vector +{ + public: + enum Mode{RECT, POL}; + private: + double x; + double y; + double mag; + double ang; + Mode mode; + void set_mag(); + void set_ang(); + void set_x(); + void set_y(); + public: + Vector(); + Vector(double n1, double n2, Mode from = RECT); + void reset(double n1, double n2, Mode form = RECT); + double xval() const {return x;} + double yval() const {return y;} + double angval() const {return ang;} + double magval() const {return mag;} + 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 \ No newline at end of file diff --git a/11#/p4/mytime4.cpp b/11#/p4/mytime4.cpp new file mode 100644 index 0000000..41d23ba --- /dev/null +++ b/11#/p4/mytime4.cpp @@ -0,0 +1,92 @@ +#include "mytime4.h" + +Time::Time() +{ + hours = minutes = 0; +} + +Time::Time(int h, int m) +{ + hours = h; + minutes = m; +} + +void Time::AddMin(int m) +{ + minutes += m; + hours += minutes / 60; + minutes %= 60; +} + +void Time::AddHr(int h) +{ + hours += h; +} + +void Time::Reset(int h, int m) +{ + hours = h; + minutes = m; +} + +Time operator+(const Time &s, const Time &t) +{ + Time sum; + sum.minutes = s.minutes + t.minutes; + sum.hours = s.hours + t.hours + sum.minutes / 60; + sum.minutes %= 60; + + return sum; +} + +Time operator-(const Time &s, const Time &t) +{ + Time diff; + + int total1, total2; + total1 = s.hours * 60 + s.minutes; + total2 = t.hours * 60 + t.minutes; + diff.hours = (total1 - total2) / 60; + diff.minutes = (total1 - total2) % 60; + + return diff; +} + +Time operator*(const Time &s, double m) +{ + Time result; + + long totalminutes = s.hours * m * 60 + s.minutes * m; + result.hours = totalminutes / 60; + result.minutes = totalminutes % 60; + + return result; +} + +Time operator*(double m, const Time &t) +{ + Time result; + + long totalminutes = t.hours * m * 60 + t.minutes * m; + result.hours = totalminutes / 60; + result.minutes = totalminutes % 60; + + return result; +} + +ostream &operator<<(ostream &os, const Time &t) +{ + os << t.hours << " hours, " << t.minutes << " minutes." << endl; + + return os; +} + +// Time operator*(double m, const Time &t) +// { +// return t * m; +// } + +// void Time::Show() const +// { +// cout << hours << " hours, " << minutes << " minutes." << endl; +// } \ No newline at end of file diff --git a/11#/p4/mytime4.h b/11#/p4/mytime4.h new file mode 100644 index 0000000..662fcd5 --- /dev/null +++ b/11#/p4/mytime4.h @@ -0,0 +1,31 @@ +#ifndef __MYTIME1_H__ +#define __MYTIME1_H__ + +#include + +using namespace std; + +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); + // Time operator +(const Time &t) const; + // Time operator -(const Time &t) const; + // Time operator *(double mult) const; + friend Time operator+(const Time &s, const Time &t); + friend Time operator-(const Time &s, const Time &t); + friend Time operator*(const Time &s, double m); + + friend Time operator*(double m, const Time &t); + friend ostream &operator<<(ostream &os, const Time &t); + // void Show() const; +}; + +#endif \ No newline at end of file diff --git a/11#/p4/usetime4.cpp b/11#/p4/usetime4.cpp new file mode 100644 index 0000000..3ab8f92 --- /dev/null +++ b/11#/p4/usetime4.cpp @@ -0,0 +1,35 @@ +#include +#include "mytime4.h" + +using namespace std; + +int main() +{ + Time coding(4, 35); + Time fixing(2, 47); + Time total; + Time diff; + Time adjusted; + + cout << "coding time = "; + cout << coding; + cout << "fxing time = "; + cout << fixing; + + total = coding + fixing; + cout << total; + + diff = coding - fixing; + cout << diff; + + adjusted = coding * 1.5; + cout << adjusted; + + adjusted = 1.5 * coding; + cout << adjusted; + + cout << "-----------------------------------------" << endl; + cout << coding << fixing; + + return 0; +} \ No newline at end of file diff --git a/11#/p5/stone.cpp b/11#/p5/stone.cpp new file mode 100644 index 0000000..1fa56d1 --- /dev/null +++ b/11#/p5/stone.cpp @@ -0,0 +1,32 @@ +#include +#include "stonewt.h" + +using namespace std; + +int main() +{ + Stonewt incogaito = 275; + Stonewt wolf(285.7); + Stonewt taft = {21, 8}; + + incogaito.Set_Type(Stonewt::POUNDS); + cout << incogaito; + wolf.Set_Type(Stonewt::FLOATPOUNDS); + cout << wolf; + taft.Set_Type(Stonewt::STONE); + cout << taft; + + taft = incogaito + wolf; + taft.Set_Type(Stonewt::STONE); + cout << "taft = " << taft; + + taft = wolf - incogaito; + taft.Set_Type(Stonewt::FLOATPOUNDS); + cout << "taft = " << taft; + + incogaito = incogaito * 2; + incogaito.Set_Type(Stonewt::POUNDS); + cout << "incogaito = " << incogaito; + + return 0; +} \ No newline at end of file diff --git a/11#/p5/stonewt.cpp b/11#/p5/stonewt.cpp new file mode 100644 index 0000000..1543770 --- /dev/null +++ b/11#/p5/stonewt.cpp @@ -0,0 +1,54 @@ +#include "stonewt.h" + +Stonewt::Stonewt(double lbs) +{ + stone = (int)lbs / Lbs_per_stn; + pds_left = (int)lbs % Lbs_per_stn + lbs - (int)lbs; + pounds = lbs; +} + +Stonewt::Stonewt(int stn, double lbs) +{ + stone = stn; + pds_left = lbs; + pounds = stn * Lbs_per_stn + lbs; +} + +Stonewt::Stonewt() +{ + stone = pds_left = pounds = 0; +} + +void Stonewt::Set_Type(Type m) +{ + type = m; +} + +Stonewt Stonewt::operator+(const Stonewt &st) const +{ + return Stonewt(pounds + st.pounds); +} + +Stonewt Stonewt::operator-(const Stonewt &st) const +{ + return Stonewt(pounds - st.pounds); +} + +Stonewt Stonewt::operator*(double n) const +{ + return Stonewt(pounds * n); +} + +std::ostream &operator<<(std::ostream &os, const Stonewt &st) +{ + if (st.type == Stonewt::STONE) + os << st.stone << " stone, " << st.pds_left << " pounds\n"; + else if (st.type == Stonewt::POUNDS) + os << (int)st.pounds << " pounds\n"; + else if (st.type == Stonewt::FLOATPOUNDS) + os << st.pounds << " pounds\n"; + else + os << "Invalid type.\n"; + + return os; +} \ No newline at end of file diff --git a/11#/p5/stonewt.h b/11#/p5/stonewt.h new file mode 100644 index 0000000..7de0652 --- /dev/null +++ b/11#/p5/stonewt.h @@ -0,0 +1,27 @@ +#ifndef __STONEWT_H__ +#define __STONEWT_H__ + +#include + +class Stonewt +{ +public: + enum Type{STONE, POUNDS, FLOATPOUNDS}; +private: + enum{Lbs_per_stn = 14}; + int stone; + double pds_left; + double pounds; + Type type; +public: + Stonewt(double lbs); + Stonewt(int stn, double lbs); + Stonewt(); + void Set_Type(Type m); + Stonewt operator+(const Stonewt &st) const; + Stonewt operator-(const Stonewt &st) const; + Stonewt operator*(double n) const; + friend std::ostream &operator<<(std::ostream &os, const Stonewt &st); +}; + +#endif \ No newline at end of file diff --git a/11#/p6/stone.cpp b/11#/p6/stone.cpp new file mode 100644 index 0000000..c7d0f20 --- /dev/null +++ b/11#/p6/stone.cpp @@ -0,0 +1,39 @@ +#include +#include "stonewt.h" + +using namespace std; + +const int SIZE = 6; + +int main() +{ + Stonewt stone_arr[SIZE] = {275, Stonewt(285.7), Stonewt(21,8)}; + double input; + Stonewt max = stone_arr[0]; + Stonewt min = stone_arr[0]; + Stonewt eleven = Stonewt(11, 0.0); + size_t count = 0; + + for (size_t i = 3; i < SIZE; i++) + { + cout << "Enter number " << i + 1 << "element.(in pounds)" << endl; + cin >> input; + stone_arr[i] = Stonewt(input); + } + + for (size_t i = 0; i < SIZE; i++) + { + max = max > stone_arr[i] ? max : stone_arr[i]; + min = min < stone_arr[i] ? min : stone_arr[i]; + if (stone_arr[i] > eleven) + count++; + } + + cout << "The max weight: "; + max.Show_lbs(); + cout << "The min weight: "; + min.Show_lbs(); + cout << count << " object are heavier than eleven" << endl; + + return 0; +} \ No newline at end of file diff --git a/11#/p6/stonewt.cpp b/11#/p6/stonewt.cpp new file mode 100644 index 0000000..a419b1d --- /dev/null +++ b/11#/p6/stonewt.cpp @@ -0,0 +1,60 @@ +#include "stonewt.h" + +Stonewt::Stonewt(double lbs) +{ + stone = (int)lbs / Lbs_per_stn; + pds_left = (int)lbs % Lbs_per_stn + lbs - (int)lbs; + pounds = lbs; +} + +Stonewt::Stonewt(int stn, double lbs) +{ + stone = stn; + pds_left = lbs; + pounds = stn * Lbs_per_stn + lbs; +} + +Stonewt::Stonewt() +{ + stone = pds_left = pounds = 0; +} + +bool Stonewt::operator<(const Stonewt &s) const +{ + return pounds < s.pounds; +} + +bool Stonewt::operator<=(const Stonewt &s) const +{ + return pounds <= s.pounds; +} + +bool Stonewt::operator>(const Stonewt &s) const +{ + return pounds > s.pounds; +} + +bool Stonewt::operator>=(const Stonewt &s) const +{ + return pounds >= s.pounds; +} + +bool Stonewt::operator==(const Stonewt &s) const +{ + return pounds == s.pounds; +} + +bool Stonewt::operator!=(const Stonewt &s) const +{ + return pounds != s.pounds; +} + +void Stonewt::Show_stn() const +{ + std::cout << stone << " stone, " << pds_left << " pounds." << std::endl; +} + +void Stonewt::Show_lbs() const +{ + std::cout << pounds << " pounds." << std::endl; +} \ No newline at end of file diff --git a/11#/p6/stonewt.h b/11#/p6/stonewt.h new file mode 100644 index 0000000..bd6e9d1 --- /dev/null +++ b/11#/p6/stonewt.h @@ -0,0 +1,30 @@ +#ifndef __STONEWT_H__ +#define __STONEWT_H__ + +#include + +class Stonewt +{ +private: + enum{Lbs_per_stn = 14}; + int stone; + double pds_left; + double pounds; +public: + Stonewt(double lbs); + Stonewt(int stn, double lbs); + Stonewt(); + + bool operator<(const Stonewt &s) const; + bool operator<=(const Stonewt &s) const; + bool operator>(const Stonewt &s) const; + bool operator>=(const Stonewt &s) const; + bool operator==(const Stonewt &s) const; + bool operator!=(const Stonewt &s) const; + + void Show_lbs() const; + void Show_stn() const; + +}; + +#endif \ No newline at end of file diff --git a/11#/p7/complex0.cpp b/11#/p7/complex0.cpp new file mode 100644 index 0000000..755c52b --- /dev/null +++ b/11#/p7/complex0.cpp @@ -0,0 +1,52 @@ +#include "complex0.h" + +Complex::Complex() +{ + real = imaginary = 0; +} + +Complex::Complex(double r, double i) +{ + real = r; + imaginary = i; +} + +Complex Complex::operator+(const Complex &c) const +{ + return Complex(real+c.real, imaginary+c.imaginary); +} + +Complex Complex::operator-(const Complex &c) const +{ + return Complex(real-c.real, imaginary-c.imaginary); +} + +Complex Complex::operator*(const Complex &c) const +{ + return Complex(real*c.real - imaginary*c.imaginary, real*c.imaginary + imaginary*c.real); +} + +Complex Complex::operator~() const +{ + return Complex(real, -imaginary); +} + +Complex operator*(double x, const Complex &c) +{ + return Complex(x*c.real, x*c.imaginary); +} + +istream &operator>>(istream &is, Complex &c) +{ + cout << "real: "; + is >> c.real; + cout << "imaginary: "; + is >> c.imaginary; + return is; +} + +ostream &operator<<(ostream &os, const Complex &c) +{ + os << "real = " << c.real << ", imaginary = " << c.imaginary << endl; + return os; +} \ No newline at end of file diff --git a/11#/p7/complex0.h b/11#/p7/complex0.h new file mode 100644 index 0000000..a37fbb6 --- /dev/null +++ b/11#/p7/complex0.h @@ -0,0 +1,25 @@ +#ifndef __COMPLEX_0_H__ +#define __COMPLEX_0_H__ + +#include + +using namespace std; + +class Complex +{ +private: + double real; + double imaginary; +public: + Complex(); + Complex(double r, double i); + Complex operator+(const Complex &c) const; + Complex operator-(const Complex &c) const; + Complex operator*(const Complex &c) const; + Complex operator~() const; + friend Complex operator*(double x, const Complex &c); + friend istream &operator>>(istream &is, Complex &c); + friend ostream &operator<<(ostream &os, const Complex &c); +}; + +#endif \ No newline at end of file diff --git a/11#/p7/main.cpp b/11#/p7/main.cpp new file mode 100644 index 0000000..4958fae --- /dev/null +++ b/11#/p7/main.cpp @@ -0,0 +1,26 @@ +#include +#include "complex0.h" + +using namespace std; + +int main() +{ + Complex a(3.0, 4.0); + Complex c; + cout << "Enter a complex number (q to quit):\n"; + while(cin >> c) + { + cout << "c is " << c << "\n"; + cout << "complex conjugate is " << ~c << "\n"; + cout << "a is " << a << "\n"; + cout << "a + c is " << a + c << "\n"; + cout << "a - c is " << a - c << "\n"; + cout << "a * c is " << a * c << "\n"; + cout << "2 * c is " << 2 * c << "\n"; + cout << "Enter a complex number (q to quit):\n"; + } + + cout << "Done!\n"; + + return 0; +} \ No newline at end of file diff --git a/11#/r1/stone.cpp b/11#/r1/stone.cpp new file mode 100644 index 0000000..9c9fe58 --- /dev/null +++ b/11#/r1/stone.cpp @@ -0,0 +1,16 @@ +#include +#include "stonewt.h" + +using namespace std; + +int main() +{ + Stonewt taft(10, 8); + + taft = taft * 2.0; + + taft.Show_lbs(); + taft.show_stn(); + + return 0; +} \ No newline at end of file diff --git a/11#/r1/stonewt.cpp b/11#/r1/stonewt.cpp new file mode 100644 index 0000000..116450f --- /dev/null +++ b/11#/r1/stonewt.cpp @@ -0,0 +1,35 @@ +#include "stonewt.h" + +Stonewt::Stonewt(double lbs) +{ + stone = (int)lbs / Lbs_per_stn; + pds_left = (int)lbs % Lbs_per_stn + lbs - (int)lbs; + pounds = lbs; +} + +Stonewt::Stonewt(int stn, double lbs) +{ + stone = stn; + pds_left = lbs; + pounds = stn * Lbs_per_stn + lbs; +} + +Stonewt::Stonewt() +{ + stone = pds_left = pounds = 0; +} + +void Stonewt::show_stn() const +{ + std::cout << stone << " stone, " << pds_left << " pounds." << std::endl; +} + +void Stonewt::Show_lbs() const +{ + std::cout << pounds << " pounds." << std::endl; +} + +Stonewt Stonewt::operator*(double mult) +{ + return Stonewt(pounds * mult); +} \ No newline at end of file diff --git a/11#/r1/stonewt.h b/11#/r1/stonewt.h new file mode 100644 index 0000000..fdf1f28 --- /dev/null +++ b/11#/r1/stonewt.h @@ -0,0 +1,23 @@ +#ifndef __STONEWT_H__ +#define __STONEWT_H__ + +#include + +class Stonewt +{ +private: + enum{Lbs_per_stn = 14}; + int stone; + double pds_left; + double pounds; +public: + // explicit Stonewt(double lbs); + Stonewt(double lbs); + Stonewt(int stn, double lbs); + Stonewt(); + void Show_lbs() const; + void show_stn() const; + Stonewt operator*(double mult); +}; + +#endif \ No newline at end of file diff --git a/11#/r4/stone.cpp b/11#/r4/stone.cpp new file mode 100644 index 0000000..2ae25d0 --- /dev/null +++ b/11#/r4/stone.cpp @@ -0,0 +1,16 @@ +#include +#include "stonewt.h" + +using namespace std; + +int main() +{ + Stonewt taft(10, 8); + + taft = 2.0 * taft; + + taft.Show_lbs(); + taft.show_stn(); + + return 0; +} \ No newline at end of file diff --git a/11#/r4/stonewt.cpp b/11#/r4/stonewt.cpp new file mode 100644 index 0000000..55443e6 --- /dev/null +++ b/11#/r4/stonewt.cpp @@ -0,0 +1,35 @@ +#include "stonewt.h" + +Stonewt::Stonewt(double lbs) +{ + stone = (int)lbs / Lbs_per_stn; + pds_left = (int)lbs % Lbs_per_stn + lbs - (int)lbs; + pounds = lbs; +} + +Stonewt::Stonewt(int stn, double lbs) +{ + stone = stn; + pds_left = lbs; + pounds = stn * Lbs_per_stn + lbs; +} + +Stonewt::Stonewt() +{ + stone = pds_left = pounds = 0; +} + +void Stonewt::show_stn() const +{ + std::cout << stone << " stone, " << pds_left << " pounds." << std::endl; +} + +void Stonewt::Show_lbs() const +{ + std::cout << pounds << " pounds." << std::endl; +} + +Stonewt operator*(double mult, const Stonewt &s) +{ + return Stonewt(mult * s.pounds); +} \ No newline at end of file diff --git a/11#/r4/stonewt.h b/11#/r4/stonewt.h new file mode 100644 index 0000000..60f5f5e --- /dev/null +++ b/11#/r4/stonewt.h @@ -0,0 +1,23 @@ +#ifndef __STONEWT_H__ +#define __STONEWT_H__ + +#include + +class Stonewt +{ +private: + enum{Lbs_per_stn = 14}; + int stone; + double pds_left; + double pounds; +public: + // explicit Stonewt(double lbs); + Stonewt(double lbs); + Stonewt(int stn, double lbs); + Stonewt(); + void Show_lbs() const; + void show_stn() const; + friend Stonewt operator*(double mult, const Stonewt &s); +}; + +#endif \ No newline at end of file diff --git a/11#/r7/vector.h b/11#/r7/vector.h new file mode 100644 index 0000000..9339cff --- /dev/null +++ b/11#/r7/vector.h @@ -0,0 +1,46 @@ +#ifndef __VECTOR_H__ +#define __VECTOR_H__ + +#include + +namespace VECTOR +{ +class Vector +{ + public: + enum Mode{RECT, POL}; + private: + double x; + double y; + double mag; + double ang; + Mode mode; + void set_mag(); + void set_ang(); + void set_x(); + void set_y(); + public: + Vector(); + Vector(double n1, double n2, Mode from = RECT); + void reset(double n1, double n2, Mode form = RECT); + double xval() const {return x;} + double yval() const {return y;} + double angval() const {return ang;} + double magval() const {return mag;} + 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); + + operator double(){return mag;} +}; + +} + +#endif \ No newline at end of file diff --git a/11#/stone/stone.cpp b/11#/stone/stone.cpp new file mode 100644 index 0000000..f9a3aec --- /dev/null +++ b/11#/stone/stone.cpp @@ -0,0 +1,24 @@ +#include +#include "stonewt.h" + +using namespace std; + +int main() +{ + Stonewt incogaito = 275; // Stonewt incogaito(275); Stonewt incogaito = Stonewt(275); + Stonewt wolf(285.7); // Stonewt(double ); + Stonewt taft = {21, 8}; + + incogaito.show_stn(); + wolf.show_stn(); + taft.Show_lbs(); + + cout << "-----------------------" << endl; + incogaito = 276.8; + taft = 325; + incogaito.show_stn(); + taft.show_stn(); + taft.Show_lbs(); + + return 0; +} \ No newline at end of file diff --git a/11#/stone/stonewt.cpp b/11#/stone/stonewt.cpp new file mode 100644 index 0000000..d0d855f --- /dev/null +++ b/11#/stone/stonewt.cpp @@ -0,0 +1,30 @@ +#include "stonewt.h" + +Stonewt::Stonewt(double lbs) +{ + stone = (int)lbs / Lbs_per_stn; + pds_left = (int)lbs % Lbs_per_stn + lbs - (int)lbs; + pounds = lbs; +} + +Stonewt::Stonewt(int stn, double lbs) +{ + stone = stn; + pds_left = lbs; + pounds = stn * Lbs_per_stn + lbs; +} + +Stonewt::Stonewt() +{ + stone = pds_left = pounds = 0; +} + +void Stonewt::show_stn() const +{ + std::cout << stone << " stone, " << pds_left << " pounds." << std::endl; +} + +void Stonewt::Show_lbs() const +{ + std::cout << pounds << " pounds." << std::endl; +} \ No newline at end of file diff --git a/11#/stone/stonewt.h b/11#/stone/stonewt.h new file mode 100644 index 0000000..6f81500 --- /dev/null +++ b/11#/stone/stonewt.h @@ -0,0 +1,23 @@ +#ifndef __STONEWT_H__ +#define __STONEWT_H__ + +#include + +class Stonewt +{ +private: + enum{Lbs_per_stn = 14}; + int stone; + double pds_left; + double pounds; +public: + // explicit Stonewt(double lbs); + Stonewt(double lbs); + Stonewt(int stn, double lbs); + Stonewt(); + void Show_lbs() const; + void show_stn() const; + +}; + +#endif \ No newline at end of file diff --git a/11#/stone1/stone1.cpp b/11#/stone1/stone1.cpp new file mode 100644 index 0000000..ffa522f --- /dev/null +++ b/11#/stone1/stone1.cpp @@ -0,0 +1,19 @@ +#include +#include "stonewt1.h" + +using namespace std; + +int main() +{ + Stonewt poppins(9, 2.8); + + double p_wt = poppins; + cout << "poppins = " << p_wt << " pounds." << endl; + + int weight = poppins; + cout << "poppins = " << weight << " pounds." << endl; + + cout << "poppins = " << double(poppins) << " pounds." << endl; + + return 0; +} \ No newline at end of file diff --git a/11#/stone1/stonewt1.cpp b/11#/stone1/stonewt1.cpp new file mode 100644 index 0000000..8a04654 --- /dev/null +++ b/11#/stone1/stonewt1.cpp @@ -0,0 +1,40 @@ +#include "stonewt1.h" + +Stonewt::Stonewt(double lbs) +{ + stone = (int)lbs / Lbs_per_stn; + pds_left = (int)lbs % Lbs_per_stn + lbs - (int)lbs; + pounds = lbs; +} + +Stonewt::Stonewt(int stn, double lbs) +{ + stone = stn; + pds_left = lbs; + pounds = stn * Lbs_per_stn + lbs; +} + +Stonewt::Stonewt() +{ + stone = pds_left = pounds = 0; +} + +void Stonewt::show_stn() const +{ + std::cout << stone << " stone, " << pds_left << " pounds." << std::endl; +} + +void Stonewt::Show_lbs() const +{ + std::cout << pounds << " pounds." << std::endl; +} + +Stonewt::operator double() const +{ + return pounds; +} + +Stonewt::operator int() const +{ + return (int)(pounds + 0.5); +} \ No newline at end of file diff --git a/11#/stone1/stonewt1.h b/11#/stone1/stonewt1.h new file mode 100644 index 0000000..372d7de --- /dev/null +++ b/11#/stone1/stonewt1.h @@ -0,0 +1,24 @@ +#ifndef __STONEWT_H__ +#define __STONEWT_H__ + +#include + +class Stonewt +{ +private: + enum{Lbs_per_stn = 14}; + int stone; + double pds_left; + double pounds; +public: + // explicit Stonewt(double lbs); + Stonewt(double lbs); + Stonewt(int stn, double lbs); + Stonewt(); + void Show_lbs() const; + void show_stn() const; + operator double() const; + operator int() const; +}; + +#endif \ No newline at end of file diff --git a/11#/usetime0.cpp b/11#/usetime0.cpp new file mode 100644 index 0000000..effec36 --- /dev/null +++ b/11#/usetime0.cpp @@ -0,0 +1,21 @@ +#include +#include "mytime0.h" + +using namespace std; + +int main() +{ + Time coding(2, 40); + Time fixing(5, 55); + Time total; + + cout << "coding time = "; + coding.Show(); + cout << "fxing time = "; + fixing.Show(); + + total = coding.Sum(fixing); + total.Show(); + + return 0; +} \ No newline at end of file diff --git a/11#/usetime1.cpp b/11#/usetime1.cpp new file mode 100644 index 0000000..b759260 --- /dev/null +++ b/11#/usetime1.cpp @@ -0,0 +1,26 @@ +#include +#include "mytime1.h" + +using namespace std; + +int main() +{ + Time coding(2, 40); + Time fixing(5, 55); + Time total; + Time Planning; + + cout << "coding time = "; + coding.Show(); + cout << "fxing time = "; + fixing.Show(); + + // total = coding.Sum(fixing); + Planning = coding.operator+(fixing); + total = coding + fixing; + + total.Show(); + Planning.Show(); + + return 0; +} \ No newline at end of file diff --git a/11#/usetime2.cpp b/11#/usetime2.cpp new file mode 100644 index 0000000..8acf312 --- /dev/null +++ b/11#/usetime2.cpp @@ -0,0 +1,29 @@ +#include +#include "mytime2.h" + +using namespace std; + +int main() +{ + Time coding(4, 35); + Time fixing(2, 47); + Time total; + Time diff; + Time adjusted; + + cout << "coding time = "; + coding.Show(); + cout << "fxing time = "; + fixing.Show(); + + total = coding + fixing; + total.Show(); + + diff = coding - fixing; + diff.Show(); + + adjusted = coding * 1.5; + adjusted.Show(); + + return 0; +} \ No newline at end of file diff --git a/11#/usetime3.cpp b/11#/usetime3.cpp new file mode 100644 index 0000000..f4e2c78 --- /dev/null +++ b/11#/usetime3.cpp @@ -0,0 +1,32 @@ +#include +#include "mytime3.h" + +using namespace std; + +int main() +{ + Time coding(4, 35); + Time fixing(2, 47); + Time total; + Time diff; + Time adjusted; + + cout << "coding time = "; + coding.Show(); + cout << "fxing time = "; + fixing.Show(); + + total = coding + fixing; + total.Show(); + + diff = coding - fixing; + diff.Show(); + + adjusted = coding * 1.5; + adjusted.Show(); + + adjusted = 1.5 * coding; + adjusted.Show(); + + return 0; +} \ No newline at end of file diff --git a/11#/usetime4.cpp b/11#/usetime4.cpp new file mode 100644 index 0000000..3ab8f92 --- /dev/null +++ b/11#/usetime4.cpp @@ -0,0 +1,35 @@ +#include +#include "mytime4.h" + +using namespace std; + +int main() +{ + Time coding(4, 35); + Time fixing(2, 47); + Time total; + Time diff; + Time adjusted; + + cout << "coding time = "; + cout << coding; + cout << "fxing time = "; + cout << fixing; + + total = coding + fixing; + cout << total; + + diff = coding - fixing; + cout << diff; + + adjusted = coding * 1.5; + cout << adjusted; + + adjusted = 1.5 * coding; + cout << adjusted; + + cout << "-----------------------------------------" << endl; + cout << coding << fixing; + + return 0; +} \ No newline at end of file diff --git a/11#/vector/main.cpp b/11#/vector/main.cpp new file mode 100644 index 0000000..14153c1 --- /dev/null +++ b/11#/vector/main.cpp @@ -0,0 +1,51 @@ +#include +#include "vector.h" +#include +#include + +using namespace std; +using namespace VECTOR; + +int main() +{ + double target; + double dstep; + Vector result(0.0, 0.0); + Vector step; + double direction; + unsigned long steps = 0; + + srand(time(NULL)); + + cout << "Enter target distance(q to quit): " << endl; + + while(cin >> target) + { + cout << "Enter the step length: "; + + if(!(cin >> dstep)) + break; + while(result.magval() < target) + { + direction = rand() % 360; + step.reset(dstep, direction, Vector::POL); + result = result + step; + steps++; + } + + cout << "After " << steps << " steps, the subject has the following location." << endl; + cout << result; + result.polar_mode(); + cout << result; + + cout << endl; + + steps = 0; + result.reset(0.0, 0.0); + + cout << "Enter target distance(q to quit): " << endl; + } + cout << "Bye." << endl; + + return 0; +} \ No newline at end of file diff --git a/11#/vector/vector.cpp b/11#/vector/vector.cpp new file mode 100644 index 0000000..47b2a2a --- /dev/null +++ b/11#/vector/vector.cpp @@ -0,0 +1,134 @@ +#include "vector.h" +#include + +namespace VECTOR +{ +const double Rad_to_reg = 45.0 / atan(1.0); + +void Vector::set_mag() +{ + mag = sqrt(x * x + y * y); +} + +void Vector::set_ang() +{ + if(x == 0.0 && y == 0.0) + ang = 0.0; + else + ang = atan2(y, x); +} + +void Vector::set_x() +{ + x = mag * cos(ang); +} + +void Vector::set_y() +{ + y = mag * sin(ang); +} + +Vector::Vector() +{ + x = y = mag = ang = 0.0; + mode = RECT; +} + +Vector::Vector(double n1, double n2, Mode form) +{ + mode = form; + if (form == RECT) + { + x = n1; + y = n2; + set_ang(); + set_mag(); + } + else if (form == POL) + { + mag = n1; + ang = n2 / Rad_to_reg; + set_x(); + set_y(); + } + else + { + std::cout << "Error" << std::endl; + x = y = mag = ang = 0.0; + mode = RECT; + } +} + +void Vector::reset(double n1, double n2, Mode form) +{ + mode = form; + if (form == RECT) + { + x = n1; + y = n2; + set_ang(); + set_mag(); + } + else if (form == POL) + { + mag = n1; + ang = n2 / Rad_to_reg; + set_x(); + set_y(); + } + else + { + std::cout << "Error" << std::endl; + x = y = mag = ang = 0.0; + mode = RECT; + } +} + +void Vector::polar_mode() +{ + mode = POL; +} + +void Vector::rect_mode() +{ + mode = RECT; +} + +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 << std::endl; + else if (v.mode == Vector::POL) + os << "mag, ang = " << v.mag << ", " << v.ang << std::endl; + else + os << "Invalid mode" << std::endl; + + return os; +} + +} \ No newline at end of file diff --git a/11#/vector/vector.h b/11#/vector/vector.h new file mode 100644 index 0000000..9689e30 --- /dev/null +++ b/11#/vector/vector.h @@ -0,0 +1,44 @@ +#ifndef __VECTOR_H__ +#define __VECTOR_H__ + +#include + +namespace VECTOR +{ +class Vector +{ + public: + enum Mode{RECT, POL}; + private: + double x; + double y; + double mag; + double ang; + Mode mode; + void set_mag(); + void set_ang(); + void set_x(); + void set_y(); + public: + Vector(); + Vector(double n1, double n2, Mode from = RECT); + void reset(double n1, double n2, Mode form = RECT); + double xval() const {return x;} + double yval() const {return y;} + double angval() const {return ang;} + double magval() const {return mag;} + 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 \ No newline at end of file diff --git a/4-compound_types/1.cpp b/4-compound_types/1.cpp new file mode 100644 index 0000000..0785446 --- /dev/null +++ b/4-compound_types/1.cpp @@ -0,0 +1,24 @@ +#include + +int main() +{ + using namespace std; + + int yams[3]; + + yams[0] = 7; + yams[1] = 8; + yams[2] = 6; + + int yamscosts[3] = {20, 30, 5}; + + cout << "The total yams = " << yams[0] + yams[1] + yams[2] << endl; + cout << "The package with " << yams[1] << " yams costs " << yamscosts[1] + << " cents per yam." << endl; + cout << "The total yams cost " << yams[0] * yamscosts[0] + yams[1] * yamscosts[1] + yams[2] * yamscosts[2] << " cents" << endl; + + cout << "Size of array = " << sizeof yams << " bytes" << endl; + cout << "Size of one element = " << sizeof yams[0] << " bytes" << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/10.cpp b/4-compound_types/10.cpp new file mode 100644 index 0000000..fb2da40 --- /dev/null +++ b/4-compound_types/10.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +int main() +{ + using namespace std; + + char charr[20]; + string str; + + cout << "Length of string cahrr before input: " << strlen(charr) << endl; + cout << "Length of string str before input: " << str.size() << endl; + + cout << "Enter a line of text: " << endl; + cin.getline(charr, 20); + cout << "You entered: " << charr << endl; + + cout << "Enter another line of text: " << endl; + getline(cin, str); + cout << "You entered: " << str << endl; + + cout << "Length of string in cahrr after input: " << strlen(charr) << endl; + cout << "Length of string in str after input: " << str.size() << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/11.cpp b/4-compound_types/11.cpp new file mode 100644 index 0000000..582b786 --- /dev/null +++ b/4-compound_types/11.cpp @@ -0,0 +1,32 @@ +#include + +struct inflatable +{ + char name[20]; + float volum; + double price; +}; + +int main() +{ + using namespace std; + + inflatable guest = + { + "Glorious Gloria", + 1.88, + 29.99 + }; + + inflatable pal = + { + "Audacious Arthur", + 3.12, + 32.99 + }; + + cout << "Expand your guest list with " << guest.name << " and " << pal.name << endl; + cout << "You can have both for " << guest.price + pal.price << endl; + + return 0; +} diff --git a/4-compound_types/12.cpp b/4-compound_types/12.cpp new file mode 100644 index 0000000..5e63f12 --- /dev/null +++ b/4-compound_types/12.cpp @@ -0,0 +1,30 @@ +#include +// #include + +using namespace std; + +struct inflatable +{ + char name[20]; + float volum; + double price; +}; + +int main() +{ + inflatable bouquet = + { + "sunflowers", + 0.20, + 12.49}; + + inflatable choice; + + cout << "bouquet: " << bouquet.name << " for $ " << bouquet.price << endl; + + choice = bouquet; + + cout << "choice: " << choice.name << " for $ " << choice.price << endl; + + return 0; +} diff --git a/4-compound_types/13.cpp b/4-compound_types/13.cpp new file mode 100644 index 0000000..ace6d32 --- /dev/null +++ b/4-compound_types/13.cpp @@ -0,0 +1,26 @@ +#include +// #include + +using namespace std; + +struct inflatable +{ + char name[20]; + float volum; + double price; +}; + +int main() +{ + inflatable guests[2] = + { + {"Bambi", 0.5, 21.99}, + {"Godzilla", 2000, 565.99} + }; + + cout << "The guests " << guests[0].name << " and " << guests[1].name + << " have a combined volume of " << guests[0].volum + guests[1].volum + << " cubic feet." << endl; + + return 0; +} diff --git a/4-compound_types/14.cpp b/4-compound_types/14.cpp new file mode 100644 index 0000000..d39a166 --- /dev/null +++ b/4-compound_types/14.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +union one2all +{ + char ch; + int n; +}; + +int main() +{ + one2all num; + + cout << "sizeof(num) = " << sizeof(num) << endl; + + num.ch = 'A'; + + cout << "num.ch = " << num.ch << endl; + cout << "num.n = " << num.n << endl; + + return 0; +} diff --git a/4-compound_types/15.cpp b/4-compound_types/15.cpp new file mode 100644 index 0000000..5f23c3e --- /dev/null +++ b/4-compound_types/15.cpp @@ -0,0 +1,14 @@ +#include + +int main() +{ + using namespace std; + + int donuts = 6; + double cups = 4.5; + + cout << "donuts value = " << donuts << " and address = " << &donuts << endl; + cout << "cups value = " << cups << " and address = " << &cups << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/16.cpp b/4-compound_types/16.cpp new file mode 100644 index 0000000..9047502 --- /dev/null +++ b/4-compound_types/16.cpp @@ -0,0 +1,20 @@ +#include + +int main() +{ + using namespace std; + int updates = 6; + int *p_updates; + p_updates = &updates; + + cout << "Value: updates = " << updates << endl; + cout << "p_updates = " << p_updates << endl; + + cout << "Address : &updates = " << &updates << endl; + cout << "*p_updates = " << *p_updates << endl; + + *p_updates = *p_updates + 1; + cout << "Now value: updates = " << updates << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/17.cpp b/4-compound_types/17.cpp new file mode 100644 index 0000000..56b9766 --- /dev/null +++ b/4-compound_types/17.cpp @@ -0,0 +1,27 @@ +#include + +int main() +{ + using namespace std; + + int nights = 1001; + int *pt = new int; + *pt = 1001; + + cout << "nightd value = " << nights << " : locarion = " << &nights << endl; + cout << "int value = " << *pt << " : location = " << pt << endl; + + double *pd = new double; + *pd = 10000001.0; + + cout << "double value = " << *pd << " : location = " << pd << endl; + cout << "location of pointer pd " << &pd << endl; + + cout << "size of pt = " << sizeof(pt) << endl; + cout << "size of *pt = " << sizeof(*pt) << endl; + + cout << "size of pd = " << sizeof(pd) << endl; + cout << "size of *pd = " << sizeof(*pd) << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/18.cpp b/4-compound_types/18.cpp new file mode 100644 index 0000000..2da8b89 --- /dev/null +++ b/4-compound_types/18.cpp @@ -0,0 +1,25 @@ +#include + +int main() +{ + using namespace std; + + double *p3 = new double [3]; + p3[0] = 0.2; + p3[1] = 0.5; + p3[2] = 0.8; + + cout << "p3[1] is " << p3[1] << endl; + + p3 = p3 + 1; + cout << "Now p3[0] is "<< p3[0] << endl; + cout << "Now p3[1] is "<< p3[1] << endl; + + p3 = p3 - 1; + cout << "Now p3[0] is "<< p3[0] << endl; + cout << "Now p3[1] is "<< p3[1] << endl; + + delete [] p3; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/19.cpp b/4-compound_types/19.cpp new file mode 100644 index 0000000..2acbd31 --- /dev/null +++ b/4-compound_types/19.cpp @@ -0,0 +1,30 @@ +#include + +int main() +{ + using namespace std; + + double wages[3] = {10000.0, 20000.0, 30000.0}; + short stacks[3] = {3, 2, 1}; + + double *pw = wages; + short *ps = &stacks[0]; + + cout << "pw = " << pw << ", *pw = " << *pw << endl; + pw = pw + 1; + cout << "add 1 to the pw pointer." << endl; + cout << "pw = " << pw << ", *pw = " << *pw << endl; + + cout << "ps = " << ps << ", *ps = " << *ps << endl; + ps = ps + 1; + cout << "add 1 to the pw pointer." << endl; + cout << "ps = " << ps << ", *ps = " << *ps << endl; + + cout << "stacks[0] = " << stacks[0] << ", stacks[1] = " << stacks[1] << endl; + cout << "*stacks = " << *stacks << ", *(stacks + 1) = " << *(stacks + 1) << endl; + + cout << "size of wages array = " << sizeof(wages) << endl; + cout << "size of pw pointer = " << sizeof(pw) << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/2.cpp b/4-compound_types/2.cpp new file mode 100644 index 0000000..4183f4e --- /dev/null +++ b/4-compound_types/2.cpp @@ -0,0 +1,19 @@ +#include +#include + +int main() +{ + using namespace std; + + const int Size = 15; + + char name1[Size]; + char name2[Size] = "C++owboy"; + + cout << "Howdy! I'm " << name2 << ". " << "What is your name?" << endl; + cin >> name1; + cout << "Well, " << name1 << ", your name has " << strlen(name1) << " letters." << endl; + cout << "And your name is stored in an array of " << sizeof(name1) << " bytes." << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/20.cpp b/4-compound_types/20.cpp new file mode 100644 index 0000000..bae8516 --- /dev/null +++ b/4-compound_types/20.cpp @@ -0,0 +1,17 @@ +#include + +int main() +{ + using namespace std; + + short tell[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; + + cout << "tell = " << tell << endl; + cout << "tell + 1 = " << tell + 1 << endl; + + cout << "&tell + 1 = " << &tell + 1 << endl; + cout << "&tell + 2 = " << &tell + 2 << endl; + + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/21.cpp b/4-compound_types/21.cpp new file mode 100644 index 0000000..e8e84cf --- /dev/null +++ b/4-compound_types/21.cpp @@ -0,0 +1,35 @@ +#include +#include + +int main() +{ + using namespace std; + + char animal[20] = "bear"; + const char *bird = "wren"; + char *ps; + + cout << animal << " and " << bird << endl; + // cout << ps << endl; + + cout << "Enter a kind of animal: "; + cin >> animal; + cout << animal << endl; + + ps = animal; + cout << ps << endl; + + cout << animal << " at " << (int *)animal << endl; + cout << ps << " at " << (int *)ps << endl; + + cout << "After using strcpy\n"; + ps = new char[strlen(animal) + 1]; + strcpy(ps, animal); + + cout << animal << " at " << (int *)animal << endl; + cout << ps << " at " << (int *)ps << endl; + + delete ps; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/22.cpp b/4-compound_types/22.cpp new file mode 100644 index 0000000..61c5164 --- /dev/null +++ b/4-compound_types/22.cpp @@ -0,0 +1,27 @@ +#include + +struct inflatable +{ + char name[20]; + float volume; + double price; +}; + +int main() +{ + using namespace std; + + inflatable *ps = new inflatable; + cout << "Enter name of inflatable item: "; + cin.get(ps->name, 20); + cout << "Enter volume in cubic feet: "; + cin >> (*ps).volume; + cout << "Enter price $"; + cin >> ps->price; + + cout << "Name: " << (*ps).name << endl; + cout << "Volume: " << ps->volume << " cubic feet" << endl; + cout << "Price: $" << ps->price < +#include + +using namespace std; + +char *getname(void); + +int main() +{ + char *name; + + name = getname(); + cout << name << " at " << (int *)name << endl; + delete [] name; + + name = getname(); + cout << name << " at " << (int *)name << endl; + + return 0; +} + +char *getname(void) +{ + char tmp[80]; + cout << "Enter laste name: "; + cin >> tmp; + + char *pn = new char[strlen(tmp) + 1]; + strcpy(pn, tmp); + + return pn; +} \ No newline at end of file diff --git a/4-compound_types/24.cpp b/4-compound_types/24.cpp new file mode 100644 index 0000000..d901966 --- /dev/null +++ b/4-compound_types/24.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +struct antarctica_years_end +{ + int year; +}; + + +int main() +{ + antarctica_years_end s01, s02, s03; + + s01.year = 1998; + + antarctica_years_end *pa = &s02; + pa->year = 1999; + + antarctica_years_end trio[3]; + trio[0].year = 2003; + + cout << trio->year << endl; + + const antarctica_years_end * arp[3] = {&s01, &s02, &s03}; + cout << arp[1]->year << endl; + + const antarctica_years_end **ppa = arp; + cout << (*ppa)->year << endl; + + auto ppb = arp; + cout << (*(ppb+1))->year << endl; // (*(ppb+1)) == &s02; + + + + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/25.cpp b/4-compound_types/25.cpp new file mode 100644 index 0000000..2dca995 --- /dev/null +++ b/4-compound_types/25.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +int main() +{ + using namespace std; + + double a1[4] = {1.2, 2.4, 3.6, 4.8}; + vector a2(4); + a2[0] = 1.0 / 3.0; + a2[1] = 1.0 / 5.0; + a2[2] = 1.0 / 7.0; + a2[3] = 1.0 / 9.0; + + array a3 = {3.14, 2.72, 1.62, 1.41}; + array a4; + + a4 = a3; + + cout << "a1[2] = " << a1[2] << " at " << &a1[2] << endl; + cout << "a2[2] = " << a2[2] << " at " << &a2[2] << endl; + cout << "a3[2] = " << a3[2] << " at " << &a3[2] << endl; + cout << "a4[2] = " << a4[2] << " at " << &a4[2] << endl; + + a1[-2] = 20.2; + cout << "a1[-2] = " << a1[-2] << " at " << &a1[-2] << endl; + cout << "a3[2] = " << a3[2] << " at " << &a3[2] << endl; + cout << "a4[2] = " << a4[2] << " at " << &a4[2] << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/3.cpp b/4-compound_types/3.cpp new file mode 100644 index 0000000..42c4525 --- /dev/null +++ b/4-compound_types/3.cpp @@ -0,0 +1,18 @@ +#include + +int main() +{ + using namespace std; + + const int ArSize = 20; + char name[ArSize]; + char dessert[ArSize]; + + cout << "Enter your name: " << endl; + cin >> name; + cout << "Enter your favorite dessert: " << endl; + cin >> dessert; + cout << "I have some delicious " << dessert << " for you " << name << "." << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/4.cpp b/4-compound_types/4.cpp new file mode 100644 index 0000000..cd34a5f --- /dev/null +++ b/4-compound_types/4.cpp @@ -0,0 +1,21 @@ +#include + +int main() +{ + using namespace std; + + const int ArSize = 20; + char name[ArSize]; + char dessert[ArSize]; + + cout << "Enter your name: " << endl; + // cin.getline(name, ArSize); + cin.get(name, ArSize).get(); + // cin.get(); // read enter + cout << "Enter your favorite dessert: " << endl; + // cin.getline(dessert, ArSize); + cin.get(dessert, ArSize); + cout << "I have some delicious " << dessert << " for you " << name << "." << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/6.cpp b/4-compound_types/6.cpp new file mode 100644 index 0000000..2f1d6b3 --- /dev/null +++ b/4-compound_types/6.cpp @@ -0,0 +1,20 @@ +#include + +int main() +{ + using namespace std; + + int year; + cout << "What year was your house built?" << endl; + // (cin >> year).get(); + cin >> year; + cin.get(); // read enter + cout << "What is its street address?" << endl; + char address[80]; + cin.getline(address, 80); + + cout << "Year bulit: " << year << endl; + cout << "Address:" << address << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/7.cpp b/4-compound_types/7.cpp new file mode 100644 index 0000000..6a903bc --- /dev/null +++ b/4-compound_types/7.cpp @@ -0,0 +1,24 @@ +#include +#include + +int main() +{ + using namespace std; + + char charr1[20]; + char charr2[20] = "jaguar"; + string str1; + string str2 = "panther"; + + cout << "Enter a kind of feline: "; + cin >> charr1; + cout << "Enter another kind of feline: "; + cin >> str1; + cout << "Here are some frlines: \n"; + cout << charr1 << " " << charr2 << " " << str1 << " " << str2 << endl; + + cout << "The third letter in " << charr2 << " is " << charr2[2] << endl; + cout << "The third letter in " << str2 << " is " << str2[2] << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/8.cpp b/4-compound_types/8.cpp new file mode 100644 index 0000000..d1e6f96 --- /dev/null +++ b/4-compound_types/8.cpp @@ -0,0 +1,26 @@ +#include +#include + +int main() +{ + using namespace std; + + string s1 = "penguin"; + string s2, s3; + + cout << "You can assign one sting object to another: s2 = s1" << endl; + s2 = s1; + cout << "s1: " << s1 << ", s2: " << s2 << endl; + cout << "You can assign a C-Style string to a string object." << endl; + s2 = "buzzard"; + cout << "s2: " << s2 << endl; + cout << "You can concatenate strings: s3 = s1 + s2" << endl; + s3 = s1 + s2; + cout << "s3: " << s3 << endl;\ + s1 += s2; + cout << "s1 += s2 yields s1 = " << s1 << endl; + s2 += " for a day"; + cout << "s2: " << s2 << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/9.cpp b/4-compound_types/9.cpp new file mode 100644 index 0000000..02ccd36 --- /dev/null +++ b/4-compound_types/9.cpp @@ -0,0 +1,28 @@ +#include +#include +#include + +int main() +{ + using namespace std; + + char charr1[20]; + char charr2[20] = "jaguar"; + string str1; + string str2 = "panther"; + + str1 = str2; + strcpy(charr1, charr2); + + str1 += " paste"; + strcat(charr1, " juice"); + + int len1 = str1.size(); + int len2 = strlen(charr1); + + cout << "The string " << str1 << " contains " << len1 << " characters." << endl; + cout << "The string " << charr1 << " contains " << len2 << " characters." << endl; + + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/p1.cpp b/4-compound_types/p1.cpp new file mode 100644 index 0000000..73a9ad1 --- /dev/null +++ b/4-compound_types/p1.cpp @@ -0,0 +1,26 @@ +#include + +int main() +{ + using namespace std; + const int size = 20; + + char first_name[size], last_name[size]; + char grade; + int age; + + cout << "What is your first name?"; + cin.getline(first_name, size); + cout << "What is your last name?"; + cin.getline(last_name, size); + cout << "What letter grade do you deserver?"; + cin >> grade; + cout << "What is your age?"; + cin >> age; + + cout << "Name: " << first_name << ", " << last_name << endl; + cout << "Grade: " << char(grade + 1) << endl; + cout << "Age: " << age << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/p10.cpp b/4-compound_types/p10.cpp new file mode 100644 index 0000000..c0793b8 --- /dev/null +++ b/4-compound_types/p10.cpp @@ -0,0 +1,26 @@ +#include +#include + +int main() +{ + using namespace std; + + array record_list; + + cout << "Please enter three records of 40 meters: " << endl; + cout << "First record: "; + cin >> record_list[0]; + cout << "Senced record: "; + cin >> record_list[1]; + cout << "Third record: "; + cin >> record_list[2]; + + cout << "First record: " << record_list[0] + << " Senced record: " << record_list[1] + << " Third record: " << record_list[2] << endl; + + float average = (record_list[0] + record_list[1] + record_list[2]) / 3; + cout << "Average: " << average << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/p2.cpp b/4-compound_types/p2.cpp new file mode 100644 index 0000000..712a1d1 --- /dev/null +++ b/4-compound_types/p2.cpp @@ -0,0 +1,18 @@ +#include +#include + +int main() +{ + using namespace std; + + string name; + string dessert; + + cout << "Enter your name: " << endl; + getline(cin, name); + cout << "Enter your favorite dessert: " << endl; + getline(cin, dessert); + cout << "I have some delicious " << dessert << " for you " << name << "." << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/p3.cpp b/4-compound_types/p3.cpp new file mode 100644 index 0000000..8404d89 --- /dev/null +++ b/4-compound_types/p3.cpp @@ -0,0 +1,25 @@ +#include +#include + +int main() +{ + using namespace std; + + const int SIZE = 20; + + char first_name[SIZE], last_name[SIZE]; + char full_name[2 * SIZE]; + + cout << "Please enter your first name: " << endl; + cin.getline(first_name, SIZE); + cout << "Please enter your last name: " << endl; + cin.getline(last_name, SIZE); + + strcpy(full_name, last_name); + strcat(full_name, ", "); + strcat(full_name, first_name); + + cout << "Here's the information in a signal string: " << full_name << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/p4.cpp b/4-compound_types/p4.cpp new file mode 100644 index 0000000..9bf1b9d --- /dev/null +++ b/4-compound_types/p4.cpp @@ -0,0 +1,19 @@ +#include +#include + +int main() +{ + using namespace std; + + string first_name, last_name, full_name; + + cout << "Please enter your first name: " << endl; + getline(cin, first_name); + cout << "Please enter your last name: " << endl; + getline(cin, last_name); + full_name = first_name + ", " + last_name; + + cout << "Here's the information in a signal string: " << full_name << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/p5.cpp b/4-compound_types/p5.cpp new file mode 100644 index 0000000..75dfc77 --- /dev/null +++ b/4-compound_types/p5.cpp @@ -0,0 +1,21 @@ +#include + +struct CandyBar +{ + char barnd[20]; + float weight; + unsigned int calorie; +}; + +int main() +{ + using namespace std; + + CandyBar sncak = {"Mocha Munch", 2.3, 350}; + + cout << "Band = " << sncak.barnd << endl; + cout << "Weight = " << sncak.weight << endl; + cout << "Calorie = " << sncak.calorie << endl; + + return 0; +} diff --git a/4-compound_types/p6.cpp b/4-compound_types/p6.cpp new file mode 100644 index 0000000..69f4f40 --- /dev/null +++ b/4-compound_types/p6.cpp @@ -0,0 +1,36 @@ +#include + +struct CandyBar +{ + char barnd[20]; + float weight; + unsigned int calorie; +}; + +int main() +{ + using namespace std; + + CandyBar sncak[3] = { + {"Mocha Munch", 2.3, 350}, + {"Whaha", 3.2, 500}, + {"Fudd Way", 4.5, 340} + }; + + cout << "1st Band = " << sncak[0].barnd << endl; + cout << "1st Weight = " << sncak[0].weight << endl; + cout << "1st Calorie = " << sncak[0].calorie << endl; + cout << "---------------------------------------------------" << endl; + + cout << "2st Band = " << sncak[1].barnd << endl; + cout << "2st Weight = " << sncak[1].weight << endl; + cout << "2st Calorie = " << sncak[1].calorie << endl; + cout << "---------------------------------------------------" << endl; + + cout << "3st Band = " << sncak[2].barnd << endl; + cout << "3st Weight = " << sncak[2].weight << endl; + cout << "3st Calorie = " << sncak[2].calorie << endl; + cout << "---------------------------------------------------" << endl; + + return 0; +} diff --git a/4-compound_types/p7.cpp b/4-compound_types/p7.cpp new file mode 100644 index 0000000..cf6a5fd --- /dev/null +++ b/4-compound_types/p7.cpp @@ -0,0 +1,28 @@ +#include + +struct Pizza +{ + char company[20]; + float diameter; + float weight; +}; + +int main() +{ + using namespace std; + + Pizza dinner; + + cout << "Please enter the Pizza's company: "; + cin.getline(dinner.company, 20); + cout << "Please enter the size of pizza in inches: "; + cin >> dinner.diameter; + cout << "Please enter the weight of pizza in pounds: "; + cin >> dinner.weight; + + cout << "Pizza company: " << dinner.company << "." + << " Diameter in inches: " << dinner.diameter << "." + << " Weight in pounds: " << dinner.weight << "." << endl; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/p8.cpp b/4-compound_types/p8.cpp new file mode 100644 index 0000000..8f8557e --- /dev/null +++ b/4-compound_types/p8.cpp @@ -0,0 +1,30 @@ +#include + +struct Pizza +{ + char company[20]; + float diameter; + float weight; +}; + +int main() +{ + using namespace std; + + Pizza *pizza = new Pizza; + + cout << "Please enter the Pizza's company: "; + cin.getline(pizza->company, 20); + cout << "Please enter the size of pizza in inches: "; + cin >> pizza->diameter; + cout << "Please enter the weight of pizza in pounds: "; + cin >> pizza->weight; + + cout << "Pizza company: " << pizza->company << "." + << " Diameter in inches: " << pizza->diameter << "." + << " Weight in pounds: " << pizza->weight << "." << endl; + + delete pizza; + + return 0; +} \ No newline at end of file diff --git a/4-compound_types/p9.cpp b/4-compound_types/p9.cpp new file mode 100644 index 0000000..912c3a1 --- /dev/null +++ b/4-compound_types/p9.cpp @@ -0,0 +1,52 @@ +#include +#include + +struct CandyBar +{ + char barnd[20]; + float weight; + unsigned int calorie; +}; + +int main() +{ + using namespace std; + + // CandyBar sncak[3] = { + // {"Mocha Munch", 2.3, 350}, + // {"Whaha", 3.2, 500}, + // {"Fudd Way", 4.5, 340} + // }; + + CandyBar *sncak = new CandyBar[3]; + strcpy(sncak[0].barnd, "Mocha Munch"); + sncak[0].weight = 2.3; + sncak[0].calorie = 350; + + strcpy(sncak[1].barnd, "Flip Fleming"); + sncak[1].weight = 3.2; + sncak[1].calorie = 500; + + strcpy(sncak[2].barnd, "Flip Fleming"); + sncak[2].weight = 4.2; + sncak[2].calorie = 450; + + cout << "1st Band = " << sncak->barnd << endl; + cout << "1st Weight = " << sncak[0].weight << endl; + cout << "1st Calorie = " << sncak[0].calorie << endl; + cout << "---------------------------------------------------" << endl; + + cout << "2st Band = " << (sncak+1)->barnd << endl; + cout << "2st Weight = " << sncak[1].weight << endl; + cout << "2st Calorie = " << sncak[1].calorie << endl; + cout << "---------------------------------------------------" << endl; + + cout << "3st Band = " << (sncak+2)->barnd << endl; + cout << "3st Weight = " << sncak[2].weight << endl; + cout << "3st Calorie = " << sncak[2].calorie << endl; + cout << "---------------------------------------------------" << endl; + + delete [] sncak; + + return 0; +} diff --git a/5-loops_and_relational_expressions/1.cpp b/5-loops_and_relational_expressions/1.cpp new file mode 100644 index 0000000..c02dc0e --- /dev/null +++ b/5-loops_and_relational_expressions/1.cpp @@ -0,0 +1,15 @@ +#include + +int main() +{ + using namespace std; + + int i; + + for (i = 0; i < 5; i++) + cout << "C++ knows loop." << endl; + + cout << "C++ knows when to stop." << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/10.cpp b/5-loops_and_relational_expressions/10.cpp new file mode 100644 index 0000000..730f465 --- /dev/null +++ b/5-loops_and_relational_expressions/10.cpp @@ -0,0 +1,24 @@ +#include +#include + +int main() +{ + using namespace std; + + cout << "Please enter a word: "; + string word; + cin >> word; + + int i, j; + char temp; + for ( j = 0, i = word.size()-1; j < i; j++, i--) + { + temp = word[i]; + word[i] = word[j]; + word[j] = temp; + } + + cout << word << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/11.cpp b/5-loops_and_relational_expressions/11.cpp new file mode 100644 index 0000000..0795274 --- /dev/null +++ b/5-loops_and_relational_expressions/11.cpp @@ -0,0 +1,19 @@ +#include + +int main() +{ + using namespace std; + + int arr[10] = {20, 20, 20, 20, 20, 19, 20, 18, 20, 20}; + + cout << "Diong it right: " << endl; + int i; + for(i = 0; arr[i] == 20; i++) + cout << "arr " << i << " is a 20." << endl; + + cout << "Doing it dangerously wrong: " << endl; + for (i = 0; arr[i] = 20; i++) + cout << "arr " << i << " is a 20." << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/12.cpp b/5-loops_and_relational_expressions/12.cpp new file mode 100644 index 0000000..458fe92 --- /dev/null +++ b/5-loops_and_relational_expressions/12.cpp @@ -0,0 +1,19 @@ +#include +#include + +int main() +{ + using namespace std; + + char word[5] = "?ate"; + + for (char ch = 'a'; strcmp(word, "mate"); ch++) + { + cout << word << endl; + word[0] = ch; + } + + cout << "After loop ends, word is " << word << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/13.cpp b/5-loops_and_relational_expressions/13.cpp new file mode 100644 index 0000000..b49eb39 --- /dev/null +++ b/5-loops_and_relational_expressions/13.cpp @@ -0,0 +1,19 @@ +#include +#include + +int main() +{ + using namespace std; + + string word = "?ate"; + + for (char ch = 'a'; word != "mate"; ch++) + { + cout << word << endl; + word[0] = ch; + } + + cout << "After loop ends, word is " << word << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/14.cpp b/5-loops_and_relational_expressions/14.cpp new file mode 100644 index 0000000..c1ec7a1 --- /dev/null +++ b/5-loops_and_relational_expressions/14.cpp @@ -0,0 +1,23 @@ +#include + +const int ArSize = 20; + +int main() +{ + using namespace std; + char name[ArSize]; + + cout << "Your first name please: "; + cin >> name; + + cout << "Here is your name: " << endl; + + int i = 0; + while (name[i] != '\0') // while(name[i]) + { + cout << name[i] << ": " << (int) name[i] << endl; + i++; + } + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/15.cpp b/5-loops_and_relational_expressions/15.cpp new file mode 100644 index 0000000..e3bb6c3 --- /dev/null +++ b/5-loops_and_relational_expressions/15.cpp @@ -0,0 +1,20 @@ +#include +#include + +int main() +{ + using namespace std; + + cout << "Enter the delay time, in seconds: "; + + float secs; + cin >> secs; + clock_t delay = secs * CLOCKS_PER_SEC; + + clock_t start = clock(); + while (clock() - start < delay); + + cout << "Done!\n"; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/16.cpp b/5-loops_and_relational_expressions/16.cpp new file mode 100644 index 0000000..54c9fda --- /dev/null +++ b/5-loops_and_relational_expressions/16.cpp @@ -0,0 +1,26 @@ +#include + +int main() +{ + using namespace std; + int n; + + cout << "Enter numbers in the range 1~10 to find my favorite number" << endl; + + // cin >> n; + + // while (n != 7) + // { + // cin >> n; + // } + + do + { + cin >> n; + } while (n != 7); + + + cout << "Yes, 7 is my favorite number." << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/17.cpp b/5-loops_and_relational_expressions/17.cpp new file mode 100644 index 0000000..8646fd6 --- /dev/null +++ b/5-loops_and_relational_expressions/17.cpp @@ -0,0 +1,24 @@ +#include + +int main() +{ + using namespace std; + + char ch; + int count = 0; + + cout << "Enter characters, enter # to quit:" << endl; + cin >> ch; + + while (ch != '#') + { + cout << ch; + ++count; + cin >> ch; + } + + cout << endl; + cout << count << " characters read " << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/18.cpp b/5-loops_and_relational_expressions/18.cpp new file mode 100644 index 0000000..1eecd26 --- /dev/null +++ b/5-loops_and_relational_expressions/18.cpp @@ -0,0 +1,24 @@ +#include + +int main() +{ + using namespace std; + + char ch; + int count = 0; + + cout << "Enter characters, enter # to quit:" << endl; + cin.get(ch); + + while (ch != '#') + { + cout << ch; + ++count; + cin.get(ch); + } + + cout << endl; + cout << count << " characters read " << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/19.cpp b/5-loops_and_relational_expressions/19.cpp new file mode 100644 index 0000000..16f7718 --- /dev/null +++ b/5-loops_and_relational_expressions/19.cpp @@ -0,0 +1,24 @@ +#include + +int main() +{ + using namespace std; + + char ch; + int count = 0; + + cout << "Enter characters, enter # to quit:" << endl; + cin.get(ch); + + while (cin.fail() == false) + { + cout << ch; + ++count; + cin.get(ch); + } + + cout << endl; + cout << count << " characters read " << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/1_1.cpp b/5-loops_and_relational_expressions/1_1.cpp new file mode 100644 index 0000000..147e9e0 --- /dev/null +++ b/5-loops_and_relational_expressions/1_1.cpp @@ -0,0 +1,16 @@ +#include + +int main() +{ + using namespace std; + + // int i; + + for (int i = 0; i < 5; i++) + cout << "C++ knows loop." << endl; + + // cout << "i = " << i << endl; + cout << "C++ knows when to stop." << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/2.cpp b/5-loops_and_relational_expressions/2.cpp new file mode 100644 index 0000000..1e5f507 --- /dev/null +++ b/5-loops_and_relational_expressions/2.cpp @@ -0,0 +1,19 @@ +#include + +int main() +{ + using namespace std; + + cout << "Enter the starting countdown value." << endl; + + int limit; + cin >> limit; + int i; + + for(i = limit; i; i--) + cout << "i = " << i << endl; + + cout << "Done, now that i = " << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/20.cpp b/5-loops_and_relational_expressions/20.cpp new file mode 100644 index 0000000..70bba2e --- /dev/null +++ b/5-loops_and_relational_expressions/20.cpp @@ -0,0 +1,39 @@ +#include + +const int Cities = 5; +const int Years = 4; + +int main() +{ + using namespace std; + + const char *cities[Cities] = + { + "Gribble City", + "Gribbletown", + "New Gribble", + "San Gribble", + "Gribble Vista"}; + + int maxtemps[Years][Cities] = + { + {96, 100, 87, 101, 105}, + {96, 98, 91, 107, 104}, + {97, 101, 93, 108, 107}, + {98, 103, 95, 109, 108}}; + + cout << "City: Maximum temperature for 2008 - 2011" << endl; + for (int city = 0; city < Cities; city++) + { + cout << cities[city] << ":\t"; + for (int year = 0; year < Years; year++) + { + cout << maxtemps[year][city] << "\t"; + } + + cout << endl; + } + + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/3.cpp b/5-loops_and_relational_expressions/3.cpp new file mode 100644 index 0000000..11f1648 --- /dev/null +++ b/5-loops_and_relational_expressions/3.cpp @@ -0,0 +1,22 @@ +#include + +int main() +{ + using namespace std; + + int x; + cout << "The expression x = 100 has the value "; + cout << (x = 100) << endl; + cout << "The expression x < 3 has the value "; + cout << (x < 3) << endl; + cout << "The expression x > 3 has the value "; + cout << (x > 3) << endl; + + cout.setf(ios_base::boolalpha); + cout << "The expression x < 3 has the value "; + cout << (x < 3) << endl; + cout << "The expression x > 3 has the value "; + cout << (x > 3) << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/4.cpp b/5-loops_and_relational_expressions/4.cpp new file mode 100644 index 0000000..4fd4abd --- /dev/null +++ b/5-loops_and_relational_expressions/4.cpp @@ -0,0 +1,17 @@ +#include + +const int ArSize = 16; + +int main() +{ + long long factorials[ArSize]; + factorials[0] = factorials[1] = 1; + + for (int i = 2; i < ArSize; i++) + factorials[i] = factorials[i-1] * i; + + for (int i = 0; i < ArSize; i++) + std::cout << i <<"!= " << factorials[i] << std::endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/5.cpp b/5-loops_and_relational_expressions/5.cpp new file mode 100644 index 0000000..42fba8e --- /dev/null +++ b/5-loops_and_relational_expressions/5.cpp @@ -0,0 +1,19 @@ +#include + +int main() +{ + using std::cout; + using std::cin; + using std::endl; + + cout << "Enter an integer: "; + int by; + cin >> by; + + cout << "Counting by " << by << endl; + + for (int i = 0; i < 100; i = i + by) + cout << i << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/6.cpp b/5-loops_and_relational_expressions/6.cpp new file mode 100644 index 0000000..4172673 --- /dev/null +++ b/5-loops_and_relational_expressions/6.cpp @@ -0,0 +1,17 @@ +#include + +int main() +{ + using namespace std; + + cout << "Enter a word: "; + string word; + cin >> word; + + for(int i = word.size() - 1; i >= 0; i--) + cout << word[i]; + + cout << "\n"; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/7.cpp b/5-loops_and_relational_expressions/7.cpp new file mode 100644 index 0000000..a04d2c4 --- /dev/null +++ b/5-loops_and_relational_expressions/7.cpp @@ -0,0 +1,20 @@ +#include + +int main() +{ + using namespace std; + + int a = 20; + int b = 20; + + cout << "a = " << a << ", " + << "b = " << b << endl; + + cout << "a++ = " << a++ << ", " + << "++b = " << ++b << endl; + + cout << "a = " << a << ", " + << "b = " << b << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/8.cpp b/5-loops_and_relational_expressions/8.cpp new file mode 100644 index 0000000..f21868b --- /dev/null +++ b/5-loops_and_relational_expressions/8.cpp @@ -0,0 +1,19 @@ +#include + +int main() +{ + using namespace std; + + double arr[5] = {21.1, 32.8, 23.4, 45.2, 37.4}; + double *pt = arr; + + cout << *pt << endl; + cout << "*++pt = " << *++pt << endl; + cout << "++*pt = " << ++*pt << endl; + cout << "(*pt)++ = " << (*pt)++ << endl; + cout << *pt << endl; + cout << "*pt++ = " << *pt++ << endl; + cout << *pt << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/9.cpp b/5-loops_and_relational_expressions/9.cpp new file mode 100644 index 0000000..723e185 --- /dev/null +++ b/5-loops_and_relational_expressions/9.cpp @@ -0,0 +1,23 @@ +#include + +int main() +{ + using namespace std; + + double number, sum = 0; + + cout << "Calculate five numbers sum and average." << endl; + cout << "Please enter five values: " << endl; + + for (int i = 1; i <= 5; i++) + { + cout << "Value " << i << ": "; + cin >> number; + sum += number; + } + + cout << "The sum = " << sum << endl; + cout << "Average = " << sum / 5 << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/p1.cpp b/5-loops_and_relational_expressions/p1.cpp new file mode 100644 index 0000000..40d69a2 --- /dev/null +++ b/5-loops_and_relational_expressions/p1.cpp @@ -0,0 +1,22 @@ +#include + +int main() +{ + using namespace std; + + int min, max, sum = 0; + + cout << "Enter the min number: "; + cin >> min; + cout << "Enter the max number: "; + cin >> max; + + for (int i = min; i <= max; i++) + { + sum += i; + } + + cout << "The sum = " << sum << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/p10.cpp b/5-loops_and_relational_expressions/p10.cpp new file mode 100644 index 0000000..d016925 --- /dev/null +++ b/5-loops_and_relational_expressions/p10.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main() +{ + int row; + cout << "Please enter the number of rows: "; + cin >> row; + + for (int i = 0; i < row; i++) + { + for (int j = 0; j < (row - i - 1); j++) + cout << "."; + + for (int j = 0; j <= i; j++) + cout << "*"; + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/p2.cpp b/5-loops_and_relational_expressions/p2.cpp new file mode 100644 index 0000000..4813efb --- /dev/null +++ b/5-loops_and_relational_expressions/p2.cpp @@ -0,0 +1,18 @@ +#include +#include + +const int ArSize = 16; + +int main() +{ + std::array factorials; // long long factorials[ArSize]; + factorials[0] = factorials[1] = 1; + + for (int i = 2; i < ArSize; i++) + factorials[i] = factorials[i-1] * i; + + for (int i = 0; i < ArSize; i++) + std::cout << i <<"!= " << factorials[i] << std::endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/p3.cpp b/5-loops_and_relational_expressions/p3.cpp new file mode 100644 index 0000000..db52329 --- /dev/null +++ b/5-loops_and_relational_expressions/p3.cpp @@ -0,0 +1,19 @@ +#include + +int main() +{ + using namespace std; + + double num, sum = 0; + + do + { + cout << "Please enter a number to add: "; + cin >> num; + sum += num; + } while (num != 0); + + cout << "Input enbd, sum = " << sum << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/p4.cpp b/5-loops_and_relational_expressions/p4.cpp new file mode 100644 index 0000000..f9c7b85 --- /dev/null +++ b/5-loops_and_relational_expressions/p4.cpp @@ -0,0 +1,25 @@ +#include + +const int DEPOSIT_BASE = 100; + +int main() +{ + using namespace std; + + double daphne_deposit = DEPOSIT_BASE; + double cleo_deposit = DEPOSIT_BASE; + int year = 0; + + while (daphne_deposit >= cleo_deposit) + { + daphne_deposit += 0.1 * DEPOSIT_BASE; + cleo_deposit += 0.05 * cleo_deposit; + year++; + } + + cout << "After " << year << "years, " << "Cleo has more money than Daphne." << endl; + cout << "Daphne has " << daphne_deposit << " dollars." << endl; + cout << "Cleo has " << cleo_deposit << " dollars." << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/p5.cpp b/5-loops_and_relational_expressions/p5.cpp new file mode 100644 index 0000000..e5c4e03 --- /dev/null +++ b/5-loops_and_relational_expressions/p5.cpp @@ -0,0 +1,29 @@ +#include + +int main() +{ + using namespace std; + + const string Month[] = {"Jan", "Feb", "Mar", "Apr", + "MAy", "Jun", "Jul", "Aug", + "Sep", "Oct", "Nov", "Dec"}; + int sale_num[12]; + int sum; + + for (int i = 0; i < 12; i++) + { + cout << "Enter the sale number of " << Month[i] << " :"; + cin >> sale_num[i]; + } + + cout << "Input Done!" << endl; + + for (int i = 0; i < 12; i++) + { + sum += sale_num[i]; + } + + cout << "Total sale: " << sum << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/p6.cpp b/5-loops_and_relational_expressions/p6.cpp new file mode 100644 index 0000000..8a22c53 --- /dev/null +++ b/5-loops_and_relational_expressions/p6.cpp @@ -0,0 +1,36 @@ +#include + +int main() +{ + using namespace std; + + const string Month[] = {"Jan", "Feb", "Mar", "Apr", + "MAy", "Jun", "Jul", "Aug", + "Sep", "Oct", "Nov", "Dec"}; + int sale_num[3][12]; + int sum; + + for (int i = 0; i < 3; i++) + { + cout << "Starting " << i + 1 << " year's data." << endl; + for (int j = 0; j < 12; j++) + { + cout << "Enter the sale number of " << Month[j] << " :"; + cin >> sale_num[i][j]; + } + } + + cout << "Input Done!" << endl; + + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 12; j++) + { + sum += sale_num[i][j]; + } + } + + cout << "Three years total sale: " << sum << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/p7.cpp b/5-loops_and_relational_expressions/p7.cpp new file mode 100644 index 0000000..413d9a9 --- /dev/null +++ b/5-loops_and_relational_expressions/p7.cpp @@ -0,0 +1,37 @@ +#include +#include + +using namespace std; + +struct car +{ + string manufacturer; + int date; +}; + +int main() +{ + int car_number; + cout << "Howa many cars do you wish to catalog?"; + cin >> car_number; + cin.get(); + + car *pcar = new car[car_number]; + + for (int i = 0; i < car_number; i++) + { + cout << "Car # " << i + 1 << ": " << endl; + cout << "Please enter the maker: "; + getline(cin, pcar[i].manufacturer); + cout << "Please enter the year made: "; + cin >> pcar[i].date; + cin.get(); + } + + cout << "Here is your collection: " << endl; + + for (int i = 0; i < car_number; i++) + cout << pcar[i].date << " " << pcar[i].manufacturer << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/p8.cpp b/5-loops_and_relational_expressions/p8.cpp new file mode 100644 index 0000000..8f6e1dd --- /dev/null +++ b/5-loops_and_relational_expressions/p8.cpp @@ -0,0 +1,24 @@ +#include +#include + +using namespace std; + +const char DONE[] = "done"; + +int main() +{ + char words[20]; + int counter = 0; + cout << "Enter words (to stop, type the word done): " << endl; + + do + { + cin >> words; + cin.get(); + counter++; + } while (strcmp(words, DONE) != 0); + + cout << "You entered a total of " << counter - 1 << " words." << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/p9.cpp b/5-loops_and_relational_expressions/p9.cpp new file mode 100644 index 0000000..0ba4d96 --- /dev/null +++ b/5-loops_and_relational_expressions/p9.cpp @@ -0,0 +1,24 @@ +#include +#include + +using namespace std; + +const string DONE = "done"; + +int main() +{ + string words; + int counter = 0; + cout << "Enter words (to stop, type the word done): " << endl; + + do + { + cin >> words; + cin.get(); + counter++; + } while ( words != DONE ); + + cout << "You entered a total of " << counter - 1 << " words." << endl; + + return 0; +} \ No newline at end of file diff --git a/5-loops_and_relational_expressions/review.md b/5-loops_and_relational_expressions/review.md new file mode 100644 index 0000000..5b098a4 --- /dev/null +++ b/5-loops_and_relational_expressions/review.md @@ -0,0 +1,25 @@ +1. 入口循环:for(); while(); + 出口循环:do while(); + +2. + 01234 +3. + 369 + 12 +4. + 6 + 8 + +5. + k = 8 +6. + ```cpp + int i; + for (i = 2; i < 64; i *= 2) + cout << i << "、"; + cout << i << endl; + ``` +7. + ```cpp + use "{; ; ; }" + ``` diff --git a/6#/1.cpp b/6#/1.cpp new file mode 100644 index 0000000..5920996 --- /dev/null +++ b/6#/1.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + int spaces = 0; + int total = 0; + char ch; + + cin.get(ch); + while (ch != '.') + { + ++total; + if (ch == ' ') + ++spaces; + cin.get(ch); + } + + cout << "spaces = " << spaces << endl; + cout << "total charachters = " << total << endl; + + return 0; +} \ No newline at end of file diff --git a/6#/10-1.cpp b/6#/10-1.cpp new file mode 100644 index 0000000..d8caa1c --- /dev/null +++ b/6#/10-1.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +void report(void); +void showmenu(void); +void comfort(void); + +int main() +{ + showmenu(); + + char choice; + cin >> choice; + while (choice != '5') + { + switch (choice) + { + case '1': + cout << "Warning!!!" << endl; + break; + case '2': + report(); + break; + case '3': + cout << "The boss was in all day." << endl; + break; + case '4': + comfort(); + break; + default: + cout << "That is not a choice." << endl; + break; + } + showmenu(); + cin >> choice; + } + + return 0; +} + +void showmenu(void) +{ + cout << "Please enter 1, 2, 3, 4, 5 as your choice: " << endl; + cout << "1) alarm 2) report" << endl; + cout << "3) alibi 4) comfort" << endl; + cout << "5) quit" << endl; +} + +void report(void) +{ + cout << "Sales are up 120%. Expenses are down 35%." << endl; +} + +void comfort(void) +{ + cout << "You are the best CEO!" << endl; +} \ No newline at end of file diff --git a/6#/10.cpp b/6#/10.cpp new file mode 100644 index 0000000..f4f177c --- /dev/null +++ b/6#/10.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +void report(void); +void showmenu(void); +void comfort(void); + +int main() +{ + showmenu(); + + int choice; + cin >> choice; + while (choice != 5) + { + switch (choice) + { + case 1: + cout << "Warning!!!" << endl; + break; + case 2: + report(); + break; + case 3: + cout << "The boss was in all day." << endl; + break; + case 4: + comfort(); + break; + default: + cout << "That is not a choice." << endl; + break; + } + showmenu(); + cin >> choice; + } + + return 0; +} + +void showmenu(void) +{ + cout << "Please enter 1, 2, 3, 4, 5 as your choice: " << endl; + cout << "1) alarm 2) report" << endl; + cout << "3) alibi 4) comfort" << endl; + cout << "5) quit" << endl; +} + +void report(void) +{ + cout << "Sales are up 120%. Expenses are down 35%." << endl; +} + +void comfort(void) +{ + cout << "You are the best CEO!" << endl; +} \ No newline at end of file diff --git a/6#/11.cpp b/6#/11.cpp new file mode 100644 index 0000000..659c999 --- /dev/null +++ b/6#/11.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +enum +{ + red, + orange, + yellow, + green, + blue, + violet, + indigo +}; + +int main() +{ + int code; + + cout << "Enter color code (0~6):"; + cin >> code; + + while (code >= red && code <= indigo) + { + switch (code) + { + case red: + cout << "You choose red!" << endl; + break; + case orange: + cout << "You choose orange!" << endl; + break; + case green: + cout << "You choose green!" << endl; + break; + case blue: + cout << "You choose blue!" << endl; + break; + case violet: + cout << "You choose violet!" << endl; + break; + case indigo: + cout << "You choose indigo!" << endl; + break; + } + cout << "Enter color code (0~6):"; + cin >> code; + } + + return 0; +} diff --git a/6#/12.cpp b/6#/12.cpp new file mode 100644 index 0000000..59c0c85 --- /dev/null +++ b/6#/12.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +const int ArSize = 80; + +int main() +{ + char line[ArSize]; + int spaces = 0; + + cout << "Enter a line of text:" << endl; + cin.get(line, ArSize); + + cout << "Complete line:" << line << endl; + + for (int i = 0; line[i] != '\0'; i++) + { + cout << line[i]; + + if (line[i] == '.') + break; + if (line[i] != ' ') + continue; + spaces++; + } + + cout << endl; + cout << "Spaces = " << spaces << endl; + + return 0; +} \ No newline at end of file diff --git a/6#/13.cpp b/6#/13.cpp new file mode 100644 index 0000000..17114a5 --- /dev/null +++ b/6#/13.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int main() +{ + int num1, num2; + + cout << "First number:"; + cin >> num1; + + cin.clear(); // reset input + while (cin.get() != '\n'); // get rid of bad input + + cout << "Last number:"; + cin >> num2; + + cout << "num1 = " << num1 << ", num2 = " << num2 << endl; + + return 0; +} \ No newline at end of file diff --git a/6#/14.cpp b/6#/14.cpp new file mode 100644 index 0000000..ec241f8 --- /dev/null +++ b/6#/14.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +const int Max = 5; + +int main() +{ + double fish[Max]; + int i = 0; + + cout << "Please enter the weigth of your fish:" << endl; + cout << "You may enter up to " << Max << " fish to terminate." << endl; + + cout << "fish #1: "; + + while (i < Max && cin >> fish[i]) + { + if (++i < Max) + cout << "fish #" << i + 1 << ": "; + } + + double total = 0.0; + for (int j = 0; j < i; j++) + total += fish[j]; + + cout << "total: " << total << endl; + if (i == 0) + cout << "No fish" << endl; + else + cout << "Average weight of " << i << " fish: " << total / i << endl; + + return 0; +} \ No newline at end of file diff --git a/6#/15.cpp b/6#/15.cpp new file mode 100644 index 0000000..2e28e9e --- /dev/null +++ b/6#/15.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +const int Max = 5; + +int main() +{ + int golf[Max]; + cout << "Please enter your golf scores." << endl; + cout << "You must enter " << Max << " rounds." << endl; + + int i; + for (i = 0; i < Max; i++) + { + cout << "Round #" << i + 1 << ": " << endl; + while ( !(cin >> golf[i]) ) + { + cin.clear(); + while (cin.get() != '\n'); + cout << "Please enter a number: "; + } + } + + double total = 0.0; + for (i = 0; i < Max; i++) + total += golf[i]; + + cout << "total: " << total << endl; + cout << "Average score: " << total / Max << "." << endl; + + return 0; +} diff --git a/6#/16.cpp b/6#/16.cpp new file mode 100644 index 0000000..bb617b1 --- /dev/null +++ b/6#/16.cpp @@ -0,0 +1,45 @@ +#include +#include // 1. include + +using namespace std; + +int main() +{ + char automobile[50]; + int year; + double a_price; + double d_price; + + ofstream outfile; // 2. create object of output file stream(ofstream) + outfile.open("carinfo.txt"); // 3. associate with a file + + cout << "Enter the make and model of automobile: "; + cin.getline(automobile, 50); + cout << "Enter the model year:"; + cin >> year; + cout << "Enter the original asking price:"; + cin >> a_price; + d_price = a_price * 0.913; + + // show on screen + cout << fixed; + cout.precision(2); + cout.setf(ios_base::showpoint); + cout << "Make and model: " << automobile << endl; + cout << "Year: " << year << endl; + cout << "Was asking: " << a_price << endl; + cout << "Now asking: " << d_price << endl; + + // write to carinfo.txt + outfile << fixed; + outfile.precision(2); + outfile.setf(ios_base::showpoint); + outfile << "Make and model: " << automobile << endl; + outfile << "Year: " << year << endl; + outfile << "Was asking: " << a_price << endl; + outfile << "Now asking: " << d_price << endl; + + outfile.close(); + + return 0; +} \ No newline at end of file diff --git a/6#/17.cpp b/6#/17.cpp new file mode 100644 index 0000000..bd86548 --- /dev/null +++ b/6#/17.cpp @@ -0,0 +1,56 @@ +#include +#include +#include + +using namespace std; + +const int SIZE = 60; + +int main() +{ + char filename[SIZE]; + ifstream infile; + + cout << "Enter name of data file: "; + cin.getline(filename, SIZE); + + infile.open(filename); + if (!infile.is_open()) + { + cout << "Could not open the file " << filename << endl; + cout << "Program treminating." << endl; + exit(EXIT_FAILURE); + } + cout << "Success open the txt file." << endl; + double value; + double sum = 0.0; + int count = 0; + + infile >> value; + while (infile.good()) + { + ++count; + sum += value; + infile >> value; + } + + if (infile.eof()) + cout << "End of file reached." << endl; + else if (infile.fail()) + cout << "Input treminated by data mismachted." << endl; + else + cout << "Input terminated by unknow reason." << endl; + + if (count == 0) + cout << "No data processed." << endl; + else + { + cout << "Items read: " << count << endl; + cout << "Sum: " << sum << endl; + cout << "Average: " << sum / count << endl; + } + + infile.close(); + + return 0; +} \ No newline at end of file diff --git a/6#/2.cpp b/6#/2.cpp new file mode 100644 index 0000000..fdb58d3 --- /dev/null +++ b/6#/2.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + char ch; + + cin.get(ch); + while (ch != '.') + { + if (ch == '\n') + cout << ch; + else + cout << ch + 1; // cout << ++ch; + + cin.get(ch); + } + + cout << endl; + cout << "Please excuse the slight confusion." << endl; + + return 0; +} diff --git a/6#/3.cpp b/6#/3.cpp new file mode 100644 index 0000000..1126936 --- /dev/null +++ b/6#/3.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +const int Fave = 27; + +int main() +{ + int n; + + cout << "Enter a number int the range 1~100 to find your favorite number: "; + do + { + cin >> n; + if (n < Fave) + cout << "Too low, guess again: "; + else if (n > Fave) + cout << "Too high, guess again: "; + else + cout << Fave << " is the right number." << endl; + }while (n != Fave); + + return 0; +} diff --git a/6#/4.cpp b/6#/4.cpp new file mode 100644 index 0000000..9e3e0cb --- /dev/null +++ b/6#/4.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main() +{ + cout << "This program may reformat your hard disk, and destory all your data." << endl; + cout << "Do you wish to continue? " << endl; + + char ch; + cin >> ch; + + if (ch == 'y' || ch == 'Y') + cout << "You were wanrned!" << endl; + else if (ch == 'n' || ch == 'N') + cout << "A wise choice ...." << endl; + else + cout << "That's wasn't a y or n! Can't follow your instruction!" << endl; + + return 0; + +} diff --git a/6#/5.cpp b/6#/5.cpp new file mode 100644 index 0000000..9f9de64 --- /dev/null +++ b/6#/5.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +const int ArSize = 6; + +int main() +{ + float value[ArSize]; + float temp; + int i = 0; + + cout << "Enter six numbers and compare with your level." << endl; + cout << "Terminate condition, when you make 6 numbers or enter a nagative number" << endl; + + cout << "First value: "; + cin >> temp; + + while (i < ArSize && temp >= 0) + { + value[i] = temp; + ++i; + if (i < ArSize) + { + cout << "Next value: "; + cin >> temp; + } + } + + if (i == 0) + cout << "No data--byte." << endl; + else + { + cout << "Enter your level: "; + float level; + cin >> level; + int count = 0; + + for (int j = 0; j < i; j++) + { + if (value[j] > level) + count++; + } + + cout << count << " numbers are bigger than your level." << endl; + } + + return 0; +} diff --git a/6#/6.cpp b/6#/6.cpp new file mode 100644 index 0000000..0bc153c --- /dev/null +++ b/6#/6.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +const char * qualifly[] = +{ + "Perfect", + "Great", + "Good", + "Just so so", + "failed" +}; + +int main() +{ + float score; + int index; + + cout << "Enter your scores: "; + cin >> score; + + if (score >= 90 && score <= 100) + index = 0; + else if (score >= 80 && score < 90) + index = 1; + else if (score >= 70 && score < 80) + index = 2; + else if (score >= 60 && score < 70) + index = 3; + else + index = 4; + + cout << "You qualify: " << qualifly[index] << endl; + + return 0; +} diff --git a/6#/7.cpp b/6#/7.cpp new file mode 100644 index 0000000..1c4b1d3 --- /dev/null +++ b/6#/7.cpp @@ -0,0 +1,34 @@ +#include +#include + +using namespace std; + +bool is_int(double x); + +int main() +{ + double number; + + cout << "Enter an integer value: "; + cin >> number; + + + while (!is_int(number)) + { + cout << "Out of range, please enter again:"; + cin >> number; + } + + int value = (int) number; + cout << "You've enter an integer: " << value << endl; + + return 0; +} + +bool is_int(double x) +{ + if (x <= INT_MAX && x >= INT_MIN) + return true; + else + return false; +} diff --git a/6#/8.cpp b/6#/8.cpp new file mode 100644 index 0000000..53b0495 --- /dev/null +++ b/6#/8.cpp @@ -0,0 +1,42 @@ +#include +#include + +using namespace std; + +int main() +{ + int whitespace = 0; + int digits = 0; + int chars = 0; + int punct = 0; + int others = 0; + + + char ch; + + cout << "Enter text for analysis, and type @ to terinate the input." << endl; + + cin.get(ch); + + while (ch != '@') + { + if (isalpha(ch)) + chars++; + else if (isspace(ch)) + whitespace++; + else if (isdigit(ch)) + digits++; + else if (ispunct(ch)) + punct++; + else + others++; + + cin.get(ch); + } + + cout << chars << " letters, " << whitespace << " whitespace, " + << digits << " digits, " << punct << " punctuations, " + << others << " others" << endl; + + return 0; +} diff --git a/6#/9.cpp b/6#/9.cpp new file mode 100644 index 0000000..3f76da8 --- /dev/null +++ b/6#/9.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main() +{ + int a, b; + int c; + + cout << "Enter two integers:"; + cin >> a >> b; + + c = a > b ? a : b; + + cout << "The larger of " << a << " and " << b << " is " << c << endl; + + return 0; +} \ No newline at end of file diff --git a/6#/carinfo.txt b/6#/carinfo.txt new file mode 100644 index 0000000..bd8ae34 --- /dev/null +++ b/6#/carinfo.txt @@ -0,0 +1,4 @@ +Make and model: nnnaas +Year: 1200000 +Was asking: 121333.00 +Now asking: 110777.03 diff --git a/6#/p1.cpp b/6#/p1.cpp new file mode 100644 index 0000000..cd0149b --- /dev/null +++ b/6#/p1.cpp @@ -0,0 +1,31 @@ +#include +#include + +using namespace std; + +int main() +{ + char input; + + cout << "Please enter a character:"; + cin >> input; + + while (input != '@') + { + if (isdigit(input)) + { + cin >> input; + continue; + } + else if (islower(input)) + input = toupper(input); + else + input = tolower(input); + + cout << input; + + cin >> input; + } + + return 0; +} \ No newline at end of file diff --git a/6#/p2.cpp b/6#/p2.cpp new file mode 100644 index 0000000..f5b40e6 --- /dev/null +++ b/6#/p2.cpp @@ -0,0 +1,41 @@ +#include +#include + +using namespace std; + +int main(void) +{ + array donation; + + double input; + int count = 0; + double sum = 0.0; + double average; + int bigger = 0; + + cout << "Please enter the double numerial: "; + + while (cin >> input) + { + donation[count++] = input; + if (count == 10) + break; + cout << "Please enter the double numerial: "; + } + + for (size_t i = 0; i < count; i++) + sum += donation[i]; + + average = sum / count; + + for (size_t i = 0; i < count; i++) + { + if (donation[i] > average) + bigger++; + } + + cout << "Average is: " << average << endl; + cout << bigger << " number are bigger than avereage." << endl; + + return 0; +} \ No newline at end of file diff --git a/6#/p3.cpp b/6#/p3.cpp new file mode 100644 index 0000000..b2af51b --- /dev/null +++ b/6#/p3.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +void showmenu(void); + +int main(void) +{ + char ch; + + showmenu(); + + cin.get(ch); + while ((ch != 'c') && (ch != 'p') && (ch != 't') && (ch != 'g')) + { + cin.get(); + cout << "Please enter a character: c, p, t, g:"; + cin.get(ch); + } + + switch (ch) + { + case 'c': break; + case 'p': break; + case 't': cout << "A maple is a tree." << endl; break; + case 'g': break; + } + + return 0; +} + +void showmenu(void) +{ + cout << "Please enter one of the following choices: " << endl; + cout << "c) carnivore\t\t p) pianist" << endl; + cout << "t) tree\t\t\t g) game" << endl; +} \ No newline at end of file diff --git a/6#/p4.cpp b/6#/p4.cpp new file mode 100644 index 0000000..dfed35e --- /dev/null +++ b/6#/p4.cpp @@ -0,0 +1,113 @@ +#include + +using namespace std; + +const int strsize = 40; +const int usersize = 4; + +struct bop +{ + char fullname[strsize]; + char title[strsize]; + char bopname[strsize]; + int preference; +}; + +bop user[usersize] = + { + {"Rick", "Level_A", "RR", 0}, + {"Jack", "Level_B", "JJ", 1}, + {"Micheal", "Level_C", "MM", 2}, + {"Rose", "Level_D", "RS", 0}}; + +void showmenu(void); +void print_by_fullname(void); +void print_by_title(void); +void print_by_bopname(void); +void print_by_pref(void); + +int main() +{ + char input; + + showmenu(); + + cin.get(input); + + while (input != 'q') + { + switch (input) + { + case 'a': + print_by_fullname(); + break; + case 'b': + print_by_title(); + break; + case 'c': + print_by_bopname(); + break; + case 'd': + print_by_pref(); + break; + default: + cout << "Please enter character a, b, c, d, q:" << endl; + break; + } + + cin.get(); + cout << "Next input:"; + cin.get(input); + } + + cout << "Bye!" << endl; + + return 0; +} + +void showmenu(void) +{ + cout << "Benevolent Order of Programmer Report" << endl; + cout << "a. display by name\t\t b. display tital" << endl; + cout << "c. display by bopname\t\t d. display by preference" << endl; + cout << "q. quit" << endl; +} + +void print_by_fullname(void) +{ + for (size_t i = 0; i < usersize; i++) + cout << user[i].fullname << endl; +} + +void print_by_title(void) +{ + for (size_t i = 0; i < usersize; i++) + cout << user[i].title << endl; +} + +void print_by_bopname(void) +{ + for (size_t i = 0; i < usersize; i++) + cout << user[i].bopname << endl; +} + +void print_by_pref(void) +{ + for (size_t i = 0; i < usersize; i++) + { + switch (user[i].preference) + { + case 0: + cout << user[i].fullname << endl; + break; + case 1: + cout << user[i].title << endl; + break; + case 2: + cout << user[i].bopname << endl; + break; + default: + break; + } + } +} \ No newline at end of file diff --git a/6#/p5.cpp b/6#/p5.cpp new file mode 100644 index 0000000..4e900bc --- /dev/null +++ b/6#/p5.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int main() +{ + double income, tax; + cout << "Please rnter your income: "; + + while ((cin >> income) && income >= 0) + { + if (income <= 5000) + tax = 0.0; + else if (income <= 15000) + tax = (income - 5000) * 0.10; + else if (income <= 35000) + tax = 10000 * 0.1 + (income - 15000) * 0.15; + else + tax = 10000 * 0.1 + 20000 * 0.15 + (income - 35000) * 0.2; + + cout << "Your tax = " << tax << endl; + cout << "Please enter your income: "; + } + + return 0; +} \ No newline at end of file diff --git a/6#/p6.cpp b/6#/p6.cpp new file mode 100644 index 0000000..07595c3 --- /dev/null +++ b/6#/p6.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +struct patrons +{ + string name; + double donation; +}; + +int main(void) +{ + int number; + bool empty = true; + + cout << "Enter the number of donors:"; + cin >> number; + + patrons *ppatrons = new patrons[number]; + + for (int i = 0; i < number; i++) + { + cout << "donor #" << i + 1 << ": " << endl; + cout << "Enter the name of donor: "; + cin >> ppatrons[i].name; + cout << "Enter the donation amount: "; + cin >> ppatrons[i].donation; + } + + cout << "*****Grand patrons*****" << endl; + for (int i = 0; i < number; i++) + { + if (ppatrons[i].donation >= 10000) + { + cout << ppatrons[i].name << ": " << ppatrons[i].donation << endl; + empty = false; + } + } + + if (empty == true) + cout << "None" << endl; + + empty = true; + + cout << "*****Patrons*****" << endl; + + for (int i = 0; i < number; i++) + { + if (ppatrons[i].donation < 10000) + { + cout << ppatrons[i].name << ": " << ppatrons[i].donation << endl; + empty = false; + } + } + + if (empty == true) + cout << "None" << endl; + + return 0; +} \ No newline at end of file diff --git a/6#/p7.cpp b/6#/p7.cpp new file mode 100644 index 0000000..f2d2cd9 --- /dev/null +++ b/6#/p7.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +using namespace std; + + + +int main() +{ + string words; + int vowels = 0, consonants = 0, others = 0; + + cout << "Enter words (q to quit!): "; + while ((cin >> words) && words != "q") + { + if (isalpha(words[0])) + { + switch (words[0]) + { + case 'a': + case 'e': + case 'i': + case 'o': + case 'u': + case 'A': + case 'E': + case 'I': + case 'O': + case 'U': + vowels++; + break; + default: + consonants++; + break; + } + } + else + others++; + } + + cout << "vowels: " << vowels << endl; + cout << "consinants: " << consonants << endl; + cout << "others: " << others << endl; + + return 0; +} \ No newline at end of file diff --git a/6#/p8.cpp b/6#/p8.cpp new file mode 100644 index 0000000..644a7bc --- /dev/null +++ b/6#/p8.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +using namespace std; + +int main() +{ + ifstream inFile; + string file_name; + char ch; + int count = 0; + + cout << "Enter the file name: "; + getline(cin, file_name); + inFile.open(file_name); + + if (!inFile.is_open()) + { + cout << "Filed to open the file!" << endl; + exit(EXIT_FAILURE); + } + + while (!inFile.eof()) + { + inFile >> ch; + count++; + } + + cout << file_name << " has " << count << " characters." << endl; + inFile.close(); + + return 0; +} \ No newline at end of file diff --git a/6#/p8.txt b/6#/p8.txt new file mode 100644 index 0000000..5d7e546 --- /dev/null +++ b/6#/p8.txt @@ -0,0 +1,3 @@ +What is your name? +My name is Rick. +I love you, Rick! diff --git a/6#/p9.cpp b/6#/p9.cpp new file mode 100644 index 0000000..df395c0 --- /dev/null +++ b/6#/p9.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include + +using namespace std; + +struct patrons +{ + string name; + double donation; +}; + +int main(void) +{ + ifstream inFile; + string file_name; + + int number; + bool empty = true; + int i = 0; + + cout << "Enter the file name:"; + getline(cin, file_name); + inFile.open(file_name); + if(!inFile.is_open()) + { + cout << "Failed to open file!" << endl; + exit(EXIT_FAILURE); + } + + inFile >> number; + if (number <= 0) + exit(EXIT_FAILURE); + + patrons *ppatrons = new patrons[number]; + inFile.get(); + + while ( (!inFile.eof()) && (i < number)) + { + getline(inFile, ppatrons[i].name); + cout << "Read name: " << ppatrons[i].name << endl; + inFile >> ppatrons[i].donation; + cout << "Donation: " << ppatrons[i].donation << endl; + i++; + inFile.get(); + } + + cout << "*****Grand patrons*****" << endl; + for (int i = 0; i < number; i++) + { + if (ppatrons[i].donation >= 10000) + { + cout << ppatrons[i].name << ": " << ppatrons[i].donation << endl; + empty = false; + } + } + + if (empty == true) + cout << "None" << endl; + + empty = true; + + cout << "*****Patrons*****" << endl; + + for (int i = 0; i < number; i++) + { + if (ppatrons[i].donation < 10000) + { + cout << ppatrons[i].name << ": " << ppatrons[i].donation << endl; + empty = false; + } + } + + if (empty == true) + cout << "None" << endl; + + return 0; +} \ No newline at end of file diff --git a/6#/p9.txt b/6#/p9.txt new file mode 100644 index 0000000..90e6fba --- /dev/null +++ b/6#/p9.txt @@ -0,0 +1,9 @@ +4 +Sam Stone +2000 +Freida Flass +100500 +Tammy Tubbs +5000 +Rich Raptor +55000 diff --git a/6#/scores.txt b/6#/scores.txt new file mode 100644 index 0000000..13848c6 --- /dev/null +++ b/6#/scores.txt @@ -0,0 +1,3 @@ +18 19 18.5 13.5 14 +16 19.5 20 18 12 18.5 +17.5 \ No newline at end of file diff --git a/7#/1.cpp b/7#/1.cpp new file mode 100644 index 0000000..070f3a2 --- /dev/null +++ b/7#/1.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +void simple(void); + +int main() +{ + cout << "main() will call the simple() function: " << endl; + simple(); + + return 0; +} + +void simple(void) +{ + cout << "I'm but a simple function." << endl; +} \ No newline at end of file diff --git a/7#/10.cpp b/7#/10.cpp new file mode 100644 index 0000000..861c96b --- /dev/null +++ b/7#/10.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +char *buildstr(char c, int n); + +int main(void) +{ + char ch; + int times; + + cout << "Enter a character: "; + cin >> ch; + cout << "Enter a interger: "; + cin >> times; + + char *ps = buildstr(ch, times); + cout << ps << endl; + delete []ps; + + return 0; +} + +char *buildstr(char c, int n) +{ + char *pstr = new char[n+1]; + pstr[n] = '\0'; + for (int i = 0; i < n; i++) + pstr[i] = c; + + return pstr; +} diff --git a/7#/11.cpp b/7#/11.cpp new file mode 100644 index 0000000..348c689 --- /dev/null +++ b/7#/11.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +struct travel_time +{ + int hours; + int mins; +}; + +const int Min_per_hr = 60; + +travel_time sum(travel_time t1, travel_time t2); +void show_time(travel_time t); + +int main() +{ + travel_time day1 = {5, 45}; + travel_time day2 = {4, 55}; + + travel_time trip = sum(day1, day2); + cout << "Two days total: "; + show_time(trip); + + travel_time day3 = {4, 32}; + cout << "Three days total: "; + show_time(sum(trip, day3)); + + return 0; +} + +travel_time sum(travel_time t1, travel_time t2) +{ + travel_time total; + + total.mins = (t1.mins + t2.mins) % Min_per_hr; + total.hours = t1.hours + t2.hours + (t1.mins + t2.mins) / Min_per_hr; + + return total; +} + +void show_time(travel_time t) +{ + cout << t.hours << " Hours, " << t.mins << " Mintues" << endl; +} diff --git a/7#/12.cpp b/7#/12.cpp new file mode 100644 index 0000000..4301694 --- /dev/null +++ b/7#/12.cpp @@ -0,0 +1,54 @@ +#include +#include + +using namespace std; + +struct polar +{ + double distance; + double angle; +}; + +struct rect +{ + double x; + double y; +}; + +polar rect_to_polar(rect xypos); +void show_polar(polar dapos); + +int main(void) +{ + rect rplace; + polar pplace; + + cout << "Enter the x and y values: "; + + while (cin >> rplace.x >> rplace.y) + { + pplace = rect_to_polar(rplace); + show_polar(pplace); + cout << "Next two bumber (q to quit): "; + } + + return 0; + +} + +polar rect_to_polar(rect xypos) +{ + polar answer; + answer.distance = sqrt(xypos.x * xypos.x + xypos.y * xypos.y); + answer.angle = atan2(xypos.y, xypos.x); + + return answer; +} + +void show_polar(polar dapos) +{ + const double Rad_to_deg = 57.29577951; + + cout << "Distance = " << dapos.distance << endl; + cout << "Angle = " << dapos.angle * Rad_to_deg << " degree" << endl; +} \ No newline at end of file diff --git a/7#/13.cpp b/7#/13.cpp new file mode 100644 index 0000000..e684531 --- /dev/null +++ b/7#/13.cpp @@ -0,0 +1,51 @@ +#include +#include + +using namespace std; + +struct polar +{ + double distance; + double angle; +}; + +struct rect +{ + double x; + double y; +}; + +void rect_to_polar(const rect *pxy, polar *pda); +void show_polar(const polar *pda); + +int main(void) +{ + rect rplace; + polar pplace; + + cout << "Enter the x and y values: "; + + while (cin >> rplace.x >> rplace.y) + { + rect_to_polar(&rplace, &pplace); + show_polar(&pplace); + cout << "Next two bumber (q to quit): "; + } + + return 0; + +} + +void rect_to_polar(const rect *pxy, polar *pda) +{ + pda->distance = sqrt(pxy->x * pxy->x + pxy->y * pxy->y); + pda->angle = atan2(pxy->y, pxy->x); +} + +void show_polar(const polar *pda) +{ + const double Rad_to_deg = 57.29577951; + + cout << "Distance = " << pda->distance << endl; + cout << "Angle = " << pda->angle * Rad_to_deg << " degree" << endl; +} \ No newline at end of file diff --git a/7#/14.cpp b/7#/14.cpp new file mode 100644 index 0000000..228318f --- /dev/null +++ b/7#/14.cpp @@ -0,0 +1,31 @@ +#include +#include + +using namespace std; + +const int SIZE = 5; + +void display(const string sa[], int n); + +int main(void) +{ + string list[SIZE]; + cout << "Enter " << " favorite food: " << endl; + + for (int i = 0; i < SIZE; i++) + { + cout << i + 1 << ": "; + getline(cin, list[i]); + } + + cout << "Your list: " << endl; + display(list, SIZE); + + return 0; +} + +void display(const string sa[], int n) +{ + for (int i = 0; i < n; i++) + cout << i + 1 << ": " << sa[i] << endl; +} \ No newline at end of file diff --git a/7#/15.cpp b/7#/15.cpp new file mode 100644 index 0000000..2454c7c --- /dev/null +++ b/7#/15.cpp @@ -0,0 +1,45 @@ +#include +#include +#include + +using namespace std; + +const int Seasons = 4; + +const array Snames = {"Spring", "Summer", "Fall", "Winter"}; + +void fill(array *pa); +void show(array da); + +int main(void) +{ + array expenses; + + fill(&expenses); + show(expenses); + + return 0; +} + +void fill(array *pa) +{ + for (int i = 0; i < Seasons; i++) + { + cout << "Enter " << Snames[i] << " expenses: "; + cin >> (*pa)[i]; + } +} + +void show(array da) +{ + double total = 0.0; + cout << "EXPENSES: " << endl; + + for (int i = 0; i < Seasons; i++) + { + cout << Snames[i] << ": $" << da[i] << endl; + total += da[i]; + } + + cout << "Total expenses: " << total << endl; +} diff --git a/7#/16.cpp b/7#/16.cpp new file mode 100644 index 0000000..3adb40a --- /dev/null +++ b/7#/16.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +void countdown(int n); + +int main() +{ + countdown(4); + + return 0; +} + +void countdown(int n) +{ + cout << "Counting down.... " << n << " (n at address: " << &n << " )" << endl; // statement1 + if(n > 0) + countdown(n-1); + cout << n << ": Kadoom" << " (n address: " << &n << " )" << endl; // statement2 +} \ No newline at end of file diff --git a/7#/17.cpp b/7#/17.cpp new file mode 100644 index 0000000..8b0f742 --- /dev/null +++ b/7#/17.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +const int Len = 66; +const int Divs = 6; + +void subdivide(char ar[], int low, int high, int levels); + +int main() +{ + char ruler[Len]; + + for (int i = 0; i < Len; i++) + ruler[i] = ' '; + + int min = 0; + int max = Len - 2; + ruler[Len-1] = '\0'; + ruler[min] = ruler[max] = '|'; + cout << ruler << endl; + + for (int i = 1; i <= Divs; i++) + { + subdivide(ruler, min, max, i); + cout << ruler << endl; + } + + return 0; +} + +void subdivide(char ar[], int low, int high, int levels) +{ + if (levels == 0) + return; + + int mid = (low + high) / 2; + ar[mid] = '|'; + + subdivide(ar, low, mid, levels-1); + subdivide(ar, mid, high, levels-1); +} \ No newline at end of file diff --git a/7#/18.cpp b/7#/18.cpp new file mode 100644 index 0000000..a493bd9 --- /dev/null +++ b/7#/18.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +double Rick(int lines); +double Jack(int lines); +void estimate(int lines, double (*pf)(int)); + +int main() +{ + int code; + + cout << "How many lines of code do you need? "; + cin >> code; + cout << "Here is Rick's estimate: " << endl; + estimate(code, Rick); + cout << "Here is Jack's estimate: " << endl; + estimate(code, Jack); + + return 0; +} + +double Rick(int lines) +{ + return lines * 0.05; +} + +double Jack(int lines) +{ + return 0.03 * lines + 0.0004 * lines * lines; +} + +void estimate(int lines, double (*pf)(int)) +{ + cout << lines << " lines code will take " << (*pf)(lines) << " seconds." << endl; +} \ No newline at end of file diff --git a/7#/19.cpp b/7#/19.cpp new file mode 100644 index 0000000..f8978ab --- /dev/null +++ b/7#/19.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +const double *f1(const double *ar, int n); +const double *f2(const double ar[], int n); +const double *f3(const double ar[], int n); + +int main() +{ + double av[3] = {1112.3, 1542.6, 2227.9}; + + // part1: + // p1(p2): pointer to a function + const double *(*p1)(const double *, int) = f1; + auto p2 = f2; + + cout << "PART1:--------------------------------" << endl; + cout << "Address Value" << endl; + cout << (*p1)(av, 3) << ": " << *(*p1)(av, 3) << endl; // (*p1)(av, 3) == f1(av, 3); + cout << p2(av, 3) << ": " << *p2(av, 3) << endl; // p2(av, 3) == f2(av, 3); + + // part2: + // pa(pb): is an array of pointers + const double *(*pa[3]) (const double *, int) = {f1, f2, f3}; + auto pb = pa; + + cout << "PART2:--------------------------------" << endl; + cout << "Address Value" << endl; + for (int i = 0; i < 3; i++) + cout << pa[i](av, 3) << ": " << *pa[i](av, 3) << endl; + + for (int i = 0; i < 3; i++) + cout << pb[i](av, 3) << ": " << *pb[i](av, 3) << endl; + + // part3: + //pc(pd) is a pointer to an array of funcyion pointers + auto pc = &pa; + const double *(*(*pd)[3])(const double *, int) = &pa; + + cout << "PART1:--------------------------------" << endl; + cout << "Address Value" << endl; + cout << (*pc)[0](av, 3) << ": " << *(*pc)[0](av, 3) << endl; // (*pc) == pa; --> (*pc)[0](av, 3) == pa[0](av, 3); + const double *pdb = (*pd)[1](av, 3); // (*pd)[1](av, 3) == pa[1](av, 3) == f2(av, 3); + cout << pdb << ": " << *pdb << endl; + // cout << (*pd)[2](av, 3) << ": " << *(*pd)[2](av, 3) << endl; + cout << (*(*pd)[2])(av, 3) << ": " << *(*(*pd)[2])(av, 3) << endl; + + return 0; +} + +const double *f1(const double *ar, int n) +{ + return ar; +} + +const double *f2(const double ar[], int n) +{ + return ar + 1; +} + +const double *f3(const double ar[], int n) +{ + return ar + 2; +} \ No newline at end of file diff --git a/7#/1_1.cpp b/7#/1_1.cpp new file mode 100644 index 0000000..070f3a2 --- /dev/null +++ b/7#/1_1.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +void simple(void); + +int main() +{ + cout << "main() will call the simple() function: " << endl; + simple(); + + return 0; +} + +void simple(void) +{ + cout << "I'm but a simple function." << endl; +} \ No newline at end of file diff --git a/7#/2.cpp b/7#/2.cpp new file mode 100644 index 0000000..04a1350 --- /dev/null +++ b/7#/2.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +void cheers(int n); +double cube(double x); + +int main(void) +{ + cheers(5); + + cout << "Give me a number: "; + double side; + cin >> side; + + double volume = cube(side); + cout << side << " side cube = " << volume << endl; + + cheers(cube(2)); + + return 0; +} + +void cheers(int n) +{ + for (int i = 0; i < n; i++) + cout << "Cheers "; + cout << endl; +} + +double cube(double x) +{ + return x * x * x; +} \ No newline at end of file diff --git a/7#/3.cpp b/7#/3.cpp new file mode 100644 index 0000000..5330529 --- /dev/null +++ b/7#/3.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +void n_chars(char c, int n); + +int main() +{ + char ch; + int times; + + cout << "Enter a character: "; + cin >> ch; // cin.get() + + while (ch != 'q') + { + cout << "Enter a integer: "; + cin >> times; + + n_chars(ch, times); + + cout << endl; + cout << "times = " << times << endl; + cout << "Enter another character or presss q_key ro quit: " << endl; + cin >> ch; + } + + return 0; +} + +void n_chars(char c, int n) +{ + while (n-- > 0) + cout << c; +} \ No newline at end of file diff --git a/7#/4.cpp b/7#/4.cpp new file mode 100644 index 0000000..d6820dc --- /dev/null +++ b/7#/4.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +long double probability(unsigned int number, unsigned int picks); + +int main() +{ + unsigned int total, choices; + + cout << "Enter the total number of choices on the game card an the number of picks allowed: " << endl; + while ( (cin >> total >> choices) && (choices <= total)) + { + cout << "You have one chance in " << probability(total, choices) << "of wining." << endl; + cout << "Please enter next to number (q to quit!)" << endl; + } + + return 0; +} + +long double probability(unsigned int number, unsigned int picks) +{ + double n, p; + long double result = 1.0; + + for (n = number, p = picks; p > 0; n--, p--) + result = result * (n / p); + + return result; +} \ No newline at end of file diff --git a/7#/5.cpp b/7#/5.cpp new file mode 100644 index 0000000..4c18ffa --- /dev/null +++ b/7#/5.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +const int ArSize = 8; + +int sum_arr(int arr[], int n); + +int main(void) +{ + int cookies[ArSize] = {1, 2, 4, 8, 16, 32, 64, 128}; + + int sum = sum_arr(cookies, ArSize); + + cout << "Total cookies eaten: " << sum << endl; + + return 0; +} + +int sum_arr(int arr[], int n) +{ + int total = 0; + + for (int i = 0; i < n; i++) + total += arr[i]; + + return total; +} diff --git a/7#/6.cpp b/7#/6.cpp new file mode 100644 index 0000000..b89e3ce --- /dev/null +++ b/7#/6.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +const int ArSize = 8; + +int sum_arr(int arr[], int n); + +int main(void) +{ + int cookies[ArSize] = {1, 2, 4, 8, 16, 32, 64, 128}; + cout << "cookies address: " << cookies << endl; + cout << "size of cookies: " << sizeof cookies << endl; + + int sum = sum_arr(cookies, ArSize); + + cout << "Total cookies eaten: " << sum << endl; + + sum = sum_arr(cookies, 3); + cout << "First three eater ate: " << sum << endl; + sum = sum_arr(cookies + 4, 4); + cout << "Last four eater ate: " << sum << endl; + + return 0; +} + +int sum_arr(int arr[], int n) +{ + int total = 0; + cout << "array address: " << arr << endl; + cout << "size of arr: " << sizeof arr << endl; + + for (int i = 0; i < n; i++) + total += arr[i]; + + return total; +} diff --git a/7#/7.cpp b/7#/7.cpp new file mode 100644 index 0000000..338a140 --- /dev/null +++ b/7#/7.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +const int Max = 5; + +int fill_array(double arr[], int limit); +void show_array(const double arr[], int n); +void revalue(double r, double arr[], int n); + +int main(void) +{ + double properties[Max]; + + int size = fill_array(properties, Max); + show_array(properties, size); + + if (size > 0) + { + cout << "Enter revaluation factor: "; + double factor; + while (!(cin >> factor)) + { + cin.clear(); + while (cin.get() != '\n') continue; + cout << "Bad input: input process terminated." << endl; + break; + } + + revalue(factor, properties, size); + show_array(properties, size); + } + + return 0; +} + +int fill_array(double arr[], int limit) +{ + double temp; + + int i = 0; + for (i = 0; i < Max; i++) + { + cout << "Enter value #" << i + 1 << ": "; + cin >> temp; + if (!cin) + { + cin.clear(); + while (cin.get() != '\n') continue; + cout << "Bad input: input process terminated." << endl; + } + else if (temp < 0) + break; + else + arr[i] = temp; + } + + return i; +} + +void show_array(const double arr[], int n) +{ + for (int i = 0; i < n; i++) + { + cout << "Property #" << i + 1 << ": $"; + cout << arr[i] << endl; + } +} + +void revalue(double r, double arr[], int n) +{ + for (int i = 0; i < n; i++) + arr[i] = arr[i] * r; +} diff --git a/7#/8.cpp b/7#/8.cpp new file mode 100644 index 0000000..38778ac --- /dev/null +++ b/7#/8.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +const int ArSize = 8; + +int sum_arr(const int *begin, const int *end); + +int main(void) +{ + int cookies[ArSize] = {1, 2, 4, 8, 16, 32, 64, 128}; + cout << "cookies address: " << cookies << endl; + cout << "size of cookies: " << sizeof cookies << endl; + + int sum = sum_arr(cookies, cookies + ArSize); + + cout << "Total cookies eaten: " << sum << endl; + + sum = sum_arr(cookies, cookies + 3); + + cout << "First three eater ate: " << sum << endl; + sum = sum_arr(cookies + 4, cookies + 8); + cout << "Last four eater ate: " << sum << endl; + + return 0; +} + +int sum_arr(const int *begin, const int *end) +{ + int total = 0; + const int *pt; + + for (pt = begin; pt != end; pt++) + total += *pt; + + return total; +} diff --git a/7#/8_1.cpp b/7#/8_1.cpp new file mode 100644 index 0000000..1167808 --- /dev/null +++ b/7#/8_1.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +const int ArSize = 8; + +int sum_arr(const int *begin, const int *end); + +int main(void) +{ + const int cookies[ArSize] = {1, 2, 4, 8, 16, 32, 64, 128}; + cout << "cookies address: " << cookies << endl; + cout << "size of cookies: " << sizeof cookies << endl; + + int sum = sum_arr(cookies, cookies + ArSize); + + cout << "Total cookies eaten: " << sum << endl; + + sum = sum_arr(cookies, cookies + 3); + + cout << "First three eater ate: " << sum << endl; + sum = sum_arr(cookies + 4, cookies + 8); + cout << "Last four eater ate: " << sum << endl; + + return 0; +} + +int sum_arr(const int *begin, const int *end) +{ + int total = 0; + const int *pt; + + for (pt = begin; pt != end; pt++) + total += *pt; + + return total; +} diff --git a/7#/9.cpp b/7#/9.cpp new file mode 100644 index 0000000..dbe3200 --- /dev/null +++ b/7#/9.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +unsigned int c_in_str(const char *str, char ch); + +int main(void) +{ + char mmm[15] = "minimum"; + const char *wail = "ululate"; // char *wail = (char *)"ululate"; + + unsigned int ms = c_in_str(mmm, 'm'); + unsigned int us = c_in_str(wail, 'u'); + + cout << ms << " m characters in" << mmm << endl; + cout << us << " u characters in" << wail << endl; + + return 0; +} + +unsigned int c_in_str(const char *str, char ch) +{ + unsigned int count = 0; + + while (*str) + { + if (*str == ch) + count++; + str++; + } + + return count; +} diff --git a/7#/const.cpp b/7#/const.cpp new file mode 100644 index 0000000..76e28e9 --- /dev/null +++ b/7#/const.cpp @@ -0,0 +1,24 @@ +// const int *pt; +// int *const pt; +// const int *const pt; + +#include + +using namespace std; + +int main(void) +{ + int n = 10; + int m = 100; + const int *pt = &n; + + cout << "1):n = " << n << endl; + // *pt = 20; + // n = 100; + pt = &m; + cout << "2):n = " << n << endl; + cout << "*pt = " << *pt << endl; + + return 0; +} + diff --git a/7#/const_1.cpp b/7#/const_1.cpp new file mode 100644 index 0000000..38a7152 --- /dev/null +++ b/7#/const_1.cpp @@ -0,0 +1,24 @@ +// const int *pt; == int const *pt; +// int *const pt; +// const int *const pt; + +#include + +using namespace std; + +int main(void) +{ + int n = 10; + int m = 100; + int *const pt = &n; + + cout << "1):n = " << n << endl; + *pt = 20; + cout << "2):n = " << n << endl; + + pt = &m; + // cout << "*pt = " << *pt << endl; + + return 0; +} + diff --git a/7#/p1.cpp b/7#/p1.cpp new file mode 100644 index 0000000..6bd603d --- /dev/null +++ b/7#/p1.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +double harmean(double x, double y); + +int main(void) +{ + double n1, n2, result; + + cout << "Please enter two numbers, until one of them is zero: "; + cin >> n1 >> n2; + + while ( (n1 != 0) && (n2 != 0)) + { + result = harmean(n1, n2); + cout << "The harmean is :" << result << endl; + cout << "Please enter two numbers, until one of them is zero: "; + cin >> n1 >> n2; + } + + return 0; +} + +double harmean(double x, double y) +{ + return 2.0 * x * y / (x + y); +} \ No newline at end of file diff --git a/7#/p10.cpp b/7#/p10.cpp new file mode 100644 index 0000000..b3dbcc6 --- /dev/null +++ b/7#/p10.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +double add (double x, double y); +double subtract(double x, double y); +double calculate(double x, double y, double (*pt)(double x, double y)); + +int main() +{ + double result = calculate(2.5, 10.4, add); + cout << "The result of add is: " << result << endl; + + result = calculate(2.5, 10.4, subtract); + cout << "The result of substract is: " << result << endl; + + return 0; +} + +double add (double x, double y) +{ + return x, y; +} + +double subtract(double x, double y) +{ + return x - y; +} + +double calculate(double x, double y, double (*pt)(double x, double y)) +{ + return (*pt)(x, y); +} \ No newline at end of file diff --git a/7#/p2.cpp b/7#/p2.cpp new file mode 100644 index 0000000..1426ceb --- /dev/null +++ b/7#/p2.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +const int MAX = 10; + +int fill_golf(double ar[], int n); +void show_golf(const double ar[], int n); +double average_golf(const double ar[], int n); + +int main() +{ + double golf[MAX]; + + int size = fill_golf(golf, MAX); + + if (size > 0) + { + show_golf(golf, size); + cout << "Average: " << average_golf(golf, size) << endl; + } + else + cout << "There is no scores!" << endl; + + return 0; +} + +int fill_golf(double ar[], int n) +{ + double temp; + int i; + + for (i = 0; i < MAX; i++) + { + cout << "Enter golf score, NO." << i + 1 << ": "; + cin >> temp; + if (!cin) + { + cin.clear(); + while (cin.get() != '\n'); + cout << "Invalid input, terminate." << endl; + break; + } + else if (temp < 0) + break; + else + ar[i] = temp; + } + + return i; +} + +void show_golf(const double ar[], int n) +{ + cout << "golf result: "; + for (int i = 0; i < n; i++) + cout << ar[i] << " "; + + cout << endl; +} + +double average_golf(const double ar[], int n) +{ + double average, sum = 0; + + for (int i = 0; i < n; i++) + sum += ar[i]; + + average = sum / n; + + return average; +} \ No newline at end of file diff --git a/7#/p3.cpp b/7#/p3.cpp new file mode 100644 index 0000000..991b508 --- /dev/null +++ b/7#/p3.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +struct box +{ + char maker[40]; + float height; + float width; + float length; + float volume; +}; + +void set_box(box *pb); +void show(box b); + +int main() +{ + box b = {"cube", 3, 4, 5}; + + set_box(&b); + show(b); + + return 0; +} + +void set_box(box *pb) +{ + pb->volume = pb->height * pb->width * pb->length; +} + +void show(box b) +{ + cout << "maker: " << b.maker << endl; + cout << "heigth: " << b.height << endl; + cout << "width: " << b.width << endl; + cout << "length: " << b.length << endl; + cout << "volume: " << b.volume << endl; +} diff --git a/7#/p4.cpp b/7#/p4.cpp new file mode 100644 index 0000000..8d1a9b6 --- /dev/null +++ b/7#/p4.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +long double probability(unsigned int number, unsigned int picks); + +int main() +{ + long double field = probability(47, 5); + long double special = probability(27, 1); + long double result = field * special; + cout << result << endl; + + return 0; +} + +long double probability(unsigned int number, unsigned int picks) +{ + double n, p; + long double result = 1.0; + + for (n = number, p = picks; p > 0; n--, p--) + result = result * (n / p); + + return result; +} \ No newline at end of file diff --git a/7#/p5.cpp b/7#/p5.cpp new file mode 100644 index 0000000..348482b --- /dev/null +++ b/7#/p5.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +long long factorial(unsigned int n); + +int main() +{ + int number; + long long result; + + cout << "Enter a number for factorial: "; + while (cin >> number) + { + result = factorial(number); + cout << number << "! = " << result << endl; + cout << "Enter a number for factorial: "; + } + + return 0; +} + +long long factorial(unsigned int n) +{ + if (n == 0) + return 1; + else + return n * factorial(n-1); +} \ No newline at end of file diff --git a/7#/p6.cpp b/7#/p6.cpp new file mode 100644 index 0000000..5b4e7db --- /dev/null +++ b/7#/p6.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +const int MAX = 40; + +int Fill_array(double arr[], int size); +void Show_array(double arr[], int size); +void Reverse_array(double arr[], int size); + +int main() +{ + double array[MAX]; + + int size = Fill_array(array, MAX); + Show_array(array, size); + Reverse_array(array, size); + Show_array(array, size); + + Reverse_array(&array[1], size - 2); + Show_array(array, size); + + return 0; +} + +int Fill_array(double arr[], int size) +{ + double temp; + int i; + + for (i = 0; i < size; i++) + { + cout << "Please enter a number: "; + cin >> temp; + if(!cin) + { + cin.clear(); + while (cin.get() != '\n'); + break; + } + else + arr[i] = temp; + } + + return i; +} + +void Show_array(double arr[], int size) +{ + cout << "The array count: " << endl; + for (int i = 0; i < size; i++) + cout << arr[i] << " "; + + cout << endl; +} + +void Reverse_array(double arr[], int size) +{ + double temp; + + for (int i = 0; i < (size / 2); i++) + { + temp = arr[i]; + arr[i] = arr[size - i - 1]; + arr[size - i - 1] = temp; + } +} \ No newline at end of file diff --git a/7#/p7.cpp b/7#/p7.cpp new file mode 100644 index 0000000..e099954 --- /dev/null +++ b/7#/p7.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +const int Max = 5; + +double *fill_array(double *beg, double *end); +void show_array(double *beg, double *end); +void revalue(double r, double *beg, double *end); + +int main(void) +{ + double properties[Max]; + + double *pa = fill_array(properties, properties + Max); + show_array(properties, pa); + + if ((pa - properties) > 0) + { + cout << "Enter revaluation factor: "; + double factor; + while (!(cin >> factor)) + { + cin.clear(); + while (cin.get() != '\n') continue; + cout << "Bad input: input process terminated." << endl; + } + + revalue(factor, properties, pa); + show_array(properties, pa); + } + + return 0; +} + +double *fill_array(double *beg, double *end) +{ + double temp; + double *pt; + + for (pt = beg; pt != end; pt++) + { + // cout << "pt: " << pt << endl; + // cout << "beg: " << beg << endl; + // cout << "pt - beg: " << pt - beg << endl; + cout << "Enter value #" << (pt - beg) + 1 << ": "; + cin >> temp; + if (!cin) + { + cin.clear(); + while (cin.get() != '\n') continue; + cout << "Bad input: input process terminated." << endl; + break; + } + else if (temp < 0) + break; + else + *pt = temp; + } + + return pt; +} + +void show_array(double *beg, double *end) +{ + for (double *pt = beg; pt != end; pt++) + { + cout << "Property #" << (pt - beg) + 1 << ": $"; + cout << *pt << endl; + } +} + +void revalue(double r, double *beg, double *end) +{ + for (double *pt = beg; pt != end; pt++) + *pt = *pt * r; +} diff --git a/7#/p8-a.cpp b/7#/p8-a.cpp new file mode 100644 index 0000000..1815cdf --- /dev/null +++ b/7#/p8-a.cpp @@ -0,0 +1,45 @@ +#include +#include +#include + +using namespace std; + +const int Seasons = 4; + +const char *Snames[] = {"Spring", "Summer", "Fall", "Winter"}; + +void fill(double arr[], int size); +void show(const double arr[], int size); + +int main(void) +{ + double expenses[Seasons]; + + fill(expenses, Seasons); + show(expenses, Seasons); + + return 0; +} + +void fill(double arr[], int size) +{ + for (int i = 0; i < size; i++) + { + cout << "Enter " << Snames[i] << " expenses: "; + cin >> arr[i]; + } +} + +void show(const double arr[], int size) +{ + double total = 0.0; + cout << "EXPENSES: " << endl; + + for (int i = 0; i < size; i++) + { + cout << Snames[i] << ": $" << arr[i] << endl; + total += arr[i]; + } + + cout << "Total expenses: " << total << endl; +} diff --git a/7#/p8-b.cpp b/7#/p8-b.cpp new file mode 100644 index 0000000..39b7e13 --- /dev/null +++ b/7#/p8-b.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + +using namespace std; + +const int Seasons = 4; + +const char *Snames[] = {"Spring", "Summer", "Fall", "Winter"}; + +struct Spend +{ + double money[Seasons]; +}; + +void fill(double arr[], int size); +void show(const double arr[], int size); + +int main(void) +{ + Spend expenses; + + fill(expenses.money, Seasons); + show(expenses.money, Seasons); + + return 0; +} + +void fill(double arr[], int size) +{ + for (int i = 0; i < size; i++) + { + cout << "Enter " << Snames[i] << " expenses: "; + cin >> arr[i]; + } +} + +void show(const double arr[], int size) +{ + double total = 0.0; + cout << "EXPENSES: " << endl; + + for (int i = 0; i < size; i++) + { + cout << Snames[i] << ": $" << arr[i] << endl; + total += arr[i]; + } + + cout << "Total expenses: " << total << endl; +} diff --git a/7#/p9.cpp b/7#/p9.cpp new file mode 100644 index 0000000..c475ce3 --- /dev/null +++ b/7#/p9.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +const int SLEN = 30; + +struct student +{ + char fullname[SLEN]; + char hobby[SLEN]; + int ooplevel; +}; + +int getinfo(student pa[], int n); +void display1(student st); +void display2(const student *ps); +void display3(const student pa[], int n); + +int main() +{ + 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 0; + + + + return 0; +} + +int getinfo(student pa[], int n) +{ + int i; + + for (i = 0; i < n; i++) + { + cout << "Enter the name of a student: "; + cin >> pa[i].fullname; + cout << "Enter the hobbyy of a student: "; + cin >> pa[i].hobby; + cout << "Enter the level of a student: "; + cin >> pa[i].ooplevel; + + if (!cin) + { + cin.clear(); + while (cin.get() != '\n'); + break; + } + } + + return i; +} + +void display1(student st) +{ + cout << "Name: " << st.fullname << endl; + cout << "Hobby: " << st.hobby << endl; + cout << "Level: " << st.ooplevel << endl; +} + +void display2(const student *ps) +{ + cout << "Name: " << ps->fullname << endl; + cout << "Hobby: " << ps->hobby << endl; + cout << "Level: " << ps->ooplevel << endl; +} + +void display3(const student pa[], int n) +{ + for (int i = 0; i < n; i++) + { + cout << "Name: " << pa[i].fullname << endl; + cout << "Hobby: " << pa[i].hobby << endl; + cout << "Level: " << pa[i].ooplevel << endl; + } +} \ No newline at end of file diff --git a/8#/1.cpp b/8#/1.cpp new file mode 100644 index 0000000..68b9dfd --- /dev/null +++ b/8#/1.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +inline double square(double x) {return x * x;} + +int main() +{ + double a, b; + double c = 13.0; + + a = square(5.0); + b = square(4.5 + 7.5); + + cout << "a = " << a << " b = " << b << endl; + + cout << "c = " << c << endl; + cout << "c square = " << square(c++) << endl; + cout << "Now c = " << c << endl; + + return 0; +} \ No newline at end of file diff --git a/8#/10.cpp b/8#/10.cpp new file mode 100644 index 0000000..5b7433b --- /dev/null +++ b/8#/10.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +const int ArSize = 80; + +char *left(const char *str, int n = 1); +unsigned long left(unsigned long num, unsigned int ct); + +int main() +{ + const char *trip = "Hawaii"; + unsigned long n = 12345678; + char *temp; + int i; + + for (i = 0; i < 10; i++) + { + cout << left(n, i) << endl; + temp = left(trip, i); + cout << temp << endl; + delete []temp; + } + + + return 0; +} + +char *left(const char *str, int n) +{ + int m = 0; + while (m < n && str[m] != '\0') + m++; + + char *p = new char[n+1]; + + int i; + for(i = 0; i < m; i++) + p[i] = str[i]; + + p[i] = '\0'; + + return p; +} + +unsigned long left(unsigned long num, unsigned int ct) +{ + unsigned long n = num; + unsigned int digits = 1; + + if(num == 0 || ct == 0) + return 0; + + while (n /= 10) + digits++; + + if(digits > ct) + { + ct = digits - ct; + while (ct--) + num /= 10; + + return num; + } + else + return num; +} \ No newline at end of file diff --git a/8#/11.cpp b/8#/11.cpp new file mode 100644 index 0000000..c8228c3 --- /dev/null +++ b/8#/11.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +template +void Swap(T&a, T&b); + +int main() +{ + int i = 10; + int j = 20; + + cout << "i, j = " << i << ", " << j << ". " << endl; + Swap(i, j); + cout << "After swap, now i, j = " << i << ", " << j << ". " << endl; + + double x = 24.5; + double y = 81.7; + cout << "x, y = " << x << ", " << y << ". " << endl; + Swap(x, y); + cout << "After swap, now x, y = " << x << ", " << y << ". " << endl; + + return 0; +} + +template +void Swap(T&a, T&b) +{ + T temp; + temp = a; + a = b; + b = temp; +} \ No newline at end of file diff --git a/8#/12.cpp b/8#/12.cpp new file mode 100644 index 0000000..3a9ef53 --- /dev/null +++ b/8#/12.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +const int LIM = 8; + +template +void Swap(T&a, T&b); + +void show(int arr[], int n); + +template +void Swap(T a[], T b[], int n); + + +int main() +{ + int i = 10; + int j = 20; + + cout << "i, j = " << i << ", " << j << ". " << endl; + Swap(i, j); + cout << "After swap, now i, j = " << i << ", " << j << ". " << endl; + + int d1[LIM] = {0, 7, 0, 4, 1, 7, 7, 6}; + int d2[LIM] = {0, 7, 2, 0, 1, 9, 6, 9}; + cout << "Original arrays: " << endl; + show(d1, LIM); + show(d2, LIM); + + Swap(d1, d2, LIM); + cout << "Swapped arrays: "<< endl; + show(d1, LIM); + show(d2, LIM); + + return 0; +} + +template +void Swap(T&a, T&b) +{ + T temp; + temp = a; + a = b; + b = temp; +} + +template +void Swap(T a[], T b[], int n) +{ + T temp; + for (int i = 0; i < n; i++) + { + temp = a[i]; + a[i] = b[i]; + b[i] = temp; + } + +} + +void show(int arr[], int n) +{ + for (int i = 0; i < n; i++) + cout << arr[i] << " "; + + cout << endl; +} \ No newline at end of file diff --git a/8#/13.cpp b/8#/13.cpp new file mode 100644 index 0000000..c0071e9 --- /dev/null +++ b/8#/13.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +struct job +{ + char name[40]; + double salary; + int floor; +}; + +template +void Swap(T&a, T&b); + +template <> +void Swap(job &j1, job &j2); + +void show(job &j); + +int main() +{ + int i = 10; + int j = 20; + + cout << "i, j = " << i << ", " << j << ". " << endl; + Swap(i, j); + cout << "After swap, now i, j = " << i << ", " << j << ". " << endl; + + double x = 24.5; + double y = 81.7; + cout << "x, y = " << x << ", " << y << ". " << endl; + Swap(x, y); + cout << "After swap, now x, y = " << x << ", " << y << ". " << endl; + + job Rick = {"Rick", 1000, 10}; + job Jack = {"Jack", 1100, 11}; + show(Rick); + show(Jack); + Swap(Rick, Jack); + cout << "After swap: " << endl; + show(Rick); + show(Jack); + + return 0; +} + +template +void Swap(T&a, T&b) +{ + T temp; + temp = a; + a = b; + b = temp; +} + +template <> +void Swap(job &j1, job &j2) +{ + double t1; + int t2; + + t1 = j1.salary; + j1.salary = j2.salary; + j2.salary = t1; + + t2 = j1.floor; + j1.floor = j2.floor; + j2.floor = t2; +} + +void show(job &j) +{ + cout << j.name << ": " << j.salary << " on floor " << j.floor << endl; +} \ No newline at end of file diff --git a/8#/13_1.cpp b/8#/13_1.cpp new file mode 100644 index 0000000..f208802 --- /dev/null +++ b/8#/13_1.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +struct job +{ + char name[40]; + double salary; + int floor; +}; + +template +void Swap(T&a, T&b); + +template <> +void Swap(job &j1, job &j2); + +void Swap(int &a, int &b); + +void show(job &j); + +int main() +{ + int i = 10; + int j = 20; + + cout << "i, j = " << i << ", " << j << ". " << endl; + Swap(i, j); + cout << "After swap, now i, j = " << i << ", " << j << ". " << endl; + + double x = 24.5; + double y = 81.7; + cout << "x, y = " << x << ", " << y << ". " << endl; + Swap(x, y); + cout << "After swap, now x, y = " << x << ", " << y << ". " << endl; + + job Rick = {"Rick", 1000, 10}; + job Jack = {"Jack", 1100, 11}; + show(Rick); + show(Jack); + Swap(Rick, Jack); + cout << "After swap: " << endl; + show(Rick); + show(Jack); + + return 0; +} + +void Swap(int &a, int &b) +{ + int temp; + temp = a; + a = b; + b = temp; + + cout << "Hello world, a = " << a << ", b = " << b << endl; +} + +template +void Swap(T&a, T&b) +{ + T temp; + temp = a; + a = b; + b = temp; +} + +template <> +void Swap(job &j1, job &j2) +{ + double t1; + int t2; + + t1 = j1.salary; + j1.salary = j2.salary; + j2.salary = t1; + + t2 = j1.floor; + j1.floor = j2.floor; + j2.floor = t2; +} + +void show(job &j) +{ + cout << j.name << ": " << j.salary << " on floor " << j.floor << endl; +} \ No newline at end of file diff --git a/8#/14.cpp b/8#/14.cpp new file mode 100644 index 0000000..17155e3 --- /dev/null +++ b/8#/14.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +struct debts +{ + char name[50]; + double amount; +}; + +template +void ShowArray(T arr[], int n); + +template +void ShowArray(T *arr[], int n); + +int main() +{ + int things[6] = {13, 31, 103, 301, 310, 130}; + + ShowArray(things, 6); + + debts mr_E[3] = + { + {"Rick", 2400.0}, + {"Jack", 1300.0}, + {"Rose", 1800.0} + }; + + double *pd[3]; + + for (int i = 0; i < 3; i++) + pd[i] = &mr_E[i].amount; + + ShowArray(things, 6); + + ShowArray(pd, 3); + + + return 0; +} + +template +void ShowArray(T arr[], int n) +{ + cout << "template A: " << endl; + + for (int i = 0; i < n; i++) + cout << arr[i] << " "; + + cout << endl; + +} + +template +void ShowArray(T *arr[], int n) +{ + cout << "template B: " << endl; + + for (int i = 0; i < n; i++) + cout << *arr[i] << " "; + + cout << endl; +} \ No newline at end of file diff --git a/8#/15.cpp b/8#/15.cpp new file mode 100644 index 0000000..ff2961c --- /dev/null +++ b/8#/15.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +template +T lesser(T a, T b) +{ + return a < b ? a : b; +} + +template +auto add(T1 a, T2 b) -> decltype(a + b) +{ + return a + b; +} + +template +void sum(T1 a, T2 b) +{ + decltype(a+b) ab = a + b; + cout << ab << endl; +} + +int lesser(int a, int b) +{ + a = a < 0 ? -a : a; + b = b < 0 ? -b : b; + + return a < b ? a : b; +} + +int main() +{ + int m = 20; + int n = -30; + + double x = 15.5; + double y = 25.9; + + cout << lesser(m, n) << endl; + cout << lesser(x, y) << endl; + + cout << lesser<>(m, n) << endl; + cout << lesser(x, y) << endl; + + cout << add(m, x) << endl; + sum(m, x); + + return 0; +} \ No newline at end of file diff --git a/8#/2.cpp b/8#/2.cpp new file mode 100644 index 0000000..db8b1bb --- /dev/null +++ b/8#/2.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main() +{ + int rats = 10; + int &rodents = rats; + + + cout << "rats = " << rats << endl; + cout << "rodents = " << rodents << endl; + + rodents++; + cout << "rats = " << rats << endl; + cout << "rodents = " << rodents << endl; + + cout << "&rats = " << &rats << endl; + cout << "&rodents = " << &rodents << endl; + + return 0; +} \ No newline at end of file diff --git a/8#/3.cpp b/8#/3.cpp new file mode 100644 index 0000000..08387db --- /dev/null +++ b/8#/3.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int main() +{ + int rats = 101; + int &rodents = rats; + + + cout << "rats = " << rats << endl; + cout << "rodents = " << rodents << endl; + + cout << "&rats = " << &rats << endl; + cout << "&rodents = " << &rodents << endl; + + int bunnies = 50; + rodents = bunnies; + + cout << "bunnies = " << bunnies << endl; + cout << "rodents = " << rodents << endl; + cout << "rats = " << rats << endl; + + cout << "&bunnies = " << &bunnies << endl; + cout << "&rodents = " << &rodents << endl; + + + return 0; +} \ No newline at end of file diff --git a/8#/4.cpp b/8#/4.cpp new file mode 100644 index 0000000..18043eb --- /dev/null +++ b/8#/4.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +void swap(int *pa, int *pb); +void swap(int &a, int &b); +void swapv(int a, int b); + +int main() +{ + int wallet1 = 300; + int wallet2 = 350; + + cout << "wallet1 = " << wallet1 << endl; + cout << "wallet2 = " << wallet2 << endl; + + cout << "Using reference to swap contents: " << endl; + swap(wallet1, wallet2); + cout << "wallet1 = " << wallet1 << endl; + cout << "wallet2 = " << wallet2 << endl; + + cout << "Using pointers to swap contents: " << endl; + swap(&wallet1, &wallet2); + cout << "wallet1 = " << wallet1 << endl; + cout << "wallet2 = " << wallet2 << endl; + + cout << "Trying to use passing by value: " << endl; + swapv(wallet1, wallet2); + cout << "wallet1 = " << wallet1 << endl; + cout << "wallet2 = " << wallet2 << endl; + + + return 0; +} + +void swap(int &a, int &b) +{ + int temp; + + temp = a; + a = b; + b = temp; +} + +void swap(int *pa, int *pb) +{ + int temp; + + temp = *pa; + *pa = *pb; + *pb = temp; +} + +void swapv(int a, int b) +{ + int temp; + + temp = a; + a = b; + b = temp; +} \ No newline at end of file diff --git a/8#/5.cpp b/8#/5.cpp new file mode 100644 index 0000000..f510f87 --- /dev/null +++ b/8#/5.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +double cube(double a); +double recube(double &ra); + +int main() +{ + double x = 3.0; + + cout << cube(x) << " = cube of " << x << endl; + + cout << recube(x) << " = cube of " << x << endl; + return 0; +} + +double cube(double a) +{ + a *= a * a; + + return a; +} + +double recube(double &ra) +{ + ra *= ra * ra; + + return ra; +} \ No newline at end of file diff --git a/8#/5_1.cpp b/8#/5_1.cpp new file mode 100644 index 0000000..e6d3d5a --- /dev/null +++ b/8#/5_1.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +double cube(double a); +double recube(const double &ra); + +int main() +{ + double x = 3.0; + + cout << cube(5.0 + x) << " = cube of " << "5.0 + x" << endl; + + cout << recube(5.0) << " = cube of " << "5.0" << endl; + return 0; +} + +double cube(double a) +{ + a *= a * a; + + return a; +} + +double recube(const double &ra) +{ + return ra * ra * ra; +} \ No newline at end of file diff --git a/8#/6.cpp b/8#/6.cpp new file mode 100644 index 0000000..d52c4ea --- /dev/null +++ b/8#/6.cpp @@ -0,0 +1,71 @@ +#include +#include + +using namespace std; + +struct free_throws +{ + string name; + int made; + int attempts; + float percent; +}; + +void set_pc(free_throws &ft); +void display(const free_throws &ft); +free_throws &accumulate(free_throws &target, const free_throws &source); + +int main() +{ + free_throws one = {"Rick", 13, 14}; + free_throws two = {"Jack" , 10, 16}; + free_throws three = {"Jerry", 7, 9}; + free_throws four = {"Jason" , 5, 9}; + free_throws five = {"Micheal", 6, 14}; + free_throws team = {"Class 6" , 0, 0}; + + free_throws dup; + + set_pc(one); + display(one); + accumulate(team, one); + display(team); + + display(accumulate(team, two)); + + accumulate(accumulate(team, three), four); + display(team); + + dup = accumulate(team, five); + cout << "Display team: " << endl; + display(dup); + + return 0; +} + +void set_pc(free_throws &ft) +{ + if(ft.attempts != 0) + ft.percent = 100.0 * (float)ft.made / (float)ft.attempts; + else + ft.percent = 0; +} + +void display(const free_throws &ft) +{ + cout << "Name: " << ft.name << endl; + cout << "Made: " << ft.made << '\t'; + cout << "Attempts: " << ft.attempts << '\t'; + cout << "Percent: " << ft.percent << '\t'; + + cout << endl; +} + +free_throws &accumulate(free_throws &target, const free_throws &source) +{ + target.attempts += source.attempts; + target.made += source.made; + set_pc(target); + + return target; +} \ No newline at end of file diff --git a/8#/7.cpp b/8#/7.cpp new file mode 100644 index 0000000..80e56ca --- /dev/null +++ b/8#/7.cpp @@ -0,0 +1,63 @@ +#include +#include + +using namespace std; + +string version1(const string &s1, const string &s2); +const string &version2(string &s1, const string &s2); +const string &version3(string &s1, const string &s2); + +int main() +{ + string input; + string result; + string copy; + + cout << "Enter a string: "; + getline(cin, input); + copy = input; + + cout << "Your string as entered: " << input << endl; + result = version1(input, "****"); + cout << "Your string enhanced: " << result << endl; + cout << "Your original string: " << input << endl; + + cout << "--------------------------------------" << endl; + + result = version2(input, "###"); + cout << "Your string enhanced: " << result << endl; + cout << "Your original string: " << input << endl; + + cout << "--------------------------------------" << endl; + + input = copy; + result = version3(input, "@@@"); + cout << "Your string enhanced: " << result << endl; + cout << "Your original string: " << input << endl; + + + return 0; +} + +string version1(const string &s1, const string &s2) +{ + string temp; + temp = s2 + s1 + s2; + + return temp; +} + +const string &version2(string &s1, const string &s2) +{ + s1 = s2 + s1 + s2; + + return s1; +} + +const string &version3(string &s1, const string &s2) +{ + string temp; + temp = s2 + s1 + s2; + + return temp; +} \ No newline at end of file diff --git a/8#/8.cpp b/8#/8.cpp new file mode 100644 index 0000000..8b6e3dd --- /dev/null +++ b/8#/8.cpp @@ -0,0 +1,51 @@ +#include +#include +#include + +using namespace std; + +const int LIMIT = 5; + +void file_it(ostream &os, double fo, const double fe[], int n); + +int main() +{ + fstream fout; + const char *fn = "ep-data.txt"; + + fout.open(fn); + if(!fout.is_open()) + { + cout << "Can't open " << fn << ". Bye." << endl; + exit(EXIT_FAILURE); + } + + double objective; + cout << "Enter the focal length of your telescope objective in mm: "; + cin >> objective; + + double eps[LIMIT]; + for(int i = 0; i< LIMIT; i++) + { + cout << "EyePieces #" << i + 1 << ": "; + cin >> eps[i]; + } + + file_it(cout, objective, eps, LIMIT); + file_it(fout, objective, eps, LIMIT); + + cout << "Done." << endl; + + return 0; +} + + +void file_it(ostream &os, double fo, const double fe[], int n) +{ + os << "Focal length of objective: " << fo << endl; + os << "f.1. eyepieces" << " magnification" << endl; + for(int i = 0; i < n; i++) + { + os << " " << fe[i] << " " << int(fo/fe[i] + 0.5) << endl; + } +} \ No newline at end of file diff --git a/8#/9.cpp b/8#/9.cpp new file mode 100644 index 0000000..e8c5336 --- /dev/null +++ b/8#/9.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +const int ArSize = 80; + +char *left(const char *str, int n = 1); + +int main() +{ + char sample[ArSize]; + + cout << "Enter a string: " << endl; + cin.get(sample, ArSize); + + char *ps = left(sample, 400); + cout << ps << endl; + delete []ps; + + ps = left(sample); + cout << ps << endl; + delete []ps; + + return 0; +} + +/* +char *left(const char *str, int n) +{ + if(n < 0) + n = 0; + + char *p = new char[n+1]; + int i; + for(i = 0; i < n && str[i]; i++) + p[i] = str[i]; + + while (i <= n) + p[i++] = '\0'; + + return p; +} +*/ + +char *left(const char *str, int n) +{ + int m = 0; + while (m < n && str[m] != '\0') + m++; + + char *p = new char[n+1]; + + int i; + for(i = 0; i < m; i++) + p[i] = str[i]; + + p[i] = '\0'; + + return p; +} \ No newline at end of file diff --git a/8#/ep-data.txt b/8#/ep-data.txt new file mode 100644 index 0000000..ab4cb8b --- /dev/null +++ b/8#/ep-data.txt @@ -0,0 +1,7 @@ +Focal length of objective: 1800 +f.1. eyepieces magnification + 30 60 + 19 95 + 14 129 + 8.8 205 + 7.5 240 diff --git a/8#/p1.cpp b/8#/p1.cpp new file mode 100644 index 0000000..ab47100 --- /dev/null +++ b/8#/p1.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +void show(const char *str, int n = 0); + +int main() +{ + show("Hello World!"); + show("Good morning"); + show("I love you, Rick!", 8); + + return 0; +} + +void show(const char *str, int n) +{ + static int num = 0; + num++; + + if (n == 0) + cout << str << endl; + else + { + for (int i = 0; i < num; i++) + cout << str << endl; + } +} \ No newline at end of file diff --git a/8#/p2.cpp b/8#/p2.cpp new file mode 100644 index 0000000..6e68d4f --- /dev/null +++ b/8#/p2.cpp @@ -0,0 +1,40 @@ +#include +#include + +using namespace std; + +const int SIZE = 30; + +struct CandyBar +{ + char brand[SIZE]; + double weight; + int heat; +}; + +void set_value(CandyBar &candybar, const char *str = "Mill Much", const double w = 2.85, const int h = 350); +void show(const CandyBar &candybar); + +int main() +{ + CandyBar bar; + + set_value(bar); + show(bar); + + return 0; +} + +void set_value(CandyBar &candybar, const char *str, const double w, const int h) +{ + strcpy(candybar.brand, str); + candybar.weight = w; + candybar.heat = h; +} + +void show(const CandyBar &candybar) +{ + cout << "Brand: " << candybar.brand << endl; + cout << "Weight: " << candybar.weight << endl; + cout << "Heat: " << candybar.heat << endl; +} \ No newline at end of file diff --git a/8#/p3.cpp b/8#/p3.cpp new file mode 100644 index 0000000..57522c6 --- /dev/null +++ b/8#/p3.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +using namespace std; + +void to_Upper(string &str); + +int main() +{ + string str; + cout << "Enter a string(q to quit): "; + getline(cin, str); + + while (str != "q") + { + to_Upper(str); + cout << str << endl; + cout << "Enter a string(q to quit): "; + getline(cin, str); + } + + cout << "Bye" << endl; + + return 0; +} + +void to_Upper(string &str) +{ + for (size_t i = 0; i < str.size(); i++) + str[i] = toupper(str[i]); +} \ No newline at end of file diff --git a/8#/p4.cpp b/8#/p4.cpp new file mode 100644 index 0000000..15a3b04 --- /dev/null +++ b/8#/p4.cpp @@ -0,0 +1,51 @@ +#include +#include + +using namespace std; + +struct stringy +{ + char *str; + int ct; +}; + +void set(stringy &str, const char *source); +void show(const stringy &str, int n = 1); +void show(const char *str, int n = 1); + +int main() +{ + stringy beany; + char testing[] = "Reality isn't what it used to be."; + + set(beany, testing); + show(beany); + show(beany, 2); + + testing[0] = 'D'; + testing[1] = 'u'; + show(testing); + show(testing, 3); + show("Done"); + + return 0; +} + +void set(stringy &str, const char *source) +{ + str.ct = strlen(source) + 1; + str.str = new char[str.ct]; + strcpy(str.str, source); +} + +void show(const stringy &str, int n) +{ + for (int i = 0; i < n; i++) + cout << str.str << endl; +} + +void show(const char *str, int n) +{ + for (int i = 0; i < n; i++) + cout << str << endl; +} \ No newline at end of file diff --git a/8#/p5.cpp b/8#/p5.cpp new file mode 100644 index 0000000..f41bf0c --- /dev/null +++ b/8#/p5.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +template +T max5(T t[]); + +int main() +{ + int arr_i[5] = {1, 3, 5, 7, 9}; + double arr_d[5] = {22.2, 13.8, 17.9, 54.2, 38.5}; + + cout << "The max value of int arr: " << max5(arr_i) << endl;; + cout << "The max value of double arr: " << max5(arr_d) << endl; + + return 0; +} + +template +T max5(T t[]) +{ + T max = t[0]; + + for (int i = 1; i < 5; i++) + { + if (max < t[i]) + max = t[i]; + } + + return max; +} \ No newline at end of file diff --git a/8#/p6.cpp b/8#/p6.cpp new file mode 100644 index 0000000..4e1dcb1 --- /dev/null +++ b/8#/p6.cpp @@ -0,0 +1,51 @@ +#include +#include + +using namespace std; + +template +T maxn(T t[], int n); + +template <> +string maxn(string str[], int n); + +int main() +{ + int arr_i[6] = {1, 3, 5, 7, 9, 11}; + double arr_d[4] = {22.2, 13.8, 17.9, 38.5}; + + string str[5] = {"Hello world", "Good morning", "I love you, YangFushou", "What's taht?", "Bye! bye!"}; + + cout << "The max value of int arr: " << maxn(arr_i, 6) << endl;; + cout << "The max value of double arr: " << maxn(arr_d, 4) << endl; + cout << "The max string of string arr: " << maxn(str, 5) << endl; + + return 0; +} + +template +T maxn(T t[], int n) +{ + T max = t[0]; + + for (int i = 1; i < n; i++) + { + if (max < t[i]) + max = t[i]; + } + + return max; +} + +template <> +string maxn(string str[], int n) +{ + int pos = 0; + for (size_t i = 0; i < n; i++) + { + if (str[pos].size() < str[i].size()) + pos = i; + } + + return str[pos]; +} \ No newline at end of file diff --git a/8#/p7.cpp b/8#/p7.cpp new file mode 100644 index 0000000..0290e36 --- /dev/null +++ b/8#/p7.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +struct debts +{ + char name[50]; + double amount; +}; + +template +void ShowArray(T arr[], int n); + +template +void ShowArray(T *arr[], int n); + +template +T SumArray(T arr[], int n); + +template +T SumArray(T *arr[], int n); + +int main() +{ + int things[6] = {13, 31, 103, 301, 310, 130}; + + ShowArray(things, 6); + + debts mr_E[3] = + { + {"Rick", 2400.0}, + {"Jack", 1300.0}, + {"Rose", 1800.0} + }; + + double *pd[3]; + + for (int i = 0; i < 3; i++) + pd[i] = &mr_E[i].amount; + + ShowArray(things, 6); + + ShowArray(pd, 3); + + cout << "The sum of things: " << SumArray(things, 6) << endl; + cout << "The sum of pd: " << SumArray(pd, 3) << endl; + + return 0; +} + +template +void ShowArray(T arr[], int n) +{ + cout << "template A: " << endl; + + for (int i = 0; i < n; i++) + cout << arr[i] << " "; + + cout << endl; + +} + +template +void ShowArray(T *arr[], int n) +{ + cout << "template B: " << endl; + + for (int i = 0; i < n; i++) + cout << *arr[i] << " "; + + cout << endl; +} + +template +T SumArray(T arr[], int n) +{ + T sum = 0; + + for (int i = 0; i < n; i++) + sum += arr[i]; + + return sum; +} + +template +T SumArray(T *arr[], int n) +{ + T sum = 0; + + for (int i = 0; i < n; i++) + sum += *arr[i]; + + return sum; +} \ No newline at end of file diff --git a/8#/r5.cpp b/8#/r5.cpp new file mode 100644 index 0000000..6837067 --- /dev/null +++ b/8#/r5.cpp @@ -0,0 +1,45 @@ +#include +#include +#include + +using namespace std; + +const int Seasons = 4; + +const array Snames = {"Spring", "Summer", "Fall", "Winter"}; + +void fill(array &pa); +void show(const array &da); + +int main(void) +{ + array expenses; + + fill(expenses); + show(expenses); + + return 0; +} + +void fill(array &pa) +{ + for (int i = 0; i < Seasons; i++) + { + cout << "Enter " << Snames[i] << " expenses: "; + cin >> pa[i]; + } +} + +void show(const array &da) +{ + double total = 0.0; + cout << "EXPENSES: " << endl; + + for (int i = 0; i < Seasons; i++) + { + cout << Snames[i] << ": $" << da[i] << endl; + total += da[i]; + } + + cout << "Total expenses: " << total << endl; +} diff --git a/8#/review.txt b/8#/review.txt new file mode 100644 index 0000000..d5a179f --- /dev/null +++ b/8#/review.txt @@ -0,0 +1,78 @@ +1. +2. + a. void song(const char *name, int times = 1); + b. none + c. void song(const char *name = "O, My Papa", int times = 1); +3. + #include + + void iquote(int n) + { + cuout << "\\"" << n << "\"" << endl; + } + + void iquote(double a) + { + cuout << "\\"" << a << "\"" << endl; + } + void iquote(string s) + { + cuout << "\\"" << s << "\"" << endl; + } + +4. + void show_box(const box &b) + { + cout << "Name:" << b.maker << endl; + cout << "Height:" << b.height << endl; + cout << "Length:" << b.length << endl; + cout << "Volume:" << b.volume << endl; + } + + void set_volume(box &b) + { + b.volume = b.height * b.length; + } + +5. + +6. +a. + double mass(double density, double volume = 1.0); + +overload: + + double mass(double density, double volume); + double mass(double density); + +b. + void repeat(int times, const char *str); + void repeat(const char *str); + +c. + int average(int a, int b); + double average(double a, double b); + +7. + template + T max(T t1, T t2) + { + return t1 > t2 ? t1 : t2; + } + +8. + template <>box max(box b1, box b2) + { + return b1.volume > b2.volume ? b1 : b2; + } + +9. + v1-->float + v2-->float & + v3-->float & + v4-->int + v5-->double + + + + diff --git a/8#/test.cpp b/8#/test.cpp new file mode 100644 index 0000000..c81cbc6 --- /dev/null +++ b/8#/test.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int main() +{ + int a = 10; + int b = 20; + // int &c = a; + + // int &d = 10; + // int &d = (a+b); + + const int &d = 10; + const int &c = (a+b); + + int &&x = 10; + int &&y = (a + b); + + return 0; +} + +/** + * @brief + * left, right = , += + * + */ \ No newline at end of file diff --git a/9#/1.cpp b/9#/1.cpp new file mode 100644 index 0000000..c9a28b5 --- /dev/null +++ b/9#/1.cpp @@ -0,0 +1,9 @@ +#include + +int errors = 10; + +int main() +{ + + return 0; +} \ No newline at end of file diff --git a/9#/10.cpp b/9#/10.cpp new file mode 100644 index 0000000..c503d66 --- /dev/null +++ b/9#/10.cpp @@ -0,0 +1,62 @@ +#include +#include + +using namespace std; + +const int BUF = 512; +const int N = 5; +char buffer[BUF]; + +int main() +{ + double *pd1, *pd2; + + cout << "Calling new and placement new: " << endl; + pd1 = new double[N]; + pd2 = new(buffer) double[N]; + + for (size_t i = 0; i < N; i++) + pd2[i] = pd1[i] = 1000 + 20.0 * i; + + cout << "pd1 = " << pd1 << " buffer = " << (void *)buffer << endl; + + for (size_t i = 0; i < N; i++) + { + cout << pd1[i] << " at " << &pd1[i] << "; " + << pd2[i] << " at " << &pd2[i] << endl; + } + + + cout << "\nCalling new and placement new a second time: " << endl; + double *pd3, *pd4; + pd3 = new double[N]; + pd4 = new(buffer) double[N]; + + for (size_t i = 0; i < N; i++) + pd4[i] = pd3[i] = 1000 + 40 * i; + + for (size_t i = 0; i < N; i++) + { + cout << pd3[i] << " at " << &pd3[i] << "; " + << pd4[i] << " at " << &pd4[i] << endl; + } + + cout << "\nCalling new and placement new a thrid time: " << endl; + delete[] pd1; + pd1 = new double[N]; + pd2 = new(buffer + N * sizeof(double)) double[N]; + + for (size_t i = 0; i < N; i++) + pd2[i] = pd1[i] = 1000 + 60.0 * i; + + for (size_t i = 0; i < N; i++) + { + cout << pd1[i] << " at " << &pd1[i] << "; " + << pd2[i] << " at " << &pd2[i] << endl; + } + + delete[] pd1; + delete[] pd3; + + return 0; +} \ No newline at end of file diff --git a/9#/2.cpp b/9#/2.cpp new file mode 100644 index 0000000..ca118df --- /dev/null +++ b/9#/2.cpp @@ -0,0 +1,2 @@ + +static int errors = 100; \ No newline at end of file diff --git a/9#/4.cpp b/9#/4.cpp new file mode 100644 index 0000000..ab69eb7 --- /dev/null +++ b/9#/4.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +void oil(int x); + +int main() +{ + int texas = 31; + int year = 2011; + + cout << "In main(), texas = " << texas << ", "; + cout <<"&texas = " << &texas << endl; + cout << "In main(), year = " << year << ", "; + cout << "&year = " << &year << endl; + + oil(texas); + + cout << "In main(), texas = " << texas << ", "; + cout <<"&texas = " << &texas << endl; + cout << "In main(), year = " << year << ", "; + cout << "&year = " << &year << endl; + + + return 0; +} + +void oil(int x) +{ + int texas = 5; + + cout << "In oil(), texas = " << texas << ", "; + cout << "&texas = " << &texas << endl; + cout << "In oil(), x = " << x << ", "; + cout << "&x = " << &x << endl; + + { + int texas = 113; + cout << "In block, texas = " << texas << ", "; + cout << "&texas = " << &texas << endl; + cout << "In block, x = " << x << ", "; + cout << "&x = " << &x << endl; + } + + cout << "Post-block, texas = " << texas << ", "; + cout << "&texas = " << &texas << endl; +} \ No newline at end of file diff --git a/9#/9.cpp b/9#/9.cpp new file mode 100644 index 0000000..648dbe1 --- /dev/null +++ b/9#/9.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +const int ArSize = 10; + +void strcount(const char *str); + +int main() +{ + char input[ArSize]; + char next; + + cout << "Enter a line: " << endl; + cin.get(input, ArSize); + while (cin) { + cin.get(next); + while (next != '\n') + cin.get(next); + strcount(input); + cout << "Enter next line (empty line to quit): " << endl; + cin.get(input, ArSize); + } + + cout << "ByeBye." << endl; + + return 0; +} + +void strcount(const char *str) +{ + static int total = 0; + int count = 0; + + while (*str++) + count++; + total += count; + + cout << count << " characters." << endl; + cout << total << " characters total." << endl; +} \ No newline at end of file diff --git a/9#/coordin.h b/9#/coordin.h new file mode 100644 index 0000000..60fef98 --- /dev/null +++ b/9#/coordin.h @@ -0,0 +1,19 @@ +#ifndef __COORDIN_H__ +#define __COORDIN_H__ + +struct polar +{ + double distance; + double angle; +}; + +struct rect +{ + double x; + double y; +}; + +polar rect_to_polar(rect xypos); +void show_polar(polar dapos); + +#endif \ No newline at end of file diff --git a/9#/external.cpp b/9#/external.cpp new file mode 100644 index 0000000..131dcc5 --- /dev/null +++ b/9#/external.cpp @@ -0,0 +1,19 @@ +#include +#include "support.h" + +using namespace std; + +double warming = 0.3; + +int main() +{ + cout << "Global warming is " << warming << " degrees." << endl; + update(0.1); + cout << "Global warming is " << warming << " degrees." << endl; + + local(); + + cout << "Global warming is " << warming << " degrees." << endl; + + return 0; +} \ No newline at end of file diff --git a/9#/file1.cpp b/9#/file1.cpp new file mode 100644 index 0000000..e90a401 --- /dev/null +++ b/9#/file1.cpp @@ -0,0 +1,22 @@ +#include +#include "coordin.h" + +using namespace std; + +int main(void) +{ + rect rplace; + polar pplace; + + cout << "Enter the x and y values: "; + + while (cin >> rplace.x >> rplace.y) + { + pplace = rect_to_polar(rplace); + show_polar(pplace); + cout << "Next two bumber (q to quit): "; + } + + return 0; + +} diff --git a/9#/file2.cpp b/9#/file2.cpp new file mode 100644 index 0000000..1911d94 --- /dev/null +++ b/9#/file2.cpp @@ -0,0 +1,22 @@ +#include +#include +#include "coordin.h" + +using namespace std; + +polar rect_to_polar(rect xypos) +{ + polar answer; + answer.distance = sqrt(xypos.x * xypos.x + xypos.y * xypos.y); + answer.angle = atan2(xypos.y, xypos.x); + + return answer; +} + +void show_polar(polar dapos) +{ + const double Rad_to_deg = 57.29577951; + + cout << "Distance = " << dapos.distance << endl; + cout << "Angle = " << dapos.angle * Rad_to_deg << " degree" << endl; +} \ No newline at end of file diff --git a/9#/namesp.cpp b/9#/namesp.cpp new file mode 100644 index 0000000..2b2b4ab --- /dev/null +++ b/9#/namesp.cpp @@ -0,0 +1,48 @@ +#include +#include "namesp.h" + +namespace pers +{ + using std::cout; + using std::cin; + + void getPreson(Person &rp) + { + cout << "Enter first name: "; + cin >> rp.fname; + cout << "Enter last name: "; + cin >> rp.lname; + } + + void showPerson(const Person &rp) + { + cout << rp.lname << ", " << rp.fname; + } +} + +namespace debts +{ + void getDebt(Debt &rd) + { + getPreson(rd.name); + std::cout << "Enter debt: "; + std::cin >> rd.amount; + } + + void showDebt(const Debt &rd) + { + showPerson(rd.name); + std::cout << ": " << rd.amount << std::endl; + } + + double sumDebts(const Debt ar[], int n) + { + double total = 0; + for (size_t i = 0; i < n; i++) + { + total += ar[i].amount; + } + + return total; + } +} \ No newline at end of file diff --git a/9#/namesp.h b/9#/namesp.h new file mode 100644 index 0000000..bf57304 --- /dev/null +++ b/9#/namesp.h @@ -0,0 +1,27 @@ +#include + +namespace pers +{ + struct Person + { + std::string fname; + std::string lname; + }; + + void getPreson(Person &rp); + void showPerson(const Person &rp); +} + +namespace debts +{ + using namespace pers; + struct Debt + { + Person name; + double amount; + }; + + void getDebt(Debt &rd); + void showDebt(const Debt &rd); + double sumDebts(const Debt ar[], int n); +} \ No newline at end of file diff --git a/9#/namessp.cpp b/9#/namessp.cpp new file mode 100644 index 0000000..8a615af --- /dev/null +++ b/9#/namessp.cpp @@ -0,0 +1,13 @@ +#include +#include "namesp.h" + +int main() +{ + using debts::Debt; + using debts::showDebt; + + Debt golf = {{"Micheal", "Jordan"}, 120.0}; + showDebt(golf); + + return 0; +} \ No newline at end of file diff --git a/9#/p1/golf.cpp b/9#/p1/golf.cpp new file mode 100644 index 0000000..a2855e0 --- /dev/null +++ b/9#/p1/golf.cpp @@ -0,0 +1,43 @@ +#include +#include "golf.h" +#include + +using namespace std; + +void setgolf(golf &g, const char *name, int hc) +{ + strcpy(g.fullname, name); + g.handicap = hc; +} + +int setgolf(golf &g) +{ + int temp = 1; + + cout << "The fullname is: "; + cin.getline(g.fullname, Len); + + if (strcmp(g.fullname, "") == 0) + { + temp = 0; + } + else + { + cout << "The handicap is: "; + cin >> g.handicap; + cin.get(); + } + + return temp; +} + +void handicap(golf &g, int hc) +{ + g.handicap = hc; +} + +void showgolf(const golf &g) +{ + cout << "The fullname is: " << g.fullname << endl; + cout << "The handicap is: " << g.handicap << endl; +} \ No newline at end of file diff --git a/9#/p1/golf.h b/9#/p1/golf.h new file mode 100644 index 0000000..f720cbb --- /dev/null +++ b/9#/p1/golf.h @@ -0,0 +1,12 @@ +const int Len = 40; + +struct golf +{ + char fullname[Len]; + int handicap; +}; + +void setgolf(golf &g, const char *name, int hc); +int setgolf(golf &g); +void handicap(golf &g, int hc); +void showgolf(const golf &g); \ No newline at end of file diff --git a/9#/p1/main.cpp b/9#/p1/main.cpp new file mode 100644 index 0000000..c284f6e --- /dev/null +++ b/9#/p1/main.cpp @@ -0,0 +1,34 @@ +#include +#include "golf.h" + +using namespace std; + + +const int SIZE = 2; + +int main() +{ + golf g[SIZE]; + int count = 0; + + cout << "Please enter the information of golf players: " << endl; + while ((count < SIZE) && (setgolf(g[count]))) + { + cout << "Please enter next player: " << endl; + count++; + } + + cout << "\nShow all golf players info: " << endl; + for (size_t i = 0; i < count; i++) + showgolf(g[i]); + + cout << "\nReset all the palyer info: " << endl; + for (size_t i = 0; i < count; i++) + { + handicap(g[i], 90); + showgolf(g[i]); + } + + + return 0; +} \ No newline at end of file diff --git a/9#/p2.cpp b/9#/p2.cpp new file mode 100644 index 0000000..b239291 --- /dev/null +++ b/9#/p2.cpp @@ -0,0 +1,36 @@ +#include +#include + +using namespace std; + +void strcount(const string str); + +int main() +{ + string input; + char next; + + cout << "Enter a line: " << endl; + getline(cin, input); + while (input != "") { + strcount(input); + cout << "Enter next line (empty line to quit): " << endl; + getline(cin, input); + } + + cout << "ByeBye." << endl; + + return 0; +} + +void strcount(const string str) +{ + static int total = 0; + int count = 0; + + count = str.size(); + total += count; + + cout << count << " characters." << endl; + cout << total << " characters total." << endl; +} \ No newline at end of file diff --git a/9#/p3.cpp b/9#/p3.cpp new file mode 100644 index 0000000..dd71328 --- /dev/null +++ b/9#/p3.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +using namespace std; + +struct chaff +{ + char dross[20]; + int slag; +}; + +const int BUF = 512; +char buffer[BUF]; + +void show(const chaff &p); +int main() +{ + chaff *pd1 = new chaff[2]; + chaff *pd2 = new(buffer) chaff[2]; + char dross[20]; + int slag; + + for (size_t i = 0; i < 2; i++) + { + cout << "# " << i+1 << ": " << endl; + cout << "Enter the dross: "; + cin.getline(dross, 20); + cout << "Enter the slag: "; + cin >> slag; + cin.get(); + + strcpy(pd1[i].dross, dross); + strcpy(pd2[i].dross, dross); + + pd1[i].slag = pd2[i].slag = slag; + } + + for (size_t i = 0; i < 2; i++) + { + show(pd1[i]); + show(pd2[i]); + } + delete[] pd1; + + return 0; +} + +void show(const chaff &p) +{ + cout << "The dross is: " << p.dross << endl; + cout << "The salg is: " << p.slag << endl; +} \ No newline at end of file diff --git a/9#/p4/main.cpp b/9#/p4/main.cpp new file mode 100644 index 0000000..456a5ae --- /dev/null +++ b/9#/p4/main.cpp @@ -0,0 +1,18 @@ +#include "p4.h" + +using namespace std; + +int main() +{ + using namespace SALES; + + double ar[4] = {11.1, 22.2, 33.3, 44.4}; + Sales sl; + + setSales(sl, ar, 3); + showSales(sl); + setSales(sl); + showSales(sl); + + return 0; +} \ No newline at end of file diff --git a/9#/p4/p4.cpp b/9#/p4/p4.cpp new file mode 100644 index 0000000..22f80cc --- /dev/null +++ b/9#/p4/p4.cpp @@ -0,0 +1,74 @@ +#include "p4.h" + +using namespace std; + +namespace SALES +{ + void setSales(Sales &s, const double ar[], int n) + { + double total; + int i; + + for (i = 0; i < n && i < 4; i++) + { + s.sales[i] = ar[i]; + total += s.sales[i]; + } + + s.average = total / i; + s.max = s.sales[0]; + s.min = s.sales[0]; + + for (size_t k = 1; k < i; k++) + { + s.max = (s.max > s.sales[k]) ? s.max : s.sales[k]; + s.min = (s.min < s.sales[k]) ? s.min : s.sales[k]; + } + + if (n < 4) + { + for (size_t k = n; k < 4; k++) + { + s.sales[k] = 0; + } + } + } + + void setSales(Sales &s) + { + double total = 0.0; + + cout << "Enter 4 sales quarters: " << endl; + for (int i = 0; i < QUARTERS; i++) + { + cout << "#" << i + 1 << ": "; + cin >> s.sales[i]; + total += s.sales[i]; + + if (i == 0) + { + s.max = s.sales[i]; + s.min = s.sales[i]; + } + else + { + s.max = (s.max > s.sales[i]) ? s.max : s.sales[i]; + s.min = (s.min < s.sales[i]) ? s.min : s.sales[i]; + } + } + + s.average = total / QUARTERS; + } + + void showSales(const Sales &s) + { + cout << "Sales of 4 quarters: " << endl; + for (size_t i = 0; i < QUARTERS; i++) + cout << s.sales[i] << " "; + + cout << endl; + cout << "Average: " << s.average << endl;; + cout << "MAx = " << s.max << endl; + cout << "Min = " << s.min << endl; + } +} diff --git a/9#/p4/p4.h b/9#/p4/p4.h new file mode 100644 index 0000000..b4b2c07 --- /dev/null +++ b/9#/p4/p4.h @@ -0,0 +1,23 @@ +#ifndef __P4_H__ +#define __P4_H__ + +#include + +namespace SALES +{ + const int QUARTERS = 4; + struct Sales + { + double sales[QUARTERS]; + double average; + double max; + double min; + }; + + void setSales(Sales &s, const double ar[], int n); + void setSales(Sales &s); + void showSales(const Sales &s); + +} + +#endif \ No newline at end of file diff --git a/9#/support.cpp b/9#/support.cpp new file mode 100644 index 0000000..c745d45 --- /dev/null +++ b/9#/support.cpp @@ -0,0 +1,18 @@ +#include "support.h" + +using namespace std; + +void update(double dt) +{ + warming += dt; + + cout << "Updating global warming to " << warming << " degrees." << endl; +} + +void local(void) +{ + double warming = 0.8; + + cout << "Local warming = " << warming << " degress." << endl; + cout << "But global wamring = " << ::warming << " degress." << endl; +} \ No newline at end of file diff --git a/9#/support.h b/9#/support.h new file mode 100644 index 0000000..4060070 --- /dev/null +++ b/9#/support.h @@ -0,0 +1,11 @@ +#ifndef __SUPPORT_H__ +#define __SUPPORT_H__ + +#include + +extern double warming; + +void update(double dt); +void local(void); + +#endif \ No newline at end of file diff --git a/9#/twofile1.cpp b/9#/twofile1.cpp new file mode 100644 index 0000000..c756c48 --- /dev/null +++ b/9#/twofile1.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int tom = 3; +int dick = 30; +static int harry = 300; + +void remote_access(void); + +int main() +{ + cout << "main() reports the following address: " << endl; + cout << "&tom = " << &tom << " &dick = " << &dick << " &harry = " << &harry << endl; + + remote_access(); + + return 0; +} \ No newline at end of file diff --git a/9#/twofile2.cpp b/9#/twofile2.cpp new file mode 100644 index 0000000..cfb323f --- /dev/null +++ b/9#/twofile2.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +extern int tom; +static int dick = 10; +int harry = 200; + +void remote_access(void) +{ + cout << "remote_access() reports the following address: " << endl; + cout << "&tom = " << &tom << " &dick = " << &dick << " &harry = " << &harry << endl; +} \ No newline at end of file -- Gitee From 223cea116b1bda25d121c2f35752c23bb3a431e4 Mon Sep 17 00:00:00 2001 From: fyang Date: Sat, 4 Jun 2022 10:52:25 +0800 Subject: [PATCH 2/2] Add exercises code for chapter -- 13 --- 13-class_inheritance/exercises/p1/classic.cpp | 89 +++++++++++++++++ 13-class_inheritance/exercises/p1/classic.h | 38 +++++++ .../exercises/p1/useclassic.cpp | 39 ++++++++ 13-class_inheritance/exercises/p2/classic.cpp | 98 +++++++++++++++++++ 13-class_inheritance/exercises/p2/classic.h | 36 +++++++ .../exercises/p2/useclassic.cpp | 39 ++++++++ 6 files changed, 339 insertions(+) create mode 100644 13-class_inheritance/exercises/p1/classic.cpp create mode 100644 13-class_inheritance/exercises/p1/classic.h create mode 100644 13-class_inheritance/exercises/p1/useclassic.cpp create mode 100644 13-class_inheritance/exercises/p2/classic.cpp create mode 100644 13-class_inheritance/exercises/p2/classic.h create mode 100644 13-class_inheritance/exercises/p2/useclassic.cpp diff --git a/13-class_inheritance/exercises/p1/classic.cpp b/13-class_inheritance/exercises/p1/classic.cpp new file mode 100644 index 0000000..6715d4d --- /dev/null +++ b/13-class_inheritance/exercises/p1/classic.cpp @@ -0,0 +1,89 @@ +#include "classic.h" +#include + +Cd::Cd(const char *s1, const char *s2, int n, double x) +{ + strncpy(performers, s1, 50); + if (strlen(s1) >= 50) + performers[49] = '\0'; + else + performers[strlen(s1)] = '\0'; + + strncpy(label, s2, 20); + if (strlen(s2) >= 20) + label[19] = '\0'; + else + label[strlen(s2)] = '\0'; + + selections = n; + playtime = x; +} + +Cd::Cd(const Cd &d) +{ + strcpy(performers, d.performers); + strcpy(label, d.label); + selections = d.selections; + playtime = d.playtime; +} + +Cd::Cd() +{ + performers[0] = '0'; + label[0] = '\0'; + selections = 0; + playtime = 0; +} + +void Cd::Report() const +{ + cout << "Performers: " << performers << endl; + cout << "Label: " << label << endl; + cout << "Selections: " << selections << endl; + cout << "Playtime: " << playtime << endl; +} + +Cd &Cd::operator=(const Cd &d) +{ + if (this == &d) + return *this; + strcpy(performers, d.performers); + strcpy(label, d.label); + selections = d.selections; + playtime = d.playtime; + return *this; +} + +Classic::Classic() : Cd() +{ + works[0] = '\0'; +} + +Classic::Classic(const Classic &c) : Cd(c) +{ + strcpy(works, c.works); +} + +Classic::Classic(const char *s1, const char *s2, const char *s3, int n, double x) : Cd(s2, s3, n, x) +{ + strncpy(works, s1, 50); + if (strlen(s1) >= 50) + works[49] = '\0'; + else + works[strlen(s1)] = '\0'; +} + +void Classic::Report() const +{ + Cd::Report(); + cout << "Works: " << works << endl; +} + +Classic &Classic::operator=(const Classic &c) +{ + if (this == &c) + return *this; + Cd::operator=(c); + stpcpy(works, c.works); + return *this; +} diff --git a/13-class_inheritance/exercises/p1/classic.h b/13-class_inheritance/exercises/p1/classic.h new file mode 100644 index 0000000..4c427ec --- /dev/null +++ b/13-class_inheritance/exercises/p1/classic.h @@ -0,0 +1,38 @@ +#ifndef __CLASSIC_H__ +#define __CLASSIC_H__ +#include + +using namespace std; + +class Cd +{ +private: + char performers[50]; + char label[20]; + int selections; + double playtime; + +public: + Cd(const char *s1, const char *s2, int n, double x); + Cd(const Cd &d); + Cd(); + virtual ~Cd() {} + virtual void Report() const; + Cd &operator=(const Cd &d); +}; + +class Classic : public Cd +{ +private: + char works[50]; + +public: + Classic(); + Classic(const Classic &c); + Classic(const char *s1, const char *s2, const char *s3, int n, double x); + ~Classic() {} + virtual void Report() const; + Classic &operator=(const Classic &c); +}; + +#endif \ No newline at end of file diff --git a/13-class_inheritance/exercises/p1/useclassic.cpp b/13-class_inheritance/exercises/p1/useclassic.cpp new file mode 100644 index 0000000..1c5c20a --- /dev/null +++ b/13-class_inheritance/exercises/p1/useclassic.cpp @@ -0,0 +1,39 @@ +#include +#include "classic.h" // which will contain #include cd.h + +using namespace std; + +void Bravo(const Cd &disk); + +int main() +{ + Cd c1("Beatles", "Capitol", 14, 35.5); + Classic c2 = Classic("Piano sonata in B flat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17); + + Cd *pcd = &c1; + + cout << "Uaing object directly:\n"; + c1.Report(); // use Cd method + c2.Report(); // use Classic method + + cout << "Using type cd * pointer to objects:\n"; + pcd->Report(); // use Cd method for Cd object + pcd = &c2; + pcd->Report(); // use Classic method for classic object + + cout << "Calling a function with a Cd reference argument:\n"; + Bravo(c1); + Bravo(c2); + + cout << "Testing assignment: "; + Classic copy; + copy = c2; + copy.Report(); + + return 0; +} + +void Bravo(const Cd &disk) +{ + disk.Report(); +} \ No newline at end of file diff --git a/13-class_inheritance/exercises/p2/classic.cpp b/13-class_inheritance/exercises/p2/classic.cpp new file mode 100644 index 0000000..396c8f2 --- /dev/null +++ b/13-class_inheritance/exercises/p2/classic.cpp @@ -0,0 +1,98 @@ +#include "classic.h" +#include + +Cd::Cd(const char *s1, const char *s2, int n, double x) +{ + performers = new char[strlen(s1) + 1]; + label = new char[strlen(s2) + 1]; + strcpy(performers, s1); + strcpy(label, s2); + selections = n; + playtime = x; +} + +Cd::Cd(const Cd &d) +{ + performers = new char[strlen(d.performers) + 1]; + label = new char[strlen(d.label)]; + strcpy(performers, d.performers); + strcpy(label, d.label); + selections = d.selections; + playtime = d.playtime; +} + +Cd::Cd() +{ + performers = nullptr; + label = nullptr; + selections = 0; + playtime = 0; +} + +Cd::~Cd() +{ + delete[] performers; + delete[] label; +} + +void Cd::Report() const +{ + cout << "Performers: " << performers << endl; + cout << "Label: " << label << endl; + cout << "Selections: " << selections << endl; + cout << "Playtime: " << playtime << endl; +} + +Cd &Cd::operator=(const Cd &d) +{ + if (this == &d) + return *this; + delete[] performers; + delete[] label; + performers = new char[strlen(d.performers) + 1]; + label = new char[strlen(d.label) + 1]; + strcpy(performers, d.performers); + strcpy(label, d.label); + selections = d.selections; + playtime = d.playtime; + return *this; +} + +Classic::Classic() : Cd() +{ + works = nullptr; +} + +Classic::Classic(const Classic &c) : Cd(c) +{ + works = new char[strlen(c.works) + 1]; + strcpy(works, c.works); +} + +Classic::Classic(const char *s1, const char *s2, const char *s3, int n, double x) : Cd(s2, s3, n, x) +{ + works = new char[strlen(s1) + 1]; + strcpy(works, s1); +} + +void Classic::Report() const +{ + Cd::Report(); + cout << "Works: " << works << endl; +} + +Classic::~Classic() +{ + delete[] works; +} + +Classic &Classic::operator=(const Classic &c) +{ + if (this == &c) + return *this; + Cd::operator=(c); + delete[] works; + works = new char[strlen(c.works) + 1]; + stpcpy(works, c.works); + return *this; +} diff --git a/13-class_inheritance/exercises/p2/classic.h b/13-class_inheritance/exercises/p2/classic.h new file mode 100644 index 0000000..ebd2fbd --- /dev/null +++ b/13-class_inheritance/exercises/p2/classic.h @@ -0,0 +1,36 @@ +#ifndef __CLASSIC_H__ +#define __CLASSIC_H__ +#include + +using namespace std; + +class Cd +{ +private: + char *performers; + char *label; + int selections; + double playtime; +public: + Cd(const char * s1, const char * s2, int n, double x); + Cd(const Cd &d); + Cd(); + virtual ~Cd(); + virtual void Report() const; + Cd &operator=(const Cd &d); +}; + +class Classic : public Cd +{ +private: + char *works; +public: + Classic(); + Classic(const Classic &c); + Classic(const char *s1, const char *s2, const char *s3, int n, double x); + ~Classic(); + virtual void Report() const; + Classic &operator=(const Classic &c); +}; + +#endif \ No newline at end of file diff --git a/13-class_inheritance/exercises/p2/useclassic.cpp b/13-class_inheritance/exercises/p2/useclassic.cpp new file mode 100644 index 0000000..4a31d4e --- /dev/null +++ b/13-class_inheritance/exercises/p2/useclassic.cpp @@ -0,0 +1,39 @@ +#include +#include "classic.h" // which will contain #include cd.h + +using namespace std; + +void Bravo(const Cd &disk); + +int main() +{ + Cd c1("Beatles", "Capitol", 14, 35.5); + Classic c2 = Classic("Piano sonata in B flat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17); + + Cd *pcd = &c1; + + cout << "Uaing object directly:\n"; + c1.Report(); // use Cd method + c2.Report(); // use Classic method + + cout << "Using type cd * pointer to objects:\n"; + pcd->Report(); // use Cd method for Cd object + pcd = &c2; + pcd->Report(); // use Classic method for classic object + + cout << "Calling a function with a Cd reference argument:\n"; + Bravo(c1); + Bravo(c2); + + cout << "Testing assignment: "; + Classic copy; + copy = c2; + copy.Report(); + + return 0; +} + +void Bravo(const Cd &disk) +{ + disk.Report(); +} \ No newline at end of file -- Gitee