- wrote the class template of the chain stack, saved it as the header. h file, and designed a simple addition, subtraction, multiplication and division calculator based on this. In the process of compilation, the following error occurred. I don"t know how to solve it. Ask for the guidance of God ~
"header. H" file is as follows:
//
-sharpinclude<cassert>
using namespace std;
template<typename T>class Stack;
template<typename T>class Node{ //
T info;
Node<T>*link;
public:
Node(T date = 0, Node<T>*next = NULL) {
info=data; //C2563:
link=next; //C2568: "="
}
friend class Stack<T>;
};
template<typename T>class Stack{ //
Node<T>*top; //
public:
Stack() { top = NULL; }
~Stack();
void Push(const T &data); //
T Pop(); //
T GetTop(); //
void MakeEmpty(); //
bool IsEmpty(){ return top == NULL; }
};
template<typename T>Stack<T>::~Stack() { MakeEmpty(); }
template<typename T>void Stack<T>::MakeEmpty() {
Node<T>*temp;
while (top != NULL) { temp = top; top = top->link; delete temp; }
}
template<typename T>void Stack<T>::Push(const T &data) {
top = new Node<T>(data, top);
}
template<typename T>T Stack<T>::Pop() {
assert(!IsEmpty());
Node<T>*temp = top;
T data = temp->info;
top = top->link; //
delete temp; //
return data; //
}
template<typename T>T Stack<T>::GetTop() {
assert(!IsEmpty());
return top->info;
}
.cpp:
-sharpinclude<iostream>
-sharpinclude<cmath>
-sharpinclude<cstdlib>
-sharpinclude".h"
using namespace std;
class Calculator { //
Stack<int>Nstack;
Stack<char>Ostack;
public:
Calculator(void) { };
void Cal(void);
void GetTwoNum(int &Num1, int &Num2);
void Compute(char Opr);
void Clear(void);
};
void Calculator::Clear() {
Nstack.MakeEmpty();
Ostack.MakeEmpty();
}
void Calculator::GetTwoNum(int &Num1, int &Num2) {
Num1 = Nstack.Pop();
Num2 = Nstack.Pop();
}
void Calculator::Compute(char Opr) {
int Num1, Num2;
if (Opr != "=")GetTwoNum(Num1, Num2);
switch (Opr) {
case "+":Nstack.Push(Num2 + Num1); break; //
case "-":Nstack.Push(Num2 - Num1); break;
case "*":Nstack.Push(Num2 * Num1); break;
case "/":Nstack.Push(Num2 / Num1); break;
case "=":cout << Nstack.Pop() << endl;
}
}
void Calculator::Cal() { //or
bool b1 = true,b2 = true;
char ch1, ch2, str[50]; //ch1ch2
int k = -1;
while (b2) {
cin >> ch1;
if (ch1 >= "0"&&ch1 <= "9") {
kPP;
str[k] = ch1;//
}
else {
if (k >= 0) {
str[k + 1] = "\0";
Nstack.Push(atoi(str)); //
k = -1;
}
switch (ch1) {
case"c":
Clear();
break;
case"+":
case"-": //+ - =
while (!Ostack.IsEmpty()) {
ch2 = Ostack.Pop();
Compute(ch2);
}
Ostack.Push(ch1);
break;
case"*":
case"/":
while (!Ostack.IsEmpty() && b1) {
ch2 = Ostack.Pop(); //
if (ch2 == "*" || ch2 == "/") //
Compute(ch2); // * / * or /
else {
Ostack.Push(ch2); // ,
b1 = false;
}
}
Ostack.Push(ch1); //
b1 = true;
break;
case"=":
while (!Ostack.IsEmpty()) {
ch2 = Ostack.Pop();
Compute(ch2);
}
Compute(ch1);
break;
}
if (ch1 == "z") b2 = false;
}
}
}
int main(){
Calculator Calcul;
cout << ":" << endl;
Calcul.Cal();
getchar();
return 0;
}
:
![][1]
[1]: /img/bVbaCGz