Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cctype>
- #include <stack>
- #include <cassert>
- #include <string>
- int main()
- {
- using namespace std;
- char c;
- stack<char> symbol;
- while (cin.get(c) && isalpha(c))
- cout << c;
- cin.putback(c);
- cout << ' ';
- string sth;
- bool flag = false;
- while ( (cin >> c) && (c != ';')) {
- if (c == ')') {
- flag = true;
- while (true) {
- assert(!symbol.empty() && "括号不匹配");
- switch (symbol.top())
- {
- case '*': cout << "->"; break;
- case 'c': cout << "const"; break;
- case '(': goto EXIT;
- default : assert(false && "未知字符");
- }
- symbol.pop();
- }
- EXIT:
- symbol.pop();
- } else if (flag) {
- if (c == '(') {
- cout << c;
- int count = 1;
- while (count != 0) {
- cin >> c;
- if (c == '(')
- ++count;
- else if (c == ')')
- --count;
- cout << c;
- }
- } else {
- cout << c;
- }
- } else {
- if (c == '*' || c == '(') {
- symbol.push(c);
- } else {
- assert((isalnum(c) || c=='_') && "非标识符");
- sth = c;
- while (c = cin.peek(), isalnum(c) || c=='_') {
- cin.ignore();
- sth.push_back(c);
- }
- if (sth == "const") {
- symbol.push('c');
- } else {
- cout << sth << ':';
- flag = true;
- }
- }
- }
- }
- while (!symbol.empty()) {
- switch (symbol.top()) {
- case 'c': cout << "const"; break;
- case '*': cout << "->"; break;
- case '(': assert(false && "括号不匹配"); break;
- default : assert(false && "未知字符");
- }
- symbol.pop();
- }
- cout << ';' << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment