C + +, using chain stack to achieve parentheses matching, friendly interface, easy to operate, smooth running

Source: Internet
Author: User

#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

Related Article

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.