代码整理器
2010年8月06日 23:55 | Comments(15) | Category:C语言 | Tags:c 转换
/*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); }
上次不小心删了,再发一次吧。
求一个数是第几个排列数
2010年8月06日 00:47 | Comments(81) | Category:C语言 | Tags:c 数学 康托 排列组合 转换
根据康托展开公式:
a[i]为第i个数比后面的都小的个数。
#include <stdio.h> #include <string.h> int FAC[10] = {1,1,2,6,24,120,720,5040,40320,362880}; int CantorExp(char *s, int n) { int i, j, t, num = 0; for(i = 0; i < n; ++i) { t = 0; for(j = i+1; j < n; ++j) if(s[j] < s[i]) ++t; num += FAC[n-i-1]*t; } return num+1; } int main(int argc, char *argv[]) { char s[] = "1324"; //输入一个排列数的字符串序列 printf("%d", CantorExp(s, strlen(s))); return 0; }
中文汉字转拼音首字母
2010年8月05日 03:43 | Comments(1) | Category:C++ | Tags:c++ 转换 宽字符 拼音
/*ConvertToPY v0.1 author:star date:2010-02-25 */ #include <iostream> #include <string> using namespace std; static char convert(wchar_t n); static bool In(wchar_t start, wchar_t end, wchar_t code); int main(int argc, char* argv[]) { string sChinese = "取得拼音的首字母"; // 输入的字符串 char chr[3]; wchar_t wchr = 0; char* buff = new char[sChinese.length()/2]; memset(buff, 0, sizeof(char)*sChinese.length()/2+1); for (int i = 0, j = 0; i < (sChinese.length()/2); ++i) { memset(chr, 0, sizeof(chr)); chr[0] = sChinese[j++]; chr[1] = sChinese[j++]; chr[2] = '\0'; wchr = 0; wchr = (chr[0] & 0xff) << (1<<3); wchr |= (chr[1] & 0xff); buff[i] = convert(wchr); } cout << "结果 :[" << buff << "]" << endl; return 0; } char convert(wchar_t n) { if (In(0xB0A1,0xB0C4,n)) return 'A'; if (In(0XB0C5,0XB2C0,n)) return 'B'; if (In(0xB2C1,0xB4ED,n)) return 'C'; if (In(0xB4EE,0xB6E9,n)) return 'D'; if (In(0xB6EA,0xB7A1,n)) return 'E'; if (In(0xB7A2,0xB8c0,n)) return 'F'; if (In(0xB8C1,0xB9FD,n)) return 'G'; if (In(0xB9FE,0xBBF6,n)) return 'H'; if (In(0xBBF7,0xBFA5,n)) return 'J'; if (In(0xBFA6,0xC0AB,n)) return 'K'; if (In(0xC0AC,0xC2E7,n)) return 'L'; if (In(0xC2E8,0xC4C2,n)) return 'M'; if (In(0xC4C3,0xC5B5,n)) return 'N'; if (In(0xC5B6,0xC5BD,n)) return 'O'; if (In(0xC5BE,0xC6D9,n)) return 'P'; if (In(0xC6DA,0xC8BA,n)) return 'Q'; if (In(0xC8BB,0xC8F5,n)) return 'R'; if (In(0xC8F6,0xCBF0,n)) return 'S'; if (In(0xCBFA,0xCDD9,n)) return 'T'; if (In(0xCDDA,0xCEF3,n)) return 'W'; if (In(0xCEF4,0xD188,n)) return 'X'; if (In(0xD1B9,0xD4D0,n)) return 'Y'; if (In(0xD4D1,0xD7F9,n)) return 'Z'; return 0; } inline bool In(wchar_t start, wchar_t end, wchar_t code) { if (code >= start && code <= end) return true; return false; }