我知道Visual Studio可以自动格式化,使我的方法和循环缩进正确,但我找不到设置。


当前回答

格式化选区:Ctrl+K, Ctrl+F

格式化文档:Ctrl+K, Ctrl+D

查看预定义的键盘快捷键。(这两个是编辑。FormatSelection和Edit.FormatDocument。)

macOS的注意事项

在macOS上,使用CMD⌘键而不是Ctrl键:

格式化选定内容:CMD⌘+K, CMD⌘+F 格式化文档: CMD⌘+ k, CMD⌘+ d

其他回答

Visual Studio Extension - CodeMaid是非常好的。

剪切/粘贴是另一种快速的方法(而且容易记住)。

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


int main(){
    char filename[15];

    printf("Enter file name:");
    scanf("%s", filename);
    Automat(filename);
    freq(filename);
}

int Automat(char filename[]){

    FILE*fp;
    char c;
    int state = 1;
    int duma = 0;
    fp = fopen(filename, "r");
    if (fp == NULL){
        printf("Error");
        system("pause");
    }
    while (!feof(fp)){
        c = fgetc(fp);

        switch (state){
        case 1:
            if (c == '\t' || c == '\n' || c == ' '){
                state = 3;
                break;
            }
        case 2:
            if (c >='A'|| c<='Z'){
                break;
            }

        case 3: duma++;
            state = 1;
            break;
        }
    }
    printf("Broq e : %d\n", duma);
    return 1;
}
int freq(char filename[]){

    FILE*fp;
    char c;
    int state = 1, sequence = 0;
    fp = fopen(filename, "r");
    if (fp == NULL){
        printf("Error");
        system("pause");
    }



    while (!feof(fp)){
        c = fgetc(fp);
        switch (state){
        case 1:
            if (c >= 'A' && c <= 'Z'){
                state = 2;
                break;
            }

        case 2:
            if (c == '\t' || c == '\n'||c == ' '){
                sequence++;
                state = 1;
                break;

            }
        }

    }
    printf("Sequence is : %d\n", sequence);
}

在较新的版本中,文档范围格式化的快捷方式是:Shift + Alt + F

我曾经使用这些组合。我在保存文档时自动化了这个过程。你可以尝试我的扩展格式文件保存。