A more specific question: Why isn't my program pushing and poping correctly?
Stack program: Develop a stack class using a linked list representation and string data and use it in a program that check C++ programs for the balancing symbols (/* */, ( ), [ ], and { }). The program should read in a C++ program and output one of three messages.
Case 1: Symbol1 does not match symbol2.
Example: “(” does not match “*/”.
Case 2: End of program reached with unmatched symbol.
Example: End of program reached with unmatched “{”.
Case 3: All symbols correctly balanced.
Read the name of the program file to be check from the command line.
Input file:[] / * */ ( ) [] / * */ ( ) ( ]
[CODE]
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include<string>
#include"StackLLImple.cpp"
using namespace std;
int main()
{
char answer;
//char InputChar1, InputChar2;
int num;
int success=0;
string InputChar1;
string InputChar2;
character link;
ifstream InFile;
ofstream OutFile;
InFile.open("input.txt");
OutFile.open("output2.txt");
OutFile << "This is the the linked list function" << endl;
OutFile<< "\n \n";
InFile>>InputChar1;
InFile>>InputChar2;
while(InFile != NULL)
{
if((InputChar1 == "[") || (InputChar2 == "]"))
{
success = link.push(num, success);
success = link.push(num, success);
}
else if((InputChar1 == "(") || (InputChar2 == ")"))
{
success = link.push(num, success);
}
else if((InputChar1 == "/*") || (InputChar2 == "*/"))
{
success = link.push(num, success);
}
else //if(InputChar1 != InputChar2)
success=link.pop(num, success);
OutFile<<InputChar1;
OutFile<<InputChar2;
InFile>>InputChar1;
InFile>>InputChar2;
}
return 0;
}
#include<iostream.h>
#include"stackLL.h"
//constructor
character::character()
{
head=NULL;
cur=NULL;
pre=NULL;
}
//destructor
character::~character()
{
node *ptr;
while(head!=NULL) //delete all the numbers
{
ptr=head->next;
delete head;
head=ptr;
}
}
int character:ush(int num, int success)
{
if(head==NULL)
{
head=new node;
head->num=num;
head->next=NULL;
success=1;
}
else
{
cur=head;
head=NULL;
head=new node;
head->num=num;
head->next=cur;
success=1;
}
return success;
}
//****remove at the head, this is stack, push at the head remove at the head.
int characterp(int num, int success)
{
node *temp; //make a temp pointer
if(head!=NULL) //if the list is not empty
{
temp=head->next; //temp equal to head ->next
delete head; //delete head, remove the first data of the list
head=temp; //now the second data of the list become the first data of the list
return success=1; //return success
}
else
return success=0; //if the list empty return fail
return success;
}
//this function will print from the beginning of the list to the end of the list
void character:rint()
{
//if the list was empty print out an error message
if(head==NULL)
{
cout << "There isn't any number" << endl;
return;
}
//else while not the end of the list, print out the number and go to the next one
else
{
cur=head;
while(cur!=NULL)
{
cout << "number: " << cur->num << endl;
cur=cur->next;
}
}
}
//the search function was search from the beginning of the head to the end of the
//head.
int character::search(int num, int success)
{
if(head==NULL) //if there isn't any data return 0
{
success=0;
return 0;
}
if(head->num == num) //if delete the first node
{
cout << num << " was found" << endl;
success=1;
return 1;
}
else //else delete any node other than the first node
{
cur=head;
cur=cur->next;
while(cur!=NULL) //search all the to the end of the list
{
if(cur->num ==num)
{
cout << num << " was found" << endl;
success=1;
return 1;
}
pre=cur;
cur=cur->next;
success=0;
}// while
} //else
return success;
}
struct node
{
int num;
node *next;
};
class character
{
public:
character();
~character();
int push(int num, int success);
int pop(int num, int success);
void print();
int search(int num, int success);
private:
node *head;
node *cur;
node *pre;
};
[/NODE]