本人想在计算器类中使用链式栈 但是不清楚为什么错了 这个代码都是书上打的 殷人昆的数据结构第二版 一共是三个文件 请大家帮帮看看到底是什么问题 小弟不甚感激
#include<math.h>
#include"LinkedStack.h"
#include<iostream>
using namespace std;
class Calculator
{
public:
Calculator(int sz):s(sz) {};//构造函数
void Run();//执行表达式计算
void Clear();
private:
LinkedStack <double>s;//栈对象定义
void AddOperand(double value);//进操作数栈
bool get2Operands(double& left,double &right);//从栈中退出两个操作数
void DoOperator(char op);//形成运算指令 进行计算
};
void Calculator::DoOperator(char op)
{
double left,right,value;int result;
result=get2Operands (left,right);
if(result==true)
switch (op)
{
case""+"":value=left+right;s.Push (value);break;
case""-"":value=left-right;s.Push (value);break;
case""*"":value=left*right;s.Push (value);break;
case""/"":if(right ==0.0)
{
cerr<<"Divide by 0!"<<endl;
Clear();
}
else{value=left/right ;s.Push (value);}
break;
}
else Clear ();
};
bool Calculator ::get2Operands (double & left,double &right)
{
if(s.IsEmpty ()==true)
{
cerr<<"缺右操作数"<<endl;return false ;}
s.Pop (right);
if(s.IsEmpty ()==true)
{
cerr<<" 缺操作数"<<endl;return false ;}
s.Pop (left);
return false;
};
void Calculator ::AddOperand (double value)
{
s.Push (value);
};
void Calculator ::Run()
{
char ch;double newOperand;
while(cin>>ch,ch!=""#"")
{
switch(ch)
{
case""+"":case""-"":case""*"":case""/"":
DoOperator (ch);break ;
default :cin.putback(ch);
cin>>newOperand ;
AddOperand (newOperand );
}
}
};
void Calculator ::Clear ()
{
s.makeEmpty ();
};
第二个
#ifndef LINKEDSTACK_H
#define LINKEDSTACK_H
#include <iostream>
#include <cassert>
using namespace std;
template <typename T>struct StackNode{
T data;
StackNode<T> *link;
StackNode(T d = 0, StackNode<T> *next = NULL):link(next),data(d){}
};
template <typename T>
class LinkedStack{
private:
StackNode<T> *top;
public:
LinkedStack():top(NULL){}//无头结点
~LinkedStack(){
makeEmpty();
}
void Push(const T &x);
bool Pop(T &x);
bool getTop(T &x)const;
int getSize()const;
bool IsEmpty()const{
return top == NULL;
}
bool IsFull()const{
return false;
}
void makeEmpty();
friend ostream& operator << (ostream &os, LinkedStack<T> &s) {
os << "Stack Size: " << s.getSize() << endl;
StackNode<T> *p = s.top;
int i = 0;
while (p){
os << ++i << ": " << p->data << endl;
p = p->link;
}
return os;
}
};
template <typename T>
void LinkedStack<T>::makeEmpty()
{
StackNode<T> *p;
while (top){//最后top为NULL
p = top;
top = top->link;
delete p;
}
}
template <typename T>
void LinkedStack<T>::Push(const T &x)
{
top = new StackNode<T>(x, top);
assert(top);
}
template <typename T>
bool LinkedStack<T>::Pop(T &x)
{
if (IsEmpty()){
return false;
}
StackNode<T> *p = top;
top = top->link;
x = p->data;
delete p;
return true;
}
template <typename T>
bool LinkedStack<T>::getTop(T &x)const
{
if (IsEmpty()) return false;
x = top->data;
return true;
}
template <typename T>
int LinkedStack<T>::getSize()const
{
StackNode<T> *p = top;
int k = 0;
while (p){
p = p->link;
k++;
}
return k;
}
#endif
第三个
#include "LinkedStack.h"
#include"Calculator.h"
#include <fstream>
#include <cassert>
using namespace std;
int main(){
Calculator cc;
cc.Run ();
LinkedStack<int> sta;
ifstream fin("data.txt");
assert(fin);
int data;
while (!fin.eof()){
assert(fin >> data);
sta.Push(data);
}
cout << "The initial Stack in the file is:\n" << sta;
cout << "The current size of the Stack is: " << sta.getSize() << endl;
sta.getTop(data);
cout << "The current Top of the Stack is : " << data << endl;
sta.Pop(data);
cout << "\nDo a Pop operation, then the stack is:\n" << sta << endl;
cout << "The data popped is: " << data << endl;
sta.getTop(data);
cout << "The current Top of the Stack is : " << data << endl;
cout << "\nTest the state of the stack:\n";
if (sta.IsEmpty()) cout << "The stack is empty now!\n";
else if (sta.IsFull()) cout << "The stack is full now!\n";
else cout << "The stack is not empty and not full now!\n";
sta.makeEmpty();
cout << "Now make the stack empty, then the state of the stack is:\n";
if (sta.IsEmpty()) cout << "The stack is empty now!\n";
else if (sta.IsFull()) cout << "The stack is full now!\n";
else cout << "The stack is not empty and not full now!\n";
return 0;
}
解决方案:15分
解决方案:5分
对可能出错的地方加上打印。
当然,逐步跟踪是最好的解决问题的办法
当然,逐步跟踪是最好的解决问题的办法
解决方案:20分
不要依赖调试器输出复杂数据结构!而要将复杂数据结构的整个内容在处理它的每一步使用一小段代码按本人很容易理解的格式输出,非常有助于调试!或可以说是“基础设施”
仅供参考:
仅供参考:
//不带表头结点的单向链表
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <locale.h>
struct NODE {
int data;
struct NODE *next;
} *head,*p,*q,*s,*p1,*p2,*q1,**ta;
int i,k,n,t,m,v,N=10;
int main() {
setlocale(LC_ALL,"chs");
srand(time(NULL));
head=NULL;
printf("创建%d个节点的单链表:",N);//创建N个节点的单链表
p=head;
for (i=0;i<N;i++) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) exit(1);
q->data=rand()%100;//填写0..99的随机值
q->next=NULL;
if (NULL==p) {
head=q;
p=head;
} else {
p->next=q;
p=q;
}
}
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
k=3;
v=5;
printf("将值为%d的结点插入到单链表的第%d个结点前:",v,k);//将值为v的结点插入到单链表的第k个结点前
n=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
n++;
if (k==1) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) exit(1);
q->data=5;
q->next=head;
head=q;
break;
} else {
if (k-1==n) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) exit(1);
q->data=5;
q->next=p->next;
p->next=q;
break;
}
}
p=p->next;
}
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
k=5;
printf("删除第%d个节点:",k);//删除第k个节点
n=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
n++;
if (k==1) {
q=head;
head=head->next;
free(q);
break;
} else {
if (k-1==n) {
q=p->next;
if (q) {
p->next=q->next;
free(q);
}
break;
}
}
p=p->next;
}
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
printf("从小到大排序:");//从小到大排序
for (p=head,p1=NULL;p!=NULL;p1=p,p=p->next) {
for (q=p->next,q1=p;q!=NULL;q1=q,q=q->next) {
if (p->data > q->data) {
//交换data
// printf("swap %02d %02d\n",p->data,q->data);
// t=p->data;p->data=q->data;q->data=t;
//或
//交换next
// printf("swap %02d %02d\n",p->data,q->data);
if (p==head) {//p是头
if (p->next==q) {//pq挨着
head=q;
p->next=q->next;
q->next=p;
q=p;
p=head;
} else {//pq不挨着
head=q;
p2=p->next;
p->next=q->next;
q->next=p2;
q1->next=p;
q=p;
p=head;
}
} else {//p不是头
if (p->next==q) {//pq挨着
p1->next=q;
p->next=q->next;
q->next=p;
q=p;
p=p1->next;
} else {//pq不挨着
p1->next=q;
p2=p->next;
p->next=q->next;
q->next=p2;
q1->next=p;
q=p;
p=p1->next;
}
}
//输出整个单链表
// s=head;
// while (1) {
// if (NULL==s) {
// printf("\n");
// break;
// }
// printf("%02d->",s->data);
// s=s->next;
// }
// getchar();
}
}
}
//输出整个单链表并计算链表长度n
n=0;
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
n++;
s=s->next;
}
printf("将整个链表逆序:");//将整个链表逆序
if (n>=2) {
p=head;
q=p->next;
p->next=NULL;
while (1) {
q1=q->next;
q->next=p;
p=q;
q=q1;
if (NULL==q) break;
}
head=p;
}
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
m=4;
n=6;
printf("将单链表中前%d个结点和后%d个结点进行互换:",m,n);//将单链表中前m个结点和后n个结点进行互换,m+n为链表总长
k=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
k++;
if (m==k) {
q=p;
}
s=p;
p=p->next;
}
q1=head;
head=q->next;
s->next=q1;
q->next=NULL;
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//释放全部节点
p=head;
while (1) {
if (NULL==p) {
break;
}
q=p->next;
free(p);
p=q;
}
return 0;
}
//创建10个节点的单链表:08->74->07->23->03->99->31->56->88->16->
//将值为5的结点插入到单链表的第3个结点前:08->74->05->07->23->03->99->31->56->88->16->
//删除第5个节点:08->74->05->07->03->99->31->56->88->16->
//从小到大排序:03->05->07->08->16->31->56->74->88->99->
//将整个链表逆序:99->88->74->56->31->16->08->07->05->03->
//将单链表中前4个结点和后6个结点进行互换:31->16->08->07->05->03->99->88->74->56->
//