Merge branch 'master' of http://gogs.zyjblogs.cn/zhuyijun/CStudy
# Conflicts: # base/CMakeLists.txt
This commit is contained in:
commit
2f5e9cb82e
@ -47,6 +47,11 @@ add_executable(external namespacetest/external.cpp)
|
||||
add_executable(support namespacetest/support.cpp)
|
||||
add_executable(ststic namespacetest/ststic.cpp)
|
||||
add_executable(newplace namespacetest/newplace.cpp)
|
||||
<<<<<<< HEAD
|
||||
add_executable(namessp namespacetest/namessp.cpp)
|
||||
add_executable(usestock0 class/usestock0.cpp)
|
||||
add_executable(stock20 class/stock20.cpp)
|
||||
add_executable(useStock20 class/useStock20.cpp)
|
||||
add_executable(useStock20 class/useStock20.cpp)
|
||||
=======
|
||||
add_executable(namessp namespacetest/namessp.cpp)
|
||||
>>>>>>> parent of 39fe553 (添加对象)
|
||||
|
34
base/abstract/stack.cpp
Normal file
34
base/abstract/stack.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/17.
|
||||
//
|
||||
|
||||
|
||||
#include "stack.h"
|
||||
|
||||
Stack::Stack() {
|
||||
top = 0;
|
||||
}
|
||||
|
||||
bool Stack::isempty() const {
|
||||
return top == 0;
|
||||
}
|
||||
|
||||
bool Stack::isfull() const {
|
||||
return top == MAX;
|
||||
}
|
||||
|
||||
bool Stack::push(const Item &item) {
|
||||
if (top < MAX) {
|
||||
items[top++] = item;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Stack::pop(Item &item) {
|
||||
if (top > 0) {
|
||||
item = items[--top];
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
29
base/abstract/stack.h
Normal file
29
base/abstract/stack.h
Normal file
@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/17.
|
||||
//
|
||||
|
||||
#ifndef BASE_STACK_H
|
||||
#define BASE_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(const Item & item);
|
||||
//出栈
|
||||
bool pop(Item & item);
|
||||
};
|
||||
|
||||
|
||||
#endif //BASE_STACK_H
|
50
base/abstract/stacker.cpp
Normal file
50
base/abstract/stacker.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/17.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <cctype>
|
||||
#include "stack.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
Stack st;
|
||||
char ch;
|
||||
unsigned long po;
|
||||
cout << "Please enter A to add a purchase order,\n Pto process a PO,or Q to quit.\n";
|
||||
while (cin >> ch && toupper(ch) != 'Q') {
|
||||
while (cin.get() != '\n') {
|
||||
continue;
|
||||
}
|
||||
if (!isalpha(ch)) {
|
||||
cout << '\a';
|
||||
continue;
|
||||
}
|
||||
switch (ch) {
|
||||
case 'A':
|
||||
case 'a':
|
||||
cout << "Enter a PO number to add: ";
|
||||
cin >> po;
|
||||
if (st.isfull())
|
||||
cout << "stack already full";
|
||||
else
|
||||
st.push(po);
|
||||
break;
|
||||
case 'P':
|
||||
case 'p':
|
||||
if (st.isempty())
|
||||
cout << "stack already empty \n";
|
||||
else {
|
||||
st.pop(po);
|
||||
cout << "PO #" << po << " popped\n";
|
||||
}
|
||||
break;
|
||||
}
|
||||
cout << "Please enter A to add a purchase order,\n "
|
||||
"P to process a PO, or Q to quit,\n";
|
||||
}
|
||||
cout << "Bye\n";
|
||||
return 0;
|
||||
}
|
||||
|
83
base/class/stock20.cpp
Normal file
83
base/class/stock20.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/17.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "stock20.h"
|
||||
|
||||
Stock::Stock() {
|
||||
company = "no name";
|
||||
shares = 0;
|
||||
share_val = 0.0;
|
||||
total_val = -0.0;
|
||||
}
|
||||
|
||||
Stock::Stock(const std::string &co, long n, double pr) {
|
||||
company = co;
|
||||
if (n < 0) {
|
||||
std::cout << "Number of shares can't be nagetive; " << company << " shares set to 0." << std::endl;
|
||||
shares = 0;
|
||||
} else {
|
||||
shares = n;
|
||||
}
|
||||
share_val = pr;
|
||||
set_tot();
|
||||
}
|
||||
|
||||
inline void Stock::set_tot() {
|
||||
total_val = shares * share_val;
|
||||
}
|
||||
|
||||
Stock::~Stock() {
|
||||
std::cout << "run " << company << " ~Stock" << std::endl;
|
||||
}
|
||||
|
||||
void Stock::buy(long num, double price) {
|
||||
if (num < 0) {
|
||||
std::cout << "Number of shares purchased can't be negative. Transaction is aborted." << std::endl;
|
||||
} else {
|
||||
shares += num;
|
||||
share_val = price;
|
||||
set_tot();
|
||||
}
|
||||
}
|
||||
|
||||
void Stock::sell(long num, double price) {
|
||||
using std::cout;
|
||||
if (num < 0) {
|
||||
std::cout << "Number of shares purchased can't be negative. Transaction is aborted." << std::endl;
|
||||
} else if (num > shares) {
|
||||
std::cout << "You can't sell more than you have! Transaction is aborted." << std::endl;
|
||||
} else {
|
||||
shares -= num;
|
||||
share_val = price;
|
||||
set_tot();
|
||||
}
|
||||
}
|
||||
|
||||
void Stock::update(double price) {
|
||||
share_val = price;
|
||||
set_tot();
|
||||
}
|
||||
|
||||
void Stock::show() const {
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using std::ios_base;
|
||||
ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
|
||||
std::streamsize prec = cout.precision(3);
|
||||
cout << "Company: " << company << endl;
|
||||
cout << "shares: " << shares << endl;
|
||||
cout << "Share Price: $" << share_val << endl;
|
||||
cout.precision(2);
|
||||
cout << "Total Worth: $" << total_val << endl;
|
||||
cout.setf(orig, ios_base::floatfield);
|
||||
cout.precision(prec);
|
||||
}
|
||||
|
||||
const Stock &Stock::topval(const Stock &s) const {
|
||||
if (s.total_val > total_val) {
|
||||
return s;
|
||||
}
|
||||
return *this;
|
||||
}
|
39
base/class/stock20.h
Normal file
39
base/class/stock20.h
Normal file
@ -0,0 +1,39 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/17.
|
||||
//
|
||||
|
||||
#ifndef BASE_STOCK20_H
|
||||
#define BASE_STOCK20_H
|
||||
|
||||
#include <string>
|
||||
|
||||
class Stock {
|
||||
|
||||
private:
|
||||
std::string company;
|
||||
int shares;
|
||||
double share_val;
|
||||
double total_val{};
|
||||
|
||||
void set_tot();
|
||||
|
||||
public:
|
||||
Stock();
|
||||
|
||||
Stock(const std::string &co, long n = 0, double pr = 0.0);
|
||||
|
||||
~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 //BASE_STOCK20_H
|
30
base/class/useStock20.cpp
Normal file
30
base/class/useStock20.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/17.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "stock20.h"
|
||||
|
||||
const int STKS =4;
|
||||
|
||||
int main() {
|
||||
Stock stock[STKS]{
|
||||
Stock("NanoSmart",12,20.0),
|
||||
Stock("Boffo Objects",200,2.0),
|
||||
Stock("Monolithic Obelisks",130,3.25),
|
||||
Stock("Fleep Enterprises",60,6.5)
|
||||
};
|
||||
std::cout <<"Stock holdings: \n";
|
||||
int st;
|
||||
for (st = 0; st < STKS; ++st) {
|
||||
stock[st].show();
|
||||
}
|
||||
const Stock * top = &stock[0];
|
||||
for (st = 1; st < STKS; ++st) {
|
||||
top = &top ->topval(stock[st]);
|
||||
}
|
||||
std::cout<< "\n the most valuble holding:\n";
|
||||
top->show();
|
||||
return 0;
|
||||
}
|
||||
|
54
base/operator/mytime0.cpp
Normal file
54
base/operator/mytime0.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/18.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#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 {
|
||||
std::cout << hours << " hours, " << minutes << " minutes" << std::endl;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
38
base/operator/mytime0.h
Normal file
38
base/operator/mytime0.h
Normal file
@ -0,0 +1,38 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/18.
|
||||
//
|
||||
|
||||
#ifndef BASE_MYTIME0_H
|
||||
#define BASE_MYTIME0_H
|
||||
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* 运算符重载
|
||||
* @param t
|
||||
* @return
|
||||
*/
|
||||
Time operator+(const Time &t) const;
|
||||
|
||||
void Show() const;
|
||||
};
|
||||
|
||||
|
||||
#endif //BASE_MYTIME0_H
|
77
base/operator/mytime2.cpp
Normal file
77
base/operator/mytime2.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/18.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#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::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 {
|
||||
std::cout << hours << " hours, " << minutes << " minutes" << std::endl;
|
||||
}
|
||||
|
||||
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*(double mult) const {
|
||||
Time result;
|
||||
long totalminutes = hours * mult * 60 + minutes * mult;
|
||||
result.hours = totalminutes / 60;
|
||||
result.minutes = totalminutes % 60;
|
||||
return result;
|
||||
}
|
||||
|
||||
Time Time::operator-(const Time &t) const {
|
||||
Time diff;
|
||||
int tot1, tot2;
|
||||
tot1 = t.minutes + 60 * t.hours;
|
||||
tot2 = minutes + 60 * hours;
|
||||
diff.minutes = (tot2 - tot1) % 60;
|
||||
diff.hours = (tot2 - tot1) / 60;
|
||||
return diff;
|
||||
}
|
||||
|
||||
Time operator*(double mult, const Time &t) {
|
||||
return t * mult;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const Time &t) {
|
||||
os << t.hours << " hours, " << t.minutes << " minutes";
|
||||
return os;
|
||||
}
|
46
base/operator/mytime2.h
Normal file
46
base/operator/mytime2.h
Normal file
@ -0,0 +1,46 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/18.
|
||||
//
|
||||
|
||||
#ifndef BASE_MYTIME0_H
|
||||
#define BASE_MYTIME0_H
|
||||
|
||||
#include <ostream>
|
||||
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;
|
||||
|
||||
/**
|
||||
* 运算符重载
|
||||
* @param t
|
||||
* @return
|
||||
*/
|
||||
Time operator+(const Time &t) const;
|
||||
|
||||
Time operator-(const Time &t) const;
|
||||
|
||||
Time operator*(double t) const;
|
||||
|
||||
void Show() const;
|
||||
//友元
|
||||
friend Time operator*(double m,const Time & t);
|
||||
//ostream & 为ostream对象的引用
|
||||
friend std::ostream & operator<<(std::ostream & os,const Time & t);
|
||||
};
|
||||
|
||||
|
||||
#endif //BASE_MYTIME0_H
|
39
base/operator/usetime0.cpp
Normal file
39
base/operator/usetime0.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/18.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "mytime0.h"
|
||||
|
||||
int main() {
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
Time planning;
|
||||
Time coding(2, 40);
|
||||
Time fixing(5, 55);
|
||||
Time total;
|
||||
|
||||
cout << "Planning time = ";
|
||||
planning.Show();
|
||||
cout << "Coding time = ";
|
||||
coding.Show();
|
||||
cout << "Fixing time = ";
|
||||
fixing.Show();
|
||||
|
||||
// total = coding.Sum(fixing);
|
||||
//cout << "coding.Sum(fixing) = ";
|
||||
total = coding + fixing;
|
||||
cout << "coding + fixing = ";
|
||||
total.Show();
|
||||
|
||||
Time morefixing(3, 28);
|
||||
cout << "more fixing time = ";
|
||||
morefixing.Show();
|
||||
cout << endl;
|
||||
total = morefixing.operator+(total);
|
||||
cout << "morefixing.operator+(total) = ";
|
||||
total.Show();
|
||||
return 0;
|
||||
}
|
||||
|
57
base/operator/usetime2.cpp
Normal file
57
base/operator/usetime2.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/18.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "mytime2.h"
|
||||
|
||||
int main() {
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
Time planning;
|
||||
Time coding(2, 40);
|
||||
Time fixing(5, 55);
|
||||
Time waxing(2, 47);
|
||||
Time weeding(4, 35);
|
||||
Time total;
|
||||
Time diff;
|
||||
Time adjusted;
|
||||
|
||||
cout << "Planning time = ";
|
||||
planning.Show();
|
||||
cout << "Coding time = ";
|
||||
coding.Show();
|
||||
cout << "Fixing time = ";
|
||||
fixing.Show();
|
||||
|
||||
// total = coding.Sum(fixing);
|
||||
//cout << "coding.Sum(fixing) = ";
|
||||
total = coding + fixing;
|
||||
cout << "coding + fixing = ";
|
||||
total.Show();
|
||||
|
||||
Time morefixing(3, 28);
|
||||
cout << "more fixing time = ";
|
||||
morefixing.Show();
|
||||
cout << endl;
|
||||
total = morefixing.operator+(total);
|
||||
cout << "morefixing.operator+(total) = ";
|
||||
total.Show();
|
||||
|
||||
diff = weeding - waxing;
|
||||
cout << "weeding time - waxing time = ";
|
||||
diff.Show();
|
||||
|
||||
adjusted = total * 1.5;
|
||||
cout << "adjusted work time = ";
|
||||
adjusted.Show();
|
||||
|
||||
adjusted = operator*(0.5 , total);
|
||||
cout << "1.5 * total = ";
|
||||
adjusted.Show();
|
||||
|
||||
cout <<"10.0 * total: "<< 10.0 * total <<endl;
|
||||
return 0;
|
||||
}
|
||||
|
51
base/overload/randwalk.cpp
Normal file
51
base/overload/randwalk.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/18.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "vector.h"
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
|
||||
using namespace std;
|
||||
using VECTOR::Vector;
|
||||
srand(time(0));
|
||||
double direction;
|
||||
Vector step;
|
||||
Vector result(0.0, 0.0);
|
||||
unsigned long steps = 0;
|
||||
double target;
|
||||
double dstep;
|
||||
cout << "Enter target distance (q to quit)";
|
||||
while (cin >> target) {
|
||||
cout << "Enter step length: ";
|
||||
if (!(cin >> dstep))
|
||||
break;
|
||||
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:\n";
|
||||
cout << result << endl;
|
||||
result.polar_mode();
|
||||
cout << "or\n" << result << endl;
|
||||
cout << "Average outward distance per step = " << result.magval() / steps << endl;
|
||||
steps = 0;
|
||||
result.reset(0.0, 0.0);
|
||||
cout << "Enter target distance {q to quit}";
|
||||
}
|
||||
cout << "Bye!\n";
|
||||
cin.clear();
|
||||
while (cin.get() != '\n')
|
||||
continue;
|
||||
return 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
52
base/overload/stone.cpp
Normal file
52
base/overload/stone.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/18.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "stonewt.h"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
void display(const Stonewt &st, int n);
|
||||
|
||||
int main() {
|
||||
|
||||
Stonewt incognito = 275;
|
||||
Stonewt wolfe(285.7);
|
||||
Stonewt taft(21, 8);
|
||||
|
||||
cout << "The celebrity weighed ";
|
||||
incognito.show_stn();
|
||||
cout << "The detective weighed";
|
||||
wolfe.show_stn();
|
||||
cout << "The President weighed";
|
||||
taft.show_lbs();
|
||||
|
||||
incognito = 276.8;
|
||||
taft = 325;
|
||||
cout << "After dinner,the Celebrity weighed ";
|
||||
incognito.show_stn();
|
||||
cout << "After dinner,the President weighed ";
|
||||
taft.show_lbs();
|
||||
display(taft, 2);
|
||||
cout << "The wrestler weighed even more.\n";
|
||||
display(422, 2);
|
||||
cout << "No stone left unearned\n";
|
||||
|
||||
cout << endl;
|
||||
Stonewt poppins(9, 2.8);
|
||||
double p_wt = poppins;
|
||||
cout << "Convert to double => ";
|
||||
cout << "Poppins: " << p_wt << " pounds." << endl;
|
||||
cout << "Convert to int => ";
|
||||
cout << "Poppins: " << int(poppins) << " pounds.\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
void display(const Stonewt &st, int n) {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cout << "Wow! ";
|
||||
st.show_stn();
|
||||
}
|
||||
}
|
42
base/overload/stonewt.cpp
Normal file
42
base/overload/stonewt.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/18.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "stonewt.h"
|
||||
|
||||
Stonewt::Stonewt(double lbs) {
|
||||
stone = int(lbs) / Lbs_per_stn;
|
||||
pds_left = int(lbs) % Lbs_per_stn;
|
||||
pounds = lbs;
|
||||
}
|
||||
|
||||
Stonewt::Stonewt(int stn, double lbs) {
|
||||
stone = stn;
|
||||
pds_left = lbs;
|
||||
pounds = stn * Lbs_per_stn + lbs;
|
||||
}
|
||||
|
||||
Stonewt::Stonewt() {
|
||||
stone = pounds = pds_left = 0;
|
||||
}
|
||||
|
||||
Stonewt::~Stonewt() {
|
||||
|
||||
}
|
||||
|
||||
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 int() const {
|
||||
return int(pounds + 0.5);
|
||||
}
|
||||
|
||||
Stonewt::operator double() const {
|
||||
return pounds;
|
||||
}
|
27
base/overload/stonewt.h
Normal file
27
base/overload/stonewt.h
Normal file
@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/18.
|
||||
//
|
||||
|
||||
#ifndef BASE_STONEWT_H
|
||||
#define BASE_STONEWT_H
|
||||
|
||||
|
||||
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();
|
||||
~Stonewt();
|
||||
void show_lbs() const;
|
||||
void show_stn() const;
|
||||
operator int() const;
|
||||
operator double () const;
|
||||
};
|
||||
|
||||
#endif //BASE_STONEWT_H
|
147
base/overload/vector.cpp
Normal file
147
base/overload/vector.cpp
Normal file
@ -0,0 +1,147 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/18.
|
||||
//
|
||||
|
||||
#include "vector.h"
|
||||
#include <cmath>
|
||||
|
||||
using namespace std;
|
||||
using std::sqrt;
|
||||
using std::sin;
|
||||
using std::cos;
|
||||
using std::atan;
|
||||
using std::atan2;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
namespace VECTOR {
|
||||
const double Rad_to_deg = 45.0 / atan(1.0);
|
||||
|
||||
void Vector::set_mag() {
|
||||
mag = Q_rsqrt(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_deg;
|
||||
set_x();
|
||||
set_y();
|
||||
} else {
|
||||
cout << "Incorrect 3rd argument to Vector() --";
|
||||
cout << "Vector set to 0 \n";
|
||||
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_deg;
|
||||
set_x();
|
||||
set_y();
|
||||
} else {
|
||||
cout << "Incorrect 3rd argument to Vector() --";
|
||||
cout << "Vector set to 0 \n";
|
||||
x = y = mag = ang = 0.0;
|
||||
mode = RECT;
|
||||
};
|
||||
}
|
||||
|
||||
Vector::~Vector() {}
|
||||
|
||||
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 << "}";
|
||||
} else if (v.mode == Vector::POL) {
|
||||
os << "(m,a) = {" << v.mag << ", " << v.ang * Rad_to_deg << "}";
|
||||
} else {
|
||||
os << "Vector object mode is invalid";
|
||||
}
|
||||
return os;
|
||||
}
|
||||
//end namespace VECTOR
|
||||
}
|
||||
|
||||
/**
|
||||
* 快速平方根倒数算法
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
float Q_rsqrt(float number) {
|
||||
long i;
|
||||
float x2, y;
|
||||
const float threehalfs = 1.5F;
|
||||
|
||||
x2 = number * 0.5F;
|
||||
y = number;
|
||||
i = *(long *) &y; // evil floating point bit level hacking
|
||||
i = 0x5f3759df - (i >> 1); // what the fuck?
|
||||
y = *(float *) &i;
|
||||
y = y * (threehalfs - (x2 * y * y)); // 1st iteration
|
||||
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
|
||||
return y;
|
||||
}
|
56
base/overload/vector.h
Normal file
56
base/overload/vector.h
Normal file
@ -0,0 +1,56 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/18.
|
||||
//
|
||||
|
||||
#ifndef BASE_VECTOR_H
|
||||
#define BASE_VECTOR_H
|
||||
#include <iostream>
|
||||
//名称空间VECTOR namespace VECTOR
|
||||
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 form = RECT);
|
||||
void reset(double n1,double n2,Mode form = RECT);
|
||||
~Vector();
|
||||
double xval() const{
|
||||
return x;
|
||||
}
|
||||
double yval(){
|
||||
return y;
|
||||
}
|
||||
double magval(){
|
||||
return mag;
|
||||
}
|
||||
double angval(){
|
||||
return ang;
|
||||
}
|
||||
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);
|
||||
};
|
||||
//end namespace VECTOR
|
||||
}
|
||||
|
||||
float Q_rsqrt(float number);
|
||||
|
||||
#endif //BASE_VECTOR_H
|
Loading…
Reference in New Issue
Block a user