PRoblem Description 一個簡單的行編輯程序的功能是:接受用戶從終端輸入的程序或數據,并存入用戶的數據區。
由于用戶在終端上進行輸入時,不能保證不出差錯,因此,若在編輯程序中,“每接受一個字符即存入用戶數據區”的做法顯然不是最恰當的。較好的做法是,設立一個輸入緩沖區,用以接受用戶輸入的一行字符,然后逐行存入用戶數據區。允許用戶輸入出差錯,并在發現有誤時可以及時更正。例如,當用戶發現剛剛鍵入的一個字符是錯的時,可補進一個退格符”#”,以表示前一個字符無效;
如果發現當前鍵入的行內差錯較多或難以補救,則可以鍵入一個退行符”@”,以表示當前行中的字符均無效。
如果已經在行首繼續輸入’#’符號無效。 Input 輸入多行字符序列,行字符總數(包含退格符和退行符)不大于250。 Output 按照上述說明得到的輸出。 Example Input
whli##ilr#e(s#*s)outcha@putchar(*s=#++);Example Output
while(*s)putchar(*s++);Hint
Author cz
#include <stdio.h>#include<math.h>#include <stack>#include <iostream>#include <algorithm>#include <bits/stdc++.h>using namespace std;int main(){ stack <char > p; stack <char > p2; char g[300]; while(gets(g)) { int b=strlen(g); for(int a=0; a<b; a++) { if(g[a]=='#') { if(!p.empty()) p.pop(); } else if(g[a]=='@') { while(!p.empty()) { p.pop(); } } else p.push(g[a]); } while(!p.empty()) { p2.push(p.top()); p.pop(); } while(!p2.empty()) { printf("%c", p2.top()); p2.pop(); } printf("/n"); } return 0;}新聞熱點
疑難解答