zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    C#函数复习
    ADO数据库访问类查询、属性扩展
    ADO.NET完整的删除与修改, 实体类和数据访问类
    ADO.NET增删改查
    类库、委托
    多态
    面向对象:封装、继承
    面向对象思想:对象和类
    sql 存储过程、事务、视图、触发器
    连接查询,结构、循环语句
  • 原文地址:https://www.cnblogs.com/vongang/p/2215898.html
Copyright © 2011-2022 走看看