c语言字符计算器库,c语言简单计算器,实现四则运算,可带括号(C language simple calculator, the realization of four operations, you...

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值