(Huawei trial) Evaluate the string conditional expression using a two-way linked list

Source: Internet
Author: User
Description: calculates the value of an arithmetic expression in string format.
Expressions support the following operations: "+,-, *,/". The priority of "*" and "/" is higher than that of "+" and "-".
Parentheses are not required and there is no space between expressions;
For example, for the expression "3-2 + 15*2", the expression value is 31.
Run Time Limit: 60 sec
Memory limit: 256 Mbyte
Input: subtraction, multiplication, division, and four arithmetic expressions. The length cannot exceed 1024 bytes. The formula does not contain parentheses or spaces.
Output: Operation Result of the expression.
Example input: 3-2 + 15*2

Sample output: 31

#include <stdio.h>#include <string.h>#include <stdlib.h>struct ListNode{int m_nValue;char m_nChar;ListNode* m_pNext;ListNode* m_pPrev;};ListNode* CreatNode(int m_nValue,int m_nChar,ListNode* m_pPrev,ListNode* m_pNext){ListNode* m_pNode=new ListNode();m_pNode->m_nChar=m_nChar;m_pNode->m_nValue=m_nValue;m_pNode->m_pNext=m_pNext;m_pNode->m_pPrev=m_pPrev;return m_pNode;}void DestroyList(ListNode* pHead){ListNode* pNode=pHead;while(pNode!=0){pHead=pNode->m_pNext;free(pNode);pNode=pHead;}}int StrCalculate(char* str){if(str==NULL)return -1;int tmp=0;int len;int i,k=0;ListNode* head=NULL;ListNode* p=NULL;ListNode* q=NULL;p=q=head=(ListNode*)malloc(sizeof(ListNode));len=strlen(str);for(i=0;i<len;i++){tmp=0;while(str[i]>='0'&&str[i]<='9'){tmp=tmp*10+str[i]-'0';++i;}if(k==0){head=CreatNode(tmp,'0',NULL,NULL);p=head;}else{q=CreatNode(tmp,'0',p,NULL);p->m_pNext=q;p=q;k++;}if(i<len){q=CreatNode(0,str[i],p,NULL);p->m_pNext=q;p=q;k++;}elsebreak;}p=head;while(p->m_pNext!=NULL){if(p->m_nChar=='*'){tmp=p->m_pPrev->m_nValue * p->m_pNext->m_nValue;p->m_pPrev->m_nValue=tmp;if(p->m_pNext->m_pNext!=NULL){p->m_pPrev->m_pNext=p->m_pNext->m_pNext;p->m_pNext->m_pNext->m_pPrev=p->m_pPrev;}else{ p=p->m_pPrev;p->m_pNext=NULL;break;}}else if(p->m_nChar=='/'){tmp=p->m_pPrev->m_nValue/p->m_pNext->m_nValue;p->m_pPrev->m_nValue=tmp;if(p->m_pNext->m_pNext!=NULL){p->m_pPrev->m_pNext=p->m_pNext->m_pNext;p->m_pNext->m_pNext->m_pPrev=p->m_pPrev;}else{p=p->m_pPrev;p->m_pNext=NULL;break;}}p=p->m_pNext;}p=head;while(p->m_pNext!=NULL){if(p->m_nChar=='+'){tmp=p->m_pPrev->m_nValue+p->m_pNext->m_nValue;p->m_pNext->m_nValue=tmp;}else if(p->m_nChar=='-'){tmp=p->m_pPrev->m_nValue-p->m_pNext->m_nValue;p->m_pNext->m_nValue=tmp;}p=p->m_pNext;}tmp=p->m_nValue;printf("the result is : %d\n",tmp);DestroyList(head);return tmp;}int main(){char* m_str="2-3-12/2*4";StrCalculate(m_str);return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.