Problem Description
一个简单的行编辑程序的功能是:接受用户从终端输入的程序或数据,并存入用户的数据区。
由于用户在终端上进行输入时,不能保证不出差错,因此,若在编辑程序中,“每接受一个字符即存入用户数据区”的做法显然不是最恰当的。较好的做法是,设立一个输入缓冲区,用以接受用户输入的一行字符,然后逐行存入用户数据区。允许用户输入出差错,并在发现有误时可以及时更正。例如,当用户发现刚刚键入的一个字符是错的时,可补进一个退格符"#",以表示前一个字符无效;
如果发现当前键入的行内差错较多或难以补救,则可以键入一个退行符"@",以表示当前行中的字符均无效。
如果已经在行首继续输入'#'符号无效。
Input
输入多行字符序列,行字符总数(包含退格符和退行符)不大于250。
Output
按照上述说明得到的输出。
Sample Input
whli##ilr#e(s#*s)
outcha@putchar(*s=#++);
Sample Output
while(*s)
putchar(*s++);
这个题本身可以算是一个栈的应用(或者用string的erase函数也可以实现),利用栈尾弹出和栈的清空实现“退格”与“行清空”。这里最恶心的地方在于它的输入,因为是未知行输入且没有特定的结束符,所以只能认为一个空字符串(空行)为结束标志,这里用string类型与getline(输入一行)这样的输入函数来实现这个功能,由于输出方便,这里使用字符数组模拟栈。
所以有如下思路:
- 严格按照题意设定输入模块
- 利用栈或者string模拟过程
代码如下:
#include<pch.h>
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf_s("%lld", &a)
#define println(a) printf("%lld\n", a)
#define reset(a, b) memset(a, b, sizeof(a))
const int INF = 0x3f3f3f3f;
using namespace std;
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1000000007;
const int tool_const = 19991126;
const int tool_const2 = 33;
inline ll lldcin()
{
ll tmp = 0, si = 1;
char c;
c = getchar();
while (c > '9' || c < '0')
{
if (c == '-')
si = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
tmp = tmp * 10 + c - '0';
c = getchar();
}
return si * tmp;
}
///Untersee Boot IXD2(1942)
/**Although there will be many obstructs ahead,
the desire for victory still fills you with determination..**/
/**Last Remote**/
string text[500],in;
char sta[50000];
int DETERMINATION()
{
ll cnt = 0;
while (getline(cin,text[cnt++]))//输入一行,以换行符为结尾,并且换行符不保留
{
if (text[cnt - 1] == "")//这是个空字符串(单敲一个回车而输入的换行符在上面被过滤了)
{
cnt--;
break;
}
}
for (int i = 0; i < cnt; i++)
{
ll len =text[i].size(), measure = 0;
for (int j = 0; j < len; j++)
{
if (text[i][j] == '#')//模拟退格键,注意防止越界
{
if (measure > 0)
measure--;
}
else if (text[i][j] == '@')
measure = 0;
else
sta[measure++] = text[i][j];
}
for (int j = 0; j < measure; j++)
cout << sta[j];
cout << endl;
reset(sta, '\0');
}
return 0;
}