-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
84 lines (66 loc) · 2.03 KB
/
Copy pathmain.cpp
File metadata and controls
84 lines (66 loc) · 2.03 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
// reads file data.txt
// create file output.txt with reversed text
#include <iostream>
#include <fstream>
using namespace std;
struct element{ //stack structure
int number;
element* next;
};
void push_stack(element* &stack, int value){ //adds element to stack
element* el=new element;
el->number=value;
el->next=stack;
stack= el;
}
void pop_stack(element* &stack){ //drops element from stack
element* temp=stack;
stack=stack->next;
delete temp;
}
bool isEmpty_Stack(element* stack){ //check if stack is empty
if(stack==nullptr){
cout<<"Stack is empty"<<endl;
return true;
}
else{
cout<<"Stack contains something"<<endl;
return false;
}
}
int top_stack(element* stack){ //shows the most recent element
return stack->number;
}
void reverseOrder(element* &stack,ifstream &file, ofstream &output){
string textLine;
while(!(file.eof())){
getline(file,textLine);
for(int a=0;a<textLine.length();a++){ //writes line of file into string variable
push_stack(stack,textLine[a]);
}
cout<<textLine<<endl;
for(int a=0;a<textLine.length();a++){ //adds elements from string to stack
output<<char(top_stack(stack)); //conversion to char (stack uses chars and functions does also)
pop_stack(stack);
}
output<<endl; //every time line needs end (getline deletes delimiters)
}}
void line(){
cout<<"\n--------------------------------------------------------------\n";
}
int main()
{
line();
element* stack=nullptr;
ifstream file;
file.open("data.txt");
ofstream output;
output.open("output.txt");
reverseOrder(stack,file,output);
output.close();
file.close();
line();
cout<<"Reverse complete - check file \"output.txt\" inside your project directory"<<endl;
line();
return 0;
}