算法学习-day11

#include<bits/stdc++.h>
using namespace std;

//优先级计算
// int priority(char x){
//     if(x == '#'){
//         return 0;
//     }else if(x == '$'){
//         return 1;
//     }else if(x == '+' || x == '-'){
//         return 2;
//     }else{
//         return 3;
//     }
// };

// double Calculate(double a,double b,char op){
//     if(op == '+'){
//         return a + b;
//     }else if(op == '-'){
//         return a - b;
//     }else if(op == '*'){
//         return a * b;
//     }else if(op == '/'){
//         return a / b;
//     }else{
//         return 0;
//     }
// };

// 字符串除法
void character(string str){
    int remain = 0;//余数
    for(int i = 0;i < str.size();i++){
        int current = remain * 10 + str[i] - '0';
        remain = current % 2;  
        str[i] = current / 2 + '0';
    }
    while(str[0] == 0){
        str.erase(0,1);
    }
};


int main(){
    // 计算表达式
    //思路:
    // 1)初始化两个栈,一个存储计算符,一个存储计算数
    // 2)设置在计算符栈中放置一个特殊运算符"#",其优先级最低
    //在表达式尾部添加一个特殊运算符"$"使其优先级刚大于"#"
    // 3)遇到计算数直接压入运算数栈,遇到计算符则先与计算符栈顶元素
    //对比优先级,若优先级小于计算符栈顶元素,则弹出栈顶运算符并依次
    //弹出运算数栈中元素进行计算,再将结果压回计算数栈,计算符入栈
    //若当前计算符优先级大于栈中计算符,直接入栈
    //当计算符栈中仅剩两个特殊运算符时,计算结束
    
    // string operat;
    // int index = 0;
    // while(cin >> operat){
    //     stack<int> number;//计算数栈
    //     stack<char> character;//计算符栈
    //     character.push('#');
    //     operat += '$';
    //     while(index < operat.size()){
    //         if(isdigit(operat[index])){
    //             number.push(int(operat[index]));
    //         }else{
    //             if(priority(operat[index]) > priority(character.top())){
    //                 character.push(operat[index]);
    //                 index++;
    //             }else{
    //                 double a = number.top();
    //                 number.pop();
    //                 double b = number.top();
    //                 number.pop();
    //                 number.push(Calculate(b,a,character.top()));
    //                 character.pop();
    //             }
    //         }
    //     }
    //     cout << number.top() << endl;
    // }
    
    
    
    //“堆栈的应用” 吉大上机
    // stack<int> mystack;//初始化栈
    // int n,num;
    // string str1;
    // while(cin >> n){
    //     while(n--){
    //         cin >> str1;
    //         if(str1 == "P"){
    //             cin >> num;
    //             mystack.push(num);
    //         }else if(str1 == "O" && !mystack.empty()){
    //             mystack.pop();
    //         }else if(str1 == "A" && !mystack.empty()){
    //             cout << mystack.top() << endl;
    //         }else if(str1 == "A" && mystack.empty()){
    //             cout << "E" << endl;
    //         }
    //     }
    // }

    // 二进制数
    // unsigned int n;
    // while(cin >> n){
    //   stack<int> num;
    //   while(n != 0){
    //       num.push(n % 2);
    //       n /= 2;
    //   }
    //   int x = num.size();    //此处不可直接在循环中使用num.size(),因为栈的长度一直在变换;导致实际输出变短
    //   for(int i = 0;i < x;i++){
    //       cout << num.top();
    //       num.pop();
    //   }
    //   cout << endl;
    // }
    
    // 进制转换
    // 本题陷阱是输入最长为30为的数字,应该用字符串表示
    // string str; 
    // while(cin >> str){
    //     vector<int> v;
    //     while(!str.empty()){
    //         v.push_back((str[str.length() -  1]));
    //         character(str);
    //     }
    //     for(int i = v.size() - 1;i <= 0;i--){
    //         cout << v[i];
    //     }
    //     cout <<endl;
    // }
    //}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值