philture 2018-05-28 12:01 采纳率: 100%
浏览 838
已采纳

数据结构表达式树,怎么让每个表达式均以“#”开始,以“#”结束

这是main

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "stack.h"


int main(void){
    int i,j,len,cnt;
    OPTR *st=(OPTR*)malloc(sizeof(OPTR));//栈的初始化 
    EXPT *nst=(EXPT*)malloc(sizeof(EXPT));//栈的初始化 
    char str[85];
    char out[85];//后缀输出?
    printf("请输入一个表达式\n"); 
    scanf("%s",str);//中序输入表达式 
    nst->top=-1;
    st->top=-1;
    cnt=0;
    len=strlen(str);//求字符串的长度 
    for(i=0;i<len;i++){
        if(isnumber(str[i]))
            out[cnt++]=str[i];
        else{
            if(str[i]=='('||isempty(st)){
                push(st,str[i]);
                continue;
            }
            if(str[i]==')'){
                while(top(st)!='('){
                    out[cnt++]=top(st);
                    pop(st);
                }
                pop(st);
                continue;
            }
            while(!isempty(st)&&top(st)!='('&&priority(str[i])<=priority(top(st))){
                out[cnt++]=top(st);
                pop(st);
            }
            push(st,str[i]);//把不是数字的字符入栈 
        }
    }
    //如果栈不为空,把栈里的内容放到Out数组 
    while(!isempty(st)){
        out[cnt++]=top(st);
        pop(st);
    }
    out[cnt]='\0';
    for(i=0;i<cnt;++i)
        printf("%c ",out[i]);
    printf("\n");
    for(i=0;i<cnt;i++){
        if(isnumber(out[i])){
            npush(nst,out[i]);
            continue;
        }else if(out[i]=='+'){
            nst->data[nst->top-1]+=ntop(nst);
            npop(nst);
        }else if(out[i]=='-'){
            nst->data[nst->top-1]-=ntop(nst);
            npop(nst);
        }else if(out[i]=='*'){
            nst->data[nst->top-1]*=ntop(nst);
            npop(nst);
        }else if(out[i]=='/'){
            nst->data[nst->top-1]/=ntop(nst);
            npop(nst);
        }
        for(j=0;j<=nst->top;++j)
            printf("%d ",nst->data[j]);
        for(j=i+1;j<cnt;++j)
            printf("%c ",out[j]);
        printf("\n");

    }
    return 0;
}

这是stack

 typedef struct{
    char data[85];
    int top;
}OPTR;
typedef struct{
    int data[85];
    int top;
}EXPT;
// 判断用户输入是否是数字 
bool isnumber(char ch){
    if(ch>='0'&&ch<='9')
        return true;
    else
        return false;
}
//判断栈是否为空 
bool isempty(OPTR *s){
    if(s->top==-1)
        return true;
    else
        return false;
}
//入栈
void push(OPTR *s,char ch){
    s->data[++s->top]=ch;
}
void npush(EXPT *s,char ch){
    s->data[++s->top]=ch-'0';
}
//出栈 
char pop(OPTR *s){
    return s->data[s->top--];
}
int npop(EXPT *s){
    return s->data[s->top--];
}
//优先级定义 
int priority(char ch){
    if(ch=='(')
        return 0;
    if(ch=='+'||ch=='-')
        return 1;
    if(ch=='*'||ch=='/')
        return 2;

    return 0;
}
//得到栈顶元素 
char top(OPTR *s){
   return s->data[s->top];
}
//得到栈顶元素 
int ntop(EXPT *s){
    return s->data[s->top];
}

要加一个新功能就是每个表达式要用#开头,#号结束,求大佬写出完整代码啊

  • 写回答

4条回答 默认 最新

  • 白色一大坨 2018-05-29 01:51
    关注

    不知是不是这个意思,我改了一下你看看:

     int main(void){
        int i, j, len, cnt;
        OPTR *st = (OPTR*)malloc(sizeof(OPTR));//栈的初始化 
        EXPT *nst = (EXPT*)malloc(sizeof(EXPT));//栈的初始化 
        char str[85];
        char out[85];//后缀输出?
        printf("请输入一个表达式\n");
        scanf("%s", str);//中序输入表达式 
        nst->top = -1;
        st->top = -1;
        cnt = 0;
        len = strlen(str);//求字符串的长度 
        char szTemp[256] = { 0 };
        memcpy(szTemp, str + 1, len - 2);//将头尾的#去掉
        char *ptr;
        ptr = strtok(szTemp, "#");
        i = 1;
        while (ptr != NULL) {
            printf("表达式%d:%s\n", i, ptr);
            i++;
            ptr = strtok(NULL, "#");
        }
    

    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊