添加template

This commit is contained in:
朱毅骏 2021-08-31 18:17:55 +08:00
parent d31083805f
commit 7ea01419f1
7 changed files with 295 additions and 4 deletions

View File

@ -56,4 +56,6 @@ add_executable(placenew new/placenew.cpp)
add_executable(usett extend/usett.cpp)
add_executable(test5_1 test5.cpp)
add_executable(studentc reusing/studentc.cpp)
add_executable(uses_stuc reusing/uses_stuc.cpp)
add_executable(uses_stuc reusing/uses_stuc.cpp)
add_executable(stacktp stl/stacktp.cpp)
add_executable(stacktp1 stl/stacktp1.cpp)

25
base/stl/arraytp.cpp Normal file
View File

@ -0,0 +1,25 @@
//
// Created by zhuyijun on 2021/8/31.
//
#include <iostream>
#include "arraytp.h"
using namespace std;
int main() {
ArrayTP<int,10> sums;
ArrayTP<double,10> aves;
ArrayTP<ArrayTP<int,5>,10> twodee;
int i,j;
for (i = 0; i < 10; ++i) {
sums[i] = 0;
for (int k = 0; k < ; ++k) {
}
}
return 0;
}

48
base/stl/arraytp.h Normal file
View File

@ -0,0 +1,48 @@
//
// Created by zhuyijun on 2021/8/31.
//
#ifndef BASE_ARRAYTP_H
#define BASE_ARRAYTP_H
#include <iostream>
#include <cstdlib>
template<class T, int n>
class ArrayTP {
private:
T ar[n];
public:
ArrayTP() {};
explicit ArrayTP(const T &v);
virtual T &operator[](int i);
virtual T operator[](int i) const;
};
template<class T, int n>
ArrayTP<T,n>::ArrayTP<class T, int n>() {
for (int i = 0; i < n; ++i) {
ar[i] = v;
}
}
template<class T, int n>
T & ArrayTP<T,n>::operator[](int i) {
if (i <0 || i >= n){
std::cerr <<"Error in array limits: "<< i <<" is out of range\n";
std::exit(EXIT_FAILURE);
}
return a[i];
}
template<class T, int n>
T & ArrayTP<T,n>::operator[](int i) const {
if (i <0 || i >= n){
std::cerr <<"Error in array limits: "<< i <<" is out of range\n";
std::exit(EXIT_FAILURE);
}
return a[i];
}
#endif //BASE_ARRAYTP_H

View File

@ -3,12 +3,48 @@
//
#include <iostream>
#include <string>
#include <cctype>
#include "stacktp.h"
using namespace std;
int main()
{
Stack<string> st;
char ch;
std::string po;
cout <<"Please enter A to add a purchase order, \n"
<<"P to 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\n";
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

@ -5,12 +5,58 @@
#ifndef BASE_STACKTP_H
#define BASE_STACKTP_H
template<class Type>
class Stack {
private:
enum {
MAX = 10
};
Type items[MAX];
int top;
public:
Stack();
bool isEmpty();
class stacktp {
bool isFull();
bool push(const Type &item);
bool pop(Type &item);
};
template<class Type>
Stack<Type>::Stack() {
top = 0;
}
template<class Type>
bool Stack<Type>::isEmpty() {
return top = 0;
}
template<class Type>
bool Stack<Type>::isFull() {
return top == MAX;
}
template<class Type>
bool Stack<Type>::push(const Type &item) {
if (top < MAX) {
items[top++] = item;
return true;
}
return false;
}
template<class Type>
bool Stack<Type>::pop(Type &item) {
if (top > 0) {
item = items[--top];
return true;
}
return false;
}
#endif //BASE_STACKTP_H

53
base/stl/stacktp1.cpp Normal file
View File

@ -0,0 +1,53 @@
//
// Created by nicemoe on 2021/8/30.
//
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "stacktp1.h"
using namespace std;
const int Num = 10;
int main()
{
srand(time(0));
cout <<"Please enter stack size: ";
int stacksize;
cin >> stacksize;
Stack<const char *> st(stacksize);
const char * in[Num] = {
"1: Hank Gilgamesh",
"2 :Kiki Ishtar",
"3 :Betty Rocker",
"4 :Ian Flagranti",
"5 :Wolfgang Kibble",
"6 :Portia Koop",
"7 :Joy Almondo",
"8 :Xaverie Paprika",
"9 :Juan Moore",
"10 :Misha Mache",
};
const char * out[Num] = {nullptr};
int processed = 0;
int nextin = 0;
while (processed < Num){
if (st.isEmpty())
st.push(in[nextin++]);
else if (st.isFull())
st.pop(out[processed++]);
else if (rand() % 2 && nextin < Num)
st.push(in[nextin++]);
else
st.pop(out[processed++]);
}
for (int i = 0; i < Num; i++) {
if (out[i] == nullptr){
continue;
}
cout << out[i] <<endl;
}
cout <<"Bye \n";
return 0;
}

81
base/stl/stacktp1.h Normal file
View File

@ -0,0 +1,81 @@
//
// Created by nicemoe on 2021/8/30.
//
#ifndef BASE_STACKTP1_H
#define BASE_STACKTP1_H
template<class Type>
class Stack {
private:
enum {
SIZE = 10
};
int stacksize;
Type * items;
int top;
public:
explicit Stack(int ss = SIZE);
Stack(const Stack & st);
~Stack(){delete []items;}
bool isEmpty(){return top = 0; };
bool isFull(){ return top == stacksize;};
bool push(const Type &item);
bool pop(Type &item);
Stack & operator=(const Stack & st);
};
template<class Type>
Stack<Type>::Stack(int ss) :stacksize(ss),top(0) {
items = new Type[stacksize];
}
template<class Type>
Stack<Type>::Stack(const Stack & st) {
stacksize = st.stacksize;
top = st.top;
items = new Type[stacksize];
for (int i = 0; i < top; ++i) {
items[i] = st.items;
}
}
template<class Type>
bool Stack<Type>::push(const Type &item) {
if (top < stacksize) {
items[top++] = item;
return true;
}
return false;
}
template<class Type>
bool Stack<Type>::pop(Type &item) {
if (top > 0) {
item = items[--top];
return true;
}
return false;
}
template<class Type>
Stack<Type> & Stack<Type>::operator=(const Stack<Type> & st) {
if(this == &st){
return *this;
}
delete[] items;
stacksize = st.stacksize;
top = st.top;
items = new Type[stacksize];
for (int i = 0; i < top; ++i) {
items[i] = st.items[i];
}
return * this;
}
#endif //BASE_STACKTP1_H