7-2 symbol pairing (20 points)

Source: Internet
Author: User

Topic:

Please write a program to check whether the following symbols are paired in the C language source program: With, with, and /* */ ( ) [ ] , { and } .

Input Format:

Enter as a C language source program. When you read a line with only a period . and a carriage return, it marks the end of the input. There are no more than 100 symbols that need to be checked for pairing in a program.

output Format:

First, if all symbols are paired correctly, output in the first line YES , otherwise NO . The first non-paired symbol is then indicated in the second line: output if the left sign is missing, or ?-右符号 if the right symbol is missing 左符号-? .

Input Sample 1:
void test(){    int i, A[10];    for (i=0; i<10; i++) /*/        A[i] = i;}.
Output Example 1:
NO/*-?
Input Sample 2:
void test(){    int i, A[10];    for (i=0; i<10; i++) /**/        A[i] = i;}].
Output Example 2:
NO?-]
Input Sample 3:
void test(){    int i    double A[10];    for (i=0; i<10; i++) /**/        A[i] = 0.1*i;}.
Output Example 3:
YES

思路:
  
1、
首先处理一下输入,把所有字符串中的左括号与右括号单独提出来组成一个字符串。这样后边如果有错误,也好调试找bug。
  2、对于‘/*‘和‘*/‘可以分别处理为‘<‘和‘>‘,这两个尖括号的ASCII码值相差为2。
  3、遍历这个字符串,当碰到左括号的时候就统统压入栈中,碰到右括号的时候,如果当前的栈是空的就输出‘NO’,否则就看看是否匹配,不匹配就把栈中的那个括号弹出并说明栈中的那个符号不匹配。
  4、很坑的是这个题没有说明只要有一不匹配就输出,然后结束就行,最后两个样例怎么都出错,搜了下答案才知道这么坑。
代码:
#include <iostream>#include<cstring>#include<cstdio>#include<string.h>#include<cstdlib>#include<algorithm>#include<queue>#include<malloc.h>#include<bits/stdc++.h>#defineStack_init_size 100#defineStackincrement 10using namespacestd;Const intMAXN = 1e4+Ten; typedefLong LongLl;typedefCharSelemtype;typedefstruct{Selemtype*Base; Selemtype*top; intstacksize;} Sqstack;intInitstack (sqstack&S) {S.Base= (selemtype*)malloc(stack_init_size*sizeof(Selemtype)); if(! S.Base) Exit (-2); S.top= S.Base; S.stacksize=stack_init_size; return 1;}intGetTop (Sqstack S, selemtype&e) {    if(S.Base==s.top)return 0; E= * (s.top-1); return 1;}intPush (sqstack&S,selemtype e) {    if(S.top-s.Base>=s.stacksize) {S.Base= (selemtype*)realloc(S.Base, (s.stacksize+stackincrement) *sizeof(Selemtype)); if(! S.Base) Exit (-2); S.top= S.Base+s.stacksize; S.stacksize+=stackincrement; }    *s.top++ =e; return 1;}intPop (sqstack&S) {    if(S.top = = S.)Base)        return 0; *s.top,s.top--; return 1;}BOOLIsEmpty (sqstack&S) {    if(S.Base==s.top)return true; return false;}intMain () {sqstack s;    Initstack (s); stringTmp,str=""; BOOLOK =false, first =true;  while(Getline (cin,tmp)) {if(tmp==".") Break;  for(inti =0; I<tmp.size (); i++)        {            if(tmp[i]=='['|| tmp[i]==']'|| tmp[i]=='{'|| tmp[i]=='}'|| tmp[i]=='('|| tmp[i]==')') Str+=Tmp[i]; Else if(tmp[i]=='/'&& tmp[i+1]=='*') {str+="<"; I++; }            Else if(tmp[i]=='*'&& tmp[i+1]=='/') {str+=">"; I++; }        }    }    //cout<<str<<endl;     for(inti =0; I<str.size (); i++)    {        Charop;        GetTop (S,OP); if(str[i]=='('|| str[i]=='['|| str[i]=='{'|| str[i]=='<') {Push (s,str[i]); Continue; }        Else if(IsEmpty (s)) {printf ("no\n"); if(str[i]=='>') printf ("?-*/\n"); Elseprintf ("?-%c\n", Str[i]); OK=true;  Break; }        Else if(str[i]-op!=1&& str[i]-op!=2) {printf ("no\n"); if(op=='<') printf ("/*-?\n"); Elseprintf ("%c-?\n", op);            Pop (s); OK=true; I--;  Break; }        ElsePop (s); }    if(!OK) {        if(IsEmpty (s)) printf ("yes\n"); Else        {            Charop;            GetTop (S,OP); printf ("no\n"); if(op=='<') printf ("/*-?\n"); Elseprintf ("%c-?\n", op); }    }    return 0;}
View Code

 




 

7-2 symbol pairing (20 points)

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.