大家帮忙看看。PKU1686 runtime error 搞不懂啊...

ud_willsmith 2010-10-15 08:30:27
#include<iostream>
#include<string>
#include<stack>
#include<queue>
using namespace std;
long calculator(string str,int x);
int main()
{
int i;
cin>>i;
while (i>0)
{
string str1,str2;
cin>>str1;
cin>>str2;
if (calculator(str1,1)==calculator(str2,1)&&calculator(str1,2)==calculator(str2,2))
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
--i;
}
return 0;
}

long calculator(string str,int x)
{
queue<long> num;
for (int t=0;t<str.size();++t)
{
if (str[t]=='(')
{
stack<char> ch;
int index=0;
for (int s=t;;++s)
{
if (str[s]=='(')
ch.push('(');
else if (str[s]==')')
{
ch.pop();
if (ch.empty())
{
index=s;
string str1;
str1.assign(str,t+1,index-t-1);
long result=calculator(str1,x);
num.push(result);
t=index;
break;
}
}
else continue;
}
}
else if (str[t]>='a'&&str[t]<='z')
{
if (x==1)
num.push((int)'a');
else num.push((int)'a'+1);
}
else if (str[t]=='*')
num.push(-32765);
else if (str[t]=='+')
num.push(-32766);
else if (str[t]=='-')
num.push(-32767);
else if (str[t]>='0'&&str[t]<='9')
{
long sum=0;
for (int k=t;;++k)
{
if (str[k]>='0'&&str[k]<='9')
sum=sum*10+(str[k]-'0');
else
{
t=k-1;
break;
}
}
num.push(sum);
}
else break;
}
stack<long> count;

while (!num.empty())
{

if (num.front()!=-32765&&num.front()!=-32766&&num.front()!=-32767)
{
count.push(num.front());
num.pop();
}
else if (num.front()==-32765)
{
num.pop();
long temp=count.top();
count.pop();
count.push(num.front()*temp);
num.pop();
}
else
{
count.push(num.front());
num.pop();
}
}
stack<long> change;
while (!count.empty())
{
change.push(count.top());
count.pop();
}
int temp=0;
while (!change.empty())
{
if (change.top()!=-32766&&change.top()!=-32767)
{
temp=change.top();
if (change.size()==1)
return change.top();
else change.pop();
}
else if (change.top()==-32766)
{
change.pop();
long temp2=change.top();
change.pop();
change.push(temp2+temp);
if (change.size()==1)
return change.top();
}
else
{
change.pop();
long temp2=change.top();
change.pop();
change.push(temp-temp2);
if (change.size()==1)
return change.top();
}
}
}

在vs2008中过了(不过有一个警告。说什么calculator()不是所有的控件都有返回值),但交上去以后就是runtime error
请各位大虾帮忙看下..谢啦..
...全文
92 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kafeixiguazhi 2011-09-14
  • 打赏
  • 举报
回复
为什么还是没看懂,我遇到相同的问题、、、
ud_willsmith 2010-10-16
  • 打赏
  • 举报
回复
搞了很久终于知道了,原来是字符串读取的问题。还是谢谢各位了.....
ud_willsmith 2010-10-16
  • 打赏
  • 举报
回复
搞了很久。终于知道了。字符串的读取问题....还是谢谢各位了
赵4老师 2010-10-15
  • 打赏
  • 举报
回复
参考这个
/*---------------------------------------
函数型计算器(VC++6.0,Win32 Console)程序由 yu_hua 于2007-07-27设计完成
功能:
目前提供了10多个常用数学函数:
⑴正弦sin
⑵余弦cos
⑶正切tan
⑷开平方sqrt
⑸反正弦arcsin
⑹反余弦arccos
⑺反正切arctan
⑻常用对数lg
⑼自然对数ln
⑽e指数exp
⑾乘幂函数∧
用法:
如果要求2的32次幂,可以打入2^32<回车>
如果要求30度角的正切可键入tan(Pi/6)<回车>
注意不能打入:tan(30)<Enter>
如果要求1.23弧度的正弦,有几种方法都有效:
sin(1.23)<Enter>
sin 1.23 <Enter>
sin1.23 <Enter>
如果验证正余弦的平方和公式,可打入sin(1.23)^2+cos(1.23)^2 <Enter>或sin1.23^2+cos1.23^2 <Enter>
此外两函数表达式连在一起,自动理解为相乘如:sin1.23cos0.77+cos1.23sin0.77就等价于sin(1.23)*cos(0.77)+cos(1.23)*sin(0.77)
当然你还可以依据三角变换,再用sin(1.23+0.77)也即sin2验证一下。
本计算器充分考虑了运算符的优先级因此诸如:2+3*4^2 实际上相当于:2+(3*(4*4))
另外函数名前面如果是数字,那么自动认为二者相乘.
同理,如果某数的右侧是左括号,则自动认为该数与括弧项之间隐含一乘号。
如:3sin1.2^2+5cos2.1^2 相当于3*sin2(1.2)+5*cos2(2.1)
又如:4(3-2(sqrt5-1)+ln2)+lg5 相当于4*(3-2*(√5 -1)+loge(2))+log10(5)
此外,本计算器提供了圆周率 Pi键入字母时不区分大小写,以方便使用。
----------------------------------------*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stdio.h>
#include <string.h>
#include <windows.h>
using namespace std;
const char Tab=0x9;
const int DIGIT=1;
const int MAXLEN=16384;
char s[MAXLEN],*endss;
int pcs=15;
double fun(double x,char op[],int *iop) {
while (op[*iop-1]<32) //本行使得函数嵌套调用时不必加括号,如 arc sin(sin(1.234)) 只需键入arc sin sin 1.234<Enter>
switch (op[*iop-1]) {
case 7: x=sin(x); (*iop)--;break;
case 8: x=cos(x); (*iop)--;break;
case 9: x=tan(x); (*iop)--;break;
case 10: x=sqrt(x); (*iop)--;break;
case 11: x=asin(x); (*iop)--;break;
case 12: x=acos(x); (*iop)--;break;
case 13: x=atan(x); (*iop)--;break;
case 14: x=log10(x);(*iop)--;break;
case 15: x=log(x); (*iop)--;break;
case 16: x=exp(x); (*iop)--;break;
}
return x;
}
double calc(char *expr,char **addr) {
static deep; //递归深度
static char *fname[]={ "sin","cos","tan","sqrt","arcsin","arccos","arctan","lg","ln","exp",NULL};
double ST[10]={0.0}; //数字栈
char op[10]={'+'}; //运算符栈
char c,*rexp,*pp,*pf;
int ist=1,iop=1,last;
if (!deep) {
pp=pf=expr;
do {
c = *pp++;
if (c!=' '&& c!=Tab)
*pf++ = c;
} while (c!='\0');
}
pp=expr;
if ((c=*pp)=='-'||c=='+') {
op[0] = c;
pp++;
}
last = !DIGIT;
while ((c=*pp)!='\0') {
if (c=='(') {//左圆括弧
deep++;
ST[ist++]=calc(++pp,addr);
deep--;
ST[ist-1]=fun(ST[ist-1],op,&iop);
pp = *addr;
last = DIGIT;
if (*pp == '('||isalpha(*pp) && strnicmp(pp,"Pi",2)) {//目的是:当右圆括弧的右恻为左圆括弧或函数名字时,默认其为乘法
op[iop++]='*';
last = !DIGIT;
c = op[--iop];
goto operate ;
}
}
else if (c==')') {//右圆括弧
pp++;
break;
} else if (isalpha(c)) {
if (!strnicmp(pp,"Pi",2)) {
if (last==DIGIT) {
cout<< "π左侧遇)" <<endl;exit(1);
}
ST[ist++]=3.14159265358979323846264338328;
ST[ist-1]=fun(ST[ist-1],op,&iop);
pp += 2;
last = DIGIT;
if (!strnicmp(pp,"Pi",2)) {
cout<< "两个π相连" <<endl;exit(2);
}
if (*pp=='(') {
cout<< "π右侧遇(" <<endl;exit(3);
}
} else {
for(int i=0; (pf=fname[i])!=NULL; i++)
if (!strnicmp(pp,pf,strlen(pf)))break;
if (pf!=NULL) {
op[iop++] = 07+i;
pp += strlen(pf);
} else {
cout<< "陌生函数名" <<endl;exit(4);
}
}
} else if (c=='+'||c=='-'||c=='*'||c=='/'||c=='^') {
char cc;
if (last != DIGIT) {
cout<< "运算符粘连" <<endl;exit(5);
}
pp++;
if (c=='+'||c=='-') {
do {
cc = op[--iop];
--ist;
switch (cc) {
case '+': ST[ist-1] += ST[ist];break;
case '-': ST[ist-1] -= ST[ist];break;
case '*': ST[ist-1] *= ST[ist];break;
case '/': ST[ist-1] /= ST[ist];break;
case '^': ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
}
} while (iop);
op[iop++] = c;
} else if (c=='*'||c=='/') {
operate: cc = op[iop-1];
if (cc=='+'||cc=='-') {
op[iop++] = c;
} else {
--ist;
op[iop-1] = c;
switch (cc) {
case '*': ST[ist-1] *= ST[ist];break;
case '/': ST[ist-1] /= ST[ist];break;
case '^': ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
}
}
} else {
cc = op[iop-1];
if (cc=='^') {
cout<< "乘幂符连用" <<endl;exit(6);
}
op[iop++] = c;
}
last = !DIGIT;
} else {
if (last == DIGIT) {
cout<< "两数字粘连" <<endl;exit(7);
}
ST[ist++]=strtod(pp,&rexp);
ST[ist-1]=fun(ST[ist-1],op,&iop);
if (pp == rexp) {
cout<< "非法字符" <<endl;exit(8);
}
pp = rexp;
last = DIGIT;
if (*pp == '('||isalpha(*pp)) {
op[iop++]='*';
last = !DIGIT;
c = op[--iop];
goto operate ;
}
}
}
*addr=pp;
if (iop>=ist) {
cout<< "表达式有误" <<endl;exit(9);
}
while (iop) {
--ist;
switch (op[--iop]) {
case '+': ST[ist-1] += ST[ist];break;
case '-': ST[ist-1] -= ST[ist];break;
case '*': ST[ist-1] *= ST[ist];break;
case '/': ST[ist-1] /= ST[ist];break;
case '^': ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
}
}
return ST[0];
}
int main(int argc,char **argv) {
if (argc<=1) {
if (GetConsoleOutputCP()!=936) system("chcp 936>NUL");//中文代码页
cout << "计算函数表达式的值。"<<endl<<"支持(),+,-,*,/,^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp"<<endl;
while (1) {
cout << "请输入表达式:";
gets(s);
if (s[0]==0) break;//
cout << s <<"=";
cout << setprecision(15) << calc(s,&endss) << endl;
}
} else {
strncpy(s,argv[1],MAXLEN-1);s[MAXLEN-1]=0;
if (argc>=3) pcs=atoi(argv[2]);
if (pcs<0||15<pcs) pcs=15;
printf("%.*lf\n",pcs,calc(s,&endss));
}
return 0;
}
Luyi06292 2010-10-15
  • 打赏
  • 举报
回复
额.. 内存... 会不会. 猜的...
logiciel 2010-10-15
  • 打赏
  • 举报
回复
请LZ要增加处理以下要求:

Arbitrary number of blank or tab characters between above tokens.

并试以下输入:
3
(a + b-c)*2
(a+a)+(b*2)-(3*c)+c
a*2-(a+c)+((a+c+e)*2)
3*a+c+(2*e)
(a-b)*(a-b)
(a*a)-(2*a*b)-(b*b)
job82824 2010-10-15
  • 打赏
  • 举报
回复
runtime error指针为空之类的。慢慢调试哈

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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