Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions offsetSecondTryThirdTask/offsetSecondTryThirdTask.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "offsetSecondTryThirdTask", "offsetSecondTryThirdTask\offsetSecondTryThirdTask.vcxproj", "{ECA430AB-37C1-4EC6-BFF6-BB81509B4043}"
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
{ECA430AB-37C1-4EC6-BFF6-BB81509B4043}.Debug|x64.ActiveCfg = Debug|x64
{ECA430AB-37C1-4EC6-BFF6-BB81509B4043}.Debug|x64.Build.0 = Debug|x64
{ECA430AB-37C1-4EC6-BFF6-BB81509B4043}.Debug|x86.ActiveCfg = Debug|Win32
{ECA430AB-37C1-4EC6-BFF6-BB81509B4043}.Debug|x86.Build.0 = Debug|Win32
{ECA430AB-37C1-4EC6-BFF6-BB81509B4043}.Release|x64.ActiveCfg = Release|x64
{ECA430AB-37C1-4EC6-BFF6-BB81509B4043}.Release|x64.Build.0 = Release|x64
{ECA430AB-37C1-4EC6-BFF6-BB81509B4043}.Release|x86.ActiveCfg = Release|Win32
{ECA430AB-37C1-4EC6-BFF6-BB81509B4043}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A3E58929-7DA2-483A-BCCE-589854B4EF84}
EndGlobalSection
EndGlobal
139 changes: 139 additions & 0 deletions offsetSecondTryThirdTask/offsetSecondTryThirdTask/lexer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include "lexer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

typedef enum Stage {
first,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

О, отступы табуляциями

second,
third,
fourth,
fifth,
sixth
} Stage;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Названия состояний не мнемоничны, не отражают их назначения и по сути ничем не лучше чисел. Как в анекдоте, const int one = 1;


char* workWithFileAndReturnString(const char* nameFile, Error* errorCheck) {
FILE* file = fopen(nameFile, "r");
if (file == NULL) {
*errorCheck = errorWithFile;
return NULL;
}
char buffer[1000] = { '\0' };
char result[10000] = { '\0' };
Comment on lines +22 to +23

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это вообще не нужно, задачей было по строке сказать "да/нет", возвращать что-либо не просили

Stage stage = first;
char symbol = 0;
int indexBuffer = 0;
bool isNeedAddLast = false;
while (fscanf(file, "%c", &symbol) == 1) {
switch (stage)
{
case first:
if (symbol >= 'A' && symbol <= 'Z' || symbol == '+' || symbol == '-'
|| symbol >= '0' && symbol <= '9' || symbol == '.' || symbol == '%') {
Comment on lines +32 to +33

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сложные логические условия, тем более если планируется их переиспользовать, лучше выносить в функцию

buffer[indexBuffer] = symbol;
++indexBuffer;
stage = second;
} else {
memset(buffer, 0, strlen(buffer));
indexBuffer = 0;
stage = first;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не-а, тут надо было вернуть false, потому что строка уже не соответствует регэкспу

}
break;
case second:
if (symbol >= 'A' && symbol <= 'Z' || symbol == '+' || symbol == '-'
|| symbol >= '0' && symbol <= '9' || symbol == '.' || symbol == '%') {
buffer[indexBuffer] = symbol;
++indexBuffer;
stage = second;
}
else if (symbol == '@') {
buffer[indexBuffer] = symbol;
++indexBuffer;
stage = third;
}
else {
memset(buffer, 0, strlen(buffer));
indexBuffer = 0;
stage = first;
}
break;
case third:
if (symbol >= 'A' && symbol <= 'Z'
|| symbol >= '0' && symbol <= '9' || symbol == '-') {
buffer[indexBuffer] = symbol;
++indexBuffer;
stage = fourth;
} else {
memset(buffer, 0, strlen(buffer));
indexBuffer = 0;
stage = first;
}
break;
case fourth:
if (symbol >= 'A' && symbol <= 'Z'
|| symbol >= '0' && symbol <= '9' || symbol == '-') {
buffer[indexBuffer] = symbol;
++indexBuffer;
stage = fourth;
}
else if (symbol == '.') {
buffer[indexBuffer] = symbol;
++indexBuffer;
stage = fifth;
}
else {
memset(buffer, 0, strlen(buffer));
indexBuffer = 0;
stage = first;
}
break;
case fifth:
if (symbol >= 'A' && symbol <= 'Z') {
buffer[indexBuffer] = symbol;
++indexBuffer;
stage = sixth;
isNeedAddLast = true;
} else if (symbol >= '0' && symbol <= '9' || symbol == '-') {
buffer[indexBuffer] = symbol;
++indexBuffer;
stage = fourth;
} else {
memset(buffer, 0, strlen(buffer));
indexBuffer = 0;
stage = first;
}
break;
case sixth:
if (symbol >= '0' && symbol <= '9' || symbol == '-') {
buffer[indexBuffer] = symbol;
++indexBuffer;
stage = fourth;
isNeedAddLast = false;
} else if (symbol == '.') {
buffer[indexBuffer] = symbol;
++indexBuffer;
stage = fifth;
isNeedAddLast = false;
} else {
strcat(result, buffer);
memset(buffer, 0, strlen(buffer));
indexBuffer = 0;
if (symbol >= 'A' && symbol <= 'Z' || symbol == '+' || symbol == '-'
|| symbol >= '0' && symbol <= '9' || symbol == '.' || symbol == '%') {
buffer[indexBuffer] = symbol;
++indexBuffer;
stage = first;
}
stage = first;
isNeedAddLast = false;
}
break;
}
}
if (isNeedAddLast) {
strcat(result, buffer);
}
fclose(file);
return result;
}
13 changes: 13 additions & 0 deletions offsetSecondTryThirdTask/offsetSecondTryThirdTask/lexer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <stdio.h>

//Variable for error checking

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//Variable for error checking
// Variable for error checking

typedef enum Error {
ok,
errorWithFile,
anotherError
} Error;

//return string with emails
char* workWithFileAndReturnString(const char* nameFile, Error* errorCheck);
41 changes: 41 additions & 0 deletions offsetSecondTryThirdTask/offsetSecondTryThirdTask/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "lexer.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

bool test() {
char nameFile[] = "test.txt";
char trueAnswer[] = "A.%@A9.A";
Error errorCheck = ok;
char* result = workWithFileAndReturnString(nameFile, &errorCheck);
if (errorCheck != ok) {
return false;
}
return strcmp(trueAnswer, result) == 0;
}

int main() {
if (test()) {
printf("Test correct\n");
} else {
printf("Error\n");
return -1;
}
printf("Write the name of the file with its extension. No more than 99 symbols\n");
char nameFile[100] = { '\0' };
int checkScanf = scanf("%s", nameFile);
while (checkScanf != 1) {
while (getchar() != '\n') {
}

checkScanf = scanf("%s", nameFile);
}
Error errorCheck = ok;
char* result = workWithFileAndReturnString(nameFile, &errorCheck);
if (errorCheck != ok) {
printf("Error\n");
return -1;
}
printf("%s\n", result);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{eca430ab-37c1-4ec6-bff6-bb81509b4043}</ProjectGuid>
<RootNamespace>offsetSecondTryThirdTask</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>false</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="lexer.c" />
<ClCompile Include="main.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="lexer.h" />
</ItemGroup>
<ItemGroup>
<Text Include="test.txt" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Loading