English 中文(简体)
How do I detect an operator vs. int in C using scanf?
原标题:

How do I read in the following input in my RPN calculator so that it will find the operator no matter what order?

2
2+
4

As of now my scanf only sees the first char in the string and I can only do this:

2
2
+
4

I m also trying to add an option for integer vs floating point mode. (ex. when i is entered, operate in floating point and vice versa.)

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

#define MAX 100

int *p;
int *tos;
int *bos;

void push(int i);
int pop(void);

int main (void)
{
int a, b;
//float c, d;
char s[80];
//char op;  //declare string of 80 chars

p = (int *) malloc(MAX*sizeof(int)); //get stack memory
if (!p){
 printf("Allocation Failure
");
 exit(1);
 }
 tos = p;
 bos = p + MAX-1;

 printf("
RPN Calculator
");
 printf("Enter  i  for integer mode
");
 printf("Enter  f  for floating point mode
");
 printf("Enter  q  to quit
");

 do {
 printf("> ");
 // gets(s);
 // scanf("%s", s);  //read integer
 scanf("%s", s);
 // switch (*s) {


 switch(*s) {
   case  i :
       printf("(Integer Mode)
");
   break;
   case  f :
       printf("(Floating Point Mode)
");
   break;
   case  + :
       a = pop();
       b = pop();
       printf("%d
", a+b);
       push(a+b);
   break;
   case  - :
       a = pop();
       b = pop();
       printf("%d
", b-a);
       push(b-a);
       break; 
  case  * :
       a = pop();
       b = pop();
       printf("%d
", a*b);
       push(a*b);
  break;
  case  / :
       a = pop();
       b = pop();
       if(a == 0){
         printf("Cannot divide by zero
");
  break;
  }
       printf("%d
", b/a);
       push(b/a);
  break;
  case  . :
       a = pop();
       push(a);
       printf("Current value on top of stack: %d
", a);
  break; 
  default:
       push(atoi(s));
  }

 } while (*s !=  q );

 return 0;
}


 // Put an element on the stack

 void push (int i)
 {
 if (p > bos){
   printf("Stack Full
");
 return;
 }
  *p = i;
  p++;
 }

// Get the element from the top of the stack

int pop (void)
{
 p--;
 if(p < 0) {
    printf("Stack Underflow
");
 return 0;
 }
 return *p;
 }
最佳回答

Your scanf reads the whole string. It s the following switch that judges by the first character and misses that + in 2+.

To improve it you can use the strtol function. It will parse an integer out of the string and return to you the location where the integer ended - if that s still not the end of the string, there may be an operator there.

A similar function for floating point numbers is strtod.


Here s some sample code of strtol applicable to your example:

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


int main()
{
    char* input = "25+";
    char* endptr;

    int val = strtol(input, &endptr, 10);

    if (*endptr ==   )
    {
        printf("Got only the integer: %d
", val);
    }
    else
    {
        printf("Got an integer %d
", val);
        printf("Leftover: %s
", endptr);
    }


    return 0;
}
问题回答

I m not sure if I fully understood your question, but you could iterate through the string like this:

for(i = 0; i < strlen(s); i++)
{
   // Here comes your switch section like this
   switch(s[i]) {
    .....
   }

}

Remember also to include string.h.

I really didn t understand your code.

If expect the user to enter one character each time, I mean one character + enter, you should use a simple char instead of char[]. And if you pretend to use a string you should receive it and parse it pzico said. You could do something like that. The problem would be in the treatment of numbers with multiple digits, but thinking a little bit you can fix this problem. I wrote an attempt, but I m pretty sure it s not going to work.

printf(" RPN Calculator ");
printf("Enter i for integer mode ");
printf("Enter f for floating point mode ");
printf("Enter q to quit ");
scanf("%c", s);

switch(*s){

case  i :  
    printf("(Integer Mode)
");  
break;  
case  f :  
    printf("(Floating Point Mode)
");  
break;  
case  q :  
    printf("Bye Bye
");  
    return;  
break;  

} printf("Enter the expression one character each time ");

do {

scanf("%c", s);   
switch(s) {   
    case  + :   
        a = pop();      
        b = pop();     
        printf("%d
", a+b);      
        push(a+b);
    break;
    case  - :
        a = pop();
        b = pop();
        printf("%d
", b-a);
        push(b-a);
    break; 
    case  * :
        a = pop();
        b = pop();
        printf("%d
", a*b);
        push(a*b);
    break;
    case  / :
        a = pop();
        b = pop();
        if(a == 0){
            printf("Cannot divide by zero
");
            break;
        }
        printf("%d
", b/a);
        push(b/a);
    break;
    case  . :
        a = pop();
        push(a);
        printf("Current value on top of stack: %d
", a);
    break; 
    default:
        a = pop()*10+atoi(s);
        push(a);
}  

} while (s != q );

Another problem in your code is in your pop function. What do you want to do with this test:

if(p < 0) {
printf("Stack Underflow ");
return 0;
}

You are expecting your pointer to reach the address 0?

Anyway I hope this is not your homework.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签