
各位C语言高手们,我急求个程序
要算S=A?B?C?D?E,其中ABCDE为0到9自然数,?处填上+-*/和(),使得S=1至100的自然数,凡是运行过程中出现小数的都不要.希望大家能够帮助我,谢谢要C...
要算S=A?B?C?D?E,其中ABCDE为0到9自然数,?处填上+-*/和(),使得S=1至100的自然数,凡是运行过程中出现小数的都不要.希望大家能够帮助我,谢谢要C++运行所有符合的公式以及答案,比如输出5*(9-3)+6/2=33,符合.很急,谢谢大家
c++和C语言有不一样吗,请大家回答点实际的,我很急呀,谢谢了一个只能填一个,不过括号可以随便加!感谢银河飞将!!这几天我终于看到希望了,呵呵,加油
可能是我表达不清楚,ABCDE是给定的数,银河飞将请你帮忙帮到底.还有就是括号的表达式子也请你帮我列举一下,我弄不出来 展开
c++和C语言有不一样吗,请大家回答点实际的,我很急呀,谢谢了一个只能填一个,不过括号可以随便加!感谢银河飞将!!这几天我终于看到希望了,呵呵,加油
可能是我表达不清楚,ABCDE是给定的数,银河飞将请你帮忙帮到底.还有就是括号的表达式子也请你帮我列举一下,我弄不出来 展开
4个回答
展开全部
最后的代码,自己挺满意的::::
#include<iostream.h>
double yunsuan(double num1,char oper,double num2) //运算函数
{
if(oper=='+')
return num1+num2;
else if(oper=='-')
return num1-num2;
else if(oper=='*')
return num1*num2;
else if(oper=='/')
return num1/num2;
else
return 0;
}
double yunsuan2(double num[],char oper[][8],int yxj[])
{
int flag[4];
double result1=0.0;
for(int k=0;k<4;k++)
{
flag[k]=0;
}
for (int i=0;i<4;i++)
{
if (yxj[i]==0)//先算优先级为0,然后是1,以此类推,算完一个运算符,则标识起来,然后把运算符两边的数字都变为结果
{
num[i]=yunsuan(num[i],oper[i+1][3],num[i+1]);
num[i+1]=num[i];
flag[i]=1;
for (int j=0;j<4;j++)
{
if (yxj[j]==1)
{
num[j]=yunsuan(num[j],oper[j+1][3],num[j+1]);
num[j+1]=num[j];
flag[j]=1;
for(int m=j;m>=0;m--) //把前面的标记(flag)为1的num数组元素的值变成运算结果
{
if(flag[m]==1)
{
num[m]=num[j];
}
else
break;
}
for(int o=j;o<=3;o++) //把后面的标记(flag)为1的num数组元素的值变成运算结果
{
if(flag[o]==1)
{
num[o+1]=num[j];
}
else
break;
}
for (int k=0;k<4;k++)
{
if (yxj[k]==2)
{
num[k]=yunsuan(num[k],oper[k+1][3],num[k+1]);
num[k+1]=num[k];
flag[k]=1;
for(int mm=k;mm>=0;mm--) //把前面的标记(flag)为1的num数组元素的值变成运算结果
{
if(flag[mm]==1)
{
num[mm]=num[k];
}
else
break;
}
for(int oo=k;oo<=3;oo++) //把后面的标记(flag)为1的num数组元素的值变成运算结果
{
if(flag[oo]==1)
{
num[oo+1]=num[k];
}
else
break;
}
for (int l=0;l<4;l++)
{
if (yxj[l]==3)
{
num[l]=yunsuan(num[l],oper[l+1][3],num[l+1]);
num[l+1]=num[l];
flag[l]=1;
result1=num[l];
}
else
{
continue;
}
}
}
else
{
continue;
}
}
}
else
{
continue;
}
}
}
else
{
continue;
}
}
return result1;
}
void main()
{
char oper0[5]={'+','-','*','/'};
char oper[6][8]={0};
double num[5];
int yxj[4];//表示优先级
double result=0.0;
cout<<"please input A B C D E :"<<endl;
for(int q=0;q<=4;q++)
{
cin>>num[q];
}
double n0=num[0];
double n1=num[1];
double n2=num[2];
double n3=num[3];
double n4=num[4];
for(int w=0;w<4;w++)
for(int r=0;r<4;r++)
for(int s=0;s<4;s++)
for(int x=0;x<4;x++)
{
oper[1][3]=oper0[w];//获得随机运算符
oper[2][3]=oper0[r];
oper[3][3]=oper0[s];
oper[4][3]=oper0[x];
char op1=oper[1][3];
char op2=oper[2][3];
char op3=oper[3][3];
char op4=oper[4][3];
for(int q=0;q<4;q++)
for(int e=0;e<4;e++)
for(int a=0;a<4;a++)
for(int d=0;d<4;d++)
{
yxj[0]=q;//获得随机优先级
if(e!=q)
yxj[1]=e;
else
continue;
if(a!=q && a!=e)
yxj[2]=a;
else
continue;
if(d!=q && d!=e && d!=a)
yxj[3]=d;
else
continue;
result=yunsuan2(num,oper,yxj);//进行运算
num[0]=n0;//重置num数组
num[1]=n1;
num[2]=n2;
num[3]=n3;
num[4]=n4;
if(result-(int)result==0 && result>=1 && result<=100)//判断输出
{
for (int nn=0;nn<4;nn++)//输出括号,代码有点复杂
{
if (yxj[nn]==0)
{
oper[nn][6]='(';
oper[nn+2][0]=')';
}
else
{
continue;
}
}
for (int nn1=0;nn1<4;nn1++)
{
if (yxj[nn1]==1)
{
if(oper[nn1-1][6]=='(')
{
oper[nn1-1][5]='(';
oper[nn1+2][1]=')';
}
else if (oper[nn1+3][0]==')')
{
oper[nn1][5]='(';
oper[nn1+3][1]=')';
}
else
{
oper[nn1][5]='(';
oper[nn1+2][1]=')';
}
}
}
for (int nn2=0;nn2<4;nn2++)
{
if (yxj[nn2]==2)
{
if(oper[nn2-2][5]=='(' && oper[nn2+1][1]==')')
{
oper[nn2-2][4]='(';
oper[nn2+2][2]=')';
}
else if(oper[nn2+4][1]==')' && oper[nn2+1][5]=='(')
{
oper[nn2][4]='(';
oper[nn2+4][2]=')';
}
else if((oper[nn2-1][6]=='(' || oper[nn2-1][5]=='(') &&
(oper[nn2+3][0]==')' || oper[nn2+3][1]==')'))
{
oper[nn2-1][4]='(';
oper[nn2+3][2]=')';
}
else if((oper[nn2-1][6]=='(' || oper[nn2-1][5]=='(') &&
(oper[nn2+3][0]!=')' || oper[nn2+3][1]!=')'))
{
oper[nn2-1][4]='(';
oper[nn2+2][2]=')';
}
else if ((oper[nn2-1][6]!='(' || oper[nn2-1][5]!='(') &&
(oper[nn2+3][0]==')' || oper[nn2+3][1]==')'))
{
oper[nn2][4]='(';
oper[nn2+3][2]=')';
}
else
{
oper[nn2][4]='(';
oper[nn2+2][2]=')';
}
}
}
cout<<result<<'=';
for (int i1=0;i1<7;i1++)
{
cout<<oper[0][i1];
}
cout<<n0;
for (int i2=0;i2<7;i2++)
{
cout<<oper[1][i2];
}
cout<<n1;
for (int i3=0;i3<7;i3++)
{
cout<<oper[2][i3];
}
cout<<n2;
for (int i4=0;i4<7;i4++)
{
cout<<oper[3][i4];
}
cout<<n3;
for (int i5=0;i5<7;i5++)
{
cout<<oper[4][i5];
}
cout<<n4;
for (int i6=0;i6<7;i6++)
{
cout<<oper[5][i6];
}
cout<<endl;
for(int qq=0;qq<6;qq++)//重置操作符数组
for(int ww=0;ww<7;ww++)
{
oper[qq][ww]=' ';
}
oper[1][3]=op1;
oper[2][3]=op2;
oper[3][3]=op3;
oper[4][3]=op4;
}
else
continue;
}
}
}
如何输出括号有点难度,想到了凌晨2点多,觉得这题还是挺好玩的,以后如果还有题,我还是很乐意再帮你的,呵呵,其实编程不难的,我也是个初学者,主要是要自己动手多编程,呵呵,能帮你的就这么多了,加油哦!!!!
#include<iostream.h>
double yunsuan(double num1,char oper,double num2) //运算函数
{
if(oper=='+')
return num1+num2;
else if(oper=='-')
return num1-num2;
else if(oper=='*')
return num1*num2;
else if(oper=='/')
return num1/num2;
else
return 0;
}
double yunsuan2(double num[],char oper[][8],int yxj[])
{
int flag[4];
double result1=0.0;
for(int k=0;k<4;k++)
{
flag[k]=0;
}
for (int i=0;i<4;i++)
{
if (yxj[i]==0)//先算优先级为0,然后是1,以此类推,算完一个运算符,则标识起来,然后把运算符两边的数字都变为结果
{
num[i]=yunsuan(num[i],oper[i+1][3],num[i+1]);
num[i+1]=num[i];
flag[i]=1;
for (int j=0;j<4;j++)
{
if (yxj[j]==1)
{
num[j]=yunsuan(num[j],oper[j+1][3],num[j+1]);
num[j+1]=num[j];
flag[j]=1;
for(int m=j;m>=0;m--) //把前面的标记(flag)为1的num数组元素的值变成运算结果
{
if(flag[m]==1)
{
num[m]=num[j];
}
else
break;
}
for(int o=j;o<=3;o++) //把后面的标记(flag)为1的num数组元素的值变成运算结果
{
if(flag[o]==1)
{
num[o+1]=num[j];
}
else
break;
}
for (int k=0;k<4;k++)
{
if (yxj[k]==2)
{
num[k]=yunsuan(num[k],oper[k+1][3],num[k+1]);
num[k+1]=num[k];
flag[k]=1;
for(int mm=k;mm>=0;mm--) //把前面的标记(flag)为1的num数组元素的值变成运算结果
{
if(flag[mm]==1)
{
num[mm]=num[k];
}
else
break;
}
for(int oo=k;oo<=3;oo++) //把后面的标记(flag)为1的num数组元素的值变成运算结果
{
if(flag[oo]==1)
{
num[oo+1]=num[k];
}
else
break;
}
for (int l=0;l<4;l++)
{
if (yxj[l]==3)
{
num[l]=yunsuan(num[l],oper[l+1][3],num[l+1]);
num[l+1]=num[l];
flag[l]=1;
result1=num[l];
}
else
{
continue;
}
}
}
else
{
continue;
}
}
}
else
{
continue;
}
}
}
else
{
continue;
}
}
return result1;
}
void main()
{
char oper0[5]={'+','-','*','/'};
char oper[6][8]={0};
double num[5];
int yxj[4];//表示优先级
double result=0.0;
cout<<"please input A B C D E :"<<endl;
for(int q=0;q<=4;q++)
{
cin>>num[q];
}
double n0=num[0];
double n1=num[1];
double n2=num[2];
double n3=num[3];
double n4=num[4];
for(int w=0;w<4;w++)
for(int r=0;r<4;r++)
for(int s=0;s<4;s++)
for(int x=0;x<4;x++)
{
oper[1][3]=oper0[w];//获得随机运算符
oper[2][3]=oper0[r];
oper[3][3]=oper0[s];
oper[4][3]=oper0[x];
char op1=oper[1][3];
char op2=oper[2][3];
char op3=oper[3][3];
char op4=oper[4][3];
for(int q=0;q<4;q++)
for(int e=0;e<4;e++)
for(int a=0;a<4;a++)
for(int d=0;d<4;d++)
{
yxj[0]=q;//获得随机优先级
if(e!=q)
yxj[1]=e;
else
continue;
if(a!=q && a!=e)
yxj[2]=a;
else
continue;
if(d!=q && d!=e && d!=a)
yxj[3]=d;
else
continue;
result=yunsuan2(num,oper,yxj);//进行运算
num[0]=n0;//重置num数组
num[1]=n1;
num[2]=n2;
num[3]=n3;
num[4]=n4;
if(result-(int)result==0 && result>=1 && result<=100)//判断输出
{
for (int nn=0;nn<4;nn++)//输出括号,代码有点复杂
{
if (yxj[nn]==0)
{
oper[nn][6]='(';
oper[nn+2][0]=')';
}
else
{
continue;
}
}
for (int nn1=0;nn1<4;nn1++)
{
if (yxj[nn1]==1)
{
if(oper[nn1-1][6]=='(')
{
oper[nn1-1][5]='(';
oper[nn1+2][1]=')';
}
else if (oper[nn1+3][0]==')')
{
oper[nn1][5]='(';
oper[nn1+3][1]=')';
}
else
{
oper[nn1][5]='(';
oper[nn1+2][1]=')';
}
}
}
for (int nn2=0;nn2<4;nn2++)
{
if (yxj[nn2]==2)
{
if(oper[nn2-2][5]=='(' && oper[nn2+1][1]==')')
{
oper[nn2-2][4]='(';
oper[nn2+2][2]=')';
}
else if(oper[nn2+4][1]==')' && oper[nn2+1][5]=='(')
{
oper[nn2][4]='(';
oper[nn2+4][2]=')';
}
else if((oper[nn2-1][6]=='(' || oper[nn2-1][5]=='(') &&
(oper[nn2+3][0]==')' || oper[nn2+3][1]==')'))
{
oper[nn2-1][4]='(';
oper[nn2+3][2]=')';
}
else if((oper[nn2-1][6]=='(' || oper[nn2-1][5]=='(') &&
(oper[nn2+3][0]!=')' || oper[nn2+3][1]!=')'))
{
oper[nn2-1][4]='(';
oper[nn2+2][2]=')';
}
else if ((oper[nn2-1][6]!='(' || oper[nn2-1][5]!='(') &&
(oper[nn2+3][0]==')' || oper[nn2+3][1]==')'))
{
oper[nn2][4]='(';
oper[nn2+3][2]=')';
}
else
{
oper[nn2][4]='(';
oper[nn2+2][2]=')';
}
}
}
cout<<result<<'=';
for (int i1=0;i1<7;i1++)
{
cout<<oper[0][i1];
}
cout<<n0;
for (int i2=0;i2<7;i2++)
{
cout<<oper[1][i2];
}
cout<<n1;
for (int i3=0;i3<7;i3++)
{
cout<<oper[2][i3];
}
cout<<n2;
for (int i4=0;i4<7;i4++)
{
cout<<oper[3][i4];
}
cout<<n3;
for (int i5=0;i5<7;i5++)
{
cout<<oper[4][i5];
}
cout<<n4;
for (int i6=0;i6<7;i6++)
{
cout<<oper[5][i6];
}
cout<<endl;
for(int qq=0;qq<6;qq++)//重置操作符数组
for(int ww=0;ww<7;ww++)
{
oper[qq][ww]=' ';
}
oper[1][3]=op1;
oper[2][3]=op2;
oper[3][3]=op3;
oper[4][3]=op4;
}
else
continue;
}
}
}
如何输出括号有点难度,想到了凌晨2点多,觉得这题还是挺好玩的,以后如果还有题,我还是很乐意再帮你的,呵呵,其实编程不难的,我也是个初学者,主要是要自己动手多编程,呵呵,能帮你的就这么多了,加油哦!!!!
展开全部
貌似有很多……
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
C语言还是C++
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询