| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2214 人关注过本帖
标题:新人~~调了好久都调不出来
只看楼主 加入收藏
YYLYY
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-4-20
收藏
 问题点数:0 回复次数:1 
新人~~调了好久都调不出来
#include<iostream>
using namespace std;
const int MAXSIZE=100;
typedef int SElemType;
bool isoperator(char op);  //判断是否为运算符
int priority(char op);     //求运算符优先级
void postfix(char pre[],char post[],int &n);//把中缀表达式转换为前缀表达式
/*uble read_number(char str[],int *i); //将数字字符串转变成相应的数字
double post_value(char post[]);   //由前缀表达式计算相应的中缀表达式的值
*/
typedef struct{
    SElemType *top;
    SElemType *base;
    int MAXSIZE;

   
}SqStack;
void InitStack(SqStack &S);  //初始化栈
bool StackEmpty(SqStack &S); //判断栈是否为空
int GetTop(SElemType &e); //读取栈顶的元素
void push(SElemType x); //进栈
int Pop(SElemType &e);//出栈


#include"expressino.h"
#include<iostream>
using namespace std;

bool isoperator(char op)  //判断是否为运算符
{
    switch(op)
    {
    case'+':
    case'-':
    case'*':
    case'/':
        return 1;
    default:
        return 0;
    }
}
int priority(char op)     //求运算符优先级
{
    switch(op)
    {
    case'#':
        return -1;
    case')':
        return 0;
    case'+':
    case'-':
        return 1;
    case'*':
    case'/':
        return 2;
    default:
        return -1;
    }
}
void postfix(char pre[],char post[],int &n)//把中缀表达式转换为前缀表达式,返回前缀表达式的长度(包括空格)
{   SElemType S,e;
    int i=n,j=n;
    InitStack(S);  //初始化存储操作符的栈
    push('#'); //把结束符#放入栈底
    while(pre[i]!='#')
    {
        if((pre[i]>='0'&&pre[i]<='9')||pre[i]=='.'||(pre[i]>='a'&&pre[i]<='z'))// 遇到数字和小数点、字母直接写入前缀表达式
        {
            post[j--]=pre[i];
            n--;
        }
        else if(pre[i]==')')  //遇到 )不用比较直接进栈
             push(pre[i]);

        else if(pre[i]=='(') //遇到 ( 将其对应左括号后的操作符(操作符栈中的)全部写入前缀表达式
        {
            while(GetTop(e)!='(')
            {
                post[j--]=GetTop(e);
                n--;
            }
            int Pop();//将")"出栈,前缀表达式中不含小括号
        }
        i--;
    }
    while(GetTop(e))  // 将所有的操作符加入前缀表达式
       {
        post[j--]=Pop(e);
        n--;
    }
}
void InitStack(SqStack &S)    //初始化栈
{S.base=(SElemType *)malloc(MAXSIZE*sizeof(SElemType));
 S.top=S.base;
if(!S.base) cout<<"出错:申请空间失败!"<<endl;
}

bool StackEmpty(SqStack &S) //判断栈是否为空
{
    if(S.top==S.base)
        return 1;
    else
        return 0;
}
int GetTop(SElemType &e)//读取栈顶的元素
{   SElemType S;
    e=*(S.top-1);
}

void push(SElemType x) //进栈
{   SElemType S;
    if(S.top-S.base>=MAXSIZE)
    cout<<"出错:表达式过长!"<<endl;
    *S.top++ =x;
}
int Pop(SElemType &e)//出栈
{  SElemType S;
    e=*--S.top;
    return e;
}


#include "expressino.h"
#include <iostream>
using namespace std;

void main()
{
    void InitStack(SqStack &s);

    char exp[100];
    cout << "请输入要转换的前缀表达式:";
    cin >> exp;

    char post[100] ;
   

    int n =0;            // 返回前缀表达式的长度
    postfix(exp,post,n);
    cout <<"前缀表达式为:";
    for( int i =n ;i>=0 ;i--)
        cout << post[i] ;


    system("pause");
}


--------------------Configuration: 前缀 - Win32 Debug--------------------
Compiling...
expressionfun.cpp
H:\前缀\expressionfun.cpp(39) : error C2664: 'InitStack' : cannot convert parameter 1 from 'int' to 'SqStack &'
        A reference that is not to 'const' cannot be bound to a non-lvalue
H:\前缀\expressionfun.cpp(83) : error C2228: left of '.top' must have class/struct/union type
H:\前缀\expressionfun.cpp(88) : error C2228: left of '.top' must have class/struct/union type
H:\前缀\expressionfun.cpp(88) : error C2228: left of '.base' must have class/struct/union type
H:\前缀\expressionfun.cpp(90) : error C2228: left of '.top' must have class/struct/union type
H:\前缀\expressionfun.cpp(94) : error C2228: left of '.top' must have class/struct/union type
执行 cl.exe 时出错.

前缀.exe - 1 error(s), 0 warning(s)
搜索更多相关主题的帖子: include double 表达式 优先级 字符串 
2015-04-20 22:54
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
#include<iostream>
#include<stdlib.h>
using namespace std;
const int MAXSIZE=100;
typedef int SElemType;
bool isoperator(char op);  //判断是否为运算符
int priority(char op);     //求运算符优先级
void postfix(char pre[],char post[],int &n);//把中缀表达式转换为前缀表达式
/*uble read_number(char str[],int *i); //将数字字符串转变成相应的数字
double post_value(char post[]);   //由前缀表达式计算相应的中缀表达式的值
*/
typedef struct{
    SElemType *top;
    SElemType *base;
    int MAXSIZE;
}SqStack;
void InitStack(SqStack &S);  //初始化栈
bool StackEmpty(SqStack &S); //判断栈是否为空
int GetTop(SElemType &e); //读取栈顶的元素
void push(SElemType x); //进栈
int Pop(SElemType &e);//出栈

bool isoperator(char op)  //判断是否为运算符
{
    switch(op)
    {
    case'+':
    case'-':
    case'*':
    case'/':
        return 1;
    default:
        return 0;
    }
}
int priority(char op)     //求运算符优先级
{
    switch(op)
    {
    case'#':
        return -1;
    case')':
        return 0;
    case'+':
    case'-':
        return 1;
    case'*':
    case'/':
        return 2;
    default:
        return -1;
    }
}
void postfix(char pre[],char post[],int &n)//把中缀表达式转换为前缀表达式,返回前缀表达式的长度(包括空格)
{   SElemType e;
    SqStack S;
    int i=n,j=n;
    InitStack(S);  //初始化存储操作符的栈
    push('#'); //把结束符#放入栈底
    while(pre[i]!='#')
    {
        if((pre[i]>='0'&&pre[i]<='9')||pre[i]=='.'||(pre[i]>='a'&&pre[i]<='z'))// 遇到数字和小数点、字母直接写入前缀表达式
        {
            post[j--]=pre[i];
            n--;
        }
        else if(pre[i]==')')  //遇到 )不用比较直接进栈
             push(pre[i]);

        else if(pre[i]=='(') //遇到 ( 将其对应左括号后的操作符(操作符栈中的)全部写入前缀表达式
        {
            while(GetTop(e)!='(')
            {
                post[j--]=GetTop(e);
                n--;
            }
            int Pop();//将")"出栈,前缀表达式中不含小括号
        }
        i--;
    }
    while(GetTop(e))  // 将所有的操作符加入前缀表达式
       {
        post[j--]=Pop(e);
        n--;
    }
}
void InitStack(SqStack &S)    //初始化栈
{S.base=(SElemType *)malloc(MAXSIZE*sizeof(SElemType));
S.top=S.base;
if(!S.base) cout<<"出错:申请空间失败!"<<endl;
}

bool StackEmpty(SqStack &S) //判断栈是否为空
{
    if(S.top==S.base)
        return 1;
    else
        return 0;
}
int GetTop(SElemType &e)//读取栈顶的元素
{   SqStack S;
    e=*(S.top-1);
}

void push(SElemType x) //进栈
{   SqStack S;
    if(S.top-S.base>=MAXSIZE)
    cout<<"出错:表达式过长!"<<endl;
    *S.top++ =x;
}
int Pop(SElemType &e)//出栈
{  SqStack S;
    e=*--S.top;
    return e;
}

int main()
{  
    char exp[100];
    cout<<"请输入要转换的前缀表达式:";
    cin>>exp;

    char post[100] ;

    int n=0;            // 返回前缀表达式的长度
    postfix(exp,post,n);
    cout <<"前缀表达式为:";
    for(int i=n ;i>=0 ;i--)
        cout<<post[i] ;

    system("pause");
}

剑栈风樯各苦辛,别时冰雪到时春
2015-04-27 18:02
快速回复:新人~~调了好久都调不出来
数据加载中...
 
   



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

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