[英]How do I evaluate decimals & floating point numbers in scientific e notation - e.g. {1.23e4}. in postfix?
我寫了我的代碼來評估從后綴到結果。 但是,當后綴將以科學計數形式使用小數和浮點數表示時,例如在{1.23e4}中,我被困在如何操作上。 任何具體建議將不勝感激。 謝謝。
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#define SIZE 50 /* Size of Stack */
double s[SIZE];
int top=-1; /* Global declarations */
int flag=0;
double pop()
{ /* Function for POP operation */
return(s[top--]);
}
double push(double elem)
{ /* Function for PUSH operation */
if(flag==1){
int num;
num=pop();
s[++top]=elem+10*num;
}
else if(flag==0){
s[++top]=elem;
flag=1;
}
}
void main()
{ /* Main Program */
char pofx[50],ch;
int i=0;
double op1,op2;
printf("Enter the Postfix Expression:");
fgets(pofx,100,stdin);
while( (ch=pofx[i++]) != '\n')
{
if(isdigit(ch)) push(ch-'0'); /* Push the operand */
else if(ch==' ')
flag=0;
else
{ /* Operator,pop two operands */
flag=0;
op2=pop();
op1=pop();
switch(ch)
{
case '+':push(op1+op2);break;
case '-':push(op1-op2);break;
case '*':push(op1*op2);break;
case '/':push(op1/op2);break;
case '^':push(pow(op1,op2));break;
default:
printf("Input invalid ... give proper input\n");
return 0;
}
}
}
printf("Result: %lf\n",s[top]);
}
如何以科學計數法評估小數和浮點數...(?)
要將字符串轉換為FP值,請使用strtod()
@M Oehm
但是代碼還有其他問題,因為運算符'-'
和'+'
也可能以-123.45
類的有效值標記-123.45
。
// insufficient test to determine if the next part of the string is a number or operator.
if(isdigit(ch))
push(ch-'0');
使用strtod()
將文本轉換為double
並確定字符串的下一部分是否為double
。
備用代碼:
const char *st = pofx;
while (*st) {
char *end; //location to store end of FP parsing
double value = strtod(st, &end);
if (end > st) {
push(value);
st = end;
} else if (isspace((unsigned char) *st)) {
st++;
} else {
switch (*st) {
case '+':push(pop() + pop());break; // pop order irrelevant
case '-':{ double t = pop(); push(pop() - t);break; } // pop order relevant
case '*':push(pop() * pop());break;
...
default: {
printf("Input invalid operator: character code %d\n", *st);
return 0;
}
} // end switch
st++;
}
}
重新編寫push()
void push(double elem) {
if (top + 1 >= SIZE) {
printf("Stack overflow\n");
return;
}
s[++top] = elem;
}
fgets()
參數錯誤
char pofx[50];
// fgets(pofx,100,stdin); // 100??
fgets(pofx, sizeof pofx, stdin); // better
<stdlib.h>
strtod
函數將為您解析一個double
<stdlib.h>
。 它需要解析的字符串和指向字符串的指針,該指針將從第一個未解析的字符開始。 (長整數有一個類似的函數strtol
。)
這是一個在您的情況下如何工作的示例。 (該代碼僅打印出令牌,並且不執行任何計算。)
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int is_number(const char *p)
{
if (*p == '-' || *p == '+') p++;
if (*p == '.') p++;
return isdigit((unsigned char) *p);
}
int main(void)
{
const char *line = " .1 20.1* 1.0e-3+2e+7 * -1.0*";
const char *p = line;
while (isspace((unsigned char) *p)) p++;
while (*p) {
if (is_number(p, after_op)) {
char *end;
double x = strtod(p, &end);
if (p == end) {
puts("Illegal number");
p++;
} else {
printf("Number %g\n", x);
p = end;
}
} else {
int c = *p++;
switch (c) {
case '+': puts("Operator add"); break;
case '-': puts("Operator sub"); break;
case '*': puts("Operator mult"); break;
case '/': puts("Operator div"); break;
case '^': puts("Operator pow"); break;
default: printf("Illegal char '%c'\n", c);
}
}
while (isspace((unsigned char) *p)) p++;
}
return 0;
}
請注意,這仍然很粗糙。 諸如20x21
類的20x21
將被解析為數字20,未知的字符x
和數字21。雖然此代碼不20x21
檢測和報告錯誤(這確實應該完成,但保留為練習yadda,yadda),但對於有效輸入。
[ 編輯 :我已經結合了chux的建議,並且還使代碼兼容於讀取帶有明顯符號的數字,但這意味着二進制運算符-
和+
不能立即跟在數字后面。 選擇你的毒葯。 我也允許使用刪節的版本,該版本省略了前導零,例如.12
,我不太喜歡這種格式。]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.