Redo or Undo

来源:互联网 发布:手机运行windows 编辑:程序博客网 时间:2024/06/18 17:48

撤销模拟程序

编写程序模拟word中的“重做Redo”“撤销Undo”两个按钮。即键盘输入一段文字(不能含#,e.g., I as Tom whether he will go to Beijingh)之后输入“#U”(“U”代表Undo)则撤销最后一个输入的字符(“h”),在输出位置重新输出一遍新撤销后的串。这时可以再继续输入。当然,也可以输入“#R”恢复刚才删除的输入h。每有一次#R都要输出一下新的结果。当然,可以同时连续输入多个#U或多个#R。比如,输入#U#U#R#U#U#U#R#R。

  • Input

    • 第一行为一个数字T,代表一共有T组测试样例,每组测试样例包含以下内容:
      每行包含一串字母(包含空格,不超过100个字符)。
      开始第一个字母不能为“#”,即第一个字母必须要敲入文字。
      “#R”表示重做。
      “#U”表示撤销。
      “##”表示本次测试样例结束。
  • Ouput

    • 每输入一次重做,都要输出操作之后的文字
#include <iostream>#include <stack>#include <cstdio>using namespace std;int main(){    int cases;    cin >> cases;   getchar();    for(int kase = 1; kase <= cases; ++kase){        stack<char> cc;        string oper = "", opert;        while(getline(cin, opert) && opert != "##"){            if(opert[0] == '#'){                if(opert[1] == 'U'){                    int num = oper.size() - 1;                    if(num >= 0){                        cc.push(oper[num]);                        //cout << oper[num] << "*-*" << cc.size() << endl;                        oper.resize(num);                    }                }                else if(opert[1] == 'R'){                    if(!cc.empty()){                        char c = cc.top();                        oper += c;                        cc.pop();                    }                    cout << oper  << endl;                }            }            else {                    oper += opert;                    while(!cc.empty()) cc.pop();            }        }    }}
0 0
原创粉丝点击