zoukankan      html  css  js  c++  java
  • 四则运算的进度和遇到的问题

    // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include "MyStack.h"
    #include "PrefixToPostfix.h"
    #include<iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int zxgb(int a, int b);
    
    void main()
    {
        MyStack<int> stack;
        stack.init();
        int result[4];
        int result1[4];
        int m = 0;
    
        char exp[100];
        ifstream myfile("a.txt");
        if (!myfile){
            cout << "无法打开文件";
            exit(1); // terminate with error  
        }
        for (int i = 0; i < 4; i++)
        {
            {
                myfile >> exp;
                char post[100];
                char get[100];
                int n = 0;            // 返回后缀表达式的长度
                postfix(exp, post, n);
                result[i] = postfix_value(post);
                cout << "" << i + 1 << "题的答案是:\n";
                cin >> result1[i];
                if (result[i] == result1[i])
                {
                    m++;
                }
            }
        }
        cout << "您本次作对了" << m << "道题\n";
        system("pause");
    }
    
    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 '*':
            return 2;
        default:
            return -1;
        }
    }
    
    //     把中缀表达式转换为后缀表达式,返回后缀表达式的长度(包括空格)
    void postfix(char pre[], char post[], int &n)
    {
        int i = 0, j = 0;
        MyStack<char> stack;
        stack.init();        // 初始化存储操作符的栈
    
        stack.push('=');    // 首先把结束标志‘#’放入栈底
    
        while (pre[i] != '=')
        {
            if ((pre[i] >= '0' && pre[i] <= '9') || pre[i] == '.' || pre[i] == '/') // 遇到数字和小数点直接写入后缀表达式
            {
                post[j++] = pre[i];
                n++;
            }
            else if (pre[i] == '(')    // 遇到“(”不用比较直接入栈
                stack.push(pre[i]);
            else if (pre[i] == ')')  // 遇到右括号将其对应左括号后的操作符(操作符栈中的)全部写入后缀表达式
            {
                while (stack.gettop() != '(')
                {
                    post[j++] = stack.pop();
                    n++;
                }
                stack.pop(); // 将“(”出栈,后缀表达式中不含小括号
            }
            else if (isoperator(pre[i]))
            {
                post[j++] = ' '; // 用空格分开操作数(
                n++;
                while (priority(pre[i]) <= priority(stack.gettop()))
                {
                    // 当前的操作符小于等于栈顶操作符的优先级时,将栈顶操作符写入到后缀表达式,重复此过程
                    post[j++] = stack.pop();
                    n++;
                }
    
                stack.push(pre[i]);    // 当前操作符优先级大于栈顶操作符的优先级,将该操作符入栈
            }
    
            i++;
        }
        while (stack.top) // 将所有的操作符加入后缀表达式
        {
            post[j++] = stack.pop();
            n++;
        }
    }
    
    double read_number(char str[], int *i)
    {
        double x = 0.0;
        int k = 0;
        while (str[*i] >= '0' && str[*i] <= '9')  // 处理整数部分
        {
            x = x * 10 + (str[*i] - '0');
            (*i)++;
        }
    
        if (str[*i] == '.') // 处理小数部分
        {
            (*i)++;
            while (str[*i] >= '0'&&str[*i] <= '9')
            {
                x = x * 10 + (str[*i] - '0');
                (*i)++;
                k++;
            }
        }
        while (k != 0)
        {
            x /= 10.0;
            k--;
        }
    
        return x;
    }
    
    double postfix_value(char post[])
    {
        MyStack<double> stack;    // 操作数栈
        stack.init();
    
        int i = 0;
        double x1, x2;
    
        while (post[i] != '=')
        {
            if (post[i] >= '0' && post[i] <= '9')
                stack.push(read_number(post, &i));
            else if (post[i] == ' ')
                i++;
            else if (post[i] == '+')
            {
                x2 = stack.pop();
                x1 = stack.pop();
                stack.push(x1 + x2);
                i++;
            }
            else if (post[i] == '-')
            {
                x2 = stack.pop();
                x1 = stack.pop();
                stack.push(x1 - x2);
                i++;
            }
            else if (post[i] == '*')
            {
                x2 = stack.pop();
                x1 = stack.pop();
                stack.push(x1*x2);
                i++;
            }
            /*else if (post[i] == '/')
            {
                x2 = stack.pop();
                x1 = stack.pop();
                stack.push(x1 / x2);
                i++;
            }*/
        }
        return stack.gettop();
    }
    
    int fenshu(int a, int b)
    {
    
    }
    //MyStack.h
    
    #include <iostream>
    using namespace std;
    
    template <class ElemType> class MyStack
    {
    public:
        const static  int MAXSIZE = 100;
        ElemType data[MAXSIZE];
        int top;
    public:
        void init();            // 初始化栈
        bool empty();            // 判断栈是否为空
        ElemType gettop();        // 读取栈顶元素(不出栈)
        void push(ElemType x);    // 进栈
        ElemType pop();            // 出栈
    };
    
    template<class T> void MyStack<T>::init()
    {
        this->top = 0;
    }
    
    template<class T> bool MyStack<T>::empty()
    {
        return this->top == 0 ? true : false;
    }
    
    template<class T> T MyStack<T>::gettop()
    {
        if (empty())
        {
            cout << "栈为空!\n";
            exit(1);
        }
        return this->data[this->top - 1];
    }
    
    template<class T> void MyStack<T>::push(T x)
    {
        if (this->top == MAXSIZE)
        {
            cout << "栈已满!\n";
            exit(1);
        }
        this->data[this->top] = x;
        this->top++;
    }
    
    template<class T> T MyStack<T>::pop()
    {
        if (this->empty())
        {
            cout << "栈为空! \n";
            exit(1);
        }
        T e = this->data[this->top - 1];
        this->top--;
        return e;
    }
    // PrefixToPostfix.h
    
    #include <vector>
    using namespace std;
    
    bool isoperator(char op);                          // 判断是否为运算符
    int priority(char op);                                // 求运算符优先级
    void postfix(char pre[], char post[], int &n);    // 把中缀表达式转换为后缀表达式
    double read_number(char str[], int *i);              // 将数字字符串转变成相应的数字
    double postfix_value(char post[]);                  // 由后缀表达式字符串计算相应的中值表达式的值 

    目前已经可以实现从文件里读取算式,然后用户输入答案与系统做得答案进行匹配,如果相同则正确数加1

    可以进行小数和整数的相加,相乘,相除,相减

    分数的四则运算还有问题,待进一步改进

    // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。//
    #include "stdafx.h"#include "MyStack.h"#include "PrefixToPostfix.h"#include<iostream>#include <string>#include <fstream>using namespace std;
    int zxgb(int a, int b);
    void main(){MyStack<int> stack;stack.init();int result[4];int result1[4];int m = 0;
    char exp[100];ifstream myfile("a.txt");if (!myfile){cout << "无法打开文件";exit(1); // terminate with error  }for (int i = 0; i < 4; i++){{myfile >> exp;char post[100];char get[100];int n = 0;            // 返回后缀表达式的长度postfix(exp, post, n);result[i] = postfix_value(post);cout << "第" << i + 1 << "题的答案是:\n";cin >> result1[i];if (result[i] == result1[i]){m++;}}}cout << "您本次作对了" << m << "道题\n";system("pause");}
    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 '*':return 2;default:return -1;}}
    //     把中缀表达式转换为后缀表达式,返回后缀表达式的长度(包括空格)void postfix(char pre[], char post[], int &n){int i = 0, j = 0;MyStack<char> stack;stack.init();        // 初始化存储操作符的栈
    stack.push('=');    // 首先把结束标志‘#’放入栈底
    while (pre[i] != '='){if ((pre[i] >= '0' && pre[i] <= '9') || pre[i] == '.' || pre[i] == '/') // 遇到数字和小数点直接写入后缀表达式{post[j++] = pre[i];n++;}else if (pre[i] == '(')    // 遇到“(”不用比较直接入栈stack.push(pre[i]);else if (pre[i] == ')')  // 遇到右括号将其对应左括号后的操作符(操作符栈中的)全部写入后缀表达式{while (stack.gettop() != '('){post[j++] = stack.pop();n++;}stack.pop(); // 将“(”出栈,后缀表达式中不含小括号}else if (isoperator(pre[i])){post[j++] = ' '; // 用空格分开操作数(n++;while (priority(pre[i]) <= priority(stack.gettop())){// 当前的操作符小于等于栈顶操作符的优先级时,将栈顶操作符写入到后缀表达式,重复此过程post[j++] = stack.pop();n++;}
    stack.push(pre[i]);    // 当前操作符优先级大于栈顶操作符的优先级,将该操作符入栈}
    i++;}while (stack.top) // 将所有的操作符加入后缀表达式{post[j++] = stack.pop();n++;}}
    double read_number(char str[], int *i){double x = 0.0;int k = 0;while (str[*i] >= '0' && str[*i] <= '9')  // 处理整数部分{x = x * 10 + (str[*i] - '0');(*i)++;}
    if (str[*i] == '.') // 处理小数部分{(*i)++;while (str[*i] >= '0'&&str[*i] <= '9'){x = x * 10 + (str[*i] - '0');(*i)++;k++;}}while (k != 0){x /= 10.0;k--;}
    return x;}
    double postfix_value(char post[]){MyStack<double> stack;    // 操作数栈stack.init();
    int i = 0;double x1, x2;
    while (post[i] != '='){if (post[i] >= '0' && post[i] <= '9')stack.push(read_number(post, &i));else if (post[i] == ' ')i++;else if (post[i] == '+'){x2 = stack.pop();x1 = stack.pop();stack.push(x1 + x2);i++;}else if (post[i] == '-'){x2 = stack.pop();x1 = stack.pop();stack.push(x1 - x2);i++;}else if (post[i] == '*'){x2 = stack.pop();x1 = stack.pop();stack.push(x1*x2);i++;}/*else if (post[i] == '/'){x2 = stack.pop();x1 = stack.pop();stack.push(x1 / x2);i++;}*/}return stack.gettop();}
    int fenshu(int a, int b){
    }




  • 相关阅读:
    网络记事本第八天
    软件工程第十周总结
    网络记事本第六,七天
    网络记事本开发,第四天
    网络记事本开发第二,三天
    leetcode 198 打家劫舍
    leetcode 46 全排列
    设计模式 之 动态代理
    设计模式 之 静态代理
    设计模式 之 桥接模式
  • 原文地址:https://www.cnblogs.com/ji5jin45/p/5272793.html
Copyright © 2011-2022 走看看