Simple Calculator

This is a discussion on Simple Calculator within the C++ Programming forums, part of the General Programming Boards category; Hello all it's been a couple of weeks but wuts up ?? well anyway here is my problem... I have ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    15

    Simple Calculator

    Hello all it's been a couple of weeks but wuts up ?? well anyway here is my problem... I have a program that i need to read from a string a set of number for ex.(1*2-3/4) i want the string to work like a simple calculator. I have the code to work for the + and - for the string but i cann't get the * or the / to work. by the way i need to keep these recursive functions. here is my code that works....
    Code:
    #include <iostream.h>
    int eval(char A[], int n)
    {
     if(n == 1)
     		return (int (A[0])- int('0'));
    
     if(A[n-2] == '+')
     	return eval(A,n-2) + (int (A[n-1]) - int('0'));
     else
     	return eval(A, n-2)- (int (A[n-1]) - int('0'));
    }
    
    
    
    void main()
    {
    char A[20];
    int n=0;
    cout << "Enter a String: ";
    int i=0;
    A[0]=cin.get();
    while(A[i] != '\n')
    {
    	i++;
       A[i]=cin.get();
    }
    n=i;
    for(i =0; i < n; i++)
    	cout << A[i];
       cout << endl;
    
       cout << eval(A,n) << endl;
    }
    p.s i think i need else if statements maybe but not sure... any help will be aprrieciated...

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    You would probably benefit by reading a couple chapters from "The C++ programming language", by Strustrup. <-probably misspelled. He gives an implementation for exactly what you are trying to write, and explains it very well.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What error did you get with for the multiplication and division operations?

    Kuphryn

  4. #4
    Registered User shuesty's Avatar
    Join Date
    Oct 2002
    Posts
    21

    Re: Simple Calculator

    [COLOR=red]OK you have a few issues with your code that I'm going to point out. If you would like ideas of how to fix it just ask and I'll help you there. However I do challenge you to think about it yourself before you look for help.

    [COLOR=blue]
    Code:
    #include <iostream.h>
    int eval(char A[], int n)
    {
     if(n == 1)
     		return (int (A[0])- int('0'));
    
     if(A[n-2] == '+')
     	return eval(A,n-2) + (int (A[n-1]) - int('0'));
     else
     	return eval(A, n-2)- (int (A[n-1]) - int('0'));
    }
    [COLOR=red]First off I checked your input and found out that you are completely screwed if the user's input is anything but single digit numbers. That needs to be taken of. After that you are right in saying that a couple extra else if statements.

    [COLOR=blue]
    Code:
    void main()
    {
    char A[20];
    int n=0;
    cout << "Enter a String: ";
    int i=0;
    A[0]=cin.get();
    while(A[i] != '\n')
    {
    	i++;
       A[i]=cin.get();
    }
    n=i;
    for(i =0; i < n; i++)
    	cout << A[i];
       cout << endl;
    
       cout << eval(A,n) << endl;
    }
    [COLOR=red]Secondly you need to do some crash guarding and put in a line of code to make sure the user didn't just hit enter when prompted to use the calc.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    ok here is what i have and it works see if it works for you guys and yes i know that it wont compute irrational numbers.
    Code:
    #include <iostream.h>
    int eval(char A[], int n)
    {
     if(n == 1)
     		return (int (A[0])- int('0'));
    
     if(A[n-2] == '+')
     	return eval(A,n-2) + (int (A[n-1]) - int('0'));
     else if (A[n-2] == '-')
     	return eval(A, n-2)- (int (A[n-1]) - int('0'));
     else if(A[n-2] == '*')
     	return eval(A,n-2) * (int (A[n-1]) - int('0'));
     else if( A[n-2] == '/')
     	return eval(A,n-2) / (int (A[n-1]) - int('0'));
    }
    
    
    
    void main()
    {
    char A[20];
    int n=0;
    do
    {
    cout << "Enter a String: ";
    int i=0;
    A[0]=cin.get();
    
    while(A[i] != '\n')
    {
    	i++;
       A[i]=cin.get();
    }
    n=i;
    for(i =0; i < n; i++)
    	cout << A[i];
       cout << endl;
    
       cout << eval(A,n) << endl;
    }while(A[0]!=0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  2. simple calculator
    By HunterCS in forum C++ Programming
    Replies: 10
    Last Post: 07-18-2007, 07:51 AM
  3. A Simple Calculator Program Not Working
    By oobootsy1 in forum C++ Programming
    Replies: 9
    Last Post: 01-09-2004, 09:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Simple simple graphics
    By triplem in forum C Programming
    Replies: 2
    Last Post: 05-19-2003, 03:52 AM

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