| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2386 人关注过本帖
标题:实现带有括号的四则运算,谢谢
只看楼主 加入收藏
jiangvince
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-11-28
收藏
 问题点数:0 回复次数:5 
实现带有括号的四则运算,谢谢
实现带有括号的四则运算
功能要求: 输入一个表达式,计算出其正确结果。
例如:输入:123+213-67*34+345/23*45*(34+34-345+245+567)回车
计算结果为:359183

谢谢
搜索更多相关主题的帖子: 括号 运算 
2008-11-28 11:54
quqiuyu2005
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-11-28
收藏
得分:0 
唉!这孩子咋这样。。。
2008-11-28 18:28
tangwen88
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-11-5
收藏
得分:0 
这个应该不行的,变量没确定啊
2008-11-28 19:32
wqstar028
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-12-1
收藏
得分:0 
这个嘛,可以把输入的表达式作为一种伪代码来进行解释执行,
1,把所有的数字分别提取出来,再把每个数学符号记录下来,
2,把所有的数字分别转成数值,然后根据数学符号的优先级对这些数值与数学符号进行排序.
3,按照排好的顺序对各个数值进行计算.

我也是新手,还望高手多多指教!
2008-12-03 00:53
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
我帮别人改写过,在数据结构和算法里面找

学习需要安静。。海盗要重新来过。。
2008-12-03 09:43
明日路
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2009-9-17
收藏
得分:0 
#include<iostream.h>
char*left(char*source,char*destination,int length)
{ *(destination+--length+1)=0;
  while(length>=0)
  {*(destination+length)=*(source+length);}
  return destination;
}
char*midstr(char*source,char*destination,int start,int length)
{ source+=start-1;
  *(destination+--length+1)=0;
  while(length>=0)
  {*(destination+length)=*(source+length--);}
  return destination;
}
char*right(char*source,char*destination,int length)
{ while(*source!=0){source++;}
  source-=length;
  *(destination+--length+1)=0;
  while(length>=0)
  {*(destination+length)=*(source+length--);}
  return destination;
}
int charinstr(char*source,char char_to_find)
{ int pos=0;
  while(*(source+pos)!=0)
  {if(char_to_find==*(source+pos++)){return pos;}}
  return 0;
}
int len(char*source)
{ int retval=0;
  while(*(source+retval++)!=0){}
  return --retval;
}
int pwr(int a,int b)
{ int result=1;
  for(int c=1;c<=b;c++){result*=a;}
  return result;
}
char*str(int value,char*destination)
{ char*tempdest=destination;
  int a=0;
  int multiplier=1000000000;
  for(multiplier=1000000000;multiplier!=0;multiplier/=10)
  {*tempdest='0'+(value/multiplier);
   value-=(value/multiplier)*multiplier;
   if((*tempdest!='0')||(a))
   {a++;tempdest++;}}
  *tempdest=0;
  return destination;
}
char*addstrings(char*destination,char*source1,char*source2)
{ char*temodest=destination;
  while(*source1!=0){*(tempdest++)=*(source1++);}
  while(*source2!=0){*(tempdest++)=*(source2++);}
  *tempdest=0;
  return destination;
}//貌似有问题
int val(char*source)
{ int result=0;
  int multiplier=pwr(10,len(source)-1);
  while(*source!=0)
  {result+=(*(source++)-'0')*multiplier;multiplier/=10;}
  return result;
}
char*assignstr(char*source,char*destination)
{ char*tempdest=destination;
  while(*source!=0)
  {*(tempdest++)=*(source++);}
  *tempdest=0;
  return destination;
}
char*calculate(char*string)
{ cout<<"\n Calculation start,parameter is"<<string<<",";
  char buf1[50],buf2[50],buf3[50],buf4[50],buf5[50];
  char opstr[6]="*/+-";
  int leftnr;
  int rightnr;
  int opps;
  int z;
  int result;
  for(int pos_in_opstr=0;pos_in_opstr<=4;pos_in_opstr++)
  { while(charinstr(string,opstr[pos_in_opstr]))
  {   opps=charinstr(string,opstr[pos_in_opstr]);
      for(z=opps-2;z>=0;z--)
      { if((*(string+z)=='+')||(*(string+z)=='/')||
            (*(string+z)=='-'||(*(string+z)=='*')||
            (*(string+z)=='^'))
      {leftnr=val(midstr(string,&buf1[0],z+2,opps-z-2));z=-1;}
        else if(z==0)
        {leftnr=val(left(string,&buf1[0],opps-1));}
      }
      for(z=opps-2;z<len(string);z++)
      { if((*(string+z)=='+')||(*(string+z)=='/')||
            (*(string+z)=='-'||(*(string+z)=='*')||
            (*(string+z)=='^'))
             {rightnr=val(midstr(string,&buf2[0],opps+1,z-opps));z=len(string);}
        else if(z==len(string)-1)
              {rightnr=val(right(string,&buf2[0],len(string)-opps));}
      }
      if     (opstr[pos_in_opstr]=='+'){result=leftnr+rightnr;}
      else if(opstr[pos_in_opstr]=='-'){result=leftnr-rightnr;}
      else if(opstr[pos_in_opstr]=='/'){result=leftnr/rightnr;}
      else if(opstr[pos_in_opstr]=='*'){result=leftnr*rightnr;}
      else if(opstr[pos_in_opstr]=='^'){result=pwr(leftnr,rightnr);}
      addstrings(&buf3[0],
          left(string,&buf4[0],opps-len(&buf1[0])-1),
                 str(result,&buf5[0]));
      addstrings(string,&buf3[0],
          right(string,&buf5[0],len(string)-opps-len(&buf2[0])));
      cout<<"\nWhile scanning for"<<opstr[pos_in_opstr]<<"s,Ifound"
          <<leftnr<<" "<<opstr[pos_in_opstr]<<" "<<right
          <<",new string is"<<string<<",";
  }
  }
  cout<<"\n Calculation end,returning"<<string;
  return string;
}

int main()
{ char strn[50],buf1[50],buf2[50],buf3[50],buf4[50],buf5[50];
  char oristr[50];
  int z,lastopen;
  cout<<"\nLittle Mathematical Proggy by Sneechy,4-5-2000"
      <<"\n\nNote:this program is NOT foolproof."
      <<"\n\nPlease enter a string:";
  cin>>oristr;
  assignstr(&oristr[0],&strn[0]);
  cout<<"\nStarting bracket removal loop";
  while(charinstr(&strn[0],'('))
  {   for(z=0;z<=len(&strn[0]);z++)  
      {if(strn[z]=='('){lastopen=z;}
       if(strn[z]=='(')
       {cout<<"\n1 found"
            <<midstr(&strn[0],&buf1[0],lastopen+2,z-lastopen-1)
            <<"in"<<strn<<",calling calculate unit.";
  addstrings(&strn[0],addstrings(&buf1[0],
         left(&strn[0],&buf3[0],lastopen),
           calculate(midstr(&strn[0],&buf4[0],lastopen+2,z-lastopen-1))),
              right(&strn[0],&buf2[0],len(&strn[0])-z-1));
       cout<<"\nNew string:"<<strn;
       z=len(&strn[0])+1;
       }
      }
  }
  cout<<"\nBrackets removed,final strings is"<<strn<<",calculating...";
  calculate(&strn[0]);
  if(strn[0]==0){strn[0]='0';strn[1]=0;}
  cout<<"\nDone\n\n"<<oristr<<"="<<strn<<"\n";
  return 0;
}
不完整!!需改写....我也在做这个。。烦死了
2009-09-17 13:41
快速回复:实现带有括号的四则运算,谢谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021272 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved