Thread: Simple Reverse Polish Notation Calculator

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    1

    Simple Reverse Polish Notation Calculator

    I have been working staring at this code for the past couple of hours trying to locate the Segmentation Fault but to no avail. Any help would be unfathomably appreciated.

    Code:
     #include<stdio.h>#define ARRAYSIZE 7
    #define STACKSIZE 8
    
    
    static int stack[STACKSIZE];
    static int operators[ARRAYSIZE];
    void printInt();
    int readOperators();
    int readInts();
    int evaluate();
    
    
    main(int argc, char*argv[]){
      int numInts, numExpectedOperators, numOperators, maxInts, solution;
      maxInts = 8;
      printf("Made it to main");
      numInts =  readInts(stack, maxInts ,argv, argc);
      numExpectedOperators = numInts-1;
      numOperators =  readOperators(operators, numExpectedOperators, argv, argc, numInts);
      if(numInts != -1 ||  numOperators != -1){
        return 0;
      }
      else{
       solution = evaluate(stack, operators, numInts);
       printInt(solution);
      }
    }
          int readInts(int stack[], int maxInts, char*argv[], int argc){
        int currIntCount = 0;
        int i, tempInt, expectedInts;
        char temp;
        /*if the number of total arguments entered is even then that means there is a mismatch of integers and operators. B/c numOperators =  numInts-1 */
        if(argc%2 == 0){
          printf("Mismatch in number of integers and operators. Quitting");
          return -1;
        }
        expectedInts = (argc/2)+1;
        for(i=0; i< expectedInts; i++){
          temp = *argv[i];
          tempInt =  atoi(temp);
          if(tempInt == 0  &&  currIntCount != expectedInts){
            printf("Bad input character enountered. Quitting");
            return -1;
          }
          if(currIntCount <= expectedInts){
            stack[i]=tempInt;
            currIntCount++;
          }else{
            printf("Minimum of 1 integer needed. Maximum of 8 integers accepted. Quitting.");
            return -1;
          }
        }
            /*reverse the order so its in stack form */
            int t1, t2, i1, i2;
            i1 = currIntCount-1;
            i2 = 0;
            while(i1 > i2){
            t1 = stack[i1];
            t2 = stack[i2];
            stack[i2] = t1;
            stack[i1] = t2;
            i1--;
            i2++;
            }
            return currIntCount;
          }
          int readOperators(char operators[], int expectedNumOperations, char*argv[],int argc, int indexArgvOperator){
        int currOperatorCount = 0;
        int y;
        char temp2;
        for(y=indexArgvOperator; y<argc;y++){
           temp2 = *argv[y];
           if(atoi(temp2) == 0){
             if(temp2 == 'a'|| temp2 == 'm' || temp2 == 's' || temp2 == 'd'){
            operators[currOperatorCount]=temp2;
            currOperatorCount++;
             }
             else{
               printf("Bad input character encountered. Quitting.");
               return -1;
             }
           }else {
             printf("Bad input character encountered. Quitting.");
             return -1;
           }
        }
        if(currOperatorCount != expectedNumOperations){
          printf("Mismatch in number of integers and operators. Quitting.");
          return -1;
        }
        return currOperatorCount;
          }
          int evaluate(int stack[], char operators[], int numItemsInStack){
        int sCounter = 0;
        int oCounter = 0;
        int a1, secondNum, currOperator;
        a1 = stack[sCounter];
        numItemsInStack--;
        sCounter++;
        while(numItemsInStack > 0){
          secondNum = stack[sCounter];
          currOperator = operators[oCounter];
          oCounter++;
          sCounter++;
          numItemsInStack--;
          if(currOperator == 'a'){
            a1 = a1+secondNum;
          }
          else if(currOperator == 'm'){
            a1 = a1*secondNum;
          }
          else if(currOperator == 's'){
            a1 = a1-secondNum;
          }
          else if(currOperator == 'd'){
            a1 = a1/secondNum;
          }
          else{
            printf("Bad input character encountered. Quitting.");
            return -1;
        }
          return a1;
        }
          }
          void printInt(int intToPrint){
        printf("%d\n", intToPrint);
          }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use your debugger. Set breakpoints all over the place (or be more intelligent, if you can), then step through until the line where the error occurs. The mistake should be somewhere before that point.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you want people to look at your code, try indenting it.
    SourceForge.net: Indentation - cpwiki

    Also, if you want us to actually RUN your code to find a segfault, you need to say what test case(s) cause it to fail.

    Maybe 1+2 works, but it barfs on (1+2)
    If it happens to be a very obscure test, we're not going to waste time stumbling around trying all sorts of things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple calculator / reverse Polish notation
    By allan01123 in forum C Programming
    Replies: 7
    Last Post: 02-04-2011, 04:16 PM
  2. Reverse Polish Notation Calculator
    By jazzyqueen in forum C Programming
    Replies: 1
    Last Post: 09-02-2009, 03:55 AM
  3. Reverse Polish Notation
    By Lucid15 in forum C Programming
    Replies: 7
    Last Post: 02-16-2009, 10:05 PM
  4. Postfix (reverse polish notation) calculator
    By ottomated in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 05:32 PM
  5. Reverse Polish Notation???
    By Wizard_D in forum C Programming
    Replies: 6
    Last Post: 11-01-2001, 12:20 AM

Tags for this Thread