uva673-栈-(字符读取和清空缓冲区)

1)先上原题代码:

#include <iostream>
#include <string.h>
#include <stack>
#include <stdio.h>
using namespace std;
int main()
{
    int num;
    scanf("%d%*c",&num);//%*c忽略下一个输入的字符,用于忽略回车,否则后面的getchar()会将缓冲区的第一个字符(“回车”)读入
    stack<char> z;
    while(num--){
        while(!z.empty()){
            z.pop();
        }
        char ch;
        while((ch=getchar())!='\n'){
        //!!要先确定栈顶元素不为空才可以比较,如果栈顶元素为空,则会崩溃;另外,如果栈顶元素为空,不能输出栈顶元素(),程序运行也会崩溃
            if(ch=='('||ch=='[')
                    z.push(ch);
            else if(ch==')'||ch==']'){
                if(z.empty())
                    z.push(ch);
                if(ch==')'){
                    if(z.top()!='(');
                    else
                        z.pop();
                }
                else {
                    if(z.top()!='[');
                    else
                        z.pop();
                }
            }
        }
        if(!z.empty())
            cout<<"No"<<endl;
        else
            cout<<"Yes"<<endl;
    }
}


2)在上述代码中,注意到有这样一句:
scanf("%d%*c",&num);//%*c忽略下一个输入的字符,用于忽略回车
如果用cin>>num;替换,也同样需要先将之前存入缓冲区的“回车”清空,但是用cin.clear(); cin.sync();不一定能解决问题,可以这样代替:

cin>>num;
char a=getchar();


3)为什么cin.clear(); cin.sync();在这里不能起到清空缓冲区?


参考cppreference(http://en.cppreference.com/w/cpp/io/basic_istream/sync

Notes是这么说的:
As with readsome(), it is implementation-defined whether this function does anything with library-supplied streams. The intent is typically for the next read operation to pick up any changes that may have been made to the associated input sequence after the stream buffer last filled its get area. To achieve that, sync() may empty the get area, or it may refill it, or it may do nothing. A notable exception is Visual Studio, where this operation discards the unprocessed input when called with a standard input stream.


结论:

cin.clear(); 重置goodbit状态位(缓冲区正常标志)

cin.sync();清空缓冲区内容

往往两者一起使用,来清空缓冲区,但一般只在vc,visual studio中可以使用,这两条语句并没有很好的可移植性。


4)原题

Description

 
 

 

You are given a string consisting of parentheses () and []. A string of this type is said to be correct:

(a)
if it is the empty string
(b)
if A and B are correct, AB is correct,
(c)
if A is correct,  (A ) and  [A ] is correct.

Write a program that takes a sequence of strings of this type and check their correctness. Your program can assume that the maximum string length is 128.

 

Input 

The file contains a positive integer n and a sequence of n strings of parentheses () and [], one string a line.

 

Output 

A sequence of Yes or No on the output file.

 

Sample Input 

 

3
([])
(([()])))
([()[]()])()

 

Sample Output 

Yes
No
Yes

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值