Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3746
  • 博文数量: 6
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 75
  • 用 户 组: 普通用户
  • 注册时间: 2014-12-31 14:08
文章分类

全部博文(6)

文章存档

2015年(4)

2014年(2)

我的朋友
最近访客

分类: Python/Ruby

2015-01-12 10:58:00

input: A, A2, C2>, A3<>, A4< B4<>, C4>>
output:
A< B, C >
B2< C, D >
C2< D >
A2< B2< C, D >, C2< D > >
A3<  >
B4<  >
D4< E4 >
C4< D4< E4 > >
A4< B4<  >, C4< D4< E4 > > >

python code:

点击(此处)折叠或打开

  1. #!/usr/bin/env python

  2. class Template(object):
  3.     def __init__(self):
  4.         self.name = ""
  5.         self.args = []

  6.     def __str__(self):
  7.         return self.name + "< " + ", ".join([str(a) for a in self.args]) + " >"
  8.     __repr__ = __str__

  9. def parse_template(ts):
  10.     tlist = []
  11.     stk = []
  12.     pos = 0
  13.     arg = ""

  14.     for i, c in enumerate(ts):
  15.         if c in "<>,":
  16.             if c == '<':
  17.                 t = Template()
  18.                 t.name = ts[pos : i].strip()
  19.                 stk.append(t)
  20.             elif c == '>':
  21.                 t = stk.pop()
  22.                 arg = ts[pos : i].strip()
  23.                 if arg:
  24.                     t.args.append(arg)
  25.                 if stk:
  26.                     stk[-1].args.append(t)
  27.                 #else:
  28.                 tlist.append(t)
  29.             else: # comma
  30.                 arg = ts[pos : i].strip()
  31.                 if arg and stk:
  32.                     stk[-1].args.append(arg)
  33.             pos = i + 1
  34.     return tlist


  35. ts = input("input:")
  36. tlist = parse_template(ts)
  37. for t in tlist:
  38.     print(str(t))

C++ code:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <stack>
  5. #include <iostream>

  6. using namespace std;

  7. struct Temp
  8. {
  9.     size_t name;
  10.     size_t start;
  11.     size_t end;
  12. };

  13. void print_temp(const string& temp)
  14. {
  15.     stack<Temp> st;
  16.     size_t pos1 = 0;
  17.     size_t pos2 = 0;

  18.     while (pos1 < temp.size())
  19.     {
  20.         pos2 = temp.find_first_of("<,>", pos1);
  21.         if (pos2 == string::npos)
  22.         {
  23.             break;
  24.         }

  25.         if (temp.at(pos2) == '<')
  26.         {
  27.             Temp t;
  28.             t.name = pos1;
  29.             t.start = pos2;
  30.             t.end = string::npos;
  31.             st.push(t);
  32.         }
  33.         else if (temp.at(pos2) == '>')
  34.         {
  35.             if (st.empty())
  36.             {
  37.                 // error
  38.                 cout << "unmatched angle brackets" << endl;
  39.                 break;
  40.             }
  41.             else
  42.             {
  43.                 st.top().end = pos2;
  44.                 Temp t = st.top();
  45.                 st.pop();
  46.                 cout << "name: " << temp.substr(t.name, t.start - t.name) << endl;
  47.                 cout << "arg: " << temp.substr(t.start + 1, pos2 - t.start - 1) << endl;
  48.             }
  49.         }
  50.         else
  51.         {
  52.             // comma
  53.         }

  54.         pos1 = pos2 + 1;
  55.     }

  56.     if (!st.empty())
  57.     {
  58.         cout << "unmatched angle brackets" << endl;
  59.     }
  60. }

  61. int main(int argc, char **argv)
  62. {
  63.     char lbuf[1024];
  64.     cin.getline(lbuf, sizeof(lbuf));
  65.     print_temp(lbuf);

  66.     return 0;
  67. }

阅读(43) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~