后綴二叉樹:
這是錯誤:
||=== Build: Debug in 后綴計算器 (compiler: GNU GCC Compiler) ===|
obj\Debug\postfixcounter.o||In function `ZN7postfix10importtreeEv':|
E:\codeblocks的實驗\后綴計算器\postfixcounter.cpp|139|undefined reference to `postfix::newtree(char)'|
E:\codeblocks的實驗\后綴計算器\postfixcounter.cpp|143|undefined reference to `postfix::newtree(char)'|
E:\codeblocks的實驗\后綴計算器\postfixcounter.cpp|147|undefined reference to `postfix::newtree(char)'|
E:\codeblocks的實驗\后綴計算器\postfixcounter.cpp|164|undefined reference to `postfix::newtree(char)'|
E:\codeblocks的實驗\后綴計算器\postfixcounter.cpp|169|undefined reference to `postfix::newtree(char)'|
obj\Debug\postfixcounter.o:E:\codeblocks的實驗\后綴計算器\postfixcounter.cpp|173|more undefined references to `postfix::newtree(char)' follow|
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|
這是源檔案:
頭檔案:
#ifndef POSTFIXCOUNTER_H_INCLUDED
#define POSTFIXCOUNTER_H_INCLUDED
#include<string>
using namespace std;
struct nodeType
{
nodeType*left;
nodeType*right;
char symbol;
int number;
};
class postfix
{
public:
postfix();
postfix(const postfix&);
~postfix();
void locate();
void logicRule();
nodeType* newtree(char );
void importtree();
int backlargeNumber();
int addressing(int);
int output();
void subtraction(nodeType*);
void add(nodeType*);
void multiply(nodeType*);
void distraction(nodeType*);
private:
int a[20][2];
int b[20];
bool c[20];
int largeNumber;
nodeType* mycounterarray[20];
nodeType* head;
nodeType* current;
nodeType* trailcurrent;
string mycounter;
const char* first;
int maxsize;
int length;
};
#endif // POSTFIXCOUNTER_H_INCLUDED
這是類的定義:
#include<string>
#include<iostream>
#include"postfixcounter.h"
using namespace std;
postfix::postfix()
{
cout<<"please input your postfix counter: ";
cin>>mycounter;
head=mycounterarray[0];
first=mycounter.c_str();
maxsize=length=mycounter.length();
largeNumber=0;
b[20]=0;
c[20]=0;
}
postfix::postfix(const postfix& other)
{
this->mycounter=other.mycounter;
first=this->mycounter.c_str();
length=maxsize=this->mycounter.length();
largeNumber=0;
}
void postfix::locate()
{
a[0][1]=-1;
a[0][0]=length+1;
largeNumber=1;
for(int i=0;i<=length;i++)
{
if(first[i]=='(')
{
a[largeNumber][1]=i;
largeNumber++;
}
if(first[i]==')')
{
for(int k=19;k>=0;k--)
{
if(a[k][1]!=0&&a[k][0]==0)
{
a[k][0]=i;
break;
}
}
}
}
}
void postfix::logicRule()
{
for(int i=largeNumber-1;i>=0;i--)
{
if(a[i][1]>a[i-1][1])
{
if(a[i][0]<a[i-1][0])
{
b[i]=largeNumber;
c[i]=false;
}
if(a[i][0]>a[i-1][0])
{
b[i]=largeNumber-1;
c[i]=false;
}
}
largeNumber--;
if(i==0)
{
b[0]=1;
}
}
}
nodeType* newtree(char temp)
{
nodeType* temp2;
switch(temp)
{
case('+'):
{
temp2=new nodeType;
temp2->symbol='+';
break;
}
case('-'):
{
temp2=new nodeType;
temp2->symbol='-';
break;
}
case('*'):
{
temp2=new nodeType;
temp2->symbol='*';
break;
}
case('/'):
{
temp2=new nodeType;
temp2->symbol='/';
break;
}
case(')'):
{
break;
}
case('('):
{
break;
}
default:
{
temp2=new nodeType;
temp2->number=int(temp)-48;
break;
}
}
return temp2;
}
void postfix::importtree()
{
nodeType* thefirst;
int temp;
for(int k=backlargeNumber()-1;k>=0;k--)
{
int start=a[k][0]-1;
int last=a[k][1]+1;
if((start-last)==2)
{
for(int i=start;i>=last;i--)
{
char cash=first[i];
if(i==start)
{
current=newtree(cash);
}
if(i==start-1)
{
current->right=newtree(cash);
}
if(i==last)
{
current->left=newtree(cash);
}
}
mycounterarray[k]=current;
}
else
{
if((start-last)>2)
{
int g=0;
for(int i=start;i>=last;i--)
{
char cash=first[i];
if(first[i]!=')')
{
if(g==0)
{
current=newtree(cash);
thefirst=current;
}
if(g==1)
{
current->right=newtree(cash);
}
if(g==2)
{
current->left=newtree(cash);
}
if(g==3)
{
current=current->left;
g=1;
current->right=newtree(cash);
}
}
else
{
if(g==1)
{
temp=addressing(k);
current->right=mycounterarray[temp];
i=a[temp][1];
}
if(g==2)
{
temp=addressing(k);
current->left=mycounterarray[temp];
i=a[temp][1];
}
if(g==3)
{
current=current->left;
g=1;
temp=addressing(k);
current->right=mycounterarray[temp];
i=a[temp][1];
}
}
g++;
mycounterarray[k]=thefirst;
}
}
}
}
}
int postfix::addressing(int k)
{
int goseek=k+1;
int i=0;
for(i=largeNumber-1;i>=0;i--)
{
if(b[i]==goseek+1&&c[i]!=true)
{
break;
}
}
c[i]=true;
return i;
}
int postfix::backlargeNumber()
{
int i;
for(i=0;i<=19;i++)
{
if(b[i]=0)
{
break;
}
}
return i;
}
void postfix::add(nodeType* temp)
{
temp->number=temp->left->number+temp->right->number;
delete temp->left;
delete temp->right;
}
void postfix::subtraction(nodeType* temp)
{
temp->number=temp->left->number-temp->right->number;
delete temp->left;
delete temp->right;
}
void postfix::multiply(nodeType* temp)
{
temp->number=temp->left->number*temp->right->number;
delete temp->left;
delete temp->right;
}
void postfix::distraction(nodeType* temp)
{
temp->number=temp->left->number/temp->right->number;
delete temp->left;
delete temp->right;
}
int postfix::output()
{
for(int i=backlargeNumber()-1;i>=0;i--)
{
switch(mycounterarray[i]->symbol)
{
case('+'):
{
add(mycounterarray[i]);
break;
}
case('-'):
{
subtraction(mycounterarray[i]);
break;
}
case('/'):
{
distraction(mycounterarray[i]);
break;
}
case('*'):
{
multiply(mycounterarray[i]);
break;
}
}
}
return mycounterarray[0]->number;
}
postfix::~postfix()
{
delete[] a;
delete[] b;
delete[] c;
delete head;
delete[] mycounterarray;
delete[] first;
mycounter="";
current=NULL;
trailcurrent=NULL;
maxsize=0;
length=0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/63017.html
標籤:基礎類
上一篇:大神們,求解決方法。
