#include "stack.h"
int CurValue(char x)
{
switch(x)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 3;
case 's':
case 'c':
return 5;
case '(':
return 7;
case ')':
return 0;
}
return 0;
}
int PreValue(char x)
{
switch(x)
{
case '+':
case '-':
return 2;
case '*':
case '/':
return 4;
case 's':
case 'c':
return 6;
case '(':
return 0;
case '#':
return -1;
}
return 0;
}
bool IsDigtial(char x)
{
if(('0' <= x && x <= '9') || x == '.')
return true;
else
return false;
}
bool IsDoubleOpe(char x)
{
switch(x)
{
case '+':
case '-':
case '*':
case '/':
return true;
default :
return false;
}
}
double Calculate(double n1,double n2,char op)
{
switch(op)
{
case '+':
return n2 + n1;
case '-':
return n2 - n1;
case '*':
return n2*n1;
case '/':
return n2/n1;
}
return 0;
}
double Transform(char *ch)
{
STACK <double>st1;
STACK <char>st2;
int i=0;
int la=0;
int n1 = 0;
char op;
double num=0,z=0.1,f1,f2,n;
st2.Push ('#');
while(ch[i] != '\0')
{
while(IsDigtial(ch[i]))
{
if(ch[i] == '.')
{
la = 1;
}
else
{
if(la == 1)
{
num = num + (ch[i] - '0')*z;
z = z/10;
}
else
{
num = num * 10 + (ch[i]-'0');
}
}
i++;
}
if(ch[i] != '(' && ch[i] != 's' && ch[i] != 'c' && ch[i-1] != ')')
{
st1.Push (num);
}
if(ch[i] == '(' && IsDigtial(ch[i-1]))
{
st1.Push (num);
st2.Push('*');
}
if(ch[i] == ')' && IsDigtial(ch[i+1]))
{
n1 = 1;
}
if(ch[i] == '\0')
{
break;
}
if(PreValue(st2.Get() ) < CurValue(ch[i]))
{
st2.Push (ch[i]);
if(ch[i] == 's' || ch[i] == 'c')
{
i = i+3;
}
else
i++;
}
else
{
while(PreValue(st2.Get() ) > CurValue(ch[i]))
{
if(IsDoubleOpe(st2.Get ()))
{
f1 = st1.Pop ();
f2 = st1.Pop ();
op = st2.Pop ();
n = Calculate(f1,f2,op);
}
else
{
f1 = st1.Pop ();
op = st2.Pop ();
if(op == 'c')
n = cos(f1);
else
n = sin(f1);
}
st1.Push (n);
}
if(PreValue(st2.Get ()) == CurValue(ch[i]))
{
i++;
st2.Pop ();
}
else
{
st2.Push (ch[i]);
if(ch[i] == 's' || ch[i] == 'c')
i = i + 3;
else
i++;
}
}
if(n1 == 1)
{
st2.Push('*');
n1 = 0;
}
la = 0;
num = 0;
z = 0.1;
}
op = st2.Pop ();
while(op != '#')
{
if(IsDoubleOpe(op))
{
f1 = st1.Pop ();
f2 = st1.Pop ();
n = Calculate(f1,f2,op);
}
else
{
f1 = st1.Pop ();
if(op == 'c')
n = cos(f1);
else
{
n = sin(f1);
}
}
st1.Push (n);
op = st2.Pop ();
}
num = st1.Pop ();
return num;
}
void TenToOthers(char *strToTrans,char*an,int unit)
{
int n,f1,f2,temp;
int la = 0,num1 = 0,num2 = 0,t=1;
double num = 0;
STACK <int>stack1;
STACK <int>stack2;
CharToInt(strToTrans,&num1,&num2,&t);
while(num1 != 0)
{
f1 = num1/unit;
f2 = num1%unit;
stack1.Push(f2);
num1 = f1;
}//整数部分处理
for(n=0;n<10;n++)
{
num2 = num2*unit;
f1 = num2/t;
f2 = num2%t;
stack2.Push(f1);
num2 = f2;
}//小数部分处理
n = 0;
while(!stack1.IsEmpty())
{
temp = stack1.Pop();
if(temp < 10 )
{
an[n] = temp + '0';
n++;
}
else
{
an[n] = (temp - 10) + 'A';
n++;
}
}//整数部分处理
an[n++] = '.';
num1 = 0;
while(num1<=stack2.GetLength())
{
if(stack2.GetI(num1) < 10 )
{
an[n] = stack2.GetI(num1) + '0';
n++;
}
else
{
an[n] = (stack2.GetI(num1) - 10) + 'A';
n++;
}
num1++;
}//小数部分处理
an[n] = '\0';
}
void TwoToTen(char *ch1,char *an)
{
int n,temp;
int la = 0,t=1;
double z = 0.5;
double num = 0;
STACK <int>stack1;
for(n=0;ch1[n] != '\0';n++)
{
if(ch1[n] == '.')
la = 1;
else
{
if(la == 1)
{
num = num + (ch1[n] - '0')*z;
z = z/2;
}
else
{
temp = ch1[n] - '0';
stack1.Push(temp);
}
}
}
while(!stack1.IsEmpty())
{
num = num + stack1.Pop() * t;
t = t*2;
}
sprintf(an,"%lf",num);
}
int IsError(char *ch)
{
if(strcmp(ch,"") == 0)
return 1;
int i=0,count1=0,count2=0;
char op;
STACK <char>st2;
st2.Push('#');
while(ch[i] != '\0')
{
if(ch[i] == '(')
{
if(ch[i+1] == ')')
return 4;
else
count2++;
}
if(ch[i] == ')')
{
if(count2 <= 0)
return 3;
else
count2--;
}
i++;
}
if(count2 != 0)
return 3;
i = 0;
while(ch[i] != '\0')
{
while(IsDigtial(ch[i]))
{
i++;
}
if(ch[i] != '(' && ch[i] != 's' && ch[i] != 'c' && ch[i-1] != ')')
count1++;
if(ch[i] == '\0')
{
break;
}
if(PreValue(st2.Get() ) < CurValue(ch[i]))
{
st2.Push (ch[i]);
if(ch[i] == 's' || ch[i] == 'c')
{
i = i+3;
}
else
i++;
}
else
{
while(PreValue(st2.Get() ) > CurValue(ch[i]))
{
if(IsDoubleOpe(st2.Pop ()))
{
if(count1 >= 2)
count1--;
else
return 2;
}
else
{
if(count1 < 1)
return 2;
}
}
if(PreValue(st2.Get ()) == CurValue(ch[i]))
{
i++;
st2.Pop ();
}
else
{
st2.Push (ch[i]);
if(ch[i] == 's' || ch[i] == 'c')
i = i + 3;
else
i++;
}
}
}
op = st2.Pop ();
while(op != '#')
{
if(IsDoubleOpe(op))
{
if(count1 >= 2)
count1--;
else
return 2;
}
else
{
if(count1 < 1)
return 2;
}
op = st2.Pop ();
}
return 0;
}
void CharToInt(char *strToTrans,int *num1, int *num2,int *t)
{
int n,la;
for(n=0;strToTrans[n]!='\0';n++)
{
if(strToTrans[n] == '.')
{
la = 1;
}
else
{
if(la == 1)
{
*num2 = *num2*10 + (strToTrans[n] - '0');
*t = *t*10;
}
else
{
*num1 = *num1*10 + (strToTrans[n] - '0');
}
}
}
}
/*void CharToDouble(char *strToTrans)
{
}*/
- 1
- 2
前往页