您的位置:首页 > 编程语言 > C语言/C++

[C++] 代码C风格缩进

2017-04-15 16:22 302 查看
Input:

#include <stdio.h>
#include "hello.h"

unsigned int foo(){vector<vector<string>> a;return 1;}
int main() {
while(true){int a=0;for(; a<10;a++) {
if(true==true)
cout<<a;cout<<a;}}
return 0;}
Output:
#include <stdio.h>
#include "hello.h"

unsigned int foo(){
vector<vector<string>> a;
return 1;
}
int main() {
while(true){
int a=0;
for(; a<10; a++) {
if(true == true)
cout<<a;
cout<<a;
}
}
return 0;
}

SourceCode Start Here:

Function: Indents your code to a generally accepted style.

#include <algorithm>
#include <functional>
#include <iostream>
#include <fstream>
#include <stack>
#include <ctype.h>
#include <string>
#include <vector>
#include <windows.h>
using namespace std;

void removeCharacter(string &str, char c){
string::iterator new_end = remove_if(str.begin(), str.end(), bind2nd(equal_to<char>(),c));
str.erase(new_end, str.end());
}

bool inBracket(string str, string::size_type current_pos){
stack<char> tmpStack;
for(string::size_type pos(0); pos!=current_pos; pos++){
if(str.at(pos) == '(') {
tmpStack.push('(');
} else if(str.at(pos) == ')') {
tmpStack.pop();
} else {
continue;
}
}
if(tmpStack.empty()){
return false;
}
return true;
}

void replace_all_string(string& str, const string old_value, const string new_value, int add_condition=0){
switch(add_condition){
case 0: // force replacement without check
for(string::size_type pos(0); pos!=string::npos; pos+=new_value.length()){
if((pos=str.find(old_value,pos))!=string::npos){
str.replace(pos, old_value.length(), new_value);
} else {
break;
}
}
break;
case 1: // Right return Check
for(string::size_type pos(0); pos!=string::npos; pos+=new_value.length()){
if((pos=str.find(old_value,pos))!=string::npos){
if(str[pos+1]!='\r' && str[pos+1]!='\n' && str[pos+1]!=';')
str.replace(pos, old_value.length(), new_value);
} else {
break;
}
}
break;
case 2: // semicolon
for(string::size_type pos(0); pos!=string::npos; pos+=new_value.length()){
if((pos=str.find(old_value,pos))!=string::npos){
if(!inBracket(str, pos)){
if(str[pos+1]!='\r' && str[pos+1]!='\n')
str.replace(pos, old_value.length(), new_value);
}
else if(str[pos+1] != ' ')
str.replace(pos, old_value.length(), old_value+" ");
else
continue;
} else {
break;
}
}
break;
case 3: // Operators' Block existence check
for(string::size_type pos(0); pos!=string::npos; pos+=new_value.length()){
if((pos=str.find(old_value,pos))!=string::npos){
if((old_value.length()>=2 || (str[pos]!=str[pos+1] && str[pos]!=str[pos-1]))
&& str[pos-1] != ' ' && str[pos+1] != ' ')
str.replace(pos, old_value.length(), new_value);
} else {
break;
}
}
break;
default:
break;
}
}

// We didn't list all of the keyword of C standard Lib.
// If u want, just add them into the suitable string vector.
// **Please Notice**
// We didn't do any syntax check before reconfiguration.
void Normalization(vector<string> &fileStringVector){
cout << "Please Notice:" << endl;
cout << "We didn't do any syntax check before reconfiguration.\n" << endl;
string operators[] = {"+", "-", "%", "&", "!=", "==", "&&", "||"};
string keywords_rightreturn[] = {"{", "}"};
int dim_operators = sizeof(operators) / sizeof(operators[0]);
int dim_keywords_rightreturn = sizeof(keywords_rightreturn) / sizeof(keywords_rightreturn[0]);
vector<string>::iterator iter;
for(iter = fileStringVector.begin(); iter != fileStringVector.end(); iter++){
for(int i = 0; i < dim_operators; i++){
replace_all_string(*iter, operators[i], " "+operators[i]+" ", 3);
}
for(int i = 0; i < dim_keywords_rightreturn; i++){
replace_all_string(*iter, keywords_rightreturn[i], keywords_rightreturn[i]+"\n", 1);
}
replace_all_string(*iter, ";", ";\n", 2);
}
}

void Indentation(vector<string> &fileStringVector){
vector<string>::iterator iter;
int depth = 0;
for(iter = fileStringVector.begin(); iter != fileStringVector.end(); iter++){
int loc_r = (*iter).rfind("}", (*iter).length()-1);
if(loc_r != string::npos){
depth -= 1;
}
for(int i = 0; i < depth; i++){
*iter = "\t" + *iter;
}
int loc_l = (*iter).find("{", 0);
if(loc_l != string::npos){
depth += 1;
}
int loc = (*iter).find(")", 0);
if(loc != string::npos &&
((*iter).find("for", 0) != string::npos ||
(*iter).find("if", 0) != string::npos ||
(*iter).find("while", 0) != string::npos ||
(*iter).find("else if", 0) != string::npos)){
if( (*iter).find("{") == string::npos){
depth += 1;
iter++;
if((*iter).find("{", 0) != string::npos){
depth -= 1;
for(int i = 0; i < depth; i++){
*iter = "\t" + *iter;
}
depth +=1;
} else {
for(int i = 0; i < depth; i++){
*iter = "\t" + *iter;
}
depth -=1;
}
}
}
if((*iter).find("else") != string::npos && (*iter).find("{") == string::npos){
depth += 1;
iter++;
if((*iter).find("{", 0) != string::npos){
depth -= 1;
for(int i = 0; i < depth; i++){
*iter = "\t" + *iter;
}
depth +=1;
} else {
for(int i = 0; i < depth; i++){
*iter = "\t" + *iter;
}
depth -=1;
}
}
}
}

main.cpp
/*
* Author : shuanholmes@outlook.com
*/

#include "code_indentation.hpp"

int main(){
char inputFilePath[] = "./data/test.c";
char outputFilePath[] = "./data/normalization_test.c";
ifstream inFile(inputFilePath,ios::in|ios::binary);
ofstream tmp_outFile("./data/tmp.c",ios::out|ios::binary);
ofstream outFile(outputFilePath,ios::out|ios::binary);
vector<string> fileStringVector;
string tmpString;
// readlines -> vector
while(getline(inFile, tmpString)){
replace_all_string(tmpString, " ", "\t"); // 4*block -> '\t'
removeCharacter(tmpString, '\t');
replace_all_string(tmpString, " ", "\t"); // 3*block -> '\t'
removeCharacter(tmpString, '\t');
replace_all_string(tmpString, " ", "\t"); // 2*block -> '\t'
removeCharacter(tmpString, '\t');
fileStringVector.push_back(tmpString);
}
Normalization(fileStringVector);
vector<string>::iterator iter;
for(iter = fileStringVector.begin(); iter != fileStringVector.end(); iter++ ){
tmp_outFile << *iter << "\n";
// cout << *iter << "\n";
}
// tmp.c temporary file
inFile.close();
tmp_outFile.close();
fileStringVector.clear();
ifstream tmp_inFile("./data/tmp.c",ios::in|ios::binary);
while(getline(tmp_inFile, tmpString)){
fileStringVector.push_back(tmpString);
}
Indentation(fileStringVector);
for(iter = fileStringVector.begin(); iter != fileStringVector.end(); iter++ ){
replace_all_string(*iter, "\t", " ");
outFile << *iter << "\n";
cout << *iter << endl;
}
tmp_inFile.close();
outFile.close();
// remove("./data/tmp.c");
r
c809
eturn 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 C++ 代码缩进