使用fgets读取表达式并使用sscanf

时间:2016-05-27 12:20:15

标签: c fgets scanf

我必须执行一个程序,该程序读取RPN(反向波兰表示法)表达式,然后对其进行评估。

为了阅读表达式,我必须使用fgets。然后为了得到这个表达式的数值,我必须使用sscanf。输入和输出将使用STDIN和STDOUT完成。

这个想法是读这样的东西:

5 2 pi cos - 4 * + 3 -

然后将此表达式保存到m x 2矩阵中。

我可以读取一个数字(浮点数或不浮点数),常数(如pi),运算符(如+ or - or /)或函数(如sin or cos)。

虽然我读了表达式的每个字符,但我必须将这些信息存储到矩阵中,如果我有一个数字,我会将它设置在matrix[0][k], where k is not important位置,如果我有常量,它将会转到位置matrix[1][k], k is not importantmatrix[2][k], k is not important中的运算符并在matrix[3][k]中运行。

所以前面的表达式将是这样的:

{{0,5},{0,2},{1,0},{3,2},{2,1},{0,4},{2,2},{2,0},{0,3},{2,1}}.

我必须在C中这样做,我面临一些问题。主要问题是获取用户输入的表达式的值。我有这个:

int main() {

// Cadena de caracteres donde guardamos la expresion rpn
char str[MAX];

int matrix[MAX][2];
int i = 0; 

printf("Enter a string: ");
// Cogemos los 100 caracteres que haya por consola, controlando errores
if(fgets(str, 100, stdin) == NULL) printf("Error leyendo por consola");

/* Si hay un salto de linea lo borramos */
i = strlen(str)-1;
if( str[ i ] == '\n') str[i] = '\0';

converteix_codis(matrix, str, i);

// Creamos la pila
pila p;
// Inicializamos la pila
initStack(&p);

int a = avalua_rpn(matrix, &p);

// Comprobar si se ha podido evaluar el rpn o no
printf("avalua = %d\n", a);

printf("This is your string: %s\n", str);

return 0;   
}


void converteix_codis(int matriu[MAX][2], char str[MAX], int i)
{
    int tamanyo = 0;

int j, k = 0;
int nombres = 0, constants = 0, operacions = 0, funcions = 0; 
char a[3] = "";
for(j = 0; j < i; ++j) {
    // Si el caracter que leemos es un digito
    if(isdigit(str[j])) {
        matriu[0][nombres] = (int) str[j];
        ++nombres;
        ++tamanyo;
    }
    // Si el caracter que leemos es una operacion
    else if(ispunct(str[j])) {
        int num;
        if(str[j] == '+') num = 0;
        else if(str[j] == '-') num = 1;
        else if(str[j] == '*') num = 2;
        else if(str[j] == '/') num = 3;
        else if(str[j] == '^') num = 4;
        matriu[2][operacions] = num;
        ++operacions;
        ++tamanyo;
    }

}
}

在函数converteix_codis中我试图存储此信息,但我不知道如何获取字符串sin, cos, pi, e and others以将它们存储到矩阵中。通过操作和数字我尝试了,但如果我有一个浮点数,它就不会读它。有关如何使用fgets和sscanf阅读此内容的任何建议? (我必须使用它们,因为它是一种复制品)。

非常感谢你!

2 个答案:

答案 0 :(得分:0)

分裂和数字决定的例子(简单方法,而非严谨方式)。

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

bool isNumber(const char *str, double *num){
    char *endp;
    double n = strtod(str, &endp);
    if(*endp == '\0'){
        *num = n;
        return true;
    }
    *num = NAN;
    return false;
}

int main(void){
    char str[] = "5 2 pi cos - 4 * + 3 -";
    char *token;
    double n;

    token = strtok(str, " \t\n");
    while(token){
        if(isNumber(token, &n)){//if(1==sscanf(token, "%lf%c", &n, &ch)){//char ch;
            printf("%g\n", n);
        } else {
            printf("%s\n", token);
        }
        token = strtok(NULL, " \t\n");
    }
    return 0;
}

答案 1 :(得分:-1)

您可以使用fgets来执行操作,然后sscanf和辅助指针,以便将给定的字符串分成他的元素。它看起来像这样

            #include <stdio.h>
            #include <string.h>
            void main(){
                char str[100];
                char str2[100];

                char * aux; 

                int i, counter, len;
                printf("Introduce operation: ");
                fgets(str,100,stdin);
                strtok(str, "\n"); // Eliminate line jump
                len = strlen(str);
                for (i =0 ,counter =0; i<len ; i++)
                    if (str[i] == ' ') counter ++;  

                aux = str;
                for (i=0; i<counter + 1 ; i++){
                    sscanf(aux, "%s", str2);
                    printf("Element: %s" , str2);
                    aux = str + strlen(str2)* sizeof(char);
                }


            }

此处不包括解析。还可以进行一些改进(例如,为了消除第一个循环)。此代码目的主要是为了让您了解如何使用sscanf

修改:如果需要消除空间:

aux = str + (strlen(str2) + 1) * sizeof(char)
相关问题