Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > The IT Pro > Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Programming A discussion forum for programs and programming used in tech-related businesses.

Reply
 
LinkBack Thread Tools
Old 10-17-2005, 04:21 PM   #1 (permalink)
Registered User
 
Join Date: Aug 2005
Posts: 18
OS: XP


Algorithm Problems

Code:
#include <iostream>
#include <cstdlib> // For EXIT_FAILURE and EXIT_SUCCESS
#include <fstream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cctype>

using namespace std;

const int MAX=50;    // initialize max string size of 50 characters

#include "Queue.h"
#include "Stack.h"


/***************************MAIN******************************/
int main()
{
	Stack S;	// initialize stack
	Queue Q;	// initialize queue
	string s;
	int i=0;	// initialze int 'i'
	char string[MAX];  // initialize char string
	bool RESULT=false;  // initilize bool RESULT to false
	
	strcpy(string," ");
	cout << "Please enter a line of text: " << endl; 
	cin.getline (string,100);

for (int j = 0; string[j] != NULL; ++j)
	string[j] = toupper(string[j]);

//	string[j] = tolower(string[j])

while(string[i]!=NULL)
	{
		S.push(string[i]);  // push chars individually from string to
		Q.addq(string[i]);		// stack and queue
		i++;      // next char
	}
	while(i>0)
	{
		if(S.topNpop()==Q.frontNremoveq())  // compare each element from
		{										// stack and queue
			RESULT=true;  // if same for all chars return true
		}
		else
		{
			RESULT=false;  // if not same for any char break and return false
			break;
		}
		i--;
	}	

if(RESULT==true)
	{
		cout<<string<<" \nThis is a palindrome\n";  // display if true
	}
	else
	{
		cout<<string<<" \nThis is not a palindrome\n"; // display if false
	}	
		cout<<s<<s<<endl;

	return 0;
}
Output:

Please enter a line of text:
Mad AdAM
MAD ADAM
This is a palindrome

Press any key to continue
melvynlyc is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 10-17-2005, 07:01 PM   #2 (permalink)
Be Free
 
LoneWolf071's Avatar
 
Join Date: Nov 2004
Location: Texas
Posts: 840
OS: Windows XP, Linux


Send a message via AIM to LoneWolf071
ok, ***? what do you need help with? and please include the header files that you made...
__________________
Suicide Command in Linux : rm -rf / ;)
AIM:TheLoneWolf071@aim.com--If You Need Help, Don't Hesitate...
LoneWolf071 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-19-2005, 09:43 AM   #3 (permalink)
Registered User
 
Join Date: Aug 2005
Posts: 18
OS: XP


Quote:
Originally Posted by LoneWolf071
ok, ***? what do you need help with? and please include the header files that you made...
Queue.h

Code:
#ifndef _Queue_h
#define _Queue_h

//const int MAX=50;    // initialize max string size of 50 characters
typedef char QueueElement;  // define QueueElement

class Queue                           // Queue class
{
public:
		Queue(){front=0, back=0;arr[MAX]=0;}   // Queue default constructor
		void addq(QueueElement & ch);          // define addq 
		QueueElement frontNremoveq();         // define frontNremove
private:
		QueueElement arr[MAX];            // initialize QueueElement array
		int front, back;                  // initialize int front and back
	
};
/*******************************************
FUNCTION: addq()
DESCRIPTION: adds an element onto the queue
PRECONDITION: Waiting for element to add
POSTCONDITION: New element now on the queue
********************************************/
inline void Queue::addq(QueueElement &ch)
{
	if(front!=(back+1)%MAX)
	{
		arr[back]=ch;     // add element to back of queue
		back=(back+1)%MAX;
	}
	else
	{
		cerr<<"Error Queue is full\n";  // display queue is full
	}
}
/*******************************************
FUNCTION: frontNremoveq()
DESCRIPTION: reads and removes front element from queue
PRECONDITION: front pointing to front of queue
POSTCONDITION: front element is returned and then incremented
********************************************/
inline QueueElement Queue::frontNremoveq()
{
	if(front!=back)
	{
		return(arr[front]);    // return front element
		front++;				// remove front element
	}
	else
	{
		cout<<"Queue is empty.\n";  // display queue is empty
		return(0);
	}
}

#endif
Stack.h

Code:
#ifndef _Stack_h
#define _Stack_h

//const int MAX=50;    // initialize max string size of 50 characters
typedef char StackElement;  // define StackElement

class Stack
{
public:
	Stack(){top=-1;arr[MAX]=0;}   // default stack constructor
		void push(StackElement & ch);  // push function
		StackElement topNpop();         // top and pop functions combined
		bool empty() const;            // empty function
private:
		StackElement arr[MAX];    // define char array
		int	top;                  // define int top
	
};
/*******************************************
FUNCTION: push()
DESCRIPTION: Pushes an element onto the stack
PRECONDITION: Waiting for function call
POSTCONTION: New element character on top of stack
*******************************************/
inline void Stack::push(StackElement & ch)
{
	if(top<MAX)
	{
		top++;          // increment top
		arr[top]=ch;  // push onto stack
	}
	else
	{
		cout<<"Stack is full.\n";  // display stack is full
	}


}
/*******************************************
FUNCTION: topNpop()
DESCRIPTION: Reads and pops top element off the stack
PRECONDIION: Waiting for function call
POSTCONDITION:  One element read and removed fromt he stack
RETURN: Top element from stack
********************************************/

inline StackElement Stack::topNpop()
{
	if(top>-1)
	{
		return(arr[top]);  // returns top element
		top--;				// remove froms stack
	}
	else
	{
		cout<<"Stack is empty.\n";  // display stack is empty
		return (0);

	}

}
/*******************************************
FUNCTION: empty()
DESCRIPTION: returns result value if stack is empty
PRECONDITION: result=false
POSTCONDITION: result may be true or remain false
RETURN: result if true or false
********************************************/			
inline bool Stack::empty() const
{
	bool result=false;   // initialize bool as false
	if (top==-1)         
	{
		result=true;        // if top is -1 return result true
		return(result);
	}
	else
	{
		return(result);     // else return false
	}
}

#endif
the problem i having...is the alphabet cant change from capital letter to small letter
see the output below..


output:

Please enter a line of text:
I am A.I
I AM A.I
This is a palindrome

Press any key to continue
melvynlyc is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 03:17 AM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

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