如何使内存变少
#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
struct qujian
{
double l,r;
};
bool chu0 = false;
void check(string & s)
{
int l = s.size()-1;
for (int i = 1;i <= l;i++)
if (s[i] == ' ')
s[i] = '$';
string ss = " ";
for (int i = 1;i <= l;i++)
if (s[i]!='$')
ss+=s[i];
s = ss;
}
bool reducebracket(string &s)
{
int l = s.size()-1;
if (s[1] != '(' || s[l] != ')')
return false;
int b = 0;
for (int i = 2;i <= l-1;i++)
{
if (s[i] == '(')
b++;
if (s[i] == ')')
b--;
if (b < 0)
return false;
}
s = s.erase(l,1);
s = s.erase(1,1);
return true;
}
char cmp(char x,char y)
{
if (x == '&')
return '>';
if (x == '*' || x == '/')
return '>';
if ( (x == '+' || x == '-') && (y == '+' || y == '-'))
return '>';
return '<';
}
int find (string s)
{
char now = '&';
int l = s.size()-1;
int i = 1,k;
while (i <= l)
{
if (s[i] == '(')
{
int b = 0;
do
{
if (s[i] == '(')
b++;
if (s[i] == ')')
b--;
i++;
}
while (b!=0);
}
else
if ((s[i] == '*' || s[i] == '/' || s[i] == '+')
||
(s[i] == '-' && i > 1 &&
(s[i-1] ==']' || s[i-1] == ')')
))
{
if (cmp(now,s[i]) == '>')
{
now = s[i];
k = i;
}
i++;
}
else
i++;
}
return k;
}
double best_max(double x,double y,double z,double w)
{
double m = x;
if (y > m)
m = y;
if (z > m)
m = z;
if (w > m)
m = w;
return m;
}
double best_min(double x,double y,double z,double w)
{
double m = x;
if (y < m)
m = y;
if (z < m)
m = z;
if (w < m)
m = w;
return m;
}
qujian reduce(string s)
{
qujian temp;
if (chu0)
return temp;
if (s == " ")
{
temp.l = 0;
temp.r = 0;
return temp;
}
bool judge = false;
int l = s.size()-1;
for (int i = 1;i <= l;i++)
if (s[i] == '*' || s[i] == '/' || s[i] == '+')
{
judge = true;
break;
}
else
if (s[i] == '-' && i > 1 && (s[i-1] ==']' ||
s[i-1] == ')'))
{
judge = true;
break;
}
if (!judge)
{
int p1 = s.find('[',0),p2 = s.find(',',0);
string s1 = s.substr(p1+1,p2-p1-1);
int p3 = s.find(']',0);
string s2 = s.substr(p2+1,p3-p2-1);
temp.l = atof(s1.c_str());
temp.r = atof(s2.c_str());
if (temp.l > temp.r)
{
double te;
te = temp.l;
temp.l = temp.r;
temp.r = te;
}
return temp;
}
bool flag = reducebracket(s);
while (flag)
flag = reducebracket(s);
int k = find(s);
string sl = s.substr(0,k);
char key = s[k];
s = s.erase(1,k);
string sr = s;
qujian temp1 = reduce(sl),temp2= reduce(sr);
if (chu0)
return temp1;
qujian temp3;
switch (key)
{
case '+':
temp.l=temp1.l+temp2.l,temp.r=temp1.r+temp2.r;
break;
case '-':
temp.l=temp1.l-temp2.r,temp.r=temp1.r-temp2.l;
break;
case '*':
{
temp.r = best_max(temp1.l*temp2.l,temp1.l*temp2.r,
temp1.r*temp2.l,temp1.r*temp2.r);
temp.l = best_min(temp1.l*temp2.l,temp1.l*temp2.r,
temp1.r*temp2.l,temp1.r*temp2.r);
}
break;
case '/':
{
if (temp2.l <=0 && temp2.r >=0)
{
chu0 = true;
return temp2;
}
temp.r = best_max(temp1.l/temp2.l,temp1.l/temp2.r,
temp1.r/temp2.l,temp1.r/temp2.r);
temp.l = best_min(temp1.l/temp2.l,temp1.l/temp2.r,
temp1.r/temp2.l,temp1.r/temp2.r);
}
break;
}
return temp;
}
void input_data()
{
string ss;
while ( getline(cin,ss))
{
string s = " +";
s += ss;
check(s);
chu0 = false;
int ll = s.size()-1;
int i = 1;
while (i <= ll-1)
{
if (i > 1 && s[i] == '-' && s[i-1]!=']' && (s[i+1] > '9' || s[i+1] <'0'))
{
s = s.insert(i,"([0,0]");
i+=6;
i++;
int j = i;
if (s[i] == '(')
{
int b = 0;
do
{
if (s[i] == '(') b++;
if (s[i] == ')') b--;
i++;
}
while (b!=0);
s = s.insert(i,")");
i = j;
}
if (s[i] == '[')
{
while (s[i] != ']') i++;
i++;
s = s.insert(i,")");
}
ll = s.size();
}
i++;
}
qujian l = reduce(s);
if (chu0)
printf("Division by zero\n");
else
printf("[%.3lf,%.3lf]\n",l.l,l.r);
}
}
int main()
{
input_data();
return 0;
}