Optimizing String Recognition

I've created a push-down stack and have a question. on line 83 is there a better way of recognizind that the first 5 letters are push( so i dont have to write out the entire thing in 5 conditions? i cant use strcmp because its not the whole string im comparing, just the first 5 letters. thank you!!
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
#include <iostream>
#include <cstring>
#include <cctype>
using std::cout;
using std::endl;
using std::cin;

const int max = 100;						// max number of digits in the stack

class pushPop
{
public:
	void pop();							// pop the first number
	void push(int number);			    // push on a new number in the first slot
	void print();						// print out the stack				

private:
	static int elements;				// number of elements
	int stack[max];					    // stack of numbers
};

int pushPop::elements = 0;

void pushPop::print()
{
	for (int i = 0; i < elements; i++)
		cout << stack[i] << endl;

	return;
}

void pushPop::pop()
{
	if (elements == 0)											// if 0 elements give an error and end the function
	{
		cout << "There are zero elements, cannot pop any more!" << endl;
		return;
	}
	for (int i = 0; i < elements - 1; i++)						// otherwise pop off the first element
		stack[i] = stack[i + 1];

	elements--;
	return;
}

void pushPop::push(int number)
{															 // if the maximum amout of numbers are already stored display an error and end the function
	if (elements == max)
	{
		cout << "There are ten elements, cannot push any more!" << endl;
		return;
	}
	int copyStack[max];										// otherwise push the new element in the first slot
	for (int i = 1; i < elements + 1; i++)
		copyStack[i] = stack[i - 1];

	copyStack[0] = number;
	for (int i = 0; i < elements + 1; i++)
		stack[i] = copyStack[i];

	elements++;
	return;
}

int main(void)
{
	pushPop stack;
	char operation[max];

	cout << "\t\t\tPUSH-DOWN STACK" << endl
		<< "Enter \"print\" to view elements" << endl
		<< "Enter \"pop\" to pop elements" << endl
		<< "Enter \"push(number)\" to add elements" << endl
		<< "Enter anything else to quit" << endl;

	for (;;)
	{
		cin.getline(operation, max, '\n');											// get the users operation if the want to push, pop or print 
		if (strcmp(operation, "print") == 0)
			stack.print();
		else if (strcmp(operation, "pop") == 0)
			stack.pop();
		else if(operation[0] == 'p' && operation[1] == 'u' && operation[2] == 's' && operation[3] == 'h' && operation[4] == '(')
		{		   
			int number = 0;															// extract the number from paranthese if the user wants to push
			for (int i = 5; isdigit(operation[i]); i++)
			{
				number *= 10;
				number += operation[i] - '0';
			}

			stack.push(number);
		}
		
		else
			exit(1);
	}

	cout << endl;
	return 0;
}
Last edited on
You are using C++, so I don't see why you don't use getline() and an std::string to read in the users command. You could then use find() to look for a substring.

If you really want to use C, I believe strstr() does what you are looking for.
Use a std::string instead of a char array -> http://cplusplus.com/reference/string/string/

1
2
3
4
5
6
7
8
9
10
11
12
13
//...

    string operation;

//...
    
    getline(cin,operation);

    if (operation=="print") {/*...*/}
    else if (operation=="pop") {/*...*/}
    else if (operation.substr(0,5)=="push(") {/*...*/} 

//... 

EDIT: Or you could use this -> http://cplusplus.com/reference/clibrary/cstring/strncmp/
Last edited on
thank you strncmp is exactly what i needed!!
now looks like this:
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
#include <iostream>
#include <cstring>
#include <cctype>
using std::cout;
using std::endl;
using std::cin;

const int max = 100;						// max number of digits in the stack

class pushPop
{
public:
	void pop();							// pop the first number
	void push(int number);			    // push on a new number in the first slot
	void print();						// print out the stack				

private:
	static int elements;				// number of elements
	int stack[max];					    // stack of numbers
};

int pushPop::elements = 0;

void pushPop::print()
{
	for (int i = 0; i < elements; i++)
		cout << stack[i] << endl;

	return;
}

void pushPop::pop()
{
	if (elements == 0)											// if 0 elements give an error and end the function
	{
		cout << "There are zero elements, cannot pop any more!" << endl;
		return;
	}
	for (int i = 0; i < elements - 1; i++)						// otherwise pop off the first element
		stack[i] = stack[i + 1];

	elements--;
	return;
}

void pushPop::push(int number)
{															 // if the maximum amout of numbers are already stored display an error and end the function
	if (elements == max)
	{
		cout << "There are ten elements, cannot push any more!" << endl;
		return;
	}
	int copyStack[max];										// otherwise push the new element in the first slot
	for (int i = 1; i < elements + 1; i++)
		copyStack[i] = stack[i - 1];

	copyStack[0] = number;
	for (int i = 0; i < elements + 1; i++)
		stack[i] = copyStack[i];

	elements++;
	return;
}

int main(void)
{
	pushPop stack;
	char operation[max];

	cout << "\t\t\tPUSH-DOWN STACK" << endl
		<< "Enter \"print\" to view elements" << endl
		<< "Enter \"pop\" to pop elements" << endl
		<< "Enter \"push(number)\" to add elements" << endl
		<< "Enter anything else to quit" << endl;

	for (;;)
	{
		cin.getline(operation, max, '\n');											// get the users operation if the want to push, pop or print 
		if (strcmp(operation, "print") == 0)
			stack.print();
		else if (strcmp(operation, "pop") == 0)
			stack.pop();
		else if(strncmp(operation, "push(", 4) == 0)
		{		 
			int number = 0;															// extract the number from paranthese if the user wants to push
			for (int i = 5; isdigit(operation[i]); i++)
			{
				number *= 10;
				number += operation[i] - '0';
			}

			stack.push(number);
		}
		
		else
			exit(1);
	}

	cout << endl;
	return 0;
}
Last edited on
Careful here -> strncmp(operation, "push(", 5)
Can I ask again why you are mixing C/C++?
It's tastier this way :D
why careful there, that was exactly what i needed and the program works now.
and whats all this hoobla about me mixing C and C++? what am i doing wrong lol :)
Yes, it works. But it also works if the user enters "pushQ"
or "push@" or "push&" etc... I assume you don't want that.
You are using C-style char arrays instead of C++ std::strings which are basically easier and safer. I was wondering what the point was when you clearly have access to C++ features.
haha when i started learning c++ i was taught char arrays long before strings and never really made the transition, old habits die hard i suppose :o.

and thanks m4ster r0shi (dragon ball z ftw) typo :(

EDIT: :O thats what your original post said too haha :(
Last edited on
Topic archived. No new replies allowed.