您的位置:首页 > 编程语言 > C语言/C++

C语言代码格式自动化(python实现)

2016-01-05 23:03 543 查看
这永远是个大坑。。

在不负责任的课堂作业上投入过分的精力是一件很愚蠢的事。。

#!usr/bin/env python3
#coding: utf-8

import re

KeywordsList = ['do', 'for', 'while', 'if', 'else']
# DoubleOpList = [r'\+', r'\-', r'\*', r'/', r'%',
# r'=', r'\+=', r'\*=', r'\=', r'%=',
# r'\&', r'\|', r'\^', r'\>\>', r'\<\<',
# r'\&=', r'\|=', r'\^=', r'\>\>=', r'\<\<=', r'\~='
# r'\>', r'\<', r'==', r'\!=', r'\>=', r'\<=']

def tab_format(code):
after_code = []
tab_count = next_line_tab_count = 0
key_flag = False
for line in code:
line = str(line).strip()
# print(line,tab_count)
if key_flag and not line.startswith('{'):
tab_count = next_line_tab_count + 1

elif 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(' ')
if words[0] in KeywordsList:
key_flag = True

return after_code

def operator_format(code):
# print(code)
res = []
for line in code:
text = line
# print(line)
# print(text)
text = re.compile(r'\s*=\s*').sub(' = ', text)
text = re.compile(r'\s*\+\s*').sub(' + ', text)
text = re.compile(r'\s*\-\s*').sub(' - ', text)
text = re.compile(r'\s*\*\s*').sub(' * ', text)
text = re.compile(r'\s*/\s*').sub(' / ', text)
text = re.compile(r'\s*%\s*').sub(' % ', text)
text = re.compile(r'\s*\&\s*').sub(' & ', text)
text = re.compile(r'\s*\^\s*').sub(' ^ ', text)
text = re.compile(r'\s*\|\s*').sub(' | ', text)
text = re.compile(r'\s*\+=\s*').sub(' += ', text)
text = re.compile(r'\s*\-=\s*').sub(' -= ', text)
text = re.compile(r'\s*\*=\s*').sub(' *= ', text)
text = re.compile(r'\s*/=\s*').sub(' /= ', text)
text = re.compile(r'\s*%=\s*').sub(' %= ', text)
text = re.compile(r'\s*\&=\s*').sub(' &= ', text)
text = re.compile(r'\s*\^=\s*').sub(' ^= ', text)
text = re.compile(r'\s*\|=\s*').sub(' |= ', text)
text = re.compile(r'\s*==\s*').sub(' == ', text)
text = re.compile(r'\s*\!=\s*').sub(' != ', text)
text = re.compile(r'\s*\>\s*').sub(' > ', text)
text = re.compile(r'\s*\<\s*').sub(' < ', text)
text = re.compile(r'\s*\<=\s*').sub(' <= ', text)
text = re.compile(r'\s*\>=\s*').sub(' >= ', text)

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 op in DoubleOpList:
# print(r'\s*' + op + r'\s*')
# text = re.compile(r'\s*' + op + r'\s*').sub(' ' + op + ' ', text)

# print(res)
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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: