c++程序为什么会崩溃?

隐逸之仙 2014-04-09 12:34:08
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
using std::string;
static const size_t npos = -1;
//不知道是不是因为对一个值在不同函数反复引用且不改变变量导致崩溃的。
void opperation(string &_expression, string &_str);
void times_divide(string &_expression, string &_str);
void plus_minus(string &_expression, string &_str);
void renew(string &_str, int _num, int i, int j);

void opperation(string &_expression, string &_str)
{
times_divide(_expression, _str);
}
void times_divide(string &_expression, string &_str)
{
int num, num_1, num_2;
int temp_1, temp_2, i = 2, j = 2;
if (_str.find_first_of('*')!=_str.npos)
//如果首先找到*号则进入判定
{
temp_1 = _str.find_first_of('*')-2;
//temp_1是*号靠左相邻的运算符,暂时考虑两运算符之间的数据为1位
temp_2 = _str.find_first_of('*')+2; //类似上
while (_str[_str.find_first_of('*')-i]>=48 && _str[_str.find_first_of('*')-i]<=57)
//当两运算符之间的数据不是一位时,两temp分别向两边推移
{
temp_1--;
i--;
}
while (_str[_str.find_first_of('*')+j]>=48 && _str[_str.find_first_of('*')+j]<=57)
{
temp_2++;
j++;
}
num_1 = atoi(_str.substr(temp_1+1, _str.find_first_of('*')-temp_1).c_str());
//截取*号两边的两个数据并转换为整形
num_2 = atoi(_str.substr(_str.find_first_of('*')+1, temp_2-_str.find_first_of('*')-2).c_str());
num = num_1*num_2;
//计算结果,实际上后面就是用结果替换掉这步乘法运算

renew(_str, num, temp_1, temp_2);
//字符串更新,减少掉一个*号,直接代入结果,
times_divide(_expression, _str);
//用递归实现逐步减少*号至没有
}
else if (_str.find_first_of('/')!=_str.npos) //这个是/号的,类似上
{
temp_1 = _str.find_first_of('/')-2;
temp_2 = _str.find_first_of('/')+2;
while (_str[_str.find_first_of('/')-i]>=48 && _str[_str.find_first_of('/')-i]<=57)
{
temp_1--;
i--;
}
while (_str[_str.find_first_of('/')+j]>=48 && _str[_str.find_first_of('/')+j]<=57)
{
temp_2++;
j++;
}
num_1 = atoi(_str.substr(temp_1+1, _str.find_first_of('/')-temp_1).c_str());
num_2 = atoi(_str.substr(_str.find_first_of('/')+1, temp_2-_str.find_first_of('/')-2).c_str());
num = num_1*num_2;

renew(_str, num, temp_1, temp_2);
times_divide(_expression, _str);
}
else //如果没有*和/就结束递归
{
return;
}
}
void plus_minus(string &_expression, string &_str)
{//这个是加减部分的,暂不考虑
}
void renew(string &_str, int _num, int i, int j)
{
stringstream ss; //这一大串是将整形转为string
string temp;
ss << _num;
ss >> temp;
_str = _str.substr(0, i) + temp + _str.substr(j); //连接成新的字符串
}

int main(void)
{
string expression("123456"); //这个暂时没用,请忽略
string str("1*2+3/4");

opperation(expression, str);
return 0;
}
...全文
316 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zybjtu 2014-04-10
  • 打赏
  • 举报
回复
str下标越界。
while(_str[_str.find_first_of('*')-i]>=48 && _str[_str.find_first_of('*')-i]<=57)
这句话,你取出了*位置,然后减2. 小于0了。c++里string对象木有小于0的下标操作啊
隐逸之仙 2014-04-10
  • 打赏
  • 举报
回复
引用 9 楼 buyong 的回复:
你这是C++代码
对呀,然后呢
buyong 2014-04-09
  • 打赏
  • 举报
回复
你这是C++代码
隐逸之仙 2014-04-09
  • 打赏
  • 举报
回复
引用 7 楼 derekrose 的回复:
楼主这种叙述的方式很好 有注释有代码有解释
汗,我知错了,不习惯写注释,等我注释下再上图吧,话说写注释有什么要求或需要知道的
derekrose 2014-04-09
  • 打赏
  • 举报
回复
楼主这种叙述的方式很好 有注释有代码有解释
隐逸之仙 2014-04-09
  • 打赏
  • 举报
回复
嗯,通过调试找出原因了,这其实是一个写计算器功能的代码,已经写出来了,但是是用c的思想,不会用c++的,望指教
hanyue03 2014-04-09
  • 打赏
  • 举报
回复
引用多了 确实很乱啊 感觉
ggglivw 2014-04-09
  • 打赏
  • 举报
回复
估计是越界导致的,多检查[]下标操作
zawgjzzl 2014-04-09
  • 打赏
  • 举报
回复
问题出在这一句:
_str[_str.find_first_of('*')-i]>=48
//_str.find_first_of('*')-i 是一个复数 -1
ouyh12345 2014-04-09
  • 打赏
  • 举报
回复
操作前,先判断参数是否在合理范围内
赵4老师 2014-04-09
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧