Thread: Implement with atoi() to convert ASCII to integer.

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    31

    Implement with atoi() to convert ASCII to integer.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <math.h>
    #include "calc.h"
    #define MAXOP 128
    int main(int argc, char *argv[]){
        
    int type;
    double op2;
    char s[MAXOP];
        
    while ((type = getop(s)) != EOF) {
        switch(type) {
            case NUMBER:
               push(atof(s));
               break;
            case '+':
                push(pop() + pop());
                break;
            case '-':
                op2 = pop();
                push(pop() - op2);
                break;
            case '*':
                push(pop() * pop());
                break;
            case '/':
                op2 = pop();
                if (op2 != 0.0)
                    push(pop() / op2);
                else {
                    printf("error : zero divisor\n");
                }
                break; 
            case '|': 
                push (pop() | pop()); 
                break; 
            case '&': 
                push (pop() & pop()); 
                break; 
            case '^': 
            case '~': 
            case '=': 
                
                if (pop() == pop())
                    push(1); 
                else 
                    push(0); 
                break; 
            case '>':
                op2 = pop();
                if (pop() > op2)
                    push (1); 
                else 
                    push(0); 
                break; 
             case '<':
                op2 = pop();
                if(pop() < op2)
                    push(1); 
                else 
                    push(0); 
                break;
    use atoi() to convert ASCII to integer. Additionally, you add
    functionality to the calculator by implementing the following bitwise operations.

    I did include bitwise operations but I'm struggling how to include atoi() instead of atof() that the program has itself.

    Thanks in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Presumably you already did the "backspace over the f and type an i" bit. And then you're set! (At least as far as the syntax is concerned; you can see push(1) and push(0) sprinkled throughout your code already, so your stack can handle plain integers.)

    Now of course, if you try typing a number with a decimal point in it, everything will fail horribly, so try not to do that. If you need to handle both, then you'll need to be more careful with your parsing to be able to distinguish the different cases of "50" and "50.5".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-30-2012, 07:27 AM
  2. Replies: 2
    Last Post: 03-15-2009, 03:17 PM
  3. Converting ascii to integer without using atoi
    By sansuki in forum C++ Programming
    Replies: 9
    Last Post: 03-30-2005, 07:38 AM
  4. can atoi convert string to float number?
    By Jasonymk in forum C++ Programming
    Replies: 6
    Last Post: 03-14-2003, 04:40 AM
  5. atoi convert problem
    By Ray Schmidt in forum C++ Programming
    Replies: 6
    Last Post: 02-07-2003, 11:05 PM

Tags for this Thread