#include <iostream>#include<string>using namespacestd;structNode {Charch; Node*Next; Node (CharC, node* p) {ch = c; next =p;} }; voidMain () {stringstr; BOOLFlag =true; while(flag) {cout<<"Please enter an arithmetic expression:"<<Endl; Getline (CIN, str); intLen =str.length (); Node* top =NULL; inti; for(i =0; i < Len; i++) { if(Str[i] = ='('|| Str[i] = ='['|| Str[i] = ='{') {Top=NewNode (Str[i], top); } if(Str[i] = =')'|| Str[i] = =']'|| Str[i] = ='}') { if(top = = NULL) {cout <<"1. Parentheses do not match! "<< Endl; Break; } Else{Node* pt =top; Top= top->Next; PT->next =NULL; Charitem = pt->ch; Delete pt; if(Item = ='('&&str[i]! =')') || (Item = ='['&&str[i]! =']') || (Item = ='{'&&str[i]! ='}') ) {cout<<"2. Parentheses do not match! "<< Endl; Break; } } } } if(i = =Len) { if(top) {cout <<"3. Parentheses do not match! "<<Endl;} Elsecout <<"parentheses match exactly! "<<Endl; } if(top) {Node*ptr; while(top) {ptr= top->Next; Delete top; Top=ptr; }} cout<<"do you want to continue? Press 1 to continue, press 0 if you exit:"<<Endl; intchoice; CIN>>choice; GetChar (); if(Choice = =0) flag =false; }} The code has been tested to run successfully on VS2013! This article has two main purposes:1To exchange experiences with people for reference. 2In the next rookie, the code is inevitably inappropriate, begged the great God to criticize. Your criticism is at the beginning of the improvement, for your criticism, will be greatly appreciated!
C + +, using chain stack to achieve parentheses matching, friendly interface, easy to operate, smooth running