473,473 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

when I entered '(' only the program said it is balanec !!!!

Rooro
16 New Member
I have finshed my work on :

"
Use the Stack classes in a program that reads a string, one character at a time, and determines whether the string contains balanced parentheses. That is, for each left parenthesis (if there are any) there is exactly one matching right parenthesis later in the string.

"
__________


//Stack.h

Expand|Select|Wrap|Line Numbers
  1. //Stack.h
  2.  
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. #ifndef STACK
  7.  
  8. #define STACK
  9.  
  10.  
  11. typedef char StackElement ;
  12.  
  13. class Stack
  14.  
  15. {
  16.  
  17. public:
  18.  
  19. Stack (int numElements =128);
  20.  
  21. Stack (const Stack &original);
  22.  
  23. ~Stack();
  24.  
  25. Stack & operator= (const Stack &rightHandSide);
  26.  
  27. bool empty() const ;
  28.  
  29. void push (const StackElement & value);
  30.  
  31. void display(ostream & out)const;
  32.  
  33.  
  34. StackElement top() const;
  35.  
  36. void pop();
  37.  
  38. void check (int c1,int c2);
  39.  
  40.  
  41. private:
  42.  
  43. int myCapacity,myTop;
  44.  
  45. StackElement *myArray;
  46.  
  47. };
  48.  
  49. #endif
  50.  
  51.  
  52.  
  53.  


//The Stack.cpp

Expand|Select|Wrap|Line Numbers
  1. //Stack.cpp
  2.  
  3.  
  4. #include"Stack.h"
  5. #include<new>
  6. #include <cassert>
  7.  
  8. using namespace std;
  9.  
  10.  
  11.  
  12.  
  13. Stack::Stack(int numElements)
  14.  
  15. {
  16.  
  17.   assert (numElements > 0);
  18.  
  19.    myCapacity = numElements;
  20.  
  21.   myArray = new (nothrow ) StackElement[myCapacity];
  22.  
  23.   if (myArray !=0)
  24.  
  25.    myTop = -1;
  26.  
  27.   else
  28.  
  29.   {
  30.  
  31.    cerr<<"Inadequate memory to allocate stack \n"
  32.     "--terminating execution\n";
  33.  
  34.    exit(1);
  35.  
  36.   }
  37. }
  38.  
  39. //-----
  40.  
  41.  
  42. Stack::Stack (const Stack & original)
  43.  
  44.   :myCapacity(original.myCapacity),myTop(original.myTop)
  45.  
  46. {
  47.  
  48.   myArray = new(nothrow) StackElement[myCapacity];
  49.  
  50.   if (myArray !=0)
  51.  
  52.    for (int pos =0 ; pos<=myTop;pos++)
  53.  
  54.     myArray[pos]=original.myArray[pos];
  55.  
  56.    else
  57.  
  58.  
  59.    {
  60.  
  61.     cerr<<"*Inadequate memory to allocate stack ***\n";
  62.  
  63.     exit(1);
  64.  
  65.    }
  66.  
  67. }
  68.  
  69. //-------------
  70.  
  71.  
  72. Stack::~Stack()
  73.  
  74. {
  75.  
  76.   delete [] myArray;
  77.  
  78. }
  79.  
  80. //-------------
  81.  
  82. Stack & Stack::operator=(const Stack & rightHandSide)
  83.  
  84. {
  85.  
  86.   if (this != &rightHandSide)
  87.  
  88.   {
  89.  
  90.    if (myCapacity != rightHandSide.myCapacity)
  91.  
  92.    {
  93.  
  94.     delete[] myArray;
  95.  
  96.  
  97.     myCapacity = rightHandSide.myCapacity;
  98.     myArray = new StackElement[myCapacity];
  99.     if (myArray == 0 )
  100.  
  101.     {
  102.  
  103.      cerr<<"***Inadequate memory ***\n";
  104.  
  105.     }
  106.  
  107.    }
  108.  
  109.    myTop = rightHandSide.myTop;
  110.  
  111.    for ( int pos =0 ; pos <=myTop ; pos++)
  112.  
  113.     myArray[pos] = rightHandSide.myArray[pos];
  114.  
  115.   }
  116.  
  117.   return *this;
  118.  
  119. }
  120.  
  121.  
  122. //----------
  123.  
  124.  
  125. bool Stack::empty() const
  126.  
  127. {
  128.  
  129.   return (myTop==-1);
  130.  
  131.  
  132. }
  133.  
  134.  
  135. //---------
  136.  
  137. void Stack::push( const StackElement & value )
  138.  
  139. {
  140.  
  141.  
  142.  
  143.   if ( myTop <myCapacity - 1)
  144.  
  145.   {
  146.  
  147.    ++myTop;
  148.  
  149.    myArray[myTop] = value;
  150.  
  151.   }
  152.  
  153.   else
  154.  
  155.   {
  156.  
  157.    cerr << " *** Stack full -- can not add new value *** \n"
  158.     "Must increase value of STACK_CAPACITY in Stack.h \n";
  159.  
  160.    exit(1);
  161.  
  162.   }
  163.  
  164.  
  165.  
  166. }
  167.  
  168.  
  169.  
  170.  
  171. void Stack::display(ostream & out ) const
  172.  
  173. {
  174.  
  175.   for ( int i = myTop ; i>=0 ; i--)
  176.  
  177.    out << myArray[i] << endl;
  178.  
  179. }
  180.  
  181.  
  182. //--------
  183.  
  184. StackElement Stack::top() const
  185.  
  186. {
  187.  
  188.   if (!empty() )
  189.  
  190.    return(myArray[myTop]);
  191.  
  192.   else
  193.  
  194.   {
  195.  
  196.    cerr<<" *** Stack is empty -- returning garbage value ***\n";
  197.  
  198.    StackElement garbage;
  199.  
  200.    return garbage;
  201.  
  202.   }
  203.  
  204. }
  205.  
  206. //---------
  207.  
  208. void Stack::pop()
  209.  
  210. {
  211.  
  212.   if ( !empty() )
  213.  
  214.    myTop--;
  215.  
  216.   else
  217.  
  218.    cerr<<"***Stack is empty  ***\n";
  219.  
  220. }
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
// The main :


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std ;
  6.  
  7.  
  8. #include "Stack.h"
  9.  
  10.  
  11.  
  12.  
  13. int main ()
  14.  
  15. {
  16.  
  17.  
  18.  
  19.   char ch;
  20.  
  21.   int count1=0,count2=0;
  22.  
  23.  
  24.   int cap;
  25.  
  26.   cout <<"Enter stack capacity: ";
  27.  
  28.   cin >> cap;
  29.  
  30.   Stack s(cap);
  31.  
  32.   cout << "Stack created. Empty? " <<boolalpha<< s.empty() <<endl;
  33.  
  34.   cout <<" How many elements to add to the stack ? ";
  35.  
  36.    int numItems;
  37.  
  38.   cin>>numItems;
  39.  
  40.     cout <<"Enter stack charcter contanent: ";
  41.  
  42.   for ( int i =1 ; i<= numItems; i++ )
  43.  
  44.     cin >> ch;
  45.  
  46.  
  47.  if ( ch == '(')
  48.  
  49.      s.push(ch);
  50.  
  51.  
  52.  if (ch ==')')
  53.  
  54.  
  55.      s.pop();
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. if(s.empty())
  63.  
  64.  cout<<"The parentheses are balanced . \n";
  65.  
  66. else
  67.  
  68.  cout<<"The parentheses are not balanced . \n";
  69.  
  70.  
  71.     return(0);
  72.  
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  

The problem is : when I entered '(' only, the program said it is balanec !!!!

the rogram in this cast should said :The parentheses are not balanced .

because we push one parenthesis .

and the empty Stack function is not empty ..

So i do not know What I have to do :(
Nov 12 '06 #1
13 2592
Banfa
9,065 Recognized Expert Moderator Expert
Correct this for loop

Expand|Select|Wrap|Line Numbers
  1.   for ( int i =1 ; i<= numItems; i++ )
  2.     cin >> ch;
  3.  
  4.  if ( ch == '(')
  5.      s.push(ch);
  6.  
  7.  if (ch ==')')
  8.      s.pop();
  9.  
The only think it is repeating is the cin >> ch; line so the only character that gets tested and added ot not added to the stack is the last character input.

What other test string have you used?
Nov 13 '06 #2
Rooro
16 New Member
when I changed the loop the function takes only one charcter to test it , what i want is to fill the stack and see is the '(' and ')' are balanced or not ..







I do not know how can I do the string with this work .. I think that I do not need it because I have "char" instead of it .. is my think right ?
Nov 13 '06 #3
Banfa
9,065 Recognized Expert Moderator Expert
when I changed the loop the function takes only one charcter to test it , what i want is to fill the stack and see is the '(' and ')' are balanced or not ..
??? what do you mean only takes 1 character to test it? The way you are using your stack it is guaranteed to be empty by the time you have go to the end of a conforming(balanced) string. It will only ever contain the character '(' and only if it does not have a matching ')'.

I do not know how can I do the string with this work .. I think that I do not need it because I have "char" instead of it .. is my think right ?
As long as you are only interested in testing the data and not storing it to use it later then using a char is fine. If you are going to have to use the string once it has been input then you will need somewhere to store the characters input, like a string.
Nov 13 '06 #4
Rooro
16 New Member
what I mean is :

Nov 13 '06 #5
Rooro
16 New Member
and here without changing the cin the program work like :










______

If you are going to have to use the string once it has been input then you will need somewhere to store the characters input, like a string.

tell me please how can i do this "reads a string, one character at a time, and determines whether the string contains balanced parentheses" in right way ...

because i think my work on " char" only is wrong :(
Nov 13 '06 #6
Banfa
9,065 Recognized Expert Moderator Expert
Sorry I am not saying the cin should be before the loop I am saying the loop should include the push and pop stack statements like so

Expand|Select|Wrap|Line Numbers
  1. for ( int i =1 ; i<= numItems; i++ )
  2. {
  3.     cin >> ch;
  4.  
  5.      if ( ch == '(')
  6.          s.push(ch);
  7.  
  8.      if (ch ==')')
  9.          s.pop();
  10. }
  11.  
Otherwise you only test the last character you input.
Nov 13 '06 #7
Rooro
16 New Member
yeeees , that is what I want : )

it is works fine now .. Thanks a lot ...

but excuse me , if I have a string what can i do ?
Nov 14 '06 #8
Banfa
9,065 Recognized Expert Moderator Expert
access it 1 character at a time using the array accessor string[i] i is an integer and should have the range 0 - (string length -1).

Also there is a logic error in you program, try the character sequence

"kjdsfg(jkds)js)d"

There is 1 ( but 2 ) so the brackets are unbalanced, however what your code will try to do is pop from an empty stack (which may be bad and crash but if it doesn't will surely show a balanced result).
Nov 14 '06 #9
Rooro
16 New Member

Also there is a logic error in you program, try the character sequence

"kjdsfg(jkds)js)d"

There is 1 ( but 2 ) so the brackets are unbalanced, however what your code will try to do is pop from an empty stack (which may be bad and crash but if it doesn't will surely show a balanced result).
yes you are right ....

i tried this :

for ( int i =1 ; i<= numItems; i++ )

{

cin >> ch;


if ( ch == '(')

s.push(ch);


else if (ch ==')')

{ if (s.empty()) { cout <<"The parantheses are not balanced \n ";}}

else
s.pop();

}


but it does not work well ...

access it 1 character at a time using the array accessor string[i] i is an integer and should have the range 0 - (string length -1).

may you write the code clearly ?

I want to learn how can I do it ...

because I'm not studying the string now ..
Nov 14 '06 #10
Banfa
9,065 Recognized Expert Moderator Expert
Rather than output an error string immediately, set a flag to indicate the error and output the string when the loop finishes.

Here is a code example of accessing the characters in a string

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main(void)
  8. {
  9.     string myStr = "Hello World!";
  10.  
  11.     for(int i=0; i<myStr.length(); i++)
  12.     {
  13.         cout << "Character " << setw(2) << i+1 << " of string is: " << myStr[i] << endl;
  14.     }
  15.  
  16.     return 0;
  17. }  
  18.  
Nov 14 '06 #11
Rooro
16 New Member
See what i did :




Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. using namespace std ;
  4.  
  5.  
  6. #include "Stack.h"
  7.  
  8.  
  9.  
  10.  
  11. int main ()
  12.  
  13. {
  14.  
  15.  
  16.  
  17.   char ch;
  18.  
  19.  
  20.   Stack s;
  21.  
  22.  
  23.     string myStr ;
  24.  
  25.     cout<<"Please Enter a sentence : ";
  26.  
  27.     cin>>myStr;
  28.  
  29.  
  30. for(int i=0; i<myStr.length(); i++)
  31.  
  32.  
  33. {
  34.  if ( myStr[i] == '(')
  35.  
  36.      s.push(ch);
  37.  
  38.  
  39.  if ( myStr[i] ==')')
  40.  
  41.  
  42.      s.pop();
  43.  
  44.  
  45. }
  46.  
  47.  
  48. if(s.empty())
  49.  
  50.  cout<<"The parentheses are balanced . \n";
  51.  
  52. else
  53.  
  54.  cout<<"The parentheses are not balanced . \n";
  55.  
  56.  
  57.     return(0);
  58.  
  59.  
  60. }
  61.  
  62.  
  63.  




Banfa Thanks for every thing you did to me : )
Nov 15 '06 #12
Banfa
9,065 Recognized Expert Moderator Expert
NP looks like it will work to me ...

except when there are more ) than (

try it with the input data

h(yyy)zzz)p

I think you will either get an error at the second ) or it will report this as a balanced string.

You need to have a boolean variable and if you get a ) and the stack is already empty set that variable to indicate that the string is not balanced.

Then when you get to printing the result check the variable and the stack.size to get the required result.
Nov 15 '06 #13
Rooro
16 New Member
thanks banfa for your checking and for helping me ...





sorry about my late reply ... because I have many exams during this week...

Thanks again

Rooro
Nov 18 '06 #14

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.