鱼C论坛

 找回密码
 立即注册
查看: 2104|回复: 2

[已解决]c++中字符转换问题

[复制链接]
发表于 2023-1-17 18:42:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
//文件名:test.cpp
#include <iostream>
#include <cmath>                                          // For pow() function
#include <iostream>                                       // For standard streams
#include <stack>                                          // For stack<T> container
#include <queue>
#include <algorithm>                                      // For remove()
#include <stdexcept>                                      // For runtime_error exception
#include <string>  

int main ()
{
    std::string s("9+(3-1)*3+10/2");
    std::stack<std::string> z;
    std::queue<std::string> d;
    int cur(0);
    //逆波兰
    for(auto c:s)
    {
        if(c>='0' && c<='9')
        {
            cur = cur*10 + c-'0';
        }
        else
        {
            d.push(std::to_string(cur));
            cur = 0;
            if((c=='(')||(c=='+')||(c=='-'))
            {
                z.push(std::to_string(c));
            }
            else if((c=='*')||(c=='/'))
            {
                while((z.top()=="+")||(z.top()=="-"))
                {
                    d.push(z.top());
                    z.pop();
                }
            }
            else if (c==')')
            {
                while(z.top()!="(")
                {
                    //std::cout << z.top()  << std::endl;
                    d.push(z.top());
                    z.pop();
                };
                z.pop();
            }
        }
   
    }
   
    //根据逆波兰表达式求值
    std::stack<int> nums;
    while(!d.empty())
    {
        std::string cur_s = d.front();
        if(cur_s[0]>='0'||cur_s[0]<='9')
        {
            int cur_num(0);
            for(int i=cur_s.size()-1;i>=0;i--)
            {
                cur_num = cur_num*10 + cur_s[i];
            }
            nums.push(cur_num);
            d.pop();
        }
        else
        {
            int num1(0);
            int num2(0);
            int num3(0);
            num1 = nums.top();
            nums.pop();
            num2 = nums.top();
            nums.pop();
            if(cur_s=="+") num3 = num2+num1;
            else if(cur_s=="-") num3 = num2-num1;
            else if(cur_s=="*") num3 = num2*num1;
            else num3 = num2/num1;
            nums.push(num3);
        }
    }
    return nums.top();
   
}

以上的代码中,其中下面这段的打印输出,应该是()+-*这样的符号才对啊,但是打印输出的是其符号的ASCII的数值,这样一执行,这个while就跑飞了,哪个大神给解释下呢?
               while(z.top()!="(")
                {
                    //std::cout << z.top()  << std::endl;
                    d.push(z.top());
                    z.pop();
                };

最佳答案
2023-1-17 18:45:44
本帖最后由 ExiaGN001 于 2023-1-17 19:01 编辑

1.因为string to_string(char)不存在 且char能转换成int
所以 z.push(std::to_string(c)) 执行时,to_string实际调用的是 string to_string(int),
而char变量强转int就是ASCII值,再弄进字符串里,就是lz得到的结果,
这个错误会导致Wrong Answer。
2.在使用z(d) .top()和z(d) .pop()的时候未考虑到z(d)为空的情况,会导致 Runtime Error 并返回3221225477.
分析异常返回值可知程序读取了无权访问的东西。
所以需要在使用以上函数前对z和d进行判空
根据这一点去修改就可得出正确结果。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-1-17 18:45:44 | 显示全部楼层    本楼为最佳答案   
本帖最后由 ExiaGN001 于 2023-1-17 19:01 编辑

1.因为string to_string(char)不存在 且char能转换成int
所以 z.push(std::to_string(c)) 执行时,to_string实际调用的是 string to_string(int),
而char变量强转int就是ASCII值,再弄进字符串里,就是lz得到的结果,
这个错误会导致Wrong Answer。
2.在使用z(d) .top()和z(d) .pop()的时候未考虑到z(d)为空的情况,会导致 Runtime Error 并返回3221225477.
分析异常返回值可知程序读取了无权访问的东西。
所以需要在使用以上函数前对z和d进行判空
根据这一点去修改就可得出正确结果。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-24 18:44:44 | 显示全部楼层
正在学C++,从C到C++,一下感觉体量大了好几倍都不止。。。。。。。。。。知识点太多了。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-7 19:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表