From 41e694d69a147604c1582e0046828018d1f77e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=BF=E4=BF=9E=E5=98=89?= Date: Mon, 12 Oct 2015 08:46:34 +0800 Subject: [PATCH] =?UTF-8?q?0.9=E6=94=AF=E6=8C=81=E5=85=88=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=B3=A8=E9=87=8A=E5=90=8E=E5=86=8D=E6=9F=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 0.9/0.9.sln | 28 +++++ 0.9/0.9/0.9.vcxproj | 154 +++++++++++++++++++++++++ 0.9/0.9/0.9.vcxproj.filters | 22 ++++ 0.9/0.9/main.c | 224 ++++++++++++++++++++++++++++++++++++ README.md | 7 +- 5 files changed, 433 insertions(+), 2 deletions(-) create mode 100644 0.9/0.9.sln create mode 100644 0.9/0.9/0.9.vcxproj create mode 100644 0.9/0.9/0.9.vcxproj.filters create mode 100644 0.9/0.9/main.c diff --git a/0.9/0.9.sln b/0.9/0.9.sln new file mode 100644 index 0000000..810a11b --- /dev/null +++ b/0.9/0.9.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.23107.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0.9", "0.9\0.9.vcxproj", "{6BE3BCA8-6DFC-4687-90B5-D36192DE9DB0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6BE3BCA8-6DFC-4687-90B5-D36192DE9DB0}.Debug|x64.ActiveCfg = Debug|x64 + {6BE3BCA8-6DFC-4687-90B5-D36192DE9DB0}.Debug|x64.Build.0 = Debug|x64 + {6BE3BCA8-6DFC-4687-90B5-D36192DE9DB0}.Debug|x86.ActiveCfg = Debug|Win32 + {6BE3BCA8-6DFC-4687-90B5-D36192DE9DB0}.Debug|x86.Build.0 = Debug|Win32 + {6BE3BCA8-6DFC-4687-90B5-D36192DE9DB0}.Release|x64.ActiveCfg = Release|x64 + {6BE3BCA8-6DFC-4687-90B5-D36192DE9DB0}.Release|x64.Build.0 = Release|x64 + {6BE3BCA8-6DFC-4687-90B5-D36192DE9DB0}.Release|x86.ActiveCfg = Release|Win32 + {6BE3BCA8-6DFC-4687-90B5-D36192DE9DB0}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/0.9/0.9/0.9.vcxproj b/0.9/0.9/0.9.vcxproj new file mode 100644 index 0000000..007bede --- /dev/null +++ b/0.9/0.9/0.9.vcxproj @@ -0,0 +1,154 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {6BE3BCA8-6DFC-4687-90B5-D36192DE9DB0} + Win32Proj + My09 + 8.1 + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/0.9/0.9/0.9.vcxproj.filters b/0.9/0.9/0.9.vcxproj.filters new file mode 100644 index 0000000..6cedf04 --- /dev/null +++ b/0.9/0.9/0.9.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + \ No newline at end of file diff --git a/0.9/0.9/main.c b/0.9/0.9/main.c new file mode 100644 index 0000000..86b314f --- /dev/null +++ b/0.9/0.9/main.c @@ -0,0 +1,224 @@ +#include +#include +#include +#define MAXLINELENGTH 200 +typedef struct Line +{ + int lineNumber; + char lineContent[200]; + struct Line * next; +}Line; +main() +{ + FILE *fp; + errno_t err; + int fileLineNumber = 0, lineLength = 0; + int i = 0, j = 0, k = 0, t = 0, q = 0; + int braceCount = 0; + int parenthesisCount = 0, squareBracketCount = 0, angleBracketCount = 0, singleQuotationMarkCount = 0, doubleQuotationMarkCount = 0; + int wrongNumber = 0; + int multilineCommentsFlag = 0, multilineCommentsLineFlag = 0; + char str[MAXLINELENGTH]; + Line * lp = NULL, *p = NULL; + char filename[] = "D:\\test.c"; +loop:;// Open for read (will fail if file "D:\\test.c" does not exist). + err = fopen_s(&fp, filename, "r"); + if (err == 0) + { + printf("The file 'test.c' was opened.\n"); + } + else + { + printf("The file 'test.c' was not opened.\n"); + goto loop; + } + //Create a linked list to store each line and line number. + lp = (Line *)malloc(sizeof(Line)); + p = lp; + while (!feof(fp)) + { + fileLineNumber++; + fgets(str, MAXLINELENGTH, fp); + if (str[strlen(str) - 1] == '\n') + { + str[strlen(str) - 1] = '\0'; + } + strcpy_s(p->lineContent, MAXLINELENGTH, str); + p->lineNumber = fileLineNumber; + p->next = (Line *)malloc(sizeof(Line)); + p = p->next; + } + free(p); + //Put out the file with line number attached. + p = lp; + printf("Please comform the content of the file.\n"); + for (i = 0; i < fileLineNumber; i++) + { + printf("%4d %s\n", p->lineNumber, p->lineContent); + p = p->next; + } + //Let user conform the file(will reload if users does not press y ). + printf("Press y to conform the file or any other key to load another file:"); + if (getchar() == 'y') + { + getchar(); + printf("The file has been conformed.\n"); + } + else { + getchar(); + if (fp) + { + err = fclose(fp); + if (err == 0) + { + printf("The file 'test.c' was closed.\n"); + } + else + { + printf("The file 'test.c' was not closed.\n"); + } + } + goto loop; + } + //Delete all the comments. + printf("Does the file have comments?\nPress y to conform or any other key to save the process time.:"); + if (getchar() != 'y') + { + getchar(); + } + else { + getchar(); + printf("The file after the commets deleted are listed below.\n"); + p = lp; + for (i = 0; i < fileLineNumber; i++) + { + lineLength = strlen(p->lineContent); + for (j = 0; j < lineLength; j++) + { + if (multilineCommentsFlag) + { + if (p->lineContent[j] == '*' && p->lineContent[j + 1] == '/') + { + if (multilineCommentsLineFlag == p->lineNumber) + { + for (k = j + 2, t = multilineCommentsFlag - 1; k < lineLength + 1; k++) + { + p->lineContent[t++] = p->lineContent[k]; + } + j= multilineCommentsFlag - 1; + } + else + { + for (k = j + 2, t = 0; k < lineLength + 1; k++) + { + p->lineContent[t++] = p->lineContent[k]; + } + } + multilineCommentsFlag = 0; + } + if (p->lineContent[j + 1] == '\0') + { + if (multilineCommentsLineFlag == p->lineNumber) + { + p->lineContent[multilineCommentsFlag - 1] = '\0'; + } + else + { + p->lineContent[0] = '\0'; + } + } + } + else if (p->lineContent[j] == '/') + { + if (p->lineContent[j + 1] == '/') + { + p->lineContent[j] = '\0'; + break; + } + else if (p->lineContent[j + 1] == '*') + { + multilineCommentsFlag = j + 1; + multilineCommentsLineFlag = p->lineNumber; + } + } + } + printf("%4d %s\n", p->lineNumber, p->lineContent); + p = p->next; + } + } + //Check the grammar mistakes. + p = lp; + for (i = 0; i < fileLineNumber; i++) + { + strcpy_s(str, MAXLINELENGTH, p->lineContent); + lineLength = strlen(str); + parenthesisCount = squareBracketCount = angleBracketCount = singleQuotationMarkCount = doubleQuotationMarkCount = 0; + for (j = 0; j < lineLength; j++) + { + switch ((int)str[j]) + { + case '{':braceCount++; break; + case '}':braceCount--; break; + case '(':parenthesisCount++; break; + case ')':if (parenthesisCount < 1) { printf("Missing \'(\' before \')\' in line %d.\n", i + 1); wrongNumber++; }parenthesisCount--; break; + case '[':squareBracketCount++; break; + case ']':if (squareBracketCount < 1) { printf("Missing \'[\' before \']\' in line %d.\n", i + 1); wrongNumber++; }squareBracketCount--; break; + case '<':angleBracketCount++; break; + case '>':if (angleBracketCount < 1) { printf("Missing \'[\' before \']\' in line %d.\n", i + 1); wrongNumber++; }angleBracketCount--; break; + case '\'':singleQuotationMarkCount++; break; + case '\"':doubleQuotationMarkCount++; break; + } + } + if (parenthesisCount) + { + printf("Missing \')\' in line %d.\n", i + 1); + wrongNumber++; + } + if (squareBracketCount) + { + printf("Missing \']\' in line %d.\n", i + 1); + wrongNumber++; + } + if (angleBracketCount) + { + printf("Missing \'>\' in line %d.\n", i + 1); + wrongNumber++; + } + if (singleQuotationMarkCount % 2) + { + printf("Missing \'\'\' in line %d.\n", i + 1); + wrongNumber++; + } + if (doubleQuotationMarkCount % 2) + { + printf("Missing \'\"\' in line %d.\n", i + 1); + wrongNumber++; + } + p = p->next; + } + if (braceCount > 0) + { + printf("Missing %d \'}\' in total.\n", braceCount); + wrongNumber += braceCount; + } + else if (braceCount < 0) + { + printf("Missing %d \'{\' in total.\n", braceCount*-1); + wrongNumber -= braceCount; + } + printf("%d wrong(s) in total.\nMission completed.\n", wrongNumber); + // Close fp if it is not NULL. + if (fp) + { + err = fclose(fp); + if (err == 0) + { + printf("The file 'test.c' was closed.\n"); + } + else + { + printf("The file 'test.c' was not closed.\n"); + } + } + getchar(); +} \ No newline at end of file diff --git a/README.md b/README.md index fda3b00..1e83ee6 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,8 @@ C语言编写简单的C语言语法检查 *2015.10.10* 实现了文件的打开并加行号显示让用户确认。 ####0.8 -*2015.10.11* -实现了逐行字数显示及括号错误匹配输出。 \ No newline at end of file +*2015.10.11* +实现了逐行字数显示及括号错误匹配输出。 +####0.9 +*2015.10.12* +支持先删除注释后再查错。 \ No newline at end of file