我目前正在编写一段代码,该代码从用户处输入一个函数作为字符串,将其转换为后缀表达式,然后将其转换为中缀,然后计算该表达式。
这是一项正在进行的工作,但我在此过程中遇到了问题。仅当数字是一位整数时,该程序才有效。 例如,如果系数是 50 或 1.4,它会给我一个错误。
我尝试了一些方法,但每次都给我一个错误。
我尝试单独拆分每个整数,然后重新组合它们:
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
typedef struct token{
int type;
int value;
double value1;
int trig;
int precedance;
struct token *next;
} token;
typedef struct {
double items[200];
int top;
//double top1;
struct Stack *previous;
} Stack;
void printStack(Stack *s) {
printf("Stack: ");
for (int i = 0; i <= s->top; i++) {
if(isdigit(s->items[i])){
printf("%d ", s->items[i]);
}else{
printf("%c ", s->items[i]);
}}
printf("\n");
}
double evaluate_postfix(Stack *postfix, double* x) {
Stack eval_stack;
initialize(&eval_stack);
for (int i = 0; i <= postfix->top; i++) {
char token = postfix->items[i];
if (isdigit(token)) {
j = 1;
double tmp = token - '0';
while (isdigit(postfix->items[i + j])) {
tmp = tmp * 10 + (postfix->items[i +j] - '0');
j++;
}
i = i + j -1;
push(&eval_stack, tmp); // Convert char to int
..}
void infix_to_postfix_and_evaluate() {
Stack infixtmp, infix;
initialize(&infixtmp);
initialize(&infix);
token *tmp = head;
while (tmp != NULL) {
switch (tmp->type) {
case T_NUMBER:
push(&infix, tmp->value1 + '0'); // Convert int to char
break;
...}
void define_type() {
int j = 0; // Start j from 0 to handle the initial digit within the loop
if (isdigit(Function[i])) {
nexttoken->type = T_NUMBER;
nexttoken->value1 = Function[i] - '0';
}
我也尝试先将它们分组,但这也不起作用......
void define_type(){
int j = 1;
if (isdigit(Function[i])) {
nexttoken->type = T_NUMBER;
nexttoken->value = Function[i] - '0';
while(isdigit(Function[i + j])) {
nexttoken->value = nexttoken->value * 10 + (Function[i + j] - '0');
j++;
}
i = i+j-1;
我不确定你为什么要将字符放入双精度数组中。
如果我迭代一个字符数组,例如:
void DoParse(char *pszFormula, unsigned nLength)
{
float fNum = -1;
for (unsigned i = 0; i < nLength; i++)
{
if ((fNum >= 0) && !isdigit(pszFormula[i]))
{
printf("Fnum: %f\n", fNum);
fNum = -1;
}
switch(pszFormula[i])
{
case '+':
case '-':
case '*':
case '/':
printf("Found operator %c\n", pszFormula[i]);
break;
default:
if (isdigit(pszFormula[i]))
{
fNum = (fNum >= 0 ? fNum * 10 : 0) + (pszFormula[i] - '0');
}
break;
}
}
if (fNum >= 0)
{
printf("Last fNum: %f\n",fNum);
}
}
输入
103+34*26
给我:
Fnum: 103.000000
Found operator +
Fnum: 34.000000
Found operator *
Last fNum: 26.000000
输入
43-64/6
给我:
Fnum: 43.000000
Found operator -
Fnum: 64.000000
Found operator /
Last fNum: 6.000000
如果我想支持非整数参数,我需要像OldBoy建议的那样
atof
。
void DoParse(char *pszFormula, unsigned nLength)
{
char szNum[12];
memset(szNum, 0, sizeof(szNum));
for (unsigned i = 0; i < nLength; i++)
{
switch(pszFormula[i])
{
case '+':
case '-':
case '*':
case '/':
if (*szNum && !isdigit(pszFormula[i]))
{
float f = atof(szNum);
printf("Found a number: %f\n", f);
/* purge buffer */
memset(szNum, 0, sizeof(szNum));
}
printf("Found operator %c\n", pszFormula[i]);
break;
case '.': /* append point to num buffer */
szNum[strlen(szNum)] = pszFormula[i];
break;
default:
if (isdigit(pszFormula[i]))
{ /* append digit to num buffer */
szNum[strlen(szNum)] = pszFormula[i];
}
break;
}
}
if (*szNum)
{
float f = atof(szNum);
printf("Found last number: %f\n", f);
}
}
像
94.3+43.4/42
这样的输入现在给出:
Found a number: 94.300003
Found operator +
Found a number: 43.400002
Found operator /
Found last number: 42.000000
您也可以尝试使用
strtok
或 sscanf
。
调试
define_type
方法的最佳建议是使其采用输入参数,而不是使用全局变量。这样,您就可以快速测试更多种类的字符串。从一个简单的整数参数开始,然后是小数部分,最后是逐渐更复杂的表达式。