template <class ItemType>
void InsertSort(ItemType A[], long N)
{ for (int pass = 1; pass < N; pass++)
{ ItemType Temp = A[pass];
int J = pass;
while( (J > 0) && (A[J-1] > Temp)) // change < to > for smallest to largest
{A[J] = A[J-1];
J--;
}
A[J] = Temp;
}
}
for(int j=0; j < N; j++)
{ F << NumbArray[j];
if(j %
10 == 9)
F << endl;
}
F.close();
}
template <class ItemType>
class stack
{
public:
void push( ItemType );
void pop ();
ItemType top();bool empty();
stack();
~stack();private:
struct node
{
ItemType item;
node *next;
};node* head;
};
#include "stack.cpp"
head
48
91
24
template <class ItemType>
void stack<ItemType>::push(ItemType I)
{ node* Temp;
Temp = new node;
(Temp -> next) = head;
(Temp -> item) = I;
head = Temp;
}
template <class ItemType>
bool stack<ItemType>::empty()
{ return (head == NULL);
}
template <class ItemType>
stack<ItemType>::stack()
{ head = NULL;
}
#include "stack.h"
const int OperatorError = -32767;
const int IncompleteError = -32766;
const int IllegalCharacter = -32768;
const int DivisionByZero = -32765;
const int EmptyStack = -32764;
int Evaluate( char Expression[] )
{ int count = 0;
int Operand1, Operand2;
stack<int> ExpStack;
while (Expression[count] != '#')
{ switch (Expression[count]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
ExpStack.push(Expression[count] - '0');
break;
case '+':
if(!ExpStack.empty()) {
Operand1 = ExpStack.top();
ExpStack.pop();}
else
return OperatorError;
if (!ExpStack.empty()) {
Operand2 = ExpStack.top();
ExpStack.pop();}
else
return OperatorError;
ExpStack.push(Operand1+Operand2);
break;
case '-':
if(!ExpStack.empty()) {
Operand1 = ExpStack.top();
ExpStack.pop();}
else
return OperatorError;
if (!ExpStack.empty()) {
Operand2 = ExpStack.top();
ExpStack.pop();}
else
return OperatorError;
ExpStack.push(Operand2-Operand1);
break;
case '*':
if(!ExpStack.empty()) {
Operand1 = ExpStack.top();
ExpStack.pop();}
else
return OperatorError;
if (!ExpStack.empty()) {
Operand2 = ExpStack.top();
ExpStack.pop();}
else
return OperatorError;
ExpStack.push(Operand1*Operand2);
break;
case '/':
if(!ExpStack.empty()) {
Operand1 = ExpStack.top();
ExpStack.pop();}
else
return OperatorError;
if (!ExpStack.empty()) {
Operand2 = ExpStack.top();
ExpStack.pop();}
else
return OperatorError;
if (Operand1 != 0)
ExpStack.push(Operand2/Operand1);
else
return DivisionByZero;
break;
default:
return IllegalCharacter;
}
count ++;
}
if (!ExpStack.empty()) {
Operand1 = ExpStack.top();
ExpStack.pop(); }
else
return EmptyStack;
if (ExpStack.empty())
return Operand1;
else
return IncompleteError;
} // end Evaluate
void main()
{ char Exp[80];
fstream InFile, OutFile;
InFile.open("Test.txt", ios::in);
OutFile.open("Result.txt", ios::out);
while( !InFile.eof()) {
InFile.getline(Exp, 80, '\n');
int value = Evaluate(Exp);
if (value == OperatorError)
OutFile << Exp <<
" error -- two few operands for operator" << endl;
else if (value == IncompleteError)
OutFile << Exp <<
" error -- expression incomplete" << endl;
else if (value == IllegalCharacter)
OutFile << Exp <<
" error -- illegal character found" << endl;
else if (value == DivisionByZero)
OutFile << Exp <<
" error -- division by zero." << endl;
else if (value == EmptyStack)
OutFile << Exp <<
" error -- no value left on stack at end";
else
OutFile << Exp <<
" has a value of " << value << endl; }
InFile.close();
}
void main()
{ char Name[80];
fstream F;
F.open("Names.txt", ios::in);
for(int j=0; j<10; j++)
{ F.getline(Name,
80, '\n');
cout <<
Name << endl;
}
F.close();
}
node* rootNode;
int Size;
void DeleteTree(node* &);
void PrintAscending(fstream
&, node*);
void PrintDescending(fstream
&, node*);
void InsertTree(node*
&, T);
};