Sie sind auf Seite 1von 9

Weiss 1

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;

// Add an item to the FRONT of the list


void LinkedList::AddNode( char x )
{
nodeptr n;
// allocate new node
n = new node;
n->info = x;
count++;
if( start == NULL )
{
start = n;
n->next = NULL;
}
else
{
nodeptr tmp = start;
n->next = tmp;
start = n;
}
}
void LinkedList::DeleteNode( char x )
{
nodeptr prev, curr;
curr = start;
while( curr != NULL && x > curr->info )
{
prev = curr;
curr = curr->next;
}
if( x == curr->info )
{
if( curr == start )
start = start->next;
else
prev->next = curr->next;
delete curr;
count--;
}
}
char LinkedList::FirstNode()
{
return start->info;
}

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;
};

// push item onto stack


// remove item from stack
// is the stack empty
// print the stack
// look at the next stack item but don't delete it

// 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 .................weis0298@vandals.uidaho.edu
*
* 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);

// checks for file failure ifstream file

void cofail(ofstream &outFile);

// checks for file failure ofstream file

string Infix (ofstream &outFile); // get the Infix equation from user
int InfixNum (ifstream &inFile);

// obtain the number of characters in the file

int main()
{
char c,d;
string inFix; // holds the infix
int Count;
// the number of characters in the infix
Stack StackIn;

// create the stack

// write the infix to the txt file


ofstream myfile;
myfile.open("infix.txt",ios::binary); // create a file that doesnt take the \n
cofail(myfile);
// check for file failure
inFix = Infix(myfile);
// determine infix
cout << "Infix: " << inFix << endl;
myfile.close();

// close the txt file

// open the txt file to read from


ifstream newfile;
newfile.open("infix.txt");
cifail(newfile);
Count = InfixNum(newfile); // determine character number in file

Weiss 7

cout << "Postfix: ";


StackIn.Push('A'); // push A onto stack to supplement for empty stack
newfile.seekg(0L, ios::beg);
// go to the beginning of the file
for (int i = 0; i < Count; i++)
{
char token = newfile.get(); // get a character from the file
if (token == '(')
{
StackIn.Push(token); // put the '(' onto the stack
}
else if (token == ')')
{
c = StackIn.Pop();

// look at what is at the top of the stack

while ( c != '(')
{
cout << c;
c = StackIn.Pop();

// look at the next stack item

}
}
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();

// close the txt file

cin.ignore();
cin.ignore();
return 0;
}

void cifail(ifstream &inFile)


// checks for file failure
{
if (inFile.fail())
{
// error for if the file cant be opened

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;
}
}

string Infix(ofstream &outFile)


{
string infix;

// create infix

cout << "Write the infix: ";


cin >> infix;
outFile << infix;
return infix;
}

int InfixNum (ifstream &inFile)


{
long last, offset;
int count = 0;

// determine character number

inFile.seekg(0L, ios::end); // go to the end of the file


last = inFile.tellg();
// find the offset of the last variable in the file
for (offset = 1L; offset <= last; offset++)
{
// start at the back of the file and traverse forward
inFile.seekg(-offset, ios::end);
char inF = inFile.get();
count++;
// find out how many variables are in the infix expression
}
return count;
}

Output:

Weiss 9

Das könnte Ihnen auch gefallen