Question: I dont know what happened. I work on this with CLion, and it works perfectfly. It created a word txt pal1.txt with all palindrom words
I dont know what happened. I work on this with CLion, and it works perfectfly. It created a word txt "pal1.txt" with all palindrom words from pal.txt.When i transfer it to Visual Studio 2013, first i got an error about a = std::tolower(c); from palindrome.h, and i fixed it with #included
//Chan
#include
#include "DoubleLink.h"
#include "stack.h"
#include "queue.h"
#include "palindrome.h"
int main() {
palindrome m1;
m1.read();
return 0;
}
#ifndef MSJCLAB4_DOUBLELINK_H
#define MSJCLAB4_DOUBLELINK_H
template
class Node{
public:
Node(T);
Node* prev;
T data;
Node* next;
T Data() { return data; };
Node* Next() { return next; };
Node* Prev() { return prev; };
};
template
Node
{
data = n; next = NULL;
prev = NULL;
}
template< class T>
class DoubleLink{
public:
DoubleLink();
void addFront(T n);
void addRear(T n);
void dele(size_t n);
T find(size_t n);
void insert(size_t n, T n1);
void print();
bool isempty();
private:
Node
Node
Node
int count;
};
template
DoubleLink
{
head = NULL;
prev = NULL;
next = NULL;
count = 0;
}
template
void DoubleLink
{
Node
Node
if (head == NULL)
head = add;
else
{
while (tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = add;
add->prev = tmp;
add->next = NULL;
}
count++;
}
template
void DoubleLink
{
Node
if (head == NULL)
{
head = add;
head->next = NULL;
head->prev = NULL;
}
else
{
add->next = head;
head->prev = add;
head = add;
head->prev = NULL;
}
count++;
}
template
void DoubleLink
{
Node
if (count == 1)
{
count--;
head = NULL;
return;
}
if (n == 0)
{
del = tmp;
tmp = tmp->next;
tmp->prev = NULL;
head = tmp;
delete del;
del = NULL;
}
else
{
while (n-- > 1)
{
tmp = tmp->next;
}
del = tmp->next;
tmp->next = del->next;
tmp1 = del->next;
tmp1->prev = tmp;
delete del;
del = NULL;
}
count--;
free(del);
}
template
void DoubleLink
{
Node
Node
if (n == 0)
{
in->next = head;
head = in;
head->prev = NULL;
}
else
{
for (size_t i = 0; i < n - 1; i++)
{
tmp = tmp->next;
}
in->next = tmp->next;
in->prev = tmp;
tmp->next = in;
tmp = in->next;
tmp->prev = in;
}
count++;
}
template
T DoubleLink
{
Node
if (n == 0)
{
return head->Data();
}
else
{
for (int i = 0; i < n; i++)
tmp = tmp->next;
}
if (tmp == NULL)
std::cout << "NULL ";
return tmp->Data();
}
template
void DoubleLink
{
Node
if (tmp == NULL)
{
std::cout << "EMPTY" << std::endl;
return;
}
if (tmp->Next() == NULL)
{
std::cout << "NULL <-- ";
std::cout << tmp->Data();
std::cout << " --> ";
std::cout << "NULL" << std::endl;
}
else
{
do {
std::cout << tmp->Data();
std::cout << " --> ";
tmp = tmp->Next();
} while (tmp->next != NULL);
std::cout << tmp->Data() << " --> NULL" << std::endl;
std::cout << tmp->Data() << " --> ";
tmp1 = tmp->prev;
do {
std::cout << tmp1->Data();
std::cout << " --> ";
tmp1 = tmp1->Prev();
} while (tmp1 != NULL);
std::cout << " NULL " << std::endl;
}
}
template
bool DoubleLink
{
if (head == NULL){
return true;
}
return false;
}
#endif //MSJCLAB4_DOUBLELINK_H
#ifndef MSJCLAB4_STACK_H
#define MSJCLAB4_STACK_H
#include "DoubleLink.h"
template
class stack{
public:
void Push(T);
T Pop();
T Peek();
private:
DoubleLink
};
template
void stack
{
this->dll.addFront(n);
}
template
T stack
{
T n = this->dll.find(0);
this->dll.dele(0);
return n;
}
template
T stack
{
return this->dll.find(0);
}
#endif //MSJCLAB4_STACK_H
#ifndef MSJCLAB4_QUEUE_H
#define MSJCLAB4_QUEUE_H
#include "DoubleLink.h"
template
class queue{
public:
void enqueue(T);
T dequeue();
T peek();
private:
DoubleLink
};
template
void queue
{
this->dll.addRear(n);
}
template
T queue
{
T n = this->dll.find(0);
this->dll.dele(0);
return n;
}
template
T queue
{
return this->dll.find(0);
}
#endif //MSJCLAB4_QUEUE_H
#ifndef MSJCLAB4_PALINDROME_H
#define MSJCLAB4_PALINDROME_H
#include
#include
#include
#include
#include
class palindrome{
public:
palindrome();
void setstring(std::string n);
void read();
int check(std::string n);
std::string uptolow(std::string n);
std::string checkspace(std::string n);
private:
queue
std::string n;
int middle;
};
palindrome::palindrome() {
this->n = "";
this->middle = 0;
}
void palindrome::setstring(std::string n) {
this->n = n;
}
int palindrome::check(std::string n) {
int mid = n.size() - 1;
int index = 0;
if (mid % 2 == 1)
{
index = (mid / 2) + 1;
}
else{
index = mid / 2;
}
std::cout << index << std::endl;
return index;
}
std::string palindrome::uptolow(std::string n) {
char c, a;
for (int i = 0; i < n.size(); i++)
{
c = n[i];
a = std::tolower(c);
n[i] = a;
}
return n;
}
std::string palindrome::checkspace(std::string n) {
std::string tmp = "";
for (int i = 0; i < n.size(); i++)
{
if (n[i] != ' ')
{
tmp = tmp + n[i];
}
}
return tmp;
}
void palindrome::read(){
std::ifstream file;
std::ofstream outfile("pal1.txt", std::ofstream::out | std::ofstream::app);
std::string input = "pal.txt";
std::vector
file.open(input);
std::string palin, str;
bool last = false;
while (getline(file, input))
{
output.push_back(input);
}
for (int i = 0; i < output.size(); i++)
{
palin = "";
palin += checkspace(output[i]);
palin = uptolow(palin);
middle = check(palin);
int true_size = palin.size() - 1;
if (i == output.size() - 1){
last = true;
int mid = palin.size();
true_size = palin.size();
if (mid % 2 == 1){
middle = mid / 2 + 1;
}
else
middle = mid / 2;
}
if (true_size % 2 == 1)
{
int size = palin.size() - 1;
if (last) {
size += 1;
}
for (int j = 0; j < size; j++) {
if (palin[j] != ' ') {
str = palin[j];
queue.enqueue(str);
}
}
std::string tmp1 = "", tmp2 = "";
for (int j = 0; j < middle - 1; j++)
{
tmp1 = tmp1 + queue.dequeue();
}
queue.dequeue();
for (int j = 0; j < middle - 1; j++)
{
tmp2 = tmp2 + queue.dequeue();
}
std::reverse(tmp2.begin(), tmp2.end());
std::cout << "tmp1 = " << tmp1 << " tmp2 = " << tmp2 << std::endl;
if (tmp1 == tmp2)
{
std::cout << "output " << output[i] << std::endl;
outfile << output[i] << std::endl;
}
}
if (true_size % 2 == 0)
{
int size = palin.size() - 1;
if (last){
size += 1;
}
for (int j = 0; j < size; j++) {
if (palin[j] != ' ') {
str = palin[j];
queue.enqueue(str);
}
}
std::string tmp3 = "", tmp4 = "";
for (int j = 0; j < middle; j++)
{
tmp3 = tmp3 + queue.dequeue();
}
for (int j = 0; j < middle; j++)
{
tmp4 = tmp4 + queue.dequeue();
}
std::reverse(tmp4.begin(), tmp4.end());
std::cout << "tmp3 = " << tmp3 << " tmp4 = " << tmp4 << std::endl;
if (tmp3 == tmp4)
{
std::cout << "output " << output[i] << std::endl;
outfile << output[i] << std::endl;
}
}
}
}
#endif //MSJCLAB4_PALINDROME_H
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
