跳到正文

个人记录

码风优化器

11514zbs约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月19日
篇幅
约 7 分钟

版本

  1. v1当前

码风优化器:

C++
//致谢:Junly_JYukio(背景优化)
//Libingyue2011(主体编辑)
//Believe_in_dreams(主要思路)
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
bitset<10010>bs;
int tb[300010],db[300010];
string tx;
int main()
{
	cerr<<"  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n";
	cerr<<"|C++格式调整机,请关闭你要调整的文件并保证此代码与你要调整的文件在一个文件夹里,且不要出现编译错误| \n";
	cerr<<"|      注意事项:\n未加大括号的if,for,else,while与分支/循环体不在一行内可能会缩进错误             | \n";
	cerr<<"  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n";
	cerr<<"请输出要调格式的文件名(不加后缀):\n";
	string fl,pl;
	cin>>fl,pl=fl;
	fl+=".cpp",pl+=".txt";
	cerr<<"处理中!!!\n";
	freopen(fl.c_str(),"r",stdin);
	freopen(pl.c_str(),"w",stdout);
	string s;
	int nober=0;
	while(getline(cin,s)) //输入并且修正行内格式
	{
		cout<<s<<"\n";
		int st=-1;
		char d[30]="<>=+-*/^%!~()?.;,[]";
		bool p=0;
		while(s[0]=='\n') s.erase(0,1);
		while(s[0]==9) s.erase(0,1);
		while(s[0]==' ') s.erase(0,1);
		if(s.size()==0) continue;
		if(nober)
		{
			int pos=int(s.find("*/"));
			if(pos!=-1) nober=0;
			tx+=s;
			continue;
		}
		bs=0;
		int _pos=int(s.find("//"));//单行注释
		if(_pos!=-1) for(int i=_pos; i<s.size(); i++) bs[i]=1;
//		cerr<<_pos<<"\n";
		_pos=int(s.find("/*"));//多行注释
		if(_pos!=-1 && !bs[_pos] && !bs[_pos+1])
		{
			int pos_=int(s.find("*/"));
			if(pos_==-1)
			{
				nober=1;
				for(int i=_pos; i<s.size(); i++) bs[i]=1;
			}
			else for(int i=_pos; i<=pos_+1; i++) bs[i]=1;
//			cerr<<_pos<<" "<<pos_<<"\n";
		}
		for(int i=0; i<s.size(); i++)
		{
			if(s[i]=='\"' || s[i]=='\'') p^=1;
			if(!bs[i]) bs[i]=p;
		}
		for(int i=0; i<19; i++,st=-1)
			while(s.find(d[i],st+1)!=-1) //删除符号左右的空格
			{
				int pos=s.find(d[i],st+1);
				st=pos;
				if(bs[i]) continue;
				while(st>0 && s[st-1]==' ') s.erase(st-1,1),st--;
				while(st<s.size()-1 && s[st+1]==' ') s.erase(st+1,1);
			}
		string t[30]= {"if","for","else","while","struct","cin","cout","cerr"};
		for(int i=0; i<8; i++,st=-1) //删除关键字前面的非换行
		{
			while(s.find(t[i],st+1)!=-1)
			{
				int pos=s.find(t[i],st+1);
				st=pos;
				if(bs[i]) continue;
				if(st<s.size()-2 && s[st+1]==' ' && s[st+2]!=' ') s.erase(st+1,1);
				if(i==0 && s[st-2]=='e' && s[st-3]=='s' && s[st-4]=='l' && s[st-5]=='e') continue;
				if(st>1 && (s[st-1]!=')' && s[st-1]!='\n')) s.insert(st,1,'\n'),st++;
				if(s[st-1]==')') s.insert(st,1,' '),st++;
			}
		}
		if(s[0]==',') tx+=s;
		else tx+=s+"\n";
	}
	string t[30]= {"if","for","else","while","struct","cin","cout","cerr"};
	Sleep(200);
	cerr<<"已完成30%\n";
	int st=-1,pos=-1;
	while(tx.find("{",st+1)!=-1) //删掉大括号前的换行
	{
		st=tx.find("{",st+1);
		while(st>0 && (tx[st-1]=='\n' || tx[st-1]==' ' || tx[st-1]==9)) tx.erase(st-1,1),st--;
		if(tx[st-3]=='n' && tx[st-4]=='i' && tx[st-5]=='a' && tx[st-6]=='m') tx.insert(st,1,' '),st++;
	}
	Sleep(200);
	cerr<<"已完成60%\n";
	st=pos=-1;
	while(tx.find("{",st+1)!=-1) //加tab
	{
		st=tx.find("{",st+1);
		pos=tx.find("}",pos+1);
		int pt=st;
		while(tx.find("\n",pt+1)!=-1 && tx.find("\n",pt+1)<pos-1)
		{
			pt=tx.find("\n",pt+1);
			tx.insert(pt+1,1,9),pos++;
		}
		//将st与pos的每一行加上tab
	}
	Sleep(300);
	cerr<<"已完成100%\n";
	freopen(fl.c_str(),"w",stdout);
	cout<<tx;
	cerr<<"欢迎使用C++格式调整机v4.0";
	Sleep(100);
	return 0;
}

来自于 @Libingyue2011 奆佬

原文