转载地址:代码链接
import re
def tab_format(code):
after_code = []
tab_count = next_line_tab_count = 0
for line in code:
line = str(line).strip()
# print(line,tab_count)
if line.startswith('{'):
tab_count = next_line_tab_count
next_line_tab_count += 1
elif line.startswith('}'):
tab_count = next_line_tab_count - 1
next_line_tab_count -= 1
else:
tab_count = next_line_tab_count
after_code.append('\t' * tab_count + line)
words = str(line).split(' ')
return after_code
def operator_format(code):
# print(code)
res = []
for line in code:
text = line
text = re.compile(r'\s*\(\s*').sub(' (', text)
text = re.compile(r'\s*\)').sub(') ', text)
text = re.compile(r'\s*\,').sub(', ', text)
text = re.compile(r'\s*\;').sub('; ', text)
text = re.compile(r'\s*\[').sub('[', text)
text = re.compile(r'\]\s*').sub(']', text)
text = text.replace('#include < ', '#include <');
text = text.replace('.h > ', '.h>\n');
text = text.replace(' + + ', '++');
text = text.replace(' - - ', '--');
res.append(text)
for line in res:
print(line)
def main():
SourceCode = tuple(open('in.c', 'r'))
code_with_tab = tab_format(SourceCode)
operator_format(code_with_tab)
if __name__ == '__main__':
main()