-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmain.cpp
145 lines (113 loc) · 4.14 KB
/
main.cpp
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
145
//*****************************************************************************************************
//
// This program reads in a file of palindromes separated by # and instantiates an array-based
// list stack of characters to store the palindromes and determines if they are valid palindromes
// using three stacks to reverse the order and compare the characters.
// Palindromes are words or phrases that read the same forwards and backwards.
//
// Other files required:
// 1. stack.h - header file for the Stack class
// 2. palindromes.txt - file containing palindromes with punctuation and spaces
//
//*****************************************************************************************************
#include "stack.h"
#include <cctype>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;
//*****************************************************************************************************
void fillStack3(Stack<char> &charStack2, Stack<char> &charStack3);
bool testNdisplayPal(Stack<char> &charStack1, Stack<char> &charStack3);
char getPalType(bool hasSpace, bool hasPunct);
void displayPalResult(bool isValid, char palType);
//*****************************************************************************************************
int main() {
Stack<char> charStack1,
charStack2,
charStack3;
bool isValid,
hasPunct,
hasSpace;
int len;
char palType;
char pal[81];
ifstream in("palindromes.txt");
if (in.is_open()) {
while (in.getline(pal, 80, '#')) {
len = int(strlen(pal));
hasSpace = false;
hasPunct = false;
for (int i = 0; i < len; ++i) {
if (isalpha(pal[i])) {
pal[i] = tolower(pal[i]);
charStack1.push(pal[i]);
charStack2.push(pal[i]);
} else if (isspace(pal[i])) {
hasSpace = true;
} else if (ispunct(pal[i])) {
hasPunct = true;
}
}
fillStack3(charStack2, charStack3);
isValid = testNdisplayPal(charStack1, charStack3);
palType = getPalType(hasSpace, hasPunct);
displayPalResult(isValid, palType);
}
} else {
cerr << "file not found" << endl;
return 1;
}
in.close();
return 0;
}
//*****************************************************************************************************
void fillStack3(Stack<char> &charStack2, Stack<char> &charStack3) {
char temp;
while (charStack2.pop(temp))
charStack3.push(temp);
}
//*****************************************************************************************************
bool testNdisplayPal(Stack<char> &charStack1, Stack<char> &charStack3) {
char s1Temp,
s3Temp;
bool isValid = true;
int i = charStack1.getNumValues();
while ((charStack1.pop(s1Temp)) && (charStack3.pop(s3Temp))) {
if (s1Temp != s3Temp)
isValid = false;
cout << s1Temp;
}
cout << setw(25 - i) << " ";
return isValid;
}
//*****************************************************************************************************
char getPalType(bool hasSpace, bool hasPunct) {
char palType = '1';
if (hasPunct)
palType = '3';
else if (hasSpace)
palType = '2';
return palType;
}
//*****************************************************************************************************
void displayPalResult(bool isValid, char palType) {
if (isValid)
cout << "type " << palType << endl;
else
cout << "invalid" << endl;
}
//*****************************************************************************************************
/*
aha type 3
isitiitisi type 3
deed type 1
srotor invalid
neveroddoreven type 2
nolemonsnomelon type 3
racecar type 2
cimoc invalid
wasitacaroracatisaw type 3
yddad invalid
*/