标题:求大神找错:数据结构前缀转后缀
只看楼主
清花淡草
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-10-27
 问题点数:0 回复次数:0 
求大神找错:数据结构前缀转后缀
数据结构作业,要求输入算式,输出计算结果和后缀表达式,我觉得自己写的很对,但是学校的oj系统一直显示第三个测试数据不对,求帮忙
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 60
#define OK 0
//#define NULL 0
char string1[MAX];
char string2[MAX];
char string3[MAX];
int j=0;
struct node
{
    char data;
    int num;
    struct node*next;
};
struct node*Initialization()
{
    struct node *top;
    top=(struct node*)malloc(sizeof(struct node));
    top->data='@';
    top->num=0;
    top->next=NULL;
    return top;
};
struct node*assort(struct node*s)
{
    struct node *p,*top;
    int i;
    top=s;
    int m;
    char a;
    gets(string1);
    m=(int)strlen(string1);
    for(i=0;i<=m;i++)
    {
        a=string1[i];
        if('0'<=string1[i]&&string1[i]<='9')
        {
            string2[j]=string1[i];
            j++;

        }
        else
        {
            switch(a)
            {
                case'(':
                {

                    p=(struct node*)malloc(sizeof(struct node));
                    p->data=a;
                    p->next=top;
                    top=p;
                    break;
                }
                case'*':
                case'/':
                   // string2[j]=' ';
                    //j++;
                    if((top->data=='*')||(top->data=='/'))
                    {
                        string2[j]=top->data;j++;
                        //string2[j]=' ';
                        //j++;
                        top->data=a;break;
                    }
                else
                    {
                        p=(struct node*)malloc(sizeof(struct node));
                        p->data=a;
                        p->next=top;
                        top=p;
                        break;
                    }
                case'+':
                case'-':
                    {
                       // string2[j]=' ';
                        //j++;

                        if(top->data=='+'||top->data=='-'||top->data=='*'||top->data=='/')
                        {
                            string2[j]=top->data;j++;
                           //string2[j]=' ';
                            //j++;
                            top->data=a;
                            break;
                        }
                        else
                        {
                            p=(struct node*)malloc(sizeof(struct node));
                            p->data=a;
                            p->next=top;
                            top=p;
                            break;
                        }
                    }
                case')':
                       // string2[j]=' ';j++;
                        if(top->data=='@'){printf("input error");break;}

                    while(top->data!='(')
                    {

                        string2[j]=top->data;j++;
                        p=top;
                        top=top->next;
                        free(p);
                    }
                    p=top;top=top->next;free(p);
                    break;

            }
        }
    }
    while (top->data!='@')
    {
       // string2[j]=' ';j++;
        string2[j]=top->data;j++;
        p=top;
        top=top->next;
        free(p);
    }
    return top;

};


int result ()
{

    double a[88];
    int j=-1,i;
    double m,n;

    for(i=0;string3[i]!='\0';i++)
    {
        //if(string2[i]==' ') ;
        if('0'<=string3[i]&&string3[i]<='9')
        {
            j++;
            a[j]=atof(&string3[i]);
          //  printf("%d\n",string2[i]);
          //  printf("%.2f",a[j]);


        }
        else
        {
            if(string3[i]=='+')
            {

                n=a[j];
                j--;
                m=a[j];
                n=m+n;
                a[j]=n;
            //printf("%c",a[j]);
            }
            if(string3[i]=='-')
            {
                n=a[j];
                //a[j]=0;
                j--;
                m=a[j];
                //a[j]=0;
                n=m-n;
                a[j]=n;
            }
            if(string3[i]=='*')
            {

                n=a[j];
                j--;
                m=a[j];
                n=m*n;
               a[j]=n;

            }
            if(string3[i]=='/')
            {
                n=a[j];
                j--;
                m=a[j];
                n=m/n;
               a[j]=n;

            }

        }

    }

    printf("%.2lf",a[j]);
    return 0;
}
int main()
{
    struct node*top,*head,*a;
    int m=0,i;
    top=Initialization();
    head=assort(top);
    for(i=0;string2[i]!='\0';i++)
    {
        string3[m]=string2[i];
        m++;
        string3[m]=' ';
        m++;
    }
    a=result();
    printf("\n");
    for(i=0;string2[i]!='\0';i++)
    {
        printf("%c ",string2[i]);
    }
}
搜索更多相关主题的帖子: include 表达式 学校 
2016-10-27 14:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-470338-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.226857 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved