• C++实现数据结构作业——表达式求值


      

        /*用到了class, template,析构函数,构造函数,C++菜鸟一只,大牛无视。各种bug,待修改。。。*/
    //My Code:

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>

    using namespace std;

    const int N = 256;
    char map[N][N];

    template <class T>

    class Node{
    public:
    T data;
    Node * next;
    Node(const T &e, Node<T> *t){
    data = e;
    next = t;
    }
    };

    template <class T>

    class LinkStack{
    private:

    Node<T> * top;
    void init(){
    top = NULL;
    }
    public:
    void Clear(){
    T tmp;
    while(!Empty())
    Pop(tmp);
    }
    LinkStack(){
    init();
    }
    virtual ~LinkStack(){
    Clear();
    }
    int Length() const
    {
    int count = 0;
    for(Node<T> * i = top; i != NULL; i = i->next){
    count++;
    }
    return count;
    }
    bool Empty() const{
    return top == NULL;
    }
    int Push(const T &e){
    Node<T> * new_top = new Node<T>(e, top);
    if(new_top == NULL)
    return 0;
    else{
    top = new_top;
    return 1;
    }
    }
    T GetTop(T &e) const{
    if(Empty()){
    return 0;
    } else {
    e = top->data;
    return e;
    }
    }
    int Pop(T &e){
    if(Empty()){
    return 0;
    } else {
    Node<T> * old_top = top;
    e = old_top->data;
    top = old_top->next;
    delete old_top;
    return 1;
    }
    }

    };

    char c[10];
    int i;

    void get_in(){

    memset(c, 0, sizeof(c)); i = 0;
    while(cin.get(c[i++])){
    if(c[i-1] == ' ')
    break;
    }
    }

    void inite(){
    map['+']['+'] = map['+']['-'] = map['+'][')'] = map['+']['#'] = '>';map['+']['*'] = map['+']['/'] = map['+']['('] = '<';
    map['-']['+'] = map['-']['-'] = map['-'][')'] = map['-']['#'] = '>';map['-']['*'] = map['-']['/'] = map['-']['('] = '<';
    map['*']['+'] = map['*']['-'] = map['*']['*'] = map['*']['/'] = map['*'][')'] = map['*']['#'] = '>'; map['*']['('] = '<';
    map['/']['+'] = map['/']['-'] = map['/']['*'] = map['/']['/'] = map['/'][')'] = map['/']['#'] = '>'; map['/']['('] = '<';
    map[')']['+'] = map[')']['-'] = map[')']['*'] = map[')']['/'] = map[')'][')'] = map[')']['#'] = '>';
    map['(']['+'] = map['(']['-'] = map['(']['*'] = map['(']['/'] = map['(']['('] = '<'; map['('][')'] = '=';
    map['#']['+'] = map['#']['-'] = map['#']['*'] = map['#']['/'] = map['#']['('] = '<'; map['#']['#'] = '=';
    }

    char Precede(char a, char b){
    return map[(int)a][(int)b];
    }

    int Oprate(int a, char t, int b){
    int x;
    switch (t){
    case '+':
    x = a + b; break;
    case '-':
    x = a - b; break;
    case '*':
    x = a * b; break;
    case '/':
    x = a / b; break;
    }
    return x;
    }

    int In(char c){
    if(c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' || c == '#')
    return 1;
    return 0;
    }

    int solve(){
    LinkStack<int> opnd;
    LinkStack<char> optr; optr.Push('#');
    char e, th;
    int f, b, a;
    get_in();
    while(c[0] != '#' || (optr.GetTop(e) !='#')){
    if(!In(c[0])){
    f = 0;
    for(int j = 0; j < i-1; j++){
    f += (c[j]-'0') * pow(10.0, i-2-j);
    }
    opnd.Push(f);
    get_in();
    } else {
    switch (Precede(optr.GetTop(e), c[0])){
    case '<':
    optr.Push(c[0]); get_in();
    break;
    case '=':
    optr.Pop(e); get_in();
    //cout << optr.GetTop(e) << endl;
    break;
    case '>':
    optr.Pop(th);
    opnd.Pop(b); opnd.Pop(a);
    opnd.Push(Oprate(a, th, b));
    //cout << a << " " << th << " " << b << endl;
    //cout << opnd.GetTop(f) << endl;
    break;
    }
    }
    }
    return opnd.GetTop(f);

    }
    int main(){
    //freopen("data.in", "r", stdin);
    inite();
    cout << "Please input the Expresssion : " << endl;
    cout << solve() << endl;
    return 0;
    }

  • 相关阅读:
    项目中常用的linux命令
    Flutter移动电商实战 --(12)首页导航区域编写
    Flutter移动电商实战 --(13)ADBanner组件的编写
    Flutter移动电商实战 --(10)使用FlutterSwiper制作轮播效果
    Flutter移动电商实战 --(9)移动商城数据请求实战
    Flutter移动电商实战 --(8)dio基础_伪造请求头获取数据
    Flutter移动电商实战 --(7)dio基础_POST请求的使用
    Flutter移动电商实战 --(6)dio基础_Get_Post请求和动态组件协作
    Flutter移动电商实战 --(5)dio基础_引入和简单的Get请求
    Flutter移动电商实战 --(4)打通底部导航栏
  • 原文地址:https://www.cnblogs.com/vongang/p/2215898.html
Copyright © 2020-2023  润新知