c - 减少 C 代码量

标签 c refactoring

<分区>

如何用更少的代码重构它? 这是家庭作业,使用频率分布破解凯撒密文。

我已经完成了作业,但希望它更干净。

int main(int argc, char **argv){

// first allocate some space for our input text (we will read from stdin).
char* text = (char*)malloc(sizeof(char)*TEXT_SIZE+1);
char textfreq[ALEN][2];
char map[ALEN][2];
char newtext[TEXT_SIZE];
char ch, opt, tmpc, tmpc2;
int i, j, tmpi;

// Check the CLI arguments and extract the mode: interactive or dump and store in opt.
if(!(argc == 2 && isalpha(opt = argv[1][1]) && (opt == 'i' || opt == 'd'))){
    printf("format is: '%s' [-d|-i]\n", argv[0]);
    exit(1);
}

// Now read TEXT_SIZE or feof worth of characters (whichever is smaller) and convert to uppercase as we do it.

for(i = 0, ch = fgetc(stdin); i < TEXT_SIZE && !feof(stdin); i++, ch = fgetc(stdin)){
    text[i] = (isalpha(ch)?upcase(ch):ch);
}
text[i] = '\0'; // terminate the string properly.

// Assign alphabet to one dimension of text frequency array and a counter to the other dimension

for (i = 0; i < ALEN; i++) {
    textfreq[i][0] = ALPHABET[i];
    textfreq[i][1] = 0;
}

// Count frequency of characters in the given text
for (i = 0; i < strlen(text); i++) {
    for (j = 0; j < ALEN; j++) {
        if (text[i] == textfreq[j][0]) textfreq[j][1]+=1;
    }
}

//Sort the character frequency array in descending order
for (i = 0; i < ALEN-1; i++) {
    for (j= 0; j < ALEN-i-1; j++) {
        if (textfreq[j][1] < textfreq[j+1][1]) {
            tmpi = textfreq[j][1];
            tmpc = textfreq[j][0];
            textfreq[j][1] = textfreq[j+1][1];
            textfreq[j][0] = textfreq[j+1][0];
            textfreq[j+1][1] = tmpi;
            textfreq[j+1][0] = tmpc;
        }
    }
}

//Map characters to most occurring English characters
for (i = 0; i < ALEN; i++) {
    map[i][0] = CHFREQ[i];
    map[i][1] = textfreq[i][0];
}

// Sort the map lexicographically
for (i = 0; i < ALEN-1; i++) {
    for (j= 0; j < ALEN-i-1; j++) {
        if (map[j][0] > map[j+1][0]) {
            tmpc = map[j][0];
            tmpc2 = map[j][1];
            map[j][0] = map[j+1][0];
            map[j][1] = map[j+1][1];
            map[j+1][0] = tmpc;
            map[j+1][1] = tmpc2;
        }
    }
}

if(opt == 'd'){
    decode_text(text, newtext, map);
} else {
// do option -i
}

// Print alphabet and map to stderr and the decoded text to stdout
fprintf(stderr, "\n%s\n", ALPHABET);
for (i = 0; i < ALEN; i++) {
    fprintf(stderr, "%c", map[i][1]);
}
printf("\n%s\n", newtext);
return 0;
}

最佳答案

嗯,重构 != 更少的代码。混淆有时可以减少代码,如果这是你的目标:)

重构是为了提高代码的可读性和降低复杂性。针对您的案例提出的改进建议:

  • 查看您已实现的逻辑 block ,并考虑将它们替换为内置函数通常是一个不错的起点。我相信您执行的某些排序可以用 qsort() 代替。然而,旁注,如果这是你的作业,你的导师可能是个 SCSS ,希望看到你使用 C 的内置函数在 FULL VS 中写出代码,并指责你太聪明了。 (抱歉,这里是个人历史:P)

  • 将您的逻辑工作单元移动到专用功能中,并有一个主要功能来执行编排。

关于c - 减少 C 代码量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25198591/

相关文章:

c - 使用临时数组的动态内存分配

c++ - 使用 strtok() 和 stringstream 的问题

php - 组合算法中的循环

python - 重构错误处理并在 Python 中获得正确的堆栈跟踪

java - 我怎样才能把它移到另一个类(class)

c - fread 和 fwrite 函数有问题

c - gcc静态库编译

c++ - dylib 中缺少符号

emacs - 随后运行相同的函数,产生不同的结果

sql - sql重构的工具?