Thread: Help! Program to evaluate expression...

  1. #1
    Unregistered
    Guest

    Question Help! Program to evaluate expression...

    I need help in writing a program that will evaluate an expression from a file and output if the expression is a valid C++ expression or not.

    EXAMPLE This is read from input file:
    Input:
    a
    ab
    (a+b)
    (a)

    This is ouput to screen after each read and test till end of file.

    Output:
    a
    ** Is a valid expression **
    ab
    ** Is N O T a valid expression **
    (a+b)
    ** Is a valid expression **
    (a)
    ** Is N O T a valid expression **

    As far as I know the rule for a valid expression in c++ is that all the operators must be paranthized. This is my program below but it doesn't work properly can someone point out mistakes?

    ------------------------------------------------------------------------------------
    #include <fstream.h>
    #include <iostream.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define in_file "input.dat"

    void FindAnExpression(int[]);
    ifstream ins;
    ofstream outs;
    char line[20];


    int main()
    {
    int test = 0;
    ins.open(in_file);
    if(ins.fail())
    {
    cout <<"*** ERROR: Cannot open "<< in_file
    <<"for input." << endl;
    exit(1);
    }

    do
    {
    ins.getline(line,20);
    cout << line;
    test= FindAnExpression(line);
    if (test==1)
    cout << "This is a valid Expression.";
    else
    cout << "This is NOT a valid Expression.";
    }while(!ins.eof())

    return 0;
    }

    FindAnExpression(int[]);
    {
    int index = 0;
    if isalpha(line[index])
    {
    index++
    if isalpha(line[index])
    return 1;
    else if (line[index]='(')
    {
    FindAnExpression
    if ((line[index]='+') || (line[index]='-') || (line[index]='*') || (line[index]='/'))
    FindAnExpression
    if (line[index]=')'
    return 1;
    }
    return 0;
    }
    }

  2. #2
    Unregistered
    Guest

    Unhappy

    bump

  3. #3
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    are you sure your allowed to assign an integer to a function?
    oh, and don't bump your thread!

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    49
    First problem is that you cannot even text your program since
    you have
    void FindAnExpression(int[])
    and you want return from that function so
    int FindAnExpression(int[])

    then
    As far as I know the rule for a valid expression in c++ is that all the operators must be paranthized
    Are you testing arithmetic, logic or ALL C++ expression ?

    ab
    ** Is N O T a valid expression **
    but a is. Why ?
    Are you using only one letter variables names?

    Is a*b a valid expression ?

  5. #5
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    ok, this is what you did wrong:

    #1: you forgot 'void' before you started ur function
    #2: FindAnExperession(line) is illigal cuz line is a char and your prototype says FindAnExpression(int[])
    #3: forgot semicolon after while statement after do statement
    #4: your making your functions 'void' when they return values!

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    11
    Originally posted by Xterria
    ok, this is what you did wrong:

    #1: you forgot 'void' before you started ur function
    #2: FindAnExperession(line) is illigal cuz line is a char and your prototype says FindAnExpression(int[])
    #3: forgot semicolon after while statement after do statement
    #4: your making your functions 'void' when they return values!
    Thank you, Mr. Compiler.

  7. #7
    Unregistered
    Guest
    Ok to answer your questions, I fixed those problems above and my program runs not but it goes in an endless loop! As far as the rules, it follows that all expressions are paranthized except when their is only one letter..

    a
    **is valid

    (a)
    **not valid

    a+b
    **not valid

    (a+b)
    **is valid

    Here is my updated program, what is wrong with the recursion?
    -----------------------------------------------------------------------------------
    #include <fstream.h>
    #include <iostream.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define in_file "input.dat"

    int FindAnExpression(char[]);
    ifstream ins;
    ofstream outs;
    char line[80];


    int main()
    {
    int test = 0;
    ins.open(in_file);
    if(ins.fail())
    {
    cout <<"*** ERROR: Cannot open "<< in_file
    <<"for input." << endl;
    exit(1);
    }

    do
    {
    ins.getline(line,20);
    cout << line;
    test= FindAnExpression(line);
    if (test==1)
    cout << "This is a valid Expression.";
    else
    cout << "This is NOT a valid Expression.";
    }while(!ins.eof());

    return 0;
    }

    int FindAnExpression(char[])
    {
    int index = 0;
    if (isalpha(line[index]))
    {
    index++;
    if (isalpha(line[index]))
    return 1;
    else if (line[index]='(')
    {
    FindAnExpression;
    if ((line[index]='+') || (line[index]='-') || (line[index]='*') || (line[index]='/'))
    FindAnExpression;
    if (line[index]=')')
    return 1;
    }
    return 0;
    }
    }

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    49
    At first look I see 2 problems.

    First : int FindAnExpression(char[]);
    which you call without arg in your recursion.


    Second :every time you call FindAnExpression you get a new instance of index so you do not parse your expression.
    Try with a global variable index or
    try static int index.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Desperate to get help w/ program
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 02:59 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM