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()
在不负责任的课堂作业上投入过分的精力是一件很愚蠢的事。。
#!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()
相关文章推荐
- 深入理解C++中的字符串
- c++学习随笔(基本概念篇)
- C 语言学习 —— 《明解C语言》
- C++11新特性应用--实现延时求值(std::function和std::bind)
- C++11新特性应用--实现延时求值(std::function和std::bind)
- POJ 1005_I Think I Need a Houseboat
- C++ 讲解:is A
- C语言笔试常考知识点
- POJ 1004_Financial Management
- C语言的文件的操作(一)
- C++ 讲解:隐藏
- c语言学习(3)
- 程序删除自己,改写自己
- c++11学习笔记1——c++11介绍
- C++ 消息中间件(MQ4CPP)
- C++中前置声明的应用与陷阱
- 求一个区间内所有的质数(C++实现)
- POJ 1002_487-3279
- queue C++
- c语言编程软件