从数组中打印双精度有问题

时间:2014-01-26 14:11:00

标签: c arrays int double

我的程序是将中缀表达式转换为后缀表达式,然后计算后缀表达式。

表达式应括在括号中。可以使用字母,如果有字母,它们将被转换为数字等价物(e.g. 'A' = 1, 'B' = 2, 'C' = 3, and 'a' = -1, 'b' = -2, 'c' = -3),结果将是数字。

一切都很完美,除了一件事:结果本身。

我的结果是一个数组(result[]),我应该使用一个双数组(用于分割)。问题是,如果我将结果数组设置为int数组,程序工作正常(除了分区,当然。它返回地板),但当我将数组更改为双数组时,它给出了错误的答案。

我尝试了很多不同的解决方案,包括类型转换和重新排列代码。有人能搞清楚吗? :(

以下是我可能与问题相关的函数的代码:

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1000

typedef struct stack
{
    int data[MAX];
    int top;
} stack;

int priority(char);
void init(stack*);
int empty(stack*);
int full(stack*);
char pop(stack*);
void push(stack*, char);
char top(stack*);
void add(char*, char, int);
void keyboard();
void read_file();
int change(char);
void pushYO(double);
double popYO();

stack s;
char x;
int i, token, topYO = -1;
double result[MAX];

char infx[MAX], filepath[MAX];

void main()
{
    char choice;
    init(&s);
    system("CLS");
    printf("[1] Keyboard \n[2] Read from text file \n[3] Exit \n");
    scanf("%c", &choice);
    if(choice != '1' && choice != '2' && choice != '3')
    {
        main();
    }
    else if(choice == '1')
    {
        keyboard();
    }
    else if(choice == '2')
    {
        read_file();
    }
    else if(choice == '3')
    {
        printf("\nThank you for using Kei Shirabe's \ninfix to postfix converter and evaluator! :)\n");
        exit(0);
    }
}


void keyboard()
{
    int z = 0, count = 0, form = -1, paren = 0;
    double one, two, three = 1;
    char pofx[MAX];
    char choice;
    FILE *text = fopen("text.txt", "a");
    printf("Enter infix expression (in parentheses): ");
    scanf("%s", &infx);
    for (i=0; i<strlen(infx); i++)
    {
        if((token = infx[i]) != '\n')
        {
            printf("%c", token);
            fprintf(text, "%c", token);
            if(isalnum(token))
            {
                form++;
            }
            else
            {

                if(token == '(')
                {
                    paren++;

                }
                else if(token == ')')
                {
                    paren--;
                }
                else
                {
                    form--;
                }

            }

            if (paren < 0)
            {
                printf("\n\nError! Not well formed :( \n-----------------\n");
                fprintf(text, "\n\nError! Not well formed :( \n-----------------\n");
                fclose(text);
                printf("\nRun again? \n[1] Yes \n[any other key] No\n");
                scanf("%c", &choice);
                scanf("%c", &choice);
                if(choice == '1')
                {
                    main();
                }
                else
                {
                    printf("\nThank you for using Kei Shirabe's \ninfix to postfix converter and evaluator! :)\n");
                    exit(0);
                }
            }


            if(isalnum(token))
            {
                add(pofx, token, count);
                count++;
            }
            else
            if(token == '(')
               {
                    push(&s, '(');
               }
               else
               {
                   if(token == ')')
                   while((x = pop(&s)) != '(')
                         {
                             add(pofx, x, count);
                             count++;
                         }
                         else
                         {
                             if(token == '^')
                             {
                                 push(&s, token);
                             }
                             else
                             {
                                 while(priority(token) <= priority(top(&s)) && !empty(&s))
                                 {
                                     x = pop(&s);
                                     add(pofx, x, count);
                                     count++;
                                 }
                                 push(&s, token);
                             }
                         }
                }
        }
        else
        {
        while(!empty(&s))
        {
            x = pop(&s);
            add(pofx, x, count);
            count++;
        }

        }
    }

    printf("\n\nPostfix: %s \n\n", pofx);
    fprintf(text, "\n\nPostfix: %s\n\n", pofx);

    if(form != 0 || paren != 0)
    {
        printf("Error! Not well formed :( \n-----------------\n\n");
        fprintf(text, "Error! Not well formed :( \n-----------------\n\n");
        fclose(text);
        printf("\nRun again? \n[1] Yes \n[any other key] No\n");
        scanf("%c", &choice);
        scanf("%c", &choice);
        if(choice == '1')
        {
            main();
        }
        else
        {
            printf("\nThank you for using Kei Shirabe's \ninfix to postfix converter and evaluator! :)\n");
            exit(0);
        }

    }
    else
    {
        while((token = pofx[z++]) != '\0')
        {
            three = 1;
            if(!isdigit(token) && !isalpha(token))
            {
                two = popYO();
                one = popYO();

                switch(token)
                {
                    case '+':
                    pushYO((one+two)); break;
                    case '-':
                    pushYO((one-two)); break;
                    case '*':
                    pushYO((one*two)); break;
                    case '/':
                    pushYO((one/two)); break;
                    case '^':
                    if (two > 0)
                    {
                    for(i=0;i<two;i++)
                    {
                        three = three * one;
                    }
                    pushYO(three);
                    three = 1; break;
                    }
                    else
                    {
                    for(i=0;i<(two-(2*two));i++)
                    {
                        three = three * one;
                    }
                    pushYO((1/three));
                    three = 1; break;
                    }
                }

            }
            else if(isalpha(token))
            {
                if(isupper(token))
                {
                    pushYO((token - '@'));
                }

                if(islower(token))
                {
                    pushYO((token - change(token)));
                }
            }
            else
            {
                pushYO((token - '0'));
            }
        }

        printf("Result: %d\n-----------------\n", result[topYO]);
        fprintf(text, "Result: %d\n-----------------\n", result[topYO]);
        fclose(text);

    }

    fclose(text);
    printf("\nRun again? \n[1] Yes \n[any other key] No\n");
        scanf("%c", &choice);
        scanf("%c", &choice);
        if(choice == '1')
        {
            main();
        }
        else
        {
            printf("\nThank you for using Kei Shirabe's \ninfix to postfix converter and evaluator! :)\n");
            exit(0);
        }

}


void read_file() //reads from text file, does the same thing
{
//code here
}


int priority(char x)
{
    if(x == '(')
    {
        return(0);
    }
    if(x == '+' || x == '-')
    {
        return(1);
    }
    if(x == '*' || x == '/')
    {
        return(2);
    }
    if(x == '^')
    {
        return(3);
    }
    return(4);
}


void init(stack *s)
{
   s->top=-1;
}


int empty(stack *s)
{
    if(s->top==-1)
 return(1);
    else
 return(0);
}


int full(stack *s)
{
    if(s->top==MAX-1)
 return(1);
    else
 return(0);
}


void push(stack *s, char x)
{
  s->top=s->top+1;
  s->data[s->top]=x;
}


char pop(stack *s)
{
   int x;
   x=s->data[s->top];
   s->top=s->top-1;
   return(x);
}


char top(stack *s)
{
   return(s->data[s->top]);
}


void pushYO (double x)
{
 result[++topYO] = x;
}


double popYO ()
{
 return(result[topYO--]);
}


void add(char* s, char c, int count)
{
        s[count] = c;
        s[count+1] = '\0';
}


int change(char c)
{
    switch(c)
    {
        case 'a':
        return(98); break;
        case 'b':
        return(100); break;
        case 'c':
        return(102); break;
        case 'd':
        return(104); break;
        case 'e':
        return(106); break;
        case 'f':
        return(108); break;
        case 'g':
        return(110); break;
        case 'h':
        return(112); break;
        case 'i':
        return(114); break;
        case 'j':
        return(116); break;
        case 'k':
        return(118); break;
        case 'l':
        return(120); break;
        case 'm':
        return(122); break;
        case 'n':
        return(124); break;
        case 'o':
        return(126); break;
        case 'p':
        return(128); break;
        case 'q':
        return(130); break;
        case 'r':
        return(132); break;
        case 's':
        return(134); break;
        case 't':
        return(136); break;
        case 'u':
        return(138); break;
        case 'v':
        return(140); break;
        case 'w':
        return(142); break;
        case 'x':
        return(144); break;
        case 'y':
        return(146); break;
        case 'z':
        return(148); break;
    }
}

0 个答案:

没有答案