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 Homework№12/lexicalAnalysis/lexicalAnalysis.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 16
VisualStudioVersion = 16.0.31410.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lexicalAnalysis", "lexicalAnalysis\lexicalAnalysis.vcxproj", "{4C58C0AB-268F-4588-8690-F3B9D99B950C}"
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
{4C58C0AB-268F-4588-8690-F3B9D99B950C}.Debug|x64.ActiveCfg = Debug|x64
{4C58C0AB-268F-4588-8690-F3B9D99B950C}.Debug|x64.Build.0 = Debug|x64
{4C58C0AB-268F-4588-8690-F3B9D99B950C}.Debug|x86.ActiveCfg = Debug|Win32
{4C58C0AB-268F-4588-8690-F3B9D99B950C}.Debug|x86.Build.0 = Debug|Win32
{4C58C0AB-268F-4588-8690-F3B9D99B950C}.Release|x64.ActiveCfg = Release|x64
{4C58C0AB-268F-4588-8690-F3B9D99B950C}.Release|x64.Build.0 = Release|x64
{4C58C0AB-268F-4588-8690-F3B9D99B950C}.Release|x86.ActiveCfg = Release|Win32
{4C58C0AB-268F-4588-8690-F3B9D99B950C}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C864152D-6DC5-4FB3-B326-DBAFFAE504BB}
EndGlobalSection
EndGlobal
107 changes: 107 additions & 0 deletions Homework№12/lexicalAnalysis/lexicalAnalysis/LexicalAnalysis.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "LexicalAnalysis.h"
#include <string.h>

bool isDigit(char symbol)
{
return symbol <= '9' && symbol >= '0';
}

bool checkExpression(char* string)

Choose a reason for hiding this comment

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

Suggested change
bool checkExpression(char* string)
bool checkExpression(const char* string)

{
const int lenghtString = strlen(string);

Choose a reason for hiding this comment

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

Оно "length"

int state = 0;

Choose a reason for hiding this comment

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

Тут бы enum с человекочитаемыми состояниями

for (int i = 0; i < lenghtString + 1; i++)
{
switch (state)
{
case 0 :
{
if (isDigit(string[i]))
{
state = 1;
break;
}
return false;
}
case 1:
{
if (isDigit(string[i]))
{
break;
}
if (string[i] == '\0')
{
return true;
}
if (string[i] == '.')
{
state = 2;
break;
}
return false;

Choose a reason for hiding this comment

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

Не-а, по регэкспу часть с точкой не обязательна, тут может быть сразу E

}
case 2:
{
if (isDigit(string[i]))
{
state = 3;
break;
}
return false;
}
case 3:
{
if (isDigit(string[i]))
{
break;
}
if (string[i] == '\0')
{
return true;
}
if (string[i] == 'E')
{
state = 4;
break;
}
return false;
}
case 4:
{
if(isDigit(string[i]))

Choose a reason for hiding this comment

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

Suggested change
if(isDigit(string[i]))
if (isDigit(string[i]))

{
state = 6;
break;
}
if (string[i] == '+' || string[i] == '-')
{
state = 5;
break;
}
return false;
}
case 5:
{
if (isDigit(string[i]))
{
state = 6;
break;
}
return false;
}
case 6:
{
if (isDigit(string[i]))
{
break;
}
if (string[i] == '\0')
{
return true;
}
return false;
Comment on lines +98 to +102

Choose a reason for hiding this comment

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

:(

}
}
}
return false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#include <stdbool.h>

// Function to check whether an expression is a real number
bool checkExpression(char* string);
30 changes: 30 additions & 0 deletions Homework№12/lexicalAnalysis/lexicalAnalysis/Main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "TestLexicalAnalysis.h"
#include <stdio.h>
#include "ReadString.h"
#include "LexicalAnalysis.h"
#include <stdlib.h>

int main()
{
if (!testCheckTestExpression())
{
printf("Test failed");
return -1;
}
printf("enter the string\n");
int error = 0;
char* string = readString(&error);

Choose a reason for hiding this comment

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

const

if (error == 1)
{
printf("failed to enter string\n");
return -1;
}
bool result = checkExpression(string);
free(string);
if (!result)
{
printf("the entered string is not a real number\n");
return -1;
}
printf("the entered string is a real number\n");
}
37 changes: 37 additions & 0 deletions Homework№12/lexicalAnalysis/lexicalAnalysis/ReadString.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "ReadString.h"
#include <malloc.h>
#include <string.h>
#include <stdio.h>

char* readString(int* error)
{
int index = 0;
int size = 100;
char* array = calloc(size, sizeof(char));
if (array == NULL)
{
*error = 1;
return NULL;
}
char symbol = getchar();
while (symbol != '\n')
{
array[index] = symbol;
if (strlen(array) == size)
{
size = size * 2;
char* temporary = realloc(array, size * sizeof(char));
if (temporary == NULL)
{
*error = 1;
free(array);
return NULL;
}
array = temporary;
}
symbol = getchar();
index++;
}
array[index] = '\0';
return array;
}
4 changes: 4 additions & 0 deletions Homework№12/lexicalAnalysis/lexicalAnalysis/ReadString.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

// Function for entering a line with memory allocation (for entering a line of arbitrary size)
char* readString(int* error);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "TestLexicalAnalysis.h"
#include "LexicalAnalysis.h"

bool testCheckTestExpression()
{
bool firstResult = checkExpression("3");
bool secondResult = checkExpression("3.734432");
bool thirdResult = checkExpression("33412.253253");
bool fourthResult = checkExpression("234.2424E-10");
bool fifthResult = checkExpression("3.0001E+10");
bool sixthResult = checkExpression("3.");
bool seventhResult = checkExpression("3.E");
bool eighthResult = checkExpression("3324.23455E+");
bool ninthResult = checkExpression("");
bool tenthResult = checkExpression("3E-1111.1221");
return firstResult && secondResult && thirdResult && fourthResult && fifthResult
&& !sixthResult && !seventhResult && !eighthResult && !ninthResult && !tenthResult;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#include <stdbool.h>

// Function for testing the defining function is and the string is a real number
bool testCheckTestExpression();
Loading