| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 900 人关注过本帖
标题:求大神找错:数据结构前缀转后缀
只看楼主 加入收藏
清花淡草
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
快速回复:求大神找错:数据结构前缀转后缀
数据加载中...
 
   



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

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