This program works but it looks like crap. Any suggestions on how to make it better and how to make the output look better
The output looks like this:
Symbol 1 / does match Symbol 2 *
Symbol 1 * does match Symbol 2 /
Symbol 1 ( does match Symbol 2 )
Symbol 1 [ does match Symbol 2 ]
Symbol 1 ( does match Symbol 2 )
End of program reached when unmatched symbol
Symbol 1 ( does not match Symbol 2 ]
)
(
]
[
)
(
/
*
*
/
All Symbols correctly balanced
I would like it to look like this
Symbol 1 /* does match Symbol 2 */
Symbol 1 ( does match Symbol 2 )
Symbol 1 [ does match Symbol 2 ]
Symbol 1 ( does match Symbol 2 )
End of program reached when unmatched symbol
Symbol 1 ( does not match Symbol 2 ]
()
[]
()
/**/
All Symbols correctly balanced
Code:
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include"stackLL.h"
#include"StackLLImple.cpp"
using namespace std;
int main()
{
char answer;
char InputChar1, InputChar2;
int Number=0;
char num;
int success=0;
character link;
int i=0;
ifstream InFile;
ofstream OutFile;
InFile.open("input1.txt");
OutFile.open("output2.txt");
InFile>>InputChar1;
i++;
InFile>>InputChar2;
i++;
while(InFile != NULL)
{
if((InputChar1 == '[') && (InputChar2 == ']') || (InputChar1 == ']') && (InputChar2 == '['))
{
link.push(InputChar1);
link.push(InputChar2);
Number = 1;
}
else if((InputChar1 == '(') && (InputChar2 == ')') || (InputChar1 == ')') && (InputChar2 == '('))
{
link.push(InputChar1);
link.push(InputChar2);
Number = 1;
}
else if((InputChar1 == '/') && (InputChar2 == '*') || (InputChar1 == '*') && (InputChar2 == '/'))
{
link.push(InputChar1);
link.push(InputChar2);
Number = 1;
}
else
Number = 2;
switch (Number)
{
case 1: OutFile<<"Symbol 1 "<<InputChar1<<" does match Symbol 2 "<<InputChar2<<endl;
Number =0;
break;
case 2 :OutFile<<" End of program reached when unmatched symbol"<<endl;
OutFile<<"Symbol 1 "<<InputChar1<<" does not match Symbol 2 "<<InputChar2<<endl;
while(i >2)
{
OutFile<<link.pop(InputChar1)<<endl;
i--;
OutFile<<link.pop(InputChar2)<<endl;
i--;
}
Number = 3;
case 3 :OutFile<<"All Symbols correctly balanced"<<endl;
default: ;
}
InFile>>InputChar1;
i++;
InFile>>InputChar2;
i++;
}
return 0;
}
#include<iostream>
//#include"stackLL.h"
using namespace std;
//constructor
character::character()
{
head=NULL;
cur=NULL;
pre=NULL;
}
//destructor
character::~character()
{
node *ptr;
while(head!=NULL) //delete all the chrbers
{
ptr=head->next;
delete head;
head=ptr;
}
}
char character::push(char chr)
{
node *temp = new node;
temp->chr = chr;
temp->next = head; // if head was NULL, next will now be NULL (end of list)
head = temp;
return chr;
}
//****remove at the head, this is stack, push at the head remove at the head.
char character::pop(char chr)
{
//int chr;
node *temp = head;
if (temp == NULL) return -1;
chr = temp->chr;
head = temp->next;
delete temp;
return chr;
}
#include<iostream>
struct node
{
char chr;
node *next;
};
class character
{
public:
character();
~character();
char push(char chr);
char pop(char chr);
private:
node *head;
node *cur;
node *pre;
};