Repository navigation
Expand file tree
/
Copy pathFileReverser.cpp
More file actions
127 lines (110 loc) · 2.41 KB
/
Copy pathFileReverser.cpp
File metadata and controls
127 lines (110 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//This program reads a file and pushes the characters
//into a stack, then pops the characters from that stack
//into another file. The resulting file is the reverse
//of the first file.
//Kodi Lein
//SDEV340-81
#include <iostream>
#include<string>
#include<fstream>
using namespace std;
//**DynStack Class**//
class DynStack
{
private:
struct StackNode
{
char value;
StackNode* next;
};
StackNode* top;
public:
DynStack() //constructor
{
top = NULL;
}
void push(char); //push function template
void pop(char&); //pop function template
bool isEmpty() //isEmpty function
{
bool status = false;
if (!top)
{
status = true;
}
return status;
}
};
//**Push Function**//
void DynStack::push(char a)
{
StackNode* newNode = NULL;
newNode = new StackNode;
newNode->value = a;
if (isEmpty()) //start new stack
{
top = newNode;
newNode->next = NULL;
}
else //add to stack
{
newNode->next = top;
top = newNode;
}
}
//**Pop Function**//
void DynStack::pop(char& ch)
{
StackNode* temp = NULL;
if (isEmpty())
{
cout << "The stack is empty.\n";
}
else //pop value from top of stack
{
ch = top->value;
temp = top->next;
delete top;
top = temp;
}
}
//**MAIN**//
int main()
{
string inputFile = "Input.txt"; //file with characters to be pushed into stack
ifstream in(inputFile.c_str());
DynStack stack;
char ch; //characters from Input.txt
cout << "\n======= Input.txt =========== \n\n";
if (in) //if there is an input file
{
while (in.get(ch)) //get characters from Input.txt
{
cout << ch; //display file
stack.push(ch); //push characters from Input.txt into stack
}
in.close(); //close Input.txt
}
else
{
cout << "\nInput.txt not found. Create Input.txt in current folder";
}
string outputFile = "Output.txt"; //Output file where stack characters will be placed
ofstream out(outputFile.c_str());
char temp;
while (!stack.isEmpty())
{
stack.pop(temp); //pop characters from stack
out.put(temp); //put characters into Output.txt
}
out.close(); //close Output.txt
cout << "\n\n======== Output.txt ==========\n\n";
ifstream readOutputFile(outputFile.c_str());
while (readOutputFile.get(ch)) //get characters from Output.txt
{
cout << ch; //display Output.txt
}
readOutputFile.close(); //close Output.txt
cout << endl;
return 0;
}