我必须编写一个程序来读取 RPN(逆波兰表示法)表达式,然后对其求值。
为了读取表达式,我必须使用 fgets。然后为了获取此表达式的数值,我必须使用 sscanf。输入和输出将使用 STDIN 和 STDOUT 完成。
这个想法是阅读类似的东西:
5 2 pi cos - 4 * + 3 -
然后将这个表达式保存到m x 2
矩阵中。
我可以读取数字( float 或非 float )、常量(如 pi
)、运算符(如 + 或 - 或/
)或函数(如sin 或 cos
).
当我读取表达式的每个字符时,我必须将此信息存储到矩阵中,如果我有数字,我会将其设置在 matrix[0][k] 位置,其中 k 是不重要
,如果我有常量,它将位于 matrix[1][k] 位置,k 不重要
,matrix[2][k] 中的运算符, k 并不重要
并且在 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 和其他
将它们存储到矩阵中。我尝试了操作和数字,但如果我有一个 float ,它就不会读取它。关于如何使用 fgets 和 sscanf 阅读本文的任何建议? (我必须使用它们,因为这是一项要求)。
非常感谢!
最佳答案
split 和数值决策的例子(简单的方法,不严格的方法)。
#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;
}
关于c - 使用 fgets 读取表达式并使用 sscanf 获取数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37483439/