一个C++数组的问题
我这有个关于简单计算器的程序:#include<iostream.h>#include<conio.h>
#include<cstdlib>
void main()
{
char a,b,l[2];
double c,d,e,p[3],q,w;
int i,j;
star:
cout<<" b:开始 r:返回主菜单 c:清屏 f:退出 d:多重运算"<<endl;
cout<<"计算开始:"<<endl;
cout<<"请输入b或d:"<<endl;
again:
cin>>b;
switch(b)
{
case'c':system("cls");
goto again;
case'C':system("cls");
goto again;
case'r':goto star;
case'R':goto star;
case'b':cout<<"运算开始:"<<endl;
break;
case'B':cout<<"运算开始:"<<endl;
break;
case'f':goto end;
case'd':cout<<"运算开始:"<<endl;
cin>>p[0];
for (i=0;i<2;i++)
{
cin>>l[i]>>p[i+1];
}
for (j=0;j<2;j++)
{
if(l[j]=='*'&&l[1-j]=='+')
{
q=p[j]*p[j+1];
w=q+p[2-j-j];
cout<<w<<endl;
goto star;
}
else if(l[j]=='/'&&l[1-j]=='+')
{
if (p[j+1]==0)
{
cout<<"错误!!"<<endl;
}
else
{
q=p[j]/p[j+1];
w=q+p[2-j-j];
cout<<w<<endl;
goto star;
}
}
else if(l[j]=='/'&&l[1-j]=='*')
{
if(j<1-j)
{
if(p[1]==0)
{
cout<<"错误!!"<<endl;
}
else
{
w=p[0]/p[1]*p[2];
cout<<w<<endl;
goto star;
}
}
else
{
if(p[2]==0)
{
cout<<"错误!!"<<endl;
}
else
{
w=p[0]*p[1]/p[2];
cout<<w<<endl;
goto star;
}
}
}
else if(l[j]=='+'&&l[1-j]=='-')
{
if(j<1-j)
{
w=p[0]+p[1]-p[2];
cout<<w<<endl;
goto star;
}
else
{
w=p[0]-p[1]+p[2];
cout<<w<<endl;
goto star;
}
}
else if(l[j]=='*'&&l[1-j]=='-')
{
if(j<1-j)
{
w=p[0]*p[1]-p[2];
cout<<w<<endl;
goto star;
}
else
{
w=p[0]-p[1]*p[2];
cout<<w<<endl;
goto star;
}
}
else if(l[j]=='/'&&l[1-j]=='-')
{
if(p[j+1]==0)
{
cout<<"错误!!"<<endl;
}
else
{
if(j<1-j)
{
w=p[0]/p[1]-p[2];
cout<<w<<endl;
goto star;
}
else
{
w=p[0]-p[1]/p[2];
cout<<w<<endl;
goto star;
}
}
}
else if(l[j]==l[1-j])
{
if(l[j]=='-')
{
w=p[0]-p[1]-p[2];
cout<<w<<endl;
goto star;
}
else if(l[j]=='+')
{
w=p[0]+p[1]+p[2];
cout<<w<<endl;
goto star;
}
else if(l[j]=='*')
{
w=p[0]*p[1]*p[2];
cout<<w<<endl;
goto star;
}
else if(l[j]=='/')
{
p[1]!=0&&p[2]!=0?w=p[0]/p[1]/p[2]:w=0;
if(w!=0)
{
cout<<w<<endl;
goto star;
}
else if (w==0)
{
cout<<"错误!!"<<endl;
goto star;
}
}
}
}
default:cout<<"错误!"<<endl;
cout<<"按r计算重新开始:"<<endl;
goto again;
}
cin>>e;
there:
for(i=0;;i++)
{
cin>>a;
switch(a)
{
case'=':d=e;
cout<<d;
case'+':cin>>c;
d=e+c;
e=d;
cout<<d;
break;
case'-':cin>>c;
d=e-c;
e=d;
cout<<d;
break;
case'*':cin>>c;
d=e*c;
e=d;
cout<<d;
break;
case'/': cin>>c;
d=e/c;
e=d;
if(c==0)
{
cout<<"错误!!按r重新开始:"<<endl;
goto again;
}
else
{
cout<<d;
}
break;
case'c':system("cls");
goto again;
case'r':goto star;
case'f':goto end;
default:cout<<"错误!"<<endl;
cout<<"按r计算重新开始:"<<endl;
goto again;
}
}
end:;
}
为了进行两重运算,我用了数组P[3]和L[2]分别储存数字和符号,可是如果将P[3]改为P[2],甚至P[1]仍然可以实现程序,我实在是想不明白,所以请朋友们帮忙看下,解决问题。谢谢大家了!!!
追问:我以后会养成个好习惯的!你说的有点问题:你的程序在输入b以后 执行到了 case'b':cout<<"运算开始:"<<endl;
break; 这一句,然后执行了一个break,直接就跳出你的switch了,程序就从cin>>e;我的目的就是输入B让程序到哪个地方!