-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream.h
executable file
·144 lines (113 loc) · 2.6 KB
/
stream.h
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
Programmers: Chhean Saur, Jason Hepp, & Brian Cardarella
Stream functions for Infinite Numbers
03/08/00
**********
Brian
**********
Moved the ostream function into its own header file
03/20/00
**********
Brian & Chhean
**********
Completed the istream and fstream overloaded functions
**********
Brian
**********
Updated the istream overload, more robust algorithm
*/
#ifndef _Stream
#define _Stream
ostream& operator << (ostream &out, LIST const &ThatList)
{
NODE *ThatNode;
ThatNode = new NODE;
ThatNode = ThatList.head->next;
// if an empty list
if(ThatNode == ThatList.tail)
{
out << "Empty List";
}
else
{
if(ThatList.Flag == Negative)
{
out << '-';
}
for(; // ThatList.head->next;
ThatNode != ThatList.tail;
ThatNode = ThatNode->next)
{
out << ThatNode->Digit;
}
}
return out;
}
void operator >> (istream &in, LIST &ThatList)
{
char InputChar;
ThatList.Purge();
InputChar = _getche();
if(InputChar == '-')
{
ThatList.Flag = Negative;
InputChar = _getche();
}
while(InputChar != (char)13) // while not new line
{
if(InputChar == (char)8) //If backspace is used this will remove the
{ //the node, echo a space and move the cursor back
ThatList.RemoveFromEnd();
putch(' ');
putch((char)8);
}
//ignore non-integer characters
else if((InputChar - '0') < 10 && (InputChar - '0') >= 0)
{
ThatList.InsertAtEnd((InputChar - '0'));
}
InputChar = _getche();
}
cout << endl;
}
fstream& operator << (fstream &out, LIST const &ThatList)
{
NODE *ThatNode;
ThatNode = new NODE;
char filename[255];
cout << "Enter the filename: ";
cin >> filename;
ofstream newfile(filename, ios::out);
if(ThatList.Flag == Negative)
{
newfile << '-';
}
for(ThatNode = ThatList.head->next;
ThatNode != ThatList.tail;
ThatNode = ThatNode->next)
{
newfile << ThatNode->Digit;
}
newfile << ' '; //The deliminiator is a space
return out;
}
void operator >> (fstream &in, LIST &ThatList)
{
ThatList.Purge(); // destroy the list
char filename[255], InputChar;
cout << "Enter the filename: ";
cin >> filename;
ifstream newfile(filename, ios::in);
newfile.get(InputChar);
while(InputChar != ' ' && InputChar != '\n' && !newfile.eof())
{
if(InputChar == '-')
{
ThatList.Flag = Negative;
newfile.get(InputChar);
}
ThatList.InsertAtEnd((InputChar - '0'));
newfile.get(InputChar);
}
}
#endif