strings help

Aug 22, 2011 at 9:15am
im trying to write a code using stack container(templated) where my program would have to read string one character at a time, and a algorithm that can compare parenthesis. this is what i need to read

1. A + B - C
2. A * B / (C +10)
3. A * ((B / C) + D + (E - 10)
4. A * (B / C) + D + (E - 10))

I guess my problem is the getline.
Aug 22, 2011 at 1:02pm
Your are trying to write some code. I assume that you are reading stdin? So you could use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

bool bReading = true;

stack< char > MyLeftParentheses;

stack< char > MyRightParentheses;

char ch = 0;

while( bReading )
    {
    cin >> ch;

    if( cin.fail() )
        {
        bReading = false;

        continue;

        }    /*    if( cin.fail() )    */

    }    /*    while( bReading )    */


to read a string.

Inside your while loop above you could do something like this:

1
2
3
4
5
6
7

    if( '(' == ch )
        MyLeftParentheses.push( ch );

    if( ')' == ch )
        MyRightParentheses.push( ch );


Aug 22, 2011 at 4:23pm
i forgot to mention that im suppose to get those character from a file
Aug 22, 2011 at 4:43pm
OK, look up istream. Everything else will be the same.
Aug 22, 2011 at 4:46pm
Look at
"http://www.cplusplus.com/reference/iostream/istream/get/".
There's an example on how to open a file and then how to get characters one at a time.
Aug 22, 2011 at 5:36pm
Here's some code, that checks if the brackets in each line match. It ignores all characters, except for '(', ')' and '\n'.
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
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

// tests if the brackets in the coming line of the input stream match.
bool doBracketsMatch( istream & is )
{
	int nOpenBrackets = 0;

	while ( is.good() )     // as long as there are characters to read...
	{
		switch ( is.get() ) // get character from input stream
		{
		case '(': ++nOpenBrackets; break; // increase open brackets
		case ')': --nOpenBrackets; break; // decrease open brackets
		case '\n': return nOpenBrackets == 0; // at line end: check if all brackets are closed
		}
		// check, if there are not more closing brackets than opening ones.
		if ( nOpenBrackets < 0 ) 
		{
			// read characters to the end of the line and return.
			while ( is.good() )
				if ( is.get() == '\n' )
					return false;
		}
	}
	return nOpenBrackets == 0;
}

int main(void)
{
	// Get filename from user.
	cout << "Enter the name of an existing text file: ";
	string s;
	cin >> s;

	// open file
	ifstream is( s.c_str() );

	// show result
	for (int line = 1; is.good(); ++line )
	{
		const bool match = doBracketsMatch( is );
		cout << "The brackets in line " << line 
			<< ( match ? " match." : " don't match." )
			<< endl;
	}
}
Aug 22, 2011 at 7:39pm
ralph83 i like the way you design ur code but when i type the name of the file it does not work
Aug 23, 2011 at 12:09pm
Did you type in the whole name of the file, like "C:\temp\test.txt"?
Aug 24, 2011 at 5:48am


im trying to make the output looking like


string:A + B - C no parenthesis
string: A * B / (C +10) matching parenthesis
string: A * ((B / C) + D + (E - 10) parenthesis dont match. missing right parenthesis
string: A * (B / C) + D + (E - 10)) parenthesis dont match. missing left parenthesis

PS: why did you write the function void as a parameter of the function int main()????
Last edited on Aug 24, 2011 at 8:15am
Aug 24, 2011 at 12:58pm
I thought you were supposed to use a stack?

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

#include <string>
#include <stack>
#include <iostream>

using namespace std;


int main( int argc; char** argv )
    {
    bool bReading = true;

    stack< char > MyLeftParentheses;

    stack< char > MyRightParentheses;

    string String= "";

    char ch = 0;

    while( bReading )
        {
        cin >> ch;

        if( cin.fail() )
            {
            bReading = false;

            continue;

            }    /*    if( cin.fail() )    */

        String += ch;

        switch( ch )
            {
            case '(':
                MyLeftParentheses.push( ch );

                break;

            case ')':
                MyRightParentheses.push( ch );

                break;

            case '\n':
            case ';':
                cout << "string: " << String << " ";

                if( 0 == MyLeftParentheses.size() && 0 == MyRightParentheses.size() )
                    cout << "no parenthesis";
                else if( MyLeftParentheses.size() == MyRightParentheses.size() )
                    cout << "matching parenthesis";
                else if( MyLeftParentheses.size() > MyRightParentheses.size() )
                    cout << "parentheses don't match: Missing right parenthesis";
                else
                    cout << "parentheses don't match: Missing left parenthesis";

               cout << endl;

               String = "";

               while( ! MyLeftParentheses.empty() )
                   MyLeftParenthesis.pop();

               while( ! MyRightParentheses.empty() )
                   MyRightParentheses.pop();

                break;

            default:
                break;

            }    /*    switch( ch )    */


        }    /*    while( bReading )    */


    }
        /*    main()    */

 



Last edited on Aug 24, 2011 at 1:07pm
Aug 24, 2011 at 6:01pm
you right. but i tried to compile ur program it didnt work. you forgot to write a code that reads ch from a file.
Aug 25, 2011 at 2:11pm
As I said, use an istream instead of cin. And it is some code, not "a" code!
Aug 25, 2011 at 8:45pm
oh yaa that's what im trying to figure out how to implemented in your code..

by the way, why did you use "int argc; char** argv" as a parameter of function main???
Aug 25, 2011 at 8:55pm
by the way, why did you use "int argc; char** argv" as a parameter of function main???

They allow you to read arguments passed from the command line. So you could execute a program like this: ./ProgramName Arg1 Arg2 Arg3 ...

It's a good habit to do even if you will never read the arguments, as you will always be able to without having to go back and add it later. Char** argv is the same as char *argv[].
Aug 25, 2011 at 9:30pm
im trying to implement this code

cout<<" enter the text file name: ";
string String= "";
cin >> String;
ifstream is(String.c_str());

in your code but i cant find where to implemented because you did everything inside the function main !!!!

Aug 26, 2011 at 1:34pm
Umm, put it before the big-hairy while-loop ...
Aug 26, 2011 at 10:00pm
tanks guys i did appreciate and learned from you.
Last edited on Aug 27, 2011 at 10:23am
Topic archived. No new replies allowed.