跳到正文

算法·理论

代码格式化

huangyiming1202约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年12月9日
收录
2026年9月9日
更新
2026年11月21日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
int main(){
	cout<<"把要格式化的代码放在本代码同路径下,命名为text.cpp,fmtext.cpp为本代码格式化后的输出文件!\n";
	system("pause"); 
	freopen("text.cpp","r",stdin);
	freopen("fmtext.cpp","w",stdout);
	int tabs=0;
	string xxs;
	while(1){
		getline(cin,xxs);
		if(xxs[0]=='}'){
			tabs--;
		}
		for(int i=1;i<=tabs;i++){
			cout<<"  ";
		}
		if(xxs[xxs.size()-1]=='{'){
			tabs++;
		}
		if(xxs.size()>0){
			cout<<xxs<<endl;
		}
	}
}
原文