Thread: Isolating ints and chars

  1. #1
    Registered User
    Join Date
    Apr 2015
    Location
    Tucson, Arizona, United States
    Posts
    10

    Isolating ints and chars

    So I understand that C is byte based. With that in mind, I'm working on a postfix implement, and I ran into a snag:
    In postfix there are 4 operators + - * /. The program appends stdin to a linked list and implements postfix arithmetic on it. The problem:
    I have integers where I am building character by character. This means I could conceivably have integer values in input that are ASCII equivalent to the operators.

    How would I differentiate the two in C without using atoi?

    Here's my code so far for reference. I'm also wondering if I need all the malloc calls I'm making or if I can use my 3 pointers to access the nodes I struct later on?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node{
        int num;
        struct node *next;
    };
    
    int main(){
        struct node *head;
        head = (struct node*)malloc( sizeof(struct node));
        head->next = 0;
        struct node *p;
        p = (struct node*)malloc( sizeof(struct node));
        p = head;
        struct node *q;
        q = (struct node*)malloc( sizeof(struct node));
        q->next = NULL;
        struct node *r;
        r = (struct node*)malloc( sizeof(struct node));
        r->next = NULL;
        int c, d, e;
        int val = 0; 
        int count = 0;
        while((c = getchar()) != 'a' || ((c >= '0' && c <= 9) || c == '+' || c == '-'
        || c == '*' || c == '/' || c == ' ' || c == '\t' || c == '\n')){
            if(c >= '0' && c <= '9'){
                while(c >= '0' && c <= '9'){
                    val = val*10 + c - '0';
                    c = getchar();
                }
                c = val;
                val = 0;
            }
            if(c != ' ' && c != '\t' && c != '\n'){
                struct node *newNode;
                newNode = (struct node*)malloc(sizeof(struct node));
                newNode->num = c;
                p->next = newNode;
                p = p->next;
            }
        }
    
    //Bases
        if(head->next == NULL)
            return 1;
            
        p = head->next;
        q = p->next;
        r = q->next;
        while(r != NULL){
            while(r != NULL && (r->num != '+' && r->num != '-' && r->num != '*' && r->num != '/'))
                r = r->next;
            if(r->num == '+' || r->num == '-' || r->num == '*' || r->num == '/'){
                while(q->next != r){
                    p = q;
                    q = q->next;
                }
                if(r->num == '+')
                    p->num = p->num + q->num;
                if(r->num == '-')
                    p->num = p->num - q->num;
                if(r->num == '*')
                    p->num = p->num * q->num;
                if(r->num == '/')
                    p->num = p->num / q->num;
                p->next = r->next;
                p = head->next;
                q = p->next;
                r = q->next;
            }
        }
    //          no valid operations left
                if(p->num == '+' || p->num == '-' || p->num == '*' || p->num == '/')
                    return 1;
                if(q->num == '+' || q->num == '-' || q->num == '*' || q->num == '/')
                    return 1;
                p = head->next;
                while(p != NULL){
                    printf("%d ", p->num);
                    p = p->next;
                }
            return 0;
        }
    Last edited by Shoone; 04-06-2015 at 01:59 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This means I could conceivably have integer values in input that are ASCII equivalent to the operators.
    Not if the integer value is valid. Presumably you're talking about the sign characters, but any valid integer must have at least one digit. What this means is your storage is problematic because it removes context that's critical for correct operation. You either need to save the context (operator versus operand) in the node, or store complete items in each node rather than breaking each character into a node.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Location
    Tucson, Arizona, United States
    Posts
    10
    I'm confused: how would some integer value like 45 not be a valid input?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Ah, my mistake. I read while in a hurry. The problem still remains though. You're storing an integer with no context, so you need to provide that context in the node that says it's an operator or an operand, and what type.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2015
    Location
    Tucson, Arizona, United States
    Posts
    10
    It's fine. What do you mean by context though?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Well, keeping it simple, you know when you parse '+', so that's clearly an operator. That information needs to be stored in the node along with the value of the operator so that you know not to treat it as an operand later. The loss of context is that you don't store that information, you only store the value. Thus later down the line you have no idea whether the value 43 represents an operator "+" or the operand "43".
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Quote Originally Posted by Shoone View Post

    How would I differentiate the two in C without using atoi?
    Trying not to confuse you, and I don't know what you have covered in Data Structures, but the standard algorithm for postfix evaluation avoids this confusion by only storing the integers - if an operator is encountered, it means there is an evaluation to be made.

    But generally the algorithm uses a stack - which could be implemented as a linked list as you have here, although you would obviously need to create pop and push functions etc.

    Googling on "postfix evaluation algorithm" should give you more info.

    But I understand postfix etc is generally taught alongside a consideration of stacks.
    Last edited by gemera; 04-06-2015 at 04:14 PM.

  8. #8
    Registered User
    Join Date
    Apr 2015
    Location
    Tucson, Arizona, United States
    Posts
    10
    Everyone in the class has a previous background in Java. I'm familiar with stacks, but it's the transition to C that I find difficult, having to work with pointers and limitations on dynamically sized memory and arbitrarily length stdinputs.
    I ended up rewriting the code after going into an office hours and being told how the optimal algorithm would work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining chars as ints help
    By jackalope in forum C Programming
    Replies: 16
    Last Post: 10-14-2010, 02:33 PM
  2. converting ints to chars
    By e66n06 in forum C Programming
    Replies: 4
    Last Post: 07-28-2007, 03:52 PM
  3. Converting Chars to Ints
    By sycorax in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2005, 10:40 PM
  4. Chars - ints
    By MethodMan in forum C Programming
    Replies: 2
    Last Post: 04-15-2002, 08:22 PM
  5. converting chars to ints
    By nebie in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2001, 11:33 AM

Tags for this Thread