zoukankan      html  css  js  c++  java
  • 洛谷P1022计算器的改良(字符串+各种细节坑点考虑)

    题目链接:https://www.luogu.org/problemnew/show/P1022

    分析和思路:

    题意好理解,就是字符串处理+方程求解,但是真的有很多坑点要考虑到

      1 #include <iostream>
      2 #include <string>
      3 #include <algorithm>
      4 #include <iomanip>
      5 #include <vector>
      6 #include <map>
      7 #include <set>
      8 #include <stack>
      9 #include <queue>
     10 #include <cstdio>
     11 #include <cstring>
     12 #include <cmath>
     13 using namespace std;
     14 typedef long long ll;
     15 typedef unsigned long long ull;
     16 const int maxn=1005;
     17 int vis[maxn];
     18 string s,s1,s2;
     19 
     20 int main()
     21 {
     22     ios::sync_with_stdio(false); cin.tie(0);
     23     
     24     //输入 
     25     cin>>s;
     26     
     27     //准备工作 
     28     int len=s.length();
     29     for(int i=0;i<=len-1;i++)
     30     {
     31         if(s[i]=='=')
     32         {
     33             string st=s.substr(0,i);
     34             s1=st;
     35             string stt=s.substr(i+1,len-i);
     36             s2=stt;
     37             break;
     38         }
     39     }
     40     int len1=s1.length();
     41      
     42     //计算a串 
     43     int x=0,y=0;//系数项,常数项
     44     int pre=0,pref=0;//前一个字符标号 
     45     for(int i=0;i<=len1-1;i++)
     46     {
     47         if(s1[i]=='+')//正数 
     48         {
     49             if(s1[i-1]>='a' && s1[i-1]<='z')//系数项 
     50             {
     51                 if(s1[i-1-1]!='+' && s1[i-1-1]!='-' && i-1-1>=0)//坑点:2x和x区别,系数不为1,a 
     52                 {
     53                     int t=0;
     54                     for(int j=pre;j<=i-1-1;j++)
     55                     {
     56                         int tt=s1[j]-'0';
     57                         t=t*10+tt;
     58                     }
     59                 
     60                     if(pref==0) x=x+t;
     61                     else if(pref==1) x=x-t;
     62                 }
     63                 else//系数为1,a 
     64                 {
     65                     if(pref==0) x=x+1;
     66                     else if(pref==1) x=x-1;
     67                 }
     68             }
     69             else//常数项
     70             {
     71                 int t=0;
     72                 for(int j=pre;j<=i-1;j++)
     73                 {
     74                     int tt=s1[j]-'0';
     75                     t=t*10+tt;
     76                 }
     77                 //cout<<t<<endl;
     78                 if(pref==0) y=y+t;
     79                 else if(pref==1) y=y-t;
     80             }
     81             
     82             pre=i+1; 
     83             pref=0;
     84         }
     85         else if(s1[i]=='-')//负数 
     86         {
     87             if(s1[i-1]>='a' && s1[i-1]<='z')//系数项 
     88             {
     89                 if(s1[i-1-1]!='+' && s1[i-1-1]!='-' && i-1-1>=0)
     90                 {
     91                     int t=0;
     92                     for(int j=pre;j<=i-1-1;j++)
     93                     {
     94                         int tt=s1[j]-'0';
     95                         t=t*10+tt;
     96                     }
     97                     if(pref==0) x=x+t;
     98                     else if(pref==1) x=x-t;
     99                 }
    100                 else
    101                 {
    102                     if(pref==0) x=x+1;
    103                     else if(pref==1) x=x-1;
    104                 }
    105             }
    106             else//常数项
    107             {
    108                 int t=0;
    109                 for(int j=pre;j<=i-1;j++)
    110                 {
    111                     int tt=s1[j]-'0';
    112                     t=t*10+tt;
    113                 }
    114                 if(pref==0) y=y+t;
    115                 else if(pref==1) y=y-t;
    116             }
    117             
    118             pre=i+1;
    119             pref=1; 
    120         }
    121         else if(i==len1-1)//末尾
    122         {
    123             if(s1[i]>='a' && s1[i]<='z')//系数项 
    124             {
    125                 if(s1[i-1]!='+' && s1[i-1]!='-' && i-1-1>=0)
    126                 {
    127                     int t=0;
    128                     for(int j=pre;j<=i-1;j++)
    129                     {
    130                         int tt=s1[j]-'0';
    131                         t=t*10+tt;
    132                     }
    133                     if(pref==0) x=x+t;
    134                     else if(pref==1) x=x-t;
    135                 }
    136                 else
    137                 {
    138                     if(pref==0) x=x+1;
    139                     else if(pref==1) x=x-1;
    140                 }
    141             }
    142             else//常数项
    143             {
    144                 int t=0;
    145                 for(int j=pre;j<=i;j++)
    146                 {
    147                     int tt=s1[j]-'0';
    148                     t=t*10+tt;
    149                 }
    150                 if(pref==0) y=y+t;
    151                 else if(pref==1) y=y-t;
    152             }
    153             
    154             pre=i+1;
    155             pref=0;//末尾了怎么都行 
    156         } 
    157     }
    158     
    159     //计算b串 
    160     int xx=x,yy=y;
    161     x=0,y=0;
    162     pre=0,pref=0;
    163     s1=s2,len1=s1.length();
    164     for(int i=0;i<=len1-1;i++)
    165     {
    166         if(s1[i]=='+')//正数 
    167         {
    168             if(s1[i-1]>='a' && s1[i-1]<='z')//系数项 
    169             {
    170                 if(s1[i-1-1]!='+' && s1[i-1-1]!='-' && i-1-1>=0)//系数不为1,a 
    171                 {
    172                     int t=0;
    173                     for(int j=pre;j<=i-1-1;j++)
    174                     {
    175                         int tt=s1[j]-'0';
    176                         t=t*10+tt;
    177                     }
    178                 
    179                     if(pref==0) x=x+t;
    180                     else if(pref==1) x=x-t;
    181                 }
    182                 else//系数为1,a 
    183                 {
    184                     if(pref==0) x=x+1;
    185                     else if(pref==1) x=x-1;
    186                 }
    187             }
    188             else//常数项
    189             {
    190                 int t=0;
    191                 for(int j=pre;j<=i-1;j++)
    192                 {
    193                     int tt=s1[j]-'0';
    194                     t=t*10+tt;
    195                 }
    196                 if(pref==0) y=y+t;
    197                 else if(pref==1) y=y-t;
    198             }
    199             
    200             pre=i+1; 
    201             pref=0;
    202         }
    203         else if(s1[i]=='-')//负数 
    204         {
    205             if(s1[i-1]>='a' && s1[i-1]<='z')//系数项 
    206             { 
    207                 if(s1[i-1-1]!='+' && s1[i-1-1]!='-' && i-1-1>=0)
    208                 {
    209                     int t=0;
    210                     for(int j=pre;j<=i-1-1;j++)
    211                     {
    212                         int tt=s1[j]-'0';
    213                         t=t*10+tt;
    214                     }
    215                     if(pref==0) x=x+t;
    216                     else if(pref==1) x=x-t;
    217                 }
    218                 else
    219                 {
    220                     if(pref==0) x=x+1;
    221                     else if(pref==1) x=x-1;
    222                 }
    223             }
    224             else//常数项
    225             {
    226                 int t=0;
    227                 for(int j=pre;j<=i-1;j++)
    228                 {
    229                     int tt=s1[j]-'0';
    230                     t=t*10+tt;
    231                 }
    232                 if(pref==0) y=y+t;
    233                 else if(pref==1) y=y-t;
    234             }
    235             
    236             pre=i+1;
    237             pref=1; 
    238         }
    239         else if(i==len1-1)//末尾
    240         {
    241             if(s1[i]>='a' && s1[i]<='z')//系数项 
    242             {
    243                 if(s1[i-1]!='+' && s1[i-1]!='-' && i-1-1>=0)
    244                 {
    245                     int t=0;
    246                     for(int j=pre;j<=i-1;j++)
    247                     {
    248                         int tt=s1[j]-'0';
    249                         t=t*10+tt;
    250                     }
    251                     if(pref==0) x=x+t;
    252                     else if(pref==1) x=x-t;
    253                 }
    254                 else
    255                 {
    256                     if(pref==0) x=x+1;
    257                     else if(pref==1) x=x-1;
    258                 }
    259             }
    260             else//常数项
    261             {
    262                 int t=0;
    263                 for(int j=pre;j<=i;j++)
    264                 {
    265                     int tt=s1[j]-'0';
    266                     t=t*10+tt;
    267                 }
    268                 if(pref==0) y=y+t;
    269                 else if(pref==1) y=y-t;
    270             }
    271             
    272             pre=i+1;
    273             pref=0;//末尾了怎么都行 
    274         } 
    275     }
    276     
    277     //计算答案+输出 
    278     xx=xx-x;
    279     yy=yy-y;
    280     yy*=-1;
    281     for(int i=0;i<=len-1;i++)
    282     {
    283         if(s[i]>='a' && s[i]<='z')
    284         {
    285             cout<<s[i];
    286             break;
    287         }
    288     }
    289     cout<<"=";
    290     if(yy==0)
    291     {
    292         cout<<"0.000"<<endl;//坑点:c运算机制,0/负数输出-0,所以需要特判!! 
    293     }
    294     else
    295     {
    296         double ans=yy*1.0/xx;
    297         cout<<setiosflags(ios::fixed)<<setprecision(3)<<ans<<endl;
    298     }
    299     
    300     return 0;
    301 }

    完。

  • 相关阅读:
    Java实现 LeetCode 209 长度最小的子数组
    Java实现 蓝桥杯 图书排列(全排列)
    Java实现 蓝桥杯 图书排列(全排列)
    Java实现 蓝桥杯 图书排列(全排列)
    C++11 FAQ中文版--转
    rtsp交互命令简介及过程参数描述
    PS流格式
    经典系统windows xp sp3官方原版下载(附XP序列号)
    Darwin Streaming Server 简介
    jrtplib跨网络通讯NAT穿透问题解决方法
  • 原文地址:https://www.cnblogs.com/redblackk/p/9613062.html
Copyright © 2011-2022 走看看