c语言简单计算器,实现四则运算,可带括号(C language simple calculator, the realization of four operations, you can bring brackets)
c语言简单计算器,实现四则运算,可带括号(C language simple calculator, the realization of four operations, you can bring brackets)
*
1, only integer arithmetic,
2, you can have parentheses nested, such as (a+b* (c+d) -e) -f,
But the formula for the shape of (a+ (), +d*e, -f) b+c cannot be calculated
* /
#include
#include
#include
#include
Using namespace std;
Struct Element
{
Double data;
Char sign;
Char bracket_left;
Char bracket_right;
Element *next;
};
Int check (char *s)
{
Int count_bracket=0;
Int i=0;
While (i
{
If (s[i]=='()) count_bracket++;
Else if (s[i]==')' 'count_bracket--;
If (count_bracket<0)
Return 0;
If ((s[i]>='0'&&s[i]<='9'||s[i]==')'||s[i]==' ('||s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'))
Return 0;
I++;
}
If (count_bracket) return 0;
Return 1;
}
Void create_expression (char, *s, Element, *head)
{
Element *p=head;
P->next= (Element *) malloc (sizeof (Element));
P=p->next, p->next=NULL;
P->bracket_left=p->bracket_right='\0';
P->sign='\0', p->data=0;
Int x=0;
While (x
{
If (s[x]>='0'&&s[x]<='9')
{
While (s[x]>='0'&&s[x]<='9'&&s[x], ='\0')
P->data*=10, p->data+=s[x++]-'0';
X--;
}
Else if (s[x]=='+'||s[x]=='-'||s[x]=='*'||s[x]=='/')
{
P->next= (Element *) malloc (sizeof (Element));
If (... P->next)
{
Cout<< "ERROR" <
Exit (0);
}
P=p->next, p->next=NULL;
P->bracket_left=p->bracket_right='\0';
P->sign=s[x], p->data=0;
}
Else if (s[x]==')'
{
P->bracket_right=');
P->bracket_left='\0';
}
Else if (s[x]=='('))
{
P->bracket_left='';
P->bracket_right='\0';
}
X++;
}
P=head->next;
While (p->next) p=p->next;
P->next= (Element *) malloc (sizeof (Element));
P=p->next;
P->data=0, p->sign='+', p->next=NULL;
P->bracket_left=p->bracket_right='\0';
}
/*double chang_int (char *s)
{
Double sum=0;
Int, x=0, mark=0;
While (x
{
If (s[x]=='.')
{
Mark=1;
Break;
}
Sum*=10, sum+=s[x]-'0';
X++;
}
If (mark)
{
Double m=0;
For (int i=strlen (s) -1; i>x; i--)
M+=s[i]-'0', m/=10;
Sum+=m;
}
Return sum;
}*/
Double calculate (Elem