C++计算四则表达式的模板

  在9月8日那天我特意编写给大家分享的,一个很方便的C++函数模板,可以并且只可以计算含括号的四则表达式,只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)

  参数解释:

  istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流

  nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定

  返回值:

  返回非0表示计算成功,0表示计算失败有错误

  程序代码:

以下是引用片段:
  namespace fy_Exp{ 
  namespace {template  
  inline _T GetExpValue(_T t[], char& csym){ 
  char c=csym; csym=0; 
  switch(c){ 
  case '+':return t[0] += t[1]; 
  case '-':return t[0] -= t[1]; 
  case '*':return t[0] *= t[1]; 
  default: return t[0] /= t[1];//case '/': 
  } 
  }} 
  template  
  /* _Tstream: inputstream, _T: get return value 
  * Return nonzero if get value successfully */ 
  int GetExpValue(_Tstream& istrin, _T& nReturn){ 
  _T t[3] = {0}; //雨中飞燕之作 
  char csym[3] = "++"; 
  int nLevel = 1, nERR = 0; 
  if(!(istrin>>t[1]))istrin.clear(); 
  for(;;){ 
  if(istrin>>csym[2]){ 
  switch(csym[2]){ 
  case '(': 
  if(!csym[1]){nLevel=0x100; nERR=1;}else 
  if(!GetExpValue(istrin, t[2]))nLevel|=0x10; 
  else{nLevel=0x100; nERR=1;} 
  break; 
  case ')': 
  {nLevel = 0x100;}break; 
  case '+':case '-':case '*':case '/': 
  {csym[nLevel++] = csym[2];}break; 
  case ' ':case '\r':case '\n':case '\t':continue; 
  default: 
  {nLevel=0x100; nERR=1;} 
  } 
  if(nLevel==0x100)break; 

profile.yahoo.com/6FUG3TSOMSITXJZMLMXVQ7KGGQ/
profile.yahoo.com/CJMWZL4ZNPWTZXPE4JCNYV3I54/
profile.yahoo.com/HS6YYCHMO7ID6OZV7NZ7L5FKSE/
profile.yahoo.com/3XGXYPZT3THKPVQ4TXQHCKHMXY/
profile.yahoo.com/GSNGQ2AINGGHGYHE4KY5MWXE2Y/
profile.yahoo.com/74RCKJTYJPA6NKLJI6KFMDNOUI/
profile.yahoo.com/WII2LSXESPSRKXWLMIKLHRGASQ/
profile.yahoo.com/JDVQEWPPQ66PUK47BZO7676MSE/
profile.yahoo.com/KQ73BQMAD42JLAW4TQYFPZHYME/
profile.yahoo.com/A3TBERS7KPAATC6M2MUDIEPPHE/
profile.yahoo.com/ME3RBTE7XV7H7OISWPBJBU4Q2A/
profile.yahoo.com/UUCTVBCHN5P3O22GSY2Y4LS3KM/
profile.yahoo.com/5DD42LIOWIVEC3C3LLVB4IR6L4/
profile.yahoo.com/YJTU4JNJCMGNSJA4BROIV5367E/
profile.yahoo.com/ACH7ALTJKXM3ZMTUH37TJMM4HU/
profile.yahoo.com/UNIRWHYVEDBUOUH7LWSM3KLCYI/
profile.yahoo.com/OLM6OCA6DCJW34V5RFQHB5S3U4/
profile.yahoo.com/5A3NBCAMAN5GHARA44FIPLAPE4/
profile.yahoo.com/IDZAQHNCQKXQU6267CN4AGOVMA/
profile.yahoo.com/FHMIPWZ5CQ2IGX3V2JGXSIWURU/
profile.yahoo.com/NF3376KORFLV5QWIEULSFTRLLQ/
profile.yahoo.com/VCZ2W47EE6V6GL7CYYXXYXR2NA/
profile.yahoo.com/KZ3GGY3HHEZ4O3Z73DS3KY7OAI/
profile.yahoo.com/JXCJSHKPCPVXBOQ7J534QFVINQ/
profile.yahoo.com/3EOKGCUUHMX22KIAI7GML5AUCI/
profile.yahoo.com/FUO6MOEDGUSYPEOIJUHQJPXU7U/
profile.yahoo.com/F6BNCL7UA4N2QU4VU7TQS4GMLE/
profile.yahoo.com/GKR4HD2WEEDOSAOEBLLVPT4BGM/
profile.yahoo.com/F4DSPOKAULYUURHT5Q2GHGRP74/
profile.yahoo.com/CAPYUDHYGK4T5OC376VAIQF6JU/
profile.yahoo.com/RL73T5NKKMIA5ATI4UOXVNYJCE/
profile.yahoo.com/PYFFJ2FNCM72O7GP7JSVLEHCTY/
profile.yahoo.com/YV4MGP3T7PLUT5Q66JR7B6NIBU/
profile.yahoo.com/Z6UG6LBRTTF7ADW4437WKUDXGU/
profile.yahoo.com/ZAWGBC2HZG3G5S5IBOVTVMNJ2A/
profile.yahoo.com/IEOM46P5K6FMQVAEFUL2735D6U/
profile.yahoo.com/JHLVQLSGI36ARJGGVCQOHYPDQI/
profile.yahoo.com/3YAUGQDSUIOIUNT7VJJTJYTJZE/
profile.yahoo.com/GEJCGLX3RSX3CFXZN7ZQ45V5RE/
profile.yahoo.com/SWFT4BQFW5YV652VO3AAR7GVY4/
profile.yahoo.com/QEWPERHSO5CGAZIW26TUPK7I3U/
profile.yahoo.com/JK26UMRCUXDVDMY6BYNVOOJYUE/
profile.yahoo.com/Y5YATSDLQWP2DY2ZRYUU3YVGP4/
profile.yahoo.com/WSRRX26XRWVJJTE5LU35MY5RLA/
profile.yahoo.com/CNA5EDOG7RO4LXDA5UBF3VGW2Y/
profile.yahoo.com/QRLN2PRXNB7CSDE6Q3766RUHVQ/
profile.yahoo.com/NYOIMY4DNKTLKEX2KA4XYNUWFA/
profile.yahoo.com/35IZIGSXVKERCC6RBFEBSSKKJ4/
profile.yahoo.com/VU6OYHDP5HAEJKYS5T3IALIALM/
profile.yahoo.com/J6NTXRNENOJ3TU47SYQ4PAHNOM/
profile.yahoo.com/Z5QEHIJUQ47BBSF4P3RQ6344DY/
profile.yahoo.com/VILU55D3NKY5NUXNOQC6H573LE/
profile.yahoo.com/3R63NVDVAF4EDRGYZA7BIVAONI/
profile.yahoo.com/EI2TDGGO55AFBU6XWSQPS7LYOA/
profile.yahoo.com/3EFNWKN37P2723HJW5LVKOPWBA/
profile.yahoo.com/MAXO4UZIPIWUTCND6ASAKUJYXM/
profile.yahoo.com/EZSBG26PW76LSBEVG3MZRG27XM/
profile.yahoo.com/YMK3J3VRN5LBUPYKV3NJHRPPHQ/
profile.yahoo.com/SVTB5BBL5ONCCFPJKAG4QJ44RM/
profile.yahoo.com/M5NIB6Q6BWRLYNRSVD2L2MNZJ4/
profile.yahoo.com/762NN2FKDNPQSVJLCMW6UJBHDI/
profile.yahoo.com/C6SLU4RABJPNBTKAEE6WAHJSQM/
profile.yahoo.com/7U43VP2ZEKEQ76S53UUNORXDMQ/
profile.yahoo.com/SU3FWXSQEFFU2YEY3PXWLIHBMQ/
profile.yahoo.com/JZ64JDPIYNDW7FVNMPXYNFQFKY/
profile.yahoo.com/FWBCGTP53CM7Z3SUZZS7GS3XX4/
profile.yahoo.com/EKNBCOQEFJBPFUPDUXCMXERD7E/
profile.yahoo.com/MGUXIKNVGJI62IHIEPB3MJ7HQU/
profile.yahoo.com/USMC6OT4BWG3YVZBGCOJKSWSHA/
profile.yahoo.com/VYKK4W6SLKVZXG5GCOPJQAP75U/
profile.yahoo.com/WMWS4L4MBNDU5NUPHBMKDY45QY/
profile.yahoo.com/PIS3KRCPGYH74IYX3MGQB5U6JM/
profile.yahoo.com/XF4NZ542TO7BS5MOGATNG3USVI/
profile.yahoo.com/UEM55PGXUN4MP4FECICJNORYPU/
profile.yahoo.com/4CVJTJ4ELRIV7X6HHZGRA4CR3Q/
profile.yahoo.com/WCBQZRW6U6XEZ3HBIOI3WIYFSQ/
profile.yahoo.com/RPHOWRTWXIVOHJDCHLZYIKR2WE/
profile.yahoo.com/X357ROFRNJNRYMZJMGEXNHTVWI/
profile.yahoo.com/5MOBSZ3CHMLO53CLEASO3EGQDY/
profile.yahoo.com/GCF4SSTXUX3RD24LTH54HIAOIE/
profile.yahoo.com/56UPEH6Z3QATUESZTB7CMTYTUQ/
profile.yahoo.com/OEOJYUVCVHBSKL74VIPNW4N5F4/
profile.yahoo.com/LFJP45USYC47T2DXSGZLUAJZYU/
profile.yahoo.com/VDX7NO4JA6DIG3I4PZTJ7EVV6U/
profile.yahoo.com/HKLGNUMAOIDKTMV3CDYGRJEXCA/
profile.yahoo.com/JTT74DDVUROQTMBCRLELTBAN7M/
profile.yahoo.com/L65HKC24BEMM74GDDQFHXP2A4M/
profile.yahoo.com/SLCNYRLUCPZR7BQPJ7XHWYHM5Q/
profile.yahoo.com/Q34AC6HMZ3REC5Q7ARCWZZQGRI/
profile.yahoo.com/4ALSGIV44UICTUOIY74NQT6IV4/
profile.yahoo.com/LDRKGLODVYLCCC5GBEQ7LVGULE/
profile.yahoo.com/OOMLENV3IGWPEZHTRQS3NDBDXM/
profile.yahoo.com/2YMAYSDEMMPJRRTC2W6SALQPCE/
profile.yahoo.com/TGY5XVMVNWVQUMKZRUHVY76OYY/
profile.yahoo.com/44DMJIDUXTTRDQ7ZNTV3RXAYRI/
profile.yahoo.com/UQ4IYBETUY5YXQXRERUE54P7EI/
profile.yahoo.com/RXNACSDGQGOZIR63XQLUBN4LFI/
profile.yahoo.com/KFUX4FW3GD4JRUQ4UV2XBDV7DU/
profile.yahoo.com/RKBV43WS3WPC3KIMJQ4IGTYQRQ/
profile.yahoo.com/BRSQ3P4ZBEFEPGWXECRPDOZ3WU/
profile.yahoo.com/O2GJWZX5WK45ALUQ77FPE5WSGA/
profile.yahoo.com/5A5WTCXH5XGOW3D4CJAAF6VB2M/
profile.yahoo.com/UVY3SDDUDNJNEDKFG45QZ6532M/
profile.yahoo.com/DMVBIHULCBR4OAFZWFALF6JRDA/
profile.yahoo.com/5BO466WFLLC25UJAYPLAWIHTFM/
profile.yahoo.com/UIVTAWNS6LXIE7JOJW5BW5DT2E/
profile.yahoo.com/ZCQXGQIPM53ZX6NGZAEW5DJKAA/
profile.yahoo.com/LOHZRZDG5F7JJLGLUN3MTPFZUU/
profile.yahoo.com/A3L7T52LXHCCSOWOJ6WFYW37QE/
profile.yahoo.com/J7MSLMKKAYAPLO5NDTMC4XIXYA/
profile.yahoo.com/LKTQ4EKICBLVXWYXTXUD3ASBVI/
profile.yahoo.com/LOR2BH4DF6WASERGGND6EBKPZM/
profile.yahoo.com/II3E7O5RQ7GZ7XPEYF2AJWDCQA/
profile.yahoo.com/DYDQM24RJSI3WSDDWGLGNCUK4A/
profile.yahoo.com/YKODTTBDER7IID6B4CJVYCTJWE/
profile.yahoo.com/UND4IWNLWFAYRJNKV5UATMUPBQ/
profile.yahoo.com/37HPFCZRAM6WF3UPOGJLK54O54/
profile.yahoo.com/4B62WGM4KZMJA4FHWNFGG2OBH4/
profile.yahoo.com/TMVRYKMFRYNPBAY5CILZS7722A/
profile.yahoo.com/MRCEISRAL4JHMPSPGGIGNOSC3M/
profile.yahoo.com/WZMZ7345NIHCMYZK2UUE2EFMYM/
profile.yahoo.com/BNQ33W6FUPMLVJJVK4XIYCSUIA/
profile.yahoo.com/KNOK4EDWXXTDLD4GBTMNCIQVIE/
profile.yahoo.com/VJA6FXHG6C6KATX53SC7OCPM6Q/
profile.yahoo.com/G6TN6WN6NIJYRSQUENJT7QCNVA/
profile.yahoo.com/KMD2WJYSQW7YRDTU5Q7HB3JHYQ/
profile.yahoo.com/ZSIZEEVM3ZJJ2V45EX7CC5522Y/
profile.yahoo.com/R7TQZRFG3BPDLPFNAGWL55FLDY/
profile.yahoo.com/YDL3WOIAXTUFAHI7LPT6TAYX34/
profile.yahoo.com/337ZFW6LOECP4X7LM3HY2MNLN4/
profile.yahoo.com/3QVEWKA4LKH3GB25ZNJHRDYI34/
profile.yahoo.com/5NDFYSLKXL6U4W7PLS63T63ZWY/
profile.yahoo.com/SMV54Y77QHXMLGAJZBFPIPSH24/
profile.yahoo.com/MUBWEHALLEHV4LR5O32GEZWAUE/
profile.yahoo.com/GQ32APIGQLJQVTSMC7XPJ6K47Q/
profile.yahoo.com/LQSUPOYMSOHDU6WQHK5HX3X6ZI/
profile.yahoo.com/3WUBNZCD7PUMCFUL7NKHZKAL3Y/
profile.yahoo.com/75TLCBFPSYFL2T42WPJ2NHTVRU/
profile.yahoo.com/IM7JPUX4OD2U6RCEDGN7QHQ74U/
profile.yahoo.com/VONWYO4GBGOQHJEC233TLVSHTE/
profile.yahoo.com/D5WP6VAWLFOHX5N7ECXCHCIBII/
profile.yahoo.com/YE6LROZT5FCB6NOYGEGL4MK3IU/
profile.yahoo.com/IBUIGON6CSXS3DTWJ2T7GIZR44/
profile.yahoo.com/VXA44ZSHTOFUJES4IBVMACL3MU/
profile.yahoo.com/SLVJ3TSMUL4AUVFQ7XPFIVRANA/
profile.yahoo.com/IB66CD5OSQ25D6HJYEBU7Q6JNU/
profile.yahoo.com/QNQKN6CV3NZHBJ6ZPSUDO3TMEM/
profile.yahoo.com/HO5HVR2TNKEHRV5HJFHHRTWMKA/
profile.yahoo.com/LJWJB6T5KNTTTLTN7SZQXKI2U4/
profile.yahoo.com/IK36FB3THEXIDK4S6YQOCRE3IY/
profile.yahoo.com/2SLY3CP3MOSHI7XB55GTFDVJH4/
profile.yahoo.com/IV7XJBLDKMB3UPJ4WGUYLPETQY/
profile.yahoo.com/KCHSQ4K7XBCD2RDVSI7DM7XJVU/
profile.yahoo.com/XGB6WOYS3CJ5EZXGXFRZIDKG6I/
profile.yahoo.com/BJ2YUMSQ742A37SZZAPHKNOYF4/
profile.yahoo.com/QBCMNI5UZ3L3XQ467ACEEXP2IY/
profile.yahoo.com/6ML4W232GAILTMTAKRULN32OQ4/
profile.yahoo.com/GYLZ5YBUWXK7VBIGIPQMXYEKHE/
profile.yahoo.com/YE355A7JS3CVE2XTZ6RRR5YTGU/
profile.yahoo.com/CL5FD4BWVR3HN46BCGPUUU4DLY/
profile.yahoo.com/N5K6PXKV3NOZLJEFNSSAGJEO5E/
profile.yahoo.com/YGLYTMUF5WGQIFL7NRWKADTDKM/
profile.yahoo.com/NHNQWTKUMFLEPFEZHZIKMJ5OZM/
profile.yahoo.com/MIZNEDMV62FSJDCCJYHNHD5Y4U/
profile.yahoo.com/3LKFYMWF33DWR4DCTIMNWKUGWY/
profile.yahoo.com/ABIJHVOWO7CUIOURWIGHPQ747U/
profile.yahoo.com/BTV5CWUM737QFZBUJ6K34AG4FI/
profile.yahoo.com/IL52HCONDNOWO25AEFMF3CHI4Y/
profile.yahoo.com/MPOTRKOJHWAEOS4KT2GY43SUSU/
profile.yahoo.com/NXEZDCEEGLIHFLJ3FRV6MZKILM/
profile.yahoo.com/WSPLRRSF7XB4EAHBNP3K4PMT54/
profile.yahoo.com/QQE7EKSOLFZRDSDKQHNTFKMLZU/
profile.yahoo.com/A7VZHQL6JEQVD7RZ3TFASQHYQQ/
profile.yahoo.com/PCTOLW55PAJ2IS76ELUCHMWB3A/
profile.yahoo.com/ESSVIGYULLRJBTNXLBONLZZTJI/
profile.yahoo.com/T73OV63XUR5DIH3HXPQJVUUR54/
profile.yahoo.com/WM5ACXPIWUXR3H7B5YB2C3DMKQ/
profile.yahoo.com/I5K2D3USGY6E6ZP6KQACPZ3VTQ/
profile.yahoo.com/ESRXM6P42GJPWJHWXABFSMGEPE/
profile.yahoo.com/UW5CPWKVHXD7NUNCNHAMK7HE3E/
profile.yahoo.com/7ZYAJKJVPXFVSRVUPAYV3Y4JVM/
profile.yahoo.com/VENORY56OMWF3B4CG252522POM/
profile.yahoo.com/MWYQNSPNKKXLTLPRGAS7SHBNF4/
profile.yahoo.com/REZJJWPT4L6WIFDOLTRI44EYTY/
profile.yahoo.com/WPJQ435V4M7JJ22GQLYXZUPNFA/
profile.yahoo.com/JD427N3F43RQ42IFWQULKLKGOE/
  if(nLevel&0x10 || istrin>>t[2]){ 
  nLevel &= 0xF; 
  if(nLevel==1){t[1]=t[2];csym[1]=0;continue;} 
  if(csym[1]=='*'||csym[1]=='/'){ 
  GetExpValue(t+1, csym[1]); 
  } 
  else{ 
  GetExpValue(t, csym[0]); 
  t[1]=t[2];csym[0]=csym[1];csym[1]=0; 
  } 
  nLevel = 1; 
  } 
  else istrin.clear(); 
  } 
  else{nERR = -1; break;} 
  } 
  if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]); 
  else nReturn=GetExpValue(t, csym[0]); 
  return nERR==-1?1:0; 
  }}

  函数模板使用示例:

  在以上那段代码的后面加上以下代码:

以下是引用片段:
  程序代码: 
  #include 
  #include 
  #include 
  using namespace std; 
  int main(void) 
  { 
  string s1; 
  while(cin>>s1) 
  { 
  istrstream isin(s1.data()); 
  double d; 
  if(fy_Exp::GetExpValue(isin, d)) 
  { 
  cout<  
  } 
  else 
  { 
  cout<<"ERROR"<  
  } 
  } 
  return 0; 
  }

  然后编译执行就可以了。

  其它:TC++上一定编译错误,不保证在VC6上也能通过编译。

  建议使用VC7或VC更高版本,或者使用GNU C++编译。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值