- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我必须编写一个程序来读取 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/
我有以下代码:(cpp14) static int const max_len = 1000; FILE* m_in_log = NULL; FILE* m_log = NULL; ... ... b
标准库函数 fgets() 有两个缺点: 函数的第二个参数是int类型 它在提供的缓冲区中留下一个尾随的换行符 我做了一个类似于 fgets() 的简单函数,排除了上述缺点,试图提高我的一个程序的效率
我看过各种关于在 fgets 之前使用 scanf 可能会导致问题的帖子。但是,对我来说,为什么在 AFTER 之后添加它会导致第一个语句中断,这是没有意义的。 这就是我所拥有的: unsigne
这可能是一个非常愚蠢的问题(可能),但我正在使用 fgets 处理文本文件。没什么太花哨的: while (fgets(buf, sizeof(buf), inputFile) != NULL) 正如
如果我想使用 fgets 从文本中读取多行,根据我的教科书,我会这样做: char str[53]; ... while(fgets(str, max, f)!=NULL){ ... }
我试图在完成未定义数量的另一个输入后获取用户输入。但问题是 while 循环之后的第二个 fgets 永远不会被调用。我用 EOF 结束循环,也许这就是错误。但我不知道应该如何结束循环。 另一个有趣的
我正在尝试将文件中的邮政编码读入 Object * 数组。文件包括123 无处不在柯克兰加州99223 我的 .h 文件看起来像 typedef struct { char *street;
我已经实现了我自己的 fgets(即 myfgets)。当我的文件中有必须由 myfgets 函数读取的 NULL 字符串时,它会打印所有字符串(好)但有一些垃圾(坏),但如果我使用预定义 fgets
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why does printf not flush after the call unless a newl
假设 FILE* 有效,请考虑: char buf[128]; if(fgets(buf,sizeof buf,myFile) != NULL) { strlen(buf) == 0; //ca
void verification() { char pass[50]; printf(" Enter Password : "); fgets(pass, 50, stdin
我想知道如何从汇编代码中调用fgets。我读到的这些问题与这个问题完全相同:How to call fgets in x86 assembly?这个:How to use c library func
我有一个程序,用户可以在其中输入两个输入。由于我无法控制用户输入的内容,因此用户可以超过数组的固定大小。由于 fgets() appends 在空字符之前保留了一个换行符,如果用户超出预期大小时换行符
我想使用 fgets() 简单地读取一行并去掉换行符。这是否适用于所有情况,包括 Windows 和 UN*X? fgets(buf, sizeof buf, stdin); strtok(buf,
我正在运行以下代码: #include #include #include int main(){ FILE *fp; if((fp=fopen("test.txt","r"))==N
如果我有以下缓冲区: char buffy[40]; // includes \0 at the end fgets 函数应该有 STLEN 40 还是 39?为什么? char buffy[40];
使用fgets输入字符串,我对读取的字符串长度有疑问。 例如,考虑以下程序。 char str[50]; int i; int len; printf("Enter name:\n"); fgets(
fgets()函数是否将文件指针自动移动到该位置,直到我提到的大小参数为止? 例如 : p.txt文件的内容是“我是个好男孩”。使用fgets(a,5,fp1)之后 文件指针会向前移动5个位置吗? 在
gcc 4.4.4 c89 我正在读取一个文件,在该文件中,我必须对每一行进行一些操作。 在一个循环中,我调用了 2 个函数,它们传入此文件指针以对文件执行 2 种不同的操作。 但是,当我使用 fge
我试图从文件中获取我的 9x9 数字网格并将其存储在 2D int 数组中。为此,我编写了代码,以便它使用文件中的 fgets(),然后将 char 数组值转换为 int,然后将其存储在数组中的相应位
我是一名优秀的程序员,十分优秀!