C++程序之高亮显示C++代码

运行结果

C++程序之高亮显示C++代码_第1张图片

 源码

#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;//The namespace.

int top = 0;
int lines = 0;
bool inDoubleQuote;
bool inSingleQuote;
bool isPreprocessor;
bool isStringInPreprocessor;
bool isSingleLineComment;
bool isMoreLinesComment;
/*The variables.*/
char word[101];
string str[1000001];
string reservedWords[56] =
{
    "if", "else", "while", "signed", "throw", "union", "this",
    "int", "char", "double", "unsigned", "const", "goto", "virtual",
    "for", "float", "break", "auto", "class", "operator", "case",
    "do", "long", "typedef", "static", "friend", "template", "default",
    "new", "void", "register", "extern", "return", "enum", "inline",
    "try", "short", "continue", "sizeof", "switch", "private", "protected",
    "asm", "catch", "delete", "public", "volatile", "struct", "using",
    "namespace", "bool", "true", "false", "not", "and", "or"
};

bool IsReservedWord();
bool IsNumber();

int main()
{
    system("color F0");
    freopen("testIO.cpp", "r", stdin);
    string s;
    while (getline(cin, s))
    {
        str[lines] = s;
        lines++;
    }
    fclose(stdin);
    COORD COORDSIZE{ 80, (short)(lines + 1) };
    SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), COORDSIZE);
    isMoreLinesComment = false;
    for (int i = 0; i < lines; i++)
    {
        CONSOLE_SCREEN_BUFFER_INFO consoleScreenBufferSize;
        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleScreenBufferSize);
        if (consoleScreenBufferSize.dwSize.X < str[i].length() + 4)
        {
            COORD COORDsize2 {
            (short)(str[i].length() + 5), consoleScreenBufferSize.dwSize.Y};
            SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), COORDsize2);
        }
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
        printf("%3d ", i + 1);
        isSingleLineComment = false;
        top = 0;
        inDoubleQuote = false;
        inSingleQuote = false;
        isPreprocessor = false;
        isStringInPreprocessor = false;
        memset(word, 0, sizeof(word));
        for (int j = 0; j < str[i].length(); j++)
        {
            if (not(isSingleLineComment or isMoreLinesComment))
            {
                if (inDoubleQuote == false and inSingleQuote == false)
                {
                    if (str[i][j] == '/' and j + 1 < str[i].length() and str[i][j + 1] == '/')
                    {
                        isSingleLineComment = true;
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
                        cout << str[i][j];
                        continue;
                    }
                    else if (str[i][j] == '/' and j + 1 < str[i].length() and str[i][j + 1] == '*')
                    {
                        isMoreLinesComment = true;
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
                        cout << str[i][j];
                        continue;
                    }
                }
                if (inDoubleQuote == false and inSingleQuote == false and isPreprocessor == false)
                {
                    if ((str[i][j] >= 'a' and str[i][j] <= 'z') or (str[i][j] >= 'A' and str[i][j] <= 'Z') or (str[i][j] >= '0' and str[i][j] <= '9'))
                    {
                        top++;
                        word[top] = str[i][j];
                    }
                    else
                    {
                        if (top > 0)
                        {
                            if (IsReservedWord())
                            {
                                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
                            }
                            else if (IsNumber())
                            {
                                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
                            }
                            else
                            {
                                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0 | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
                            }
                            for (int k = 0; k < top; k++)
                            {
                                cout << word[k];
                            }
                            top = 0;
                        }
                        if (str[i][j] == '\"' and inSingleQuote == false)
                        {
                            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
                            cout << str[i][j];
                            inDoubleQuote = !inDoubleQuote;
                        }
                        else if (str[i][j] != '#')
                        {
                            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0 | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
                            if (inDoubleQuote == false and str[i][j] == '\'')
                            {
                                inSingleQuote = !inSingleQuote;
                            }
                            cout << str[i][j];
                        }
                        else
                        {
                            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
                            cout << str[i][j];
                            isPreprocessor = true;
                        }
                    }
                }
                else if (isPreprocessor == false)
                {
                    if (str[i][j] == '\"' and inDoubleQuote == true)
                    {
                        inDoubleQuote = !inDoubleQuote;
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY | BACKGROUND_RED);
                    }
                    else if (str[i][j] == '\'' and inSingleQuote == true)
                    {
                        inSingleQuote = !inSingleQuote;
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0 | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
                    }
                    cout << str[i][j];
                }
                else
                {
                    if (not isStringInPreprocessor)
                    {
                        if (str[i][j] == '<' or str[i][j] == '\"')
                        {
                            isStringInPreprocessor = true;
                            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY | BACKGROUND_RED);
                        }
                        else
                        {
                            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY | BACKGROUND_RED);
                        }
                        cout << str[i][j];
                    }
                    else
                    {
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | BACKGROUND_RED | BACKGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE);
                        cout << str[i][j];
                        if (str[i][j] == '>' or str[i][j] == '\"')
                        {
                            isStringInPreprocessor = false;
                        }
                    }

                }
            }
            else
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY | BACKGROUND_RED);
                cout << str[i][j];
                if (j > 0 and str[i][j - 1] == '*' and str[i][j] == '/')
                {
                    isMoreLinesComment = false;
                }
            }

        }
        if (top > 0)
        {
            if (IsReservedWord())
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
            }
            else if (IsNumber())
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
            }
            else
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0 | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
            }
            for (int k = 0; k < top; k++)
            {
                cout << word[k];
            }
            top = 0;
        }
        cout << endl;
    }
    _getch();
    return 0;
}

bool IsReservedWord()
{
    for (int i = 0; i < top; i++)
    {
        word[i] = word[i + 1];
    }
    word[top] = 0;
    for (int i = 0; i < 56; i++)
    {
        if (word == reservedWords[i])
        {
            return true;
        }
    }
    return false;
}

bool IsNumber()
{
    for (int i = 0; i < top; i++)
    {
        if (not(word[i] >= '0' and word[i] <= '9'))
        {
            return false;
        }
    }
    return true;
}

你可能感兴趣的:(C/C++,c++)