From 86eabe54764ed715f936640a57801a5d8b7cde3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=87=E8=B6=85?= <973448713@qq.com> Date: Tue, 16 Jul 2024 09:44:52 +0800 Subject: [PATCH] code submit --- .../Boost-Graph/interior_property_map.cpp" | 97 ++++++++++++++ .../Chapter07/1.cpp" | 34 +++++ .../Chapter07/2.cpp" | 53 ++++++++ .../Chapter08/1.cpp" | 32 +++++ .../Chapter08/2.cpp" | 39 ++++++ .../Chapter09/golf.cpp" | 40 ++++++ .../Chapter09/golf.h" | 17 +++ .../Chapter09/main.cpp" | 25 ++++ .../Chapter10/Stock/stock10.h" | 118 ++++++++++++++++++ .../Chapter10/Stock/usestock.cpp" | 37 ++++++ 10 files changed, 492 insertions(+) create mode 100644 "C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Boost-Graph/interior_property_map.cpp" create mode 100644 "C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter07/1.cpp" create mode 100644 "C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter07/2.cpp" create mode 100644 "C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter08/1.cpp" create mode 100644 "C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter08/2.cpp" create mode 100644 "C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter09/golf.cpp" create mode 100644 "C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter09/golf.h" create mode 100644 "C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter09/main.cpp" create mode 100644 "C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter10/Stock/stock10.h" create mode 100644 "C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter10/Stock/usestock.cpp" diff --git "a/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Boost-Graph/interior_property_map.cpp" "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Boost-Graph/interior_property_map.cpp" new file mode 100644 index 0000000..c78cca8 --- /dev/null +++ "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Boost-Graph/interior_property_map.cpp" @@ -0,0 +1,97 @@ + + +#include +#include +#include +#include + +#include +#include + +using namespace std; +using namespace boost; + +/* + Interior Property Map Basics + + An interior property map is a way of associating properties + with the vertices or edges of a graph. The "interior" part means + that the properties are stored inside the graph object. This can be + convenient when the need for the properties is somewhat permanent, + and when the properties will be with a graph for the duration of its + lifetime. A "distance from source vertex" property is often of this + kind. + + Sample Output + + Jeremy owes Rich some money + Jeremy owes Andrew some money + Jeremy owes Jeff some money + Jeremy owes Kinis some money + Andrew owes Jeremy some money + Andrew owes Kinis some money + Jeff owes Jeremy some money + Jeff owes Rich some money + Jeff owes Kinis some money + Kinis owes Jeremy some money + Kinis owes Rich some money + + */ + +// create a tag for our new property + +enum vertex_first_name_t { vertex_first_name }; +namespace boost { +BOOST_INSTALL_PROPERTY(vertex, first_name); +} + +template +void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G) { + // Access the propety acessor type for this graph + typedef typename property_map::const_type NamePA; + NamePA name = get(vertex_first_name, G); + + typedef typename boost::property_traits::value_type NameType; + + NameType src_name, targ_name; + + while (first != last) { + src_name = boost::get(name, source(*first, G)); + targ_name = boost::get(name, target(*first, G)); + cout << src_name << " owes " << targ_name << " some money" << endl; + ++first; + } +} + +int main() { + { + // Create the graph, and specify that we will use std::string to + // store the first name's. + typedef adjacency_list >MyGraphType; + + typedef pair Pair; + Pair edge_array[11] = {Pair(0, 1), Pair(0, 2), Pair(0, 3), Pair(0, 4), + Pair(2, 0), Pair(3, 0), Pair(2, 4), Pair(3, 1), + Pair(3, 4), Pair(4, 0), Pair(4, 1)}; + + MyGraphType G(5); + for (int i = 0; i < 11; ++i) + add_edge(edge_array[i].first, edge_array[i].second, G); + + property_map::type name = + get(vertex_first_name, G); + + boost::put(name, 0, "Jeremy"); + boost::put(name, 1, "Rich"); + boost::put(name, 2, "Andrew"); + boost::put(name, 3, "Jeff"); + name[4] = "Kinis"; // you can use operator[] too + + who_owes_who(edges(G).first, edges(G).second, G); + } + + cout << endl; + + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter07/1.cpp" "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter07/1.cpp" new file mode 100644 index 0000000..ff2669a --- /dev/null +++ "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter07/1.cpp" @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +int Tsum(int x,int y) +{ + return 2.0*x*y/(x+y); +} + +int main() { + int x, y; + int resule; + while (cin >> x >> y) { + if (x == 0 || y == 0) break; + resule=Tsum(x,y); + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct box { + /* data */ + char maker[40]; + float height; + float width; + float length; + float volume; +}; + +void sset(box *b) { + b->volume = b->height * b->length * b->width; + } + +box ssset(box b) { + box bb; + bb = b; + bb.volume = bb.height * bb.length * bb.width; + return bb; +} + +void show(box a1) { + cout << a1.height << ' ' << a1.length << ' ' << a1.width << ' ' << a1.volume + << ' ' << a1.maker << endl; +} +int main(int argc, char const *argv[]) { + /* code */ + box a1 = {"wangwanchao", 10, 20, 30}; + box a2 = {"chenshijiao", 1, 2, 3}; + box a; + sset(&a1); + show(a1); + a=ssset(a2); + show(a); + show(a2); + return 0; +} diff --git "a/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter08/1.cpp" "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter08/1.cpp" new file mode 100644 index 0000000..86145bc --- /dev/null +++ "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter08/1.cpp" @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +void show(string *p,int n=0) +{ + for(int i=0;i>num; + show(&p,num); + + return 0; +} diff --git "a/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter08/2.cpp" "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter08/2.cpp" new file mode 100644 index 0000000..cf57c90 --- /dev/null +++ "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter08/2.cpp" @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct CandyBar { + string brand; + double weight; + int calories; +}; +void sets(CandyBar &cd, string brand, double weight, int calories) +{ + cd.brand = brand; + cd.weight = weight; + cd.calories = calories; +} +void print(const CandyBar &candy) +{ + cout << candy.brand << " " << candy.weight << " " << candy.calories << endl; +} +int main(int argc, char const *argv[]) { + /* code */ + CandyBar candy; + sets(candy, "Mars", 2.3, 230); + print(candy); + return 0; +} diff --git "a/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter09/golf.cpp" "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter09/golf.cpp" new file mode 100644 index 0000000..29d2b38 --- /dev/null +++ "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter09/golf.cpp" @@ -0,0 +1,40 @@ +#include +#include "golf.h" +#include +using namespace std; + +int setgolf(golf &g) +{ + cout << "Enter the name of the player: "; + cin.getline(g.fullname, Len); + if(g.fullname[0] == '\0') + return 0; + cout<<"Enter the handicap of the player: "<> g.handicap)) + { + cin.clear(); + while (cin.get() != '\n') + continue; + cout << "Please enter a number: "; + } + cin.get(); + return 1; +} + +void setgolf(golf &g, const char *name, int hc) +{ + strncpy_s(g.fullname, name, Len); + g.handicap = hc; +} + +void handicap(golf &g, int hc) +{ + g.handicap = hc; +} + + +void showgolf(const golf &g) +{ + cout << "Name: " << g.fullname << endl; + cout << "Handicap: " << g.handicap << endl; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter09/golf.h" "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter09/golf.h" new file mode 100644 index 0000000..c1bc0cd --- /dev/null +++ "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter09/golf.h" @@ -0,0 +1,17 @@ + + +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); + diff --git "a/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter09/main.cpp" "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter09/main.cpp" new file mode 100644 index 0000000..194270b --- /dev/null +++ "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter09/main.cpp" @@ -0,0 +1,25 @@ +#include +#include "golf.h" + +int main() +{ + using namespace std; + golf golfer[5]; + int i ; + for(i = 0;i<1;i++) + { + if(setgolf(golfer[i]) == 0) + break; + } + for(int j = 0;j +#include +class Stock +{ +private: + std::string company; + long shares; + double share_val; + double total_val; + void set_tot() { total_val = shares * share_val; } +public: + Stock();// default constructor + Stock(const std::string & co, long n = 0, double pr = 0.0);// constructor + ~Stock();// destructor + + 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; +}; + + +//类的实现 + +Stock::Stock() // default constructor +{ + company = "no name"; + shares = 0; + share_val = 0.0; + total_val = 0.0; +} +Stock::Stock(const std::string & co, long n, double pr) // constructor +{ + company = co; + if (n < 0) + { + std::cout << "Number of shares can't be negative; " + << company << " shares set to 0.\n"; + shares = 0; + } + else + { + shares = n; + } + share_val = pr; + set_tot(); +} + +Stock::~Stock() // destructor +{ +} + +//buy +void Stock::buy(long num, double price) +{ + if (num < 0) + { + std::cout << "Number of shares purchased can't be negative. " + << "Transaction is aborted.\n"; + } + else + { + shares += num; + share_val = price; + set_tot(); + } +} +//sell +void Stock::sell(long num, double price) +{ + if (num < 0) + { + std::cout<<"Number of shares sold can't be negative. " + << "Transaction is aborted.\n"; + } + else if (num > shares) + { + std::cout << "You can't sell more than you have! " + << "Transaction is aborted.\n"; + } + else + { + shares -= num; + share_val = price; + set_tot(); + } + +} +//update +void Stock::update(double price) +{ + share_val = price; + set_tot(); +} + +//show +void Stock::show() const +{ + using std::cout; + using std::ios_base; + // set format to #.### + ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield); + std::streamsize prec = cout.precision(3); + cout << "Company: " << company + << " Shares: " << shares << '\n' + << " Share Price: $" << share_val; + // set format to #.## + cout.precision(2); + cout << " Total Worth: $" << total_val << '\n'; + // restore original format + cout.setf(orig, ios_base::floatfield); + cout.precision(prec); +} + +#endif \ No newline at end of file diff --git "a/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter10/Stock/usestock.cpp" "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter10/Stock/usestock.cpp" new file mode 100644 index 0000000..bd1c124 --- /dev/null +++ "b/C++/codes/2024-07/\347\216\213\344\270\207\350\266\205/Chapter10/Stock/usestock.cpp" @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "stock10.h" +using namespace std; + +int main(int argc, char const *argv[]) { + /* code */ + cout << "Using constructors to create new objects\n"; + Stock stock1("NanoSmart", 12, 20.0); + stock1.show(); + cout<<"-------------------------------------"<