首页 > 其他分享 >山东大学数据结构实验六

山东大学数据结构实验六

时间:2023-04-25 20:55:53浏览次数:38  
标签:return number else 实验 && 数据结构 山东大学 stack 表达式

计算表达式

tips:不要全文复制,会被查重哦

注意

因为精度问题,请使用double存数据。

要求

  1. 创建栈类,采用数组描述;
  2. 计算数学表达式的值。
    输入数学表达式,输出表达式的计算结果。数学表达式由单个数字和运算符+-*/() 构成,例如 2+3*(4+5)-6/4。假定表达式输入格式合法。

格式

输入

第一行一个整数n(1<=n<=100),代表表达式的个数。
接下来n行,每行一个表达式,保证表达式内的数字为单个整数,表达式内各运算符和数字间没有空格,且表达式的长度不超过2000。

输出

每行表达式输出一个浮点数,要求保留两位小数,保证输入表达式合法。

样例

输入

3
1+6/1*7+2*1*4+9/1+2*0*9+9+7/(9*5)-1*6-0*8-7-9*2+6-(0-5-2*8-7-9*5*(6-5*5*2*6-2-7-5+6*7+6*9-1*0*0+3*0+2/1-6/6+5))
0-4-1/6*(1-(6/7)-4+6+2+6*1)-1*7+2-8*2+0-(4+6-6*1+(3-8*6/4-6-5)*6/4/8+7-1*4/9*5)-0/6+1-0-2+7-2+6*4-3*6+2/8+6+1*6*2
5-3*9+5/1*5-9+1*8-6-8-4*1+5-2+9/3*2-2/5/(2-6)*2/7-9*0-2+4/6*6*7*8-8-8*6+8*9*(3+0*1/5/2*7*8+0-8*8-5+8/5*2-0)

输出

-9197.84
-3.47
-4362.57

限制

1s, 65536KiB for each test case.

#include<iostream>
#include <string>
#include <stdio.h>
#include <cstdio>
#include <iomanip>


using namespace std;

const int N = 2010;

template<class T>
class stack {
public:
    stack() {
        stackTop = -1;
        element = new T[N];
    }

    bool isEmpty() { return stackTop == -1; }

    ~stack() { delete[]element; }

    int size() { return stackTop + 1; }

    T &top();

    void pop();

    void push(const T &theElement);

private:
    T *element;
    int stackTop;
};

template<class T>
T &stack<T>::top() {
    return element[stackTop];
}

template<class T>
void stack<T>::pop() {
    stackTop--;
}

template<class T>
void stack<T>::push(const T &theElement) {
//在栈顶插入元素
    element[++stackTop] = theElement;
}


void calculate(stack<double> &number, stack<char> &operate) {
    auto a = number.top();
    number.pop();
    auto b = number.top();
    number.pop();
    auto c = operate.top();
    switch (c) {
        case '+': {
            number.push((double) (b + a));
            break;
        }
        case '-': {
            number.push((double) (b - a));
            break;
        }
        case '*': {
            number.push((double) (a * b));
            break;
        }
        case '/': {
            number.push((double) (b / a));
            break;
        }
    }
    operate.pop();
}

int priorityop(char a, char b)//判断a的优先级是否比b高或者相等
{
    if (a == '+' && b == '+')return 1;
    else if (a == '+' && b == '-')return 1;
    else if (a == '+' && b == '*')return 0;
    else if (a == '+' && b == '/')return 0;
    else if (a == '-' && b == '-')return 1;
    else if (a == '-' && b == '+')return 1;
    else if (a == '-' && b == '*')return 0;
    else if (a == '-' && b == '/')return 0;
    else if (a == '*' && b == '*')return 1;
    else if (a == '*' && b == '/')return 1;
    else if (a == '*' && b == '+')return 1;
    else if (a == '*' && b == '-')return 1;
    else if (a == '/' && b == '*')return 1;
    else if (a == '/' && b == '/')return 1;
    else if (a == '/' && b == '+')return 1;
    else if (a == '/' && b == '-')return 1;

}


int main() {

    double n;
    cin >> n;

    while (n--) {
        string str;
        cin >> str;
        int len = str.length();
        stack<double> number;
        stack<char> operate;
        for (int i = 0; i < len; ++i) {
            if (str[i] - '0' >= 0 && str[i] - '0' <= 9) {
                int x = 0, j = i;
                while (j < len && str[j] - '0' >= 0 && str[j] - '0' <= 9)
                    x = x * 10 + str[j++] - '0';
                i = j - 1;
                number.push((double) x);

            } else if (str[i] == '(') operate.push(str[i]);
            else if (str[i] == ')') {
                while (operate.top() != '(') calculate(number, operate);
                operate.pop();
            } else {
                while (operate.size() && operate.top() != '(' && priorityop(operate.top(), str[i]))
                    calculate(number, operate);
                operate.push(str[i]);

            }
        }
        while (operate.size()) calculate(number, operate);
        double res = number.top();
        printf("%.2lf\n",res);
    }


    return 0;
}

标签:return,number,else,实验,&&,数据结构,山东大学,stack,表达式
From: https://www.cnblogs.com/lyz103/p/17353844.html

相关文章

  • 山东大学数据结构实验一(2)
    题目描述现有一个有n个元素的序列\(a=[a_{1},a_{2},\cdots,a_{n}]\),定义其价值为\(\sum_{i=1}^{n}a_{i}\oplusi\)给出这样一个序列,求其所有排列的价值\(v_{i}\)的或\(v_{1}|v_{2}|\cdots|v_{n-1}|v_{n}\)其中\(|\)为位运算或操作,\(\oplus\)为位运算异......
  • 山东大学数据结构实验一(1)
    题目描述现有一个有\(n\)个元素的序列\(a=[a_1,a_2,\cdots,a_n]\),定义这个序列的价值为\(\sum_{i=1}^{n}i\timesa_i\)。空序列的价值为\(0\)。先给你一个长度为\(n\)的序列\(a\),求\(a\)中所有子集价值的异或和,要求子集中元素的相对位置保持不变。异或和:位运算的一种。如果a......
  • 实验三
    任务一实验源码运行结果截图任务二实验源码1lst=[55,92,88,79,96]23i=04whilei<len(lst):5print(lst[i],end='')6i+=17print()89foriinrange(len(lst)):10print(lst[i],end='')11print()1213forii......
  • 实验3 控制语句与组合数据类型应用编程
    实验任务1实验源码1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('......
  • 实验3
    1.实验任务1task1.py实验代码:importrandomprint('用列表储存随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合储存随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合储存随机整数:')s......
  • 实验3
    实验任务1:源码:importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set(......
  • 实验三
    实验任务一:实验源码:1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('\......
  • 实验三
    task1源代码importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()whilelen(......
  • 实验3 控制语句与组合数据类型应用编程
    一.实验目的:1.知道Python中组合数据类型字符串(str)、列表(list)、元组(tuple)、集合(set)、字典的表示、特性2.能够正确、熟练使用字符串(str)、列表(list)、元组(tuple)、集合(set)、字典的常用操作3.针对具体问题场景,能够灵活、组合使用多种数据类型,应用或设计算法,使用......
  • 实验3
    实验任务1源代码:importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()whil......