简体   繁体   English

使用链表的 C 编程中的 RPN 计算器

[英]RPN calculator in C programming using Linked Lists

I have an assignment due to tomorrow and I have just started it.我明天有一个任务,我刚刚开始。

I was asked to do an RPN calculator using linked lists.我被要求使用链表做一个 RPN 计算器。

The idea is I have to write an input string , like ( 25 35 + ) , and display the result using linked lists.这个想法是我必须写一个输入字符串,比如 (25 35 +),并使用链表显示结果。

the structure used are使用的结构是

typedef struct {
int data;
struct cell *next;}cell;

and

typedef struct {
int positif;
struct cell *datas;
int ref;} num ;

in the example above , when I write 25 35 + , I have to stock the 25 as a number and push it into the stack , and do the same with the 35 , and when the operator is read , I do the operation calling 2 pops.在上面的例子中,当我写 25 35 + 时,我必须将 25 作为一个数字存入堆栈并将其压入堆栈,并对 35 执行相同的操作,当操作符被读取时,我执行调用 2 pops 的操作.

the problem is I do not know how to separate the numbers from the string when it reads a space.问题是我不知道如何在读取空格时将数字与字符串分开。

this is my main这是我的主要

 char strIn[250];
num *lenumero = initialisation(); 
printf(">");                      
scanf("%s", &strIn);               

int i=0;
while(strIn[i] != '\0')
{
    /*Here I want to write the code that reads the string untill it finds a space , 

then it push the number before the space into the stack !然后它将空间之前的数字压入堆栈!

}

For example StrIn[0]=2 STrIn[1]=5 strIn[2]=(space) So I will put 2 into a cell->data , 5 into the cell->next->data , and then I put all the cell into the cell used in the structure number, and push the structure number into the stack .例如 StrIn[0]=2 STrIn[1]=5 strIn[2]=(space) 所以我将 2 放入 cell->data , 5 放入 cell->next->data ,然后我把所有将单元格放入结构编号中使用的单元格,并将结构编号压入堆栈。

Thank you谢谢

I am going to assume it is a C assignment, not C++.我将假设它是一个 C 赋值,而不是 C++。

For polish notation, you do not need paranthises.对于波兰语符号,您不需要副词。 Probably the easiest way would be to use strtok() to break input string into space-separated tokens, and than just check if token equals to '+' '-' '/' or '*'.可能最简单的方法是使用strtok()将输入字符串分解为空格分隔的标记,而不仅仅是检查标记是否等于 '+' '-' '/' 或 '*'。 If it does not, read it as integer (using sscanf, for example) and push as a number.如果没有,则将其作为整数读取(例如使用 sscanf)并作为数字推送。 Otherwise, push as an operation.否则,将推送作为操作。

As mentioned in SergeyA's answer you can use strtok with a whitespace as delimter.正如 SergeyA 的回答中提到的,您可以使用带有空格的strtok作为分隔符。

pointer = strtok(strIn, " ");
while(pointer != NULL) {
  printf("%s\n", pointer); // this is where you put your logic to push the number to the stack.
  pointer = strtok(NULL, " ");
}

If you want to test if it's an operator (ie any of "+-/*") you can use strchr如果你想测试它是否是一个运算符(即“+-/*”中的任何一个),你可以使用strchr

const char *operators = "+-/*";
...
char *c = pointer;
if(strchr(operators, *c)) {
  printf("%c is in \"%s\"\n", *c, operators); // replace this with your logic to read from the stack and calculate the numbers with the given operator.
}else {
  while(*c) {
    printf("%c\n", *c); // this will print all the digits of the numbers
    c++;
  }
}
...

The problem with your code right now is that you're using scanf("%s", strIn);你的代码现在的问题是你正在使用scanf("%s", strIn); which will only read the first string until the space.它只会读取第一个字符串直到空格。 What you should do is use fgets instead.你应该做的是使用fgets代替。

fgets(strIn, 25, stdin);

Here is a live demo.这是一个现场演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM