使用 fgets 读取表达式并使用 sscanf 获取数值
Read expression using fgets and get the numeric values using sscanf
我必须编写一个程序来读取 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 important
位置,matrix[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] = '[=11=]';
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 阅读本文的任何建议? (我必须使用它们,因为这是一项要求)。
非常感谢!
您可以使用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
编辑:如果space需要删除:
aux = str + (strlen(str2) + 1) * sizeof(char)
拆分和数值决策的例子(简单的方法,不严格的方法)。
#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 == '[=10=]'){
*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;
}
我必须编写一个程序来读取 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 important
位置,matrix[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] = '[=11=]';
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 阅读本文的任何建议? (我必须使用它们,因为这是一项要求)。
非常感谢!
您可以使用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
编辑:如果space需要删除:
aux = str + (strlen(str2) + 1) * sizeof(char)
拆分和数值决策的例子(简单的方法,不严格的方法)。
#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 == '[=10=]'){
*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;
}