star

搜索

RSS

RSS Link

计数器

133364
求一个数是第几个排列数
乱代码之右三角

代码整理器

Star posted @ 2010年8月06日 23:55 in C语言 with tags c 转换 , 6052 阅读
/*Neat Code v0.1 author:star date:2010
没有经过优化和维护,一次写成版本。为了方便地整理排版乱的代码。
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void NeatSpace(FILE *fin, FILE *fout);		//格式化空格字符
int  NeatType(FILE *fin, FILE *fout);		//格式化缩进
void Putlevel(FILE *out, int lv);			//输出缩进

int main(int argc, char *argv[])
{
	FILE *fin, *fout;
	char buf[255]= "t_";
	int  ErrorCode;

	if(1 == argc)
	{
		printf("Neat Code v0.1 -- star\n\n");
		printf("整理成Star喜欢的代码样式。\n");		
		printf("Usage:\nNeatCode source [destination]\n\n");
		printf(" source         指定要整理的文件。\n");		
		printf(" [destination]  可生成的目标文件。\n\n");	
		printf("如果没有输入目标文件,将会生成默认的。\n");					
	}
	else
		if(2 == argc)
		{
			if(!(fin = fopen(argv[1], "r")))
			{
				printf("错误001 - 无法打开%s文件。", argv[1]);
				return EXIT_FAILURE;
			}
			fout = fopen("~TEMP.NTC", "w");
			NeatSpace(fin, fout);
			fclose(fin);fclose(fout);
			fin = fopen("~TEMP.NTC", "r");
			strncpy(buf+2, argv[1], 253);
			if(!(fout = fopen(buf, "w")))
			{
				printf("错误002 - 创建%s文件失败。", buf);
				return EXIT_FAILURE;
			}
			ErrorCode = NeatType(fin, fout);
			fclose(fin);fclose(fout);
			system("del ~TEMP.NTC");
			if(ErrorCode) printf("错误005 - 所格式化的代码不标准 代码%d。", ErrorCode);
			else printf("成功执行!");
		}
		else
			if(3 == argc)
			{
				if(!(fin = fopen(argv[1], "r")))
				{
					printf("错误001 - 无法打开%s文件。", argv[1]);
					return EXIT_FAILURE;
				}
				fout = fopen("~TEMP.NTC", "w");
				NeatSpace(fin, fout);
				fclose(fin);fclose(fout);
				fin = fopen("~TEMP.NTC", "r");
				if(!(fout = fopen(argv[2], "w")))
				{
					printf("错误003 - 创建新的%s文件失败。", argv[2]);
					return EXIT_FAILURE; 
				}
				ErrorCode = NeatType(fin, fout);
				fclose(fin);fclose(fout);
				system("del ~TEMP.NTC");
				if(ErrorCode) printf("错误005 - 所格式化的代码不标准 代码%d。", ErrorCode);
				else printf("成功执行!");
			}else
			{
				printf("错误004 - 无效的命令。");
			}

			return EXIT_SUCCESS;
}

void NeatSpace(FILE *fin, FILE *fout)
{
	char ch = fgetc(fin);
	short cflag = 1;

	while(!feof(fin))
	{
		if(ch == '/' &&  fputc(ch, fout) && (ch=fgetc(fin)))
		{
			if(ch == '/' && fputc(ch, fout))
				while((ch=fgetc(fin)) != '\n') fputc(ch, fout);
			else if(ch == '*' && fputc(ch, fout))
				while((ch=fgetc(fin)) != '/') fputc(ch, fout);
		}
		if(ch == '\n' && (cflag = 1) && fputc(ch, fout))
		{
			ch = fgetc(fin);
			while(!feof(fin) && ch == '\n' || ch == ' ' || ch == '\t') ch = fgetc(fin);
			continue;
		}
		if(ch == ' ' || ch == '\t')
		{
			if(cflag == 0) fputc(ch, fout);
			while(ch == ' ' || ch == '\t') ch = fgetc(fin);
			continue;
		}
		else
		{
			if (ch != '{') cflag = 0;
			do{fputc(ch, fout);ch = fgetc(fin);}
			while(ch != '\n' && ch != ' ' && ch != '\t');
		}
	}
}

int NeatType(FILE *fin, FILE *fout)
{
	char ch = fgetc(fin);
	short IsTop = 1;
	signed int level = 0;

	if(ch == '\n') ch = fgetc(fin);
	while(!feof(fin))
	{
		while(ch != '\n' && ch != '{' && ch != '}')
		{
			IsTop = 0;
			fputc(ch, fout);
			ch = fgetc(fin);
		}
		if(ch == '\n' && (IsTop = 1))
		{
			fputc(ch, fout);
			ch = fgetc(fin);
			if(ch != '}')
				Putlevel(fout, level);
		}else
			if(ch == '{')
			{
				++level;
				if(!IsTop) fputc('\n', fout);				
				while(ch = fgetc(fin))
					if(ch == ' ' || ch == '\t');
					else if(ch == '\n') {fputc('{', fout);break;}
					else
					{
						Putlevel(fout, level-1);
						fputc('{', fout);
						fputc('\n', fout);
						break;
					}
				Putlevel(fout, level);				
			}else
				if(ch == '}')
				{
					--level;
					if(!IsTop) fputc('\n', fout);
					Putlevel(fout, level);
					fputc('}', fout);
					ch = fgetc(fin);
				}		
	}
	return level;
}

void Putlevel(FILE *out, int lv)
{
	if(lv > 0)
		while(lv--) fputc('\t', out);
}

上次不小心删了,再发一次吧。

Tyler Hamilton 说:
2020年3月20日 15:07

Any time you're exploring the web occasionally I find a web site this really is rather thought invoking such as this one. Happy that I discovered your website because I very much liked this and so I anticipate your future post. An excellent web site and i'll come back once more for more useful content…  mexico virtual number

Tyler Hamilton 说:
2020年4月01日 23:22

During this website, you will see this shape, i highly recommend you learn this review. 토토사이트

Tyler Hamilton 说:
2020年4月02日 21:58

Such sites are important because they provide a large dose of useful information ...  맞고사이트

Tyler Hamilton 说:
2020年4月02日 22:04

I personally use them exclusively high-quality elements : you will notice these folks during: 그래프사이트

호빠, 호스트바, 호빠 구인, 선수하 说:
2020年7月06日 03:48

Your blogs further more each else volume is so entertaining further serviceable It appoints me befall retreat encore. I will instantly grab your rss feed to stay informed of any updates.garage doors centereach

Anonymous 说:
2020年7月11日 22:21

I invite you to the page where     see how much we have in common. edm

Anonymous 说:
2020年7月18日 03:28

I simply want to tell you that I am new to weblog and definitely liked this blog site. Very likely I’m going to bookmark your blog . You absolutely have wonderful stories. Cheers for sharing with us your blog. smm panel

먹튀검증 说:
2020年8月16日 21:32

So lot to occur over your amazing blog. Your blog procures me a fantastic transaction of enjoyable.. Salubrious lot beside the scene.

article to psalms ve 说:
2020年10月15日 03:31

Your blogs further more each else volume is so entertaining further serviceable It appoints me befall retreat encore. I will instantly grab your rss feed to stay informed of any updates.

dark web market link 说:
2020年12月18日 17:55

I am always searching online for storys that can accommodate me. There is obviously a multiple to understand about this. I feel you made few salubrious points in Attributes moreover. Detain busy, awesome career!

Buying magic mushroo 说:
2021年1月25日 00:55

1st, deciding the strain you need is perhaps the most predominant types of Magic Mushrooms is Psilocybe Cubensis and you can locate a wide scope of various strains becoming both in the wild and inside everywhere on the world. Right now, there are more than 60 diverse Psilocybe Cubensis strains that we are aware of. Subsequently, not all Psilocybe Cubensis strains are practically a mirror image of one another. The strength and stumbling levels are distinctive in every one of these strains. On the off chance that you are searching for cheerful sentiments and bunches of chuckles, you may pick in for Magic Mushrooms, while B+ Magic Mushrooms will give you one of the hottest visual and otherworldly excursions. In the same way as other others, you might be looking for the mending advantages of psilocybin mushrooms or truffles. Before you begin investigating this segment, if it's not too much trouble note that psilocybin is an unlawful and exceptionally controlled substance in many pieces of this world. We just energize protected, capable, utilization of psilocybin mushrooms.

Buy modafinil online 说:
2021年3月22日 20:06

Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written.

cleaning services du 说:
2021年8月03日 19:26

A lot of our family-owned vacuuming company could be happy and pleased to provide one range from services to all your company needs. We have quite a few years of past experiences in supporting professional residential and clinic cleaning all around London. Our impassioned and solid team will cover you big standard services considering that first explore. We make available End from Tenancy Vacuuming, After Architects Cleaning, Subsequent to Party Vacuuming, One apart deep vacuuming, Carpet, Mattress mattress and Upholstery Vacuuming, Office, Higher education and Play room, Shop Vacuuming.

office cleaning serv 说:
2021年11月15日 23:25

Many of us are really smart and consider doing this kind of cleaning all on your own. This is completed to spend less which just isn't as huge sum as compared to your work-time and well being. For right after construction washing mess oahu is the job regarding expert maids who learn how to clean making use of cleaning products and guard self from your dust.

CGBSE 6th Class Mode 说:
2022年7月01日 16:02

CG Board 6th Model Paper 2023 Prepare by Senior Specialist Teachers. so Students who are Reading in 6th Class Model Paper. Those Students are Need Very Hard Work for CGBSE 6th Class Annual Examination for Higher Marks in 10th Annual Examination to Support CG 6th Solved Paper 2023 Blueprint. CGBSE 6th Class Model Paper CG 6th Class Model Paper 2023 is Very Important for Public Exam Preparation, Each Student adopts All sorts of ways to get a good score in the CG Board Exam, Good News Students who are Searching for the Chhattisgarh Board 6th Class Model Test Paper 2023.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter