1

I want to make a parser and first step I have in mind is to extract integers and operators from an input string and store them in their respective arrays. What I have so far is this...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

/*  Grammar for simple arithmetic expression
E = E + T | E - T | T
T = T * F | T / F | F
F = (E)

Legend:
E -> expression
T -> term
F -> factor
*/

void reader(char *temp_0){
char *p = temp_0;
while(*p){
    if (isdigit(*p)){
        long val = strtol(p, &p, 10);
        printf("%ld\n",val);
    }else{
    p++;
    }
}

}

int main(){
char expr[20], temp_0[20];

printf("Type an arithmetic expression \n");
gets(expr);

strcpy(temp_0, expr);

reader( temp_0 );

return 0;
    }

Say I have an input of "65 + 9 - 4" and I want to store the integers 65, 9, 4 to an integer array and the operators +, - in an operators array and also ignores the whitespaces in the input. How should I do it?

P.S. I am using the code in my reader function which I got from here :How to extract numbers from string in c?

Community
  • 1
  • 1
Ingvaru
  • 53
  • 1
  • 11
  • 1
    What is your query (as you have already separated digits from the string do the same to extract the operators)? Also check this http://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used on why `gets() is dangerous` – Santosh A Sep 16 '15 at 04:51
  • What is your question? Maybe you forgot to add "This is FYI" at the end :P – CinCout Sep 16 '15 at 04:55
  • Say I have an input of "65 + 9 - 4" and I want to store the integers 65, 9, 4 to an integer array and the operators +, - in an array. Thanks for the warning about gets(). – Ingvaru Sep 16 '15 at 04:58
  • 1
    Which notation you want to use? Postfix notation or others? – ashiquzzaman33 Sep 16 '15 at 05:32
  • prefix notation/polish notation – Ingvaru Sep 16 '15 at 05:35

2 Answers2

0

You can pass the integer array and operator array to the render() function as parameters like render( temp_0, arrNum, arrOperator, &numCount, &opCount), where arrNum is an array of long and arrOperator is an array of char and the numCount and opCount are two integers which will denote the number of integers and operators respectively. These last two integer will be populated in render(). Then the modified render() function might look like -

void reader(char *temp_0, long *arri, char *arro, int *numCount, int *opCount){
char *p = temp_0;
int integerCount = 0;
int operatorCount = 0;

while(*p){
    if (isdigit(*p)){
        long val = strtol(p, &p, 10);
        arri[integerCount++] = val;
    }else{
       if((*p == '+') || (*p == '-') || 
          (*p == '/') || (*p == '*'))/* Add other operators here if you want*/
       {
          arro[operatorCount++] = *p;
       }
    p++;
    }
}

    *numCount = integerCount;
    *opCount  = operatorCount;

}

Note that there is no error checking done in the code. You might want to add that.

kuro
  • 3,214
  • 3
  • 15
  • 31
  • I'm having problems in printing it...I placed printf("%d\n", arri[*p]); after the arri[integerCount++] = val; and printf("%c\n", arro[*p]); after the arro[operatorCount++] = *p; the output is different from my input – Ingvaru Sep 16 '15 at 12:18
  • The printf should be like `printf("%d\n", arri[integerCount - 1]);` and `printf("%c\n", arro[operatorCount - 1]);` as `operatorCount` and `integerCount` are the index of the array, not `*p`. Also in the `printf()` the indexes are less than that of `operatorCount` and `integerCount` as they are already incremented in the previous statement. – kuro Sep 18 '15 at 05:04
0

I wrote a sample test. Sorry for the tough code, since not have too much time. But it works well on my VS.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include <ctype.h>

int main(){
    //Here I think By default this string is started with an integer.
    char *str = "65 + 9 - 4";
    char *ptr = str;
    char ch;
    char buff[32];
    int  valArray[32];
    int  val, len = 0, num = 0;
    while ((ch = *ptr++) != '\0'){
        if (isdigit(ch) && *ptr != '\0'){
            buff[len++] = ch;
        }
        else{
            if (len != 0){
                val = atoi(buff);
                printf("%d\n", val);
                valArray[num++] = val;
                memset(buff, 0, 32);
                len = 0;
            }
            else if (ch == ' ')
                continue;
            else
                printf("%c\n",ch);
            }
        }
    }
chrischeng021
  • 123
  • 10