码迷,mamicode.com
首页 > 其他好文 > 详细

北京理工大学复试上机--2010

时间:2020-03-25 01:44:32      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:erase   algo   while   char   str   div   中缀   int   cas   

1.输入一串整数,输入命令排序!
输入 a t 在这串整数后面添加整数 t,
输入 c\m\n 有 n 替换 m,
输入 d t 删除 t,
输入 s 排序。
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

int tonum(string s, int l) {
    int n = 1, sum = 0;
    for(int i = l - 1; i > 1; i--) {
        sum += (s[i] - 0) * n;
        n *= 10;
    }
    return sum;
}

int main() {
    int n;
    vector<int> v;
    while(cin >> n) {
        v.push_back(n);
        if(getchar() == \n) {
            string s;
            while (getline(cin, s)) {
                if (s[0] == a) {
                    v.push_back(tonum(s, s.length()));
                    for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
                    cout << endl;
                }
                if (s[0] == c) {
                    string s1, s2;
                    int cnt = 0, num1, num2, temp;
                    for(int k = 0; k < s.length(); k++) {
                        if(s[k] == \\) cnt++;
                        if(cnt == 2) {
                            s1 = s.substr(0, k);
                            s2 = s.substr(k - 1, s.length() - k + 1);//这里比较麻烦 为了前面的统一化转数字
                            break;
                        }
                    }
                    num1 = tonum(s1, s1.length());
                    num2 = tonum(s2, s2.length());
                    for (int i = 0; i < v.size(); i++)
                    {
                        
                        if(v[i] == num1) {
                            temp = v[i];
                            v.erase(v.begin() + i);
                            v.insert(v.begin() + i, num2);
                        }
                    }
                    for (int i = 0; i < v.size(); i++)
                    {
                        cout << v[i] << " ";
                    }
                    cout << endl;
                }
                if (s[0] == s) {
                    sort(v.begin(), v.end());
                    for (int i = 0; i < v.size(); i++) {
                        cout << v[i] << " ";
                    }
                    cout << endl;
                }
                if (s[0] == d) {
                    int num = tonum(s, s.length());
                    for (int i = 0; i < v.size(); i++) {
                        if(v[i] == num) v.erase(v.begin() + i);
                    }
                    for (int i = 0; i < v.size(); i++) {
                        cout << v[i] << " ";
                    }
                    cout << endl;
                }
            }
        }
    }
    return 0;
}
2、输入表达式,输出值。分两种情况:中缀表达式和后缀表达式。 
中缀表达式求值:先将中缀表达式建立二叉树转后缀表达式(应该不会严格要求建树解决),然后再求值。
input:  1+2-1*((3+4)/5-6)+7
           12+134+5/6-*-7+
result: 14.6
#include <iostream>
#include <vector>
#include <map>
#include <stack>
using namespace std;

string toSuf(string s) {
    map<char, int> isp, icp; //isp--in stack preority icp--in coming preority
    isp[(] = 1; isp[*] = 5; isp[/] = 5; isp[+] = 3; isp[-] = 3; isp[)] = 6;
    icp[(] = 6; icp[*] = 4; icp[/] = 4; icp[+] = 2; icp[-] = 2; icp[)] = 1;
    string sufstr;
    stack<char> opt;//operator
    for (int i = 0; i < s.length(); i++) {
        if (isalpha(s[i]) || (s[i] >= 0 && s[i] <= 9)) {
            sufstr += s[i];
        }
        else if (opt.empty() || icp[s[i]] > isp[opt.top()]) {
            opt.push(s[i]);
        }
        else
        {
            if(s[i] == )) {
                while(opt.top() != () {
                    sufstr += opt.top();
                    opt.pop();
                }
                opt.pop();
            }
            else {
                while(!opt.empty() && isp[opt.top()] >= icp[s[i]]) {
                    sufstr += opt.top();
                    opt.pop();
                }
                opt.push(s[i]);
            }
        }
    }
    while(!opt.empty()) {
        sufstr += opt.top();
        opt.pop();
    }
    return sufstr;
}

double calSuf(string s) {
    stack<double> opd;//operand
    for(int i  = 0; i < s.length(); i++) {
        if(s[i] >= 0 && s[i] <= 9) {
            opd.push(s[i] - 0);
        }
        else {
            double opd2 = opd.top();
            opd.pop();
            double opd1 = opd.top();
            opd.pop();
            switch (s[i]) {
                case +: opd.push(opd1 + opd2); break;
                case -: opd.push(opd1 - opd2); break;
                case *: opd.push(opd1 * opd2); break;
                case /: opd.push(opd1 / opd2); break;
            }
        }
    }
    return opd.top();
}

int main() {
    int sum1, sum2;
    string s1, s2, sufs;
    cout << "请输入中缀表达式: ";
    cin >> s1;
    sufs = toSuf(s1);
    cout << "后缀表达式为: " << sufs << endl;
    cout<< "结果为: " << calSuf(sufs) << endl;
    cout << "请输入后缀表达式: ";
    cin >> s2;
    cout << "结果为: " << calSuf(s2) << endl;
    return 0;
}

 

北京理工大学复试上机--2010

标签:erase   algo   while   char   str   div   中缀   int   cas   

原文地址:https://www.cnblogs.com/ache/p/12563380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!