How do I automatically clean up code in C++?

2019-08-01 17:31发布

I'm working as a TA in an introductory programming class, and the students tend to submit their programs as either one line, or without any indentation. Is there any tool that allows me to insert indents and things like that automatically? (We're using C++ and VisualStudio)

6条回答
看我几分像从前
2楼-- · 2019-08-01 18:11

Rather than answering your question I will advise:

Don't let them do that.

Making their code readable for human beings is a part of programming, and you are fully justified in grading them on it. You might want to point them at the pretty printers listed in the other answers, however. Just to be nice.

查看更多
太酷不给撩
3楼-- · 2019-08-01 18:12

There is a gnu program called indent, usually shipped with linux but also available here (gnu indent) and available under Cygwin.

However, you are using VS, so you could use it to format code. They have hidden the feature just a bit: Edit -> Advanced -> Format Document, or Control/E, D.

查看更多
做个烂人
4楼-- · 2019-08-01 18:12

In Vim it's gg=G.

查看更多
时光不老,我们不散
5楼-- · 2019-08-01 18:17

Select the entire file (Ctrl-A) and then hit Ctrl-K Ctrl-F, which is essentially format the entire document.

EDIT: Of course in Visual Studio IDE

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-08-01 18:18

If you need to do this in batch mode, try using astyle, also available in the Cygwin installer.

查看更多
【Aperson】
7楼-- · 2019-08-01 18:31

You're after a pretty printer. I'd suggest Googling for C++ pretty printer, and looking for something that meets your requirements (price, platform).

As an aside, you may find that deducting marks for poorly formatted code will work just as well. Students need to learn that good code layout is an important part of writing maintainable code.

查看更多
登录 后发表回答