-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmain.cpp
130 lines (102 loc) · 3.67 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
//*****************************************************************************************************
//
// This program reads in a file of palindromes separated by # and instantiates both an
// array-based list stack and a singly linked list queue of characters to store the palindromes.
// It then determines if they are valid palindromes using the stack and queue to 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. queue.h - header file for the Queue class
// 3. palindromes.txt - file containing palindromes with punctuation and spaces
//
//*****************************************************************************************************
#include "queue.h"
#include "stack.h"
#include <cctype>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;
//*****************************************************************************************************
bool testNdisplayPal(Stack<char> &charStack, Queue<char> &charQueue);
char getPalType(bool hasSpace, bool hasPunct);
void displayPalResult(bool isValid, char palType);
//*****************************************************************************************************
int main() {
Stack<char> charStack;
Queue<char> charQueue;
bool isValid,
hasPunct,
hasSpace;
int len;
char palType;
char pal[81];
ifstream in("palindromes.txt");
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]);
charStack.push(pal[i]);
charQueue.enqueue(pal[i]);
} else if (isspace(pal[i])) {
hasSpace = true;
} else if (ispunct(pal[i])) {
hasPunct = true;
}
}
isValid = testNdisplayPal(charStack, charQueue);
palType = getPalType(hasSpace, hasPunct);
displayPalResult(isValid, palType);
}
in.close();
return 0;
}
//*****************************************************************************************************
bool testNdisplayPal(Stack<char> &charStack, Queue<char> &charQueue) {
char sTemp,
qTemp;
bool isValid = true;
int numVal = charStack.getNumValues();
while ((charStack.pop(sTemp)) && (charQueue.dequeue(qTemp))) {
if (sTemp != qTemp)
isValid = false;
cout << sTemp;
}
cout << setw(25 - numVal) << " ";
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
*/