請問這C語言程式碼傳前序式的字串進入eval函式為什麼計算出來的答案都是0.000000,是switch內出問題嗎?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <cmath>
#define MAX 256
void inToPrefix(char*, char*);
int priority(char);
double eval(char*);
double cal(char, double, double);
main()
{
char infix[MAX] = {'\0'};
char prefix[MAX]= {'\0'};
printf("中序運算式:");
gets(infix);
fflush(stdin);
inToPrefix(infix, prefix);
int i;
printf("前序運算式:");
for(i = strlen(prefix) -1; i >= 0; i--)
{
printf("%c", prefix[i]);
}
printf("\n");
printf("前序運算結果:");
printf("%f\n\n", eval(infix));
}
void inToPrefix(char* infix, char* prefix)
{
char stack[MAX] = {'\0'};
int i,j,top;
for(i = strlen(infix) -1 ,j = 0,top = 0 ; i >= 0 ; i--)
{
switch(infix[i])
{
case ')':
stack[++top] = infix[i];
break;
case '+': case '-': case '*': case '/':case '%':
while(priority(stack[top]) > priority(infix[i]))
{
prefix[j++] = stack[top--];
}
stack[++top] = infix[i];
break;
case '(':
while(stack[top] != ')')
{
prefix[j++] = stack[top--];
}
top--;
break;
default:
prefix[j++] = infix[i];
break;
}
}
while(top > 0)
{
prefix[j++] = stack[top--];
}
}
int priority(char op)
{
switch(op)
{
case '+': case '-':
return 1;
break;
case '*': case '/':case '%':
return 2;
break;
default:
return 0;
}
}
double eval(char *prefix)
{
char opne[2],stack[MAX];
int top,i;
for(top = -1 , i = strlen(prefix) ; i >= 0 ; i--)
{
switch(prefix[i])
{
case '+':case '-':case '*':case '/':case '%':
stack[top -1] = cal(prefix[i],stack[top - 1],stack[top]);
top--;
break;
default:
opne[0] = prefix[i];
stack[++top] = atof(opne);
break;
}
}
return stack[top];
}
double cal(char op, double p1, double p2) {
switch(op){
case '+':
return p1 + p2;
break;
case '-':
return p1 - p2;
break;
case '*':
return p1 * p2;
break;
case '/':
return p1 / p2;
case '%':
return fmod(p1,p2);
break;
default:
break;
}
}
只有四則運算
其他的你自己加
參考資料來源
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 80
void inToPostfix(char*, char*); // 中序轉後序
int priority(char); // 運算子優先權
double eval(char*);
double cal(char, double, double);
int main(int argc, char *argv[]) {
char infix[MAX] = {'\0'};
printf("請輸入中序運算式:");
scanf("%s", infix);
printf("\n得到的結果為 : %.0f", eval(infix));
return 0;
}
void inToPrefix(char* infix, char* prefix)
{
char stack[MAX] = {'\0'};
int i,j,top;
for(i = strlen(infix) -1 ,j = 0,top = 0 ; i >= 0 ; i--)
{
switch(infix[i])
{
case ')':
stack[++top] = infix[i];
break;
case '+': case '-': case '*': case '/':case '%':
while(priority(stack[top]) > priority(infix[i]))
{
prefix[j++] = stack[top--];
}
stack[++top] = infix[i];
break;
case '(':
while(stack[top] != ')')
{
prefix[j++] = stack[top--];
}
top--;
break;
default:
prefix[j++] = infix[i];
break;
}
}
while(top > 0)
{
prefix[j++] = stack[top--];
}
printf("前序運算式:");
for(i = strlen(prefix) -1; i>=0; i--) {
printf("%c", prefix[i]);
}
printf("\n");
}
int priority(char op) {
switch(op) {
case '+': case '-': return 1;
case '*': case '/': return 2;
default: return 0;
}
}
double eval(char* infix) {
char postfix[MAX]= {'\0'};
char opnd[2] = {'\0'};
double stack[MAX] = {0.0};
inToPrefix(infix, postfix);
int top, i;
for(top = 0, i = 0; postfix[i] != '\0'; i++){
switch(postfix[i]) {
case '+': case '-': case '*': case '/':
stack[top - 1] = cal(postfix[i], stack[top - 1], stack[top]);
top--;
break;
default:
opnd[0] = postfix[i];
stack[++top] = atof(opnd); //將字串中的數字轉換為 double 型態的浮點數
}
}
return stack[top];
}
double cal(char op, double p1, double p2) {
switch(op) {
case '+': return p1 + p2;
case '-': return p1 - p2;
case '*': return p1 * p2;
case '/': return p1 / p2;
}
}