SaidJan
سه شنبه 13 اسفند 1392, 05:52 صبح
سلام
نصف بیشتره این برنامه رو نوشتم، ولی توی ساختن stack گیر کردم. نمیدونم چجوری باید این رو حل کرد.
میخوام یه برنامه بنویسم که یوزر یه کلمه وارد کنه. بعد با استفاده از stack به یوزر بگه که این کلمه بر عکس هم همون جوریه یا نه. مثل کلمهٔ: dad یا mom که اگه بر عکسش کنی همون جوری نوشته میشه.
تا اینجا رو نوشتم (در ضمن میخوام حتما stackهایی که قبل از main ساختم بمونه)
#include "stdafx.h"
#include <iostream>
//#ifndef STACK_H
#define STACK_H
using namespace std;
const int SIZE=25;
enum {EMPTY=-1,FULL=SIZE-1};
class Stack
{
public:
Stack();
bool emptyStack();
bool fullStack();
void push(char c);
char pop();
char topElement();
private:
char s[SIZE];
int top;
};
Stack::Stack()
{
top=EMPTY;
}
bool Stack::emptyStack()
{
if (top==EMPTY)
return true;
else
return false;
}
bool Stack::fullStack()
{
if (top==FULL)
return true;
else
return false;
}
void Stack::push(char c)
{
if (!fullStack())
{
++top;
s[top]=c;
}
else
cout<<"is full"<<endl;
}
char Stack::pop()
{
char c;
if (!emptyStack())
{
c=s[top];
--top;
return c;
}
else
{
cout<<"The stack is empty! pop"<<endl;
return '#';
}
}
char Stack::topElement()
{
if (!emptyStack())
return s[top];
else
{
cout<<"The stack is empty! topE"<<endl;
return '#';
}
}
int main()
{
char c;
int number=0;
int count=0;
cout<<"\nEnter a character element: ";
cin>>c;
stk.push(c);
}
cout <<"\nThe word is: " << endl;
while (!stk.emptyStack())
cout << stk.pop()<<endl;
stk.pop();
return 0;
}
نصف بیشتره این برنامه رو نوشتم، ولی توی ساختن stack گیر کردم. نمیدونم چجوری باید این رو حل کرد.
میخوام یه برنامه بنویسم که یوزر یه کلمه وارد کنه. بعد با استفاده از stack به یوزر بگه که این کلمه بر عکس هم همون جوریه یا نه. مثل کلمهٔ: dad یا mom که اگه بر عکسش کنی همون جوری نوشته میشه.
تا اینجا رو نوشتم (در ضمن میخوام حتما stackهایی که قبل از main ساختم بمونه)
#include "stdafx.h"
#include <iostream>
//#ifndef STACK_H
#define STACK_H
using namespace std;
const int SIZE=25;
enum {EMPTY=-1,FULL=SIZE-1};
class Stack
{
public:
Stack();
bool emptyStack();
bool fullStack();
void push(char c);
char pop();
char topElement();
private:
char s[SIZE];
int top;
};
Stack::Stack()
{
top=EMPTY;
}
bool Stack::emptyStack()
{
if (top==EMPTY)
return true;
else
return false;
}
bool Stack::fullStack()
{
if (top==FULL)
return true;
else
return false;
}
void Stack::push(char c)
{
if (!fullStack())
{
++top;
s[top]=c;
}
else
cout<<"is full"<<endl;
}
char Stack::pop()
{
char c;
if (!emptyStack())
{
c=s[top];
--top;
return c;
}
else
{
cout<<"The stack is empty! pop"<<endl;
return '#';
}
}
char Stack::topElement()
{
if (!emptyStack())
return s[top];
else
{
cout<<"The stack is empty! topE"<<endl;
return '#';
}
}
int main()
{
char c;
int number=0;
int count=0;
cout<<"\nEnter a character element: ";
cin>>c;
stk.push(c);
}
cout <<"\nThe word is: " << endl;
while (!stk.emptyStack())
cout << stk.pop()<<endl;
stk.pop();
return 0;
}