添加class的demo

This commit is contained in:
zhuyijun 2021-08-17 20:01:39 +08:00
parent 39fe55348c
commit e04bd3dd9e
8 changed files with 7 additions and 266 deletions

1
base/.gitignore vendored
View File

@ -4,4 +4,5 @@
.vscode
/cmake-build-debug
*.o
*.exe

View File

@ -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 (添加对象)

View File

@ -1,34 +0,0 @@
//
// 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;
}

View File

@ -1,29 +0,0 @@
//
// 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

View File

@ -1,50 +0,0 @@
//
// 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;
}

View File

@ -1,83 +0,0 @@
//
// 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;
}

View File

@ -1,39 +0,0 @@
//
// 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

View File

@ -1,30 +0,0 @@
//
// 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;
}