Link.h / // This Is A Class For A Linked List of Characters
Link.h / // This Is A Class For A Linked List of Characters
Gavin Weiss
Descriptions of Functions:
cifail is a function that checks for file failure in ifstream and alerts the user if there is a
failure. The arguments passed into it are: ifstream &inFile.
cofail is a function that checks for file failure in ofstream and alerts the user if there is a
failure. The arguments passed into it are: ofstream &outFile.
Infix is a function that asks the user to input an infix expression and returns the infix to
the main function. The arguments passed into it are: ofstream &outFile.
InfixNum is a function that finds out how many characters are in the text file that holds
the infix expression. The function then passes the character number back to the main
function. The arguments passed into it are: ifstream &inFile.
Program Code:
/* link.h
*/
// This is a class for a linked list of characters.
#ifndef LINK_H
#define LINK_H
#include <iostream>
using namespace std;
class LinkedList
{
private:
struct node
{
char info;
node * next;
};
typedef node * nodeptr;
nodeptr start;
int count;
public:
// Constructor
Weiss 2
LinkedList()
{
start = NULL;
count = 0;
}
// Destructor
~LinkedList()
{
nodeptr p = start, n;
while (p != NULL)
{
n = p;
p = p->next;
delete n;
}
}
// Add a node onto the front of the linked list.
void AddNode(char x);
// Delete the first node found with the value x, if one exists.
void DeleteNode(char x);
// Return the first node found in the list
char FirstNode();
// Output the values in the nodes, one character per line.
void PrintNodes();
// Return true if there in a node in the list with the value x.
char IsInList(char x);
// Return a count of the number of nodes in the list.
int Size();
};
#endif
______________________________________________________________________________
/* link.cpp
*
* Class for a linked list of characters.
*/
#include "stdafx.h"
#include <iostream>
#include "link.h"
Weiss 3
using namespace std;
void LinkedList::PrintNodes()
{
Weiss 4
nodeptr p = start;
while( p != NULL )
{
cout << p->info << endl;
p = p->next;
}
}
char LinkedList::IsInList(char x)
{
nodeptr p = start;
while( p != NULL && x > p->info )
p = p->next;
return (x == p->info);
}
int LinkedList::Size()
{
return count;
}
______________________________________________________________________________
/* stack.h
*
* Definition of Stack class
*/
#ifndef STACK_H
#define STACK_H
#include <iostream>
#include "link.h"
using namespace std;
class Stack {
public:
Stack();
~Stack();
void Push(char n);
char Pop();
int IsEmpty();
void Print();
char Peek();
private:
LinkedList topPtr;
};
// pointer to list
Weiss 5
#endif
______________________________________________________________________________
/* stack.cpp
*
* Definition of Stack class member functions.
*/
#include "stdafx.h"
#include <iostream>
#include <assert.h>
#include "stack.h"
using namespace std;
Stack::Stack()
{
}
Stack::~Stack()
{
//delete topPtr;
while( !IsEmpty() ) {
int n = topPtr.FirstNode();
topPtr.DeleteNode( n );
}
}
void Stack::Push(char n)
{
topPtr.AddNode( n );
}
char Stack::Pop()
{
assert(!IsEmpty());
char n = topPtr.FirstNode();
topPtr.DeleteNode( n );
return n;
}
char Stack::Peek()
{
assert(!IsEmpty());
char n = topPtr.FirstNode();
return n;
}
int Stack::IsEmpty()
{
int n = topPtr.Size();
return (n==0);
}
Weiss 6
void Stack::Print()
{
topPtr.PrintNodes();
}
______________________________________________________________________________
/* program2.cpp : Defines the entry point for the console application.
* CS 121.Bolden.........Microsoft Visual Studio...........Gavin Weiss
* 02/12/2013 [email protected]
*
* This programs converts an infix expression to a postfix expression.
*/
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include "stack.h"
using namespace std;
void cifail(ifstream &inFile);
string Infix (ofstream &outFile); // get the Infix equation from user
int InfixNum (ifstream &inFile);
int main()
{
char c,d;
string inFix; // holds the infix
int Count;
// the number of characters in the infix
Stack StackIn;
Weiss 7
while ( c != '(')
{
cout << c;
c = StackIn.Pop();
}
}
else if (token >= '0' && token <= '9')
{
cout << token;
}
else
{
d = StackIn.Peek();
if (d == 'A') // check for "empty stack"
{
cout << token;
}
else
StackIn.Push(token);
}
}
newfile.close();
cin.ignore();
cin.ignore();
return 0;
}
Weiss 8
cout << "\nThe output infix file was not successfully created"
<< endl;
}
}
void cofail(ofstream &outFile)
// checks for file failure
{
if (outFile.fail())
{
// error for if the file cant be opened
cout << "\nThe output infix file was not successfully opened"
<< endl;
}
}
// create infix
Output:
Weiss 9