-
Notifications
You must be signed in to change notification settings - Fork 0
Postfix form entry #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 32 commits
1a83386
2549f2f
14417f4
8d6727e
f43380d
d2884fc
e7e22d6
2f9e2a0
9a061fa
a67b184
015e87e
75fe192
5c8f0cf
d85663e
123d94e
cc6ba23
6a502c1
2ab34ad
a9965e1
58913fa
e4c55b4
e0ec8d8
c7141c5
7c6d74e
cad0914
fe7b4ae
465c325
ff4b737
a759b10
3bd1352
f6d3e25
e0515c6
18ca3e3
c2f8ecf
7fe0b6b
6dfd83e
954d78b
98a42e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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}") = "Postfixform", "Postfixform\Postfixform.vcxproj", "{15009E36-9D86-441E-A9EA-947AAC734F19}" | ||
| 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 | ||
| {15009E36-9D86-441E-A9EA-947AAC734F19}.Debug|x64.ActiveCfg = Debug|x64 | ||
| {15009E36-9D86-441E-A9EA-947AAC734F19}.Debug|x64.Build.0 = Debug|x64 | ||
| {15009E36-9D86-441E-A9EA-947AAC734F19}.Debug|x86.ActiveCfg = Debug|Win32 | ||
| {15009E36-9D86-441E-A9EA-947AAC734F19}.Debug|x86.Build.0 = Debug|Win32 | ||
| {15009E36-9D86-441E-A9EA-947AAC734F19}.Release|x64.ActiveCfg = Release|x64 | ||
| {15009E36-9D86-441E-A9EA-947AAC734F19}.Release|x64.Build.0 = Release|x64 | ||
| {15009E36-9D86-441E-A9EA-947AAC734F19}.Release|x86.ActiveCfg = Release|Win32 | ||
| {15009E36-9D86-441E-A9EA-947AAC734F19}.Release|x86.Build.0 = Release|Win32 | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {C330BC54-81A6-4E6B-9775-51E396ACC586} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #include "../../Stack/Stack/Stack.h" | ||
| #include "Postfix.h" | ||
| #include "postfixFormTest.h" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. На самом деле этот файл называется с заглавной, так что под Linux программа не соберётся (там регистрозависимые файловые системы обычно, для них postfixFormTest.h и PostfixFormTest.h — это разные файлы). Поэтому обычно договариваются именовать либо всё с заглавной, либо всё со строчной. |
||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <errno.h> | ||
|
|
||
| int main() | ||
| { | ||
| if (!areTestPassingPostfixForm()) | ||
| { | ||
| printf("Test failed"); | ||
| return -1; | ||
| } | ||
| char postfixEntry[250] = { '\0' }; | ||
| printf("enter the expression in postfix form\n"); | ||
| scanf_s("%[^\n]s", postfixEntry, (unsigned)sizeof(postfixEntry)); | ||
| errno = 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не стоит это использовать для возврата ошибок. Это глобальная переменная, глобальные переменные нельзя (тем более что конкретно тут можно с ума сойти проверять, что errno не перетрётся каким-нибудь вызовом стандартной библиотеки). Отдельный параметр для кода возврата ничем не хуже, разве что более громоздкий. |
||
| const float answer = countTheExpression(postfixEntry); | ||
| if (errno == 1) | ||
| { | ||
| printf("Stack is empty"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я как пользователь функции ничего не знаю про стек. Тут надо более высокоуровневое сообщение об ошибке выводить, типа "у вас больше операторов, чем операндов", ну или просто что некорректное выражение. |
||
| return -1; | ||
| } | ||
| if (errno == 2) | ||
| { | ||
| printf("invalid character in the expression entry"); | ||
| return -1; | ||
| } | ||
| if (errno == 3) | ||
| { | ||
| printf("Incorrect input of an expression in postfix form"); | ||
| return -1; | ||
| } | ||
| printf("%f", answer); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #include "../../Stack/Stack/Stack.h" | ||
| #include "Postfix.h" | ||
| #include <stdlib.h> | ||
| #include <errno.h> | ||
|
|
||
| float countTheExpression(char* postfixEntry) | ||
| { | ||
| Stack* head = NULL; | ||
| int counter = 0; | ||
| while (postfixEntry[counter] != '\0') | ||
| { | ||
| if (postfixEntry[counter] >= '0' && postfixEntry[counter] <= '9') | ||
| { | ||
| push(&head, (float)postfixEntry[counter] - '0'); | ||
| counter++; | ||
| continue; | ||
| } | ||
| else if (postfixEntry[counter] == ' ') | ||
| { | ||
| counter++; | ||
| continue; | ||
| } | ||
| float secondNumber = pop(&head); | ||
| if (errno == 1) | ||
| { | ||
| return 0; | ||
| } | ||
| float firstNumber = pop(&head); | ||
| if (errno == 1) | ||
| { | ||
| return 0; | ||
| } | ||
| if (postfixEntry[counter] == '-') | ||
| { | ||
| push(&head, (firstNumber - secondNumber)); | ||
| } | ||
| else if (postfixEntry[counter] == '+') | ||
| { | ||
| push(&head, firstNumber + secondNumber); | ||
| } | ||
| else if (postfixEntry[counter] == '*') | ||
| { | ||
| push(&head, firstNumber * secondNumber); | ||
| } | ||
| else if (postfixEntry[counter] == '/') | ||
| { | ||
| push(&head, firstNumber / secondNumber); | ||
| } | ||
| else | ||
| { | ||
| deleteStack(&head); | ||
| errno = 2; | ||
| return 0; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| } | ||
| counter++; | ||
| } | ||
| const float answer = pop(&head); | ||
| if (errno == 1) | ||
| { | ||
| return 0; | ||
| } | ||
| if (!isEmpty(head)) | ||
| { | ||
| deleteStack(&head); | ||
| errno = 3; | ||
| return 0; | ||
| } | ||
| return answer; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #pragma once | ||
|
|
||
| // Function for calculating the value of an expression in postfix form | ||
| float countTheExpression(char* postfixEntry); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #include "PostfixFormTest.h" | ||
| #include "Postfix.h" | ||
|
|
||
| bool areTestPassingPostfixForm() | ||
| { | ||
| char firstCorrectPostfixEntry[250] = "3 2 - 4 5 * + 2 9 - *"; | ||
| char secondCorrectPostfixEntry[250] = "2 3 - 4 5 * +"; | ||
| char thirdCorrectPostfixEntry[250] = "9 6 - 1 2 + *"; | ||
| char fourthCorrectPostfixEntry[250] = "9 6 - 5 2 / +"; | ||
| char fifthCorrectPostfixEntry[250] = "9 6 - 3 -"; | ||
|
|
||
| char firstIncorrectPostfixEntry[250] = "--35 a-"; | ||
| char secondIncorrectPostfixEntry[250] = "456 26"; | ||
| char thirdIncorrectPostfixEntry[250] = "34 - 345"; | ||
| char fourthIncorrectPostfixEntry[250] = "12 - 34 + 4"; | ||
| char fifthIncorrectPostfixEntry[250] = "1234 - 12"; | ||
|
|
||
| return countTheExpression(firstCorrectPostfixEntry) == -147 | ||
| && countTheExpression(secondCorrectPostfixEntry) == 19 | ||
| && countTheExpression(thirdCorrectPostfixEntry) == 9 | ||
| && countTheExpression(fourthCorrectPostfixEntry) == 5.5 | ||
| && countTheExpression(fifthCorrectPostfixEntry) == 0 | ||
|
|
||
| && countTheExpression(firstIncorrectPostfixEntry) == 0 | ||
| && countTheExpression(secondIncorrectPostfixEntry) == 0 | ||
| && countTheExpression(thirdIncorrectPostfixEntry) == 0 | ||
| && countTheExpression(fourthIncorrectPostfixEntry) == 0 | ||
| && countTheExpression(fifthIncorrectPostfixEntry) == 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #pragma once | ||
| #include <stdbool.h> | ||
|
|
||
| // Function for testing a function that considers the value of an expression in postfix form | ||
| bool areTestPassingPostfixForm(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| <?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> | ||
| <ItemGroup> | ||
| <ClCompile Include="..\..\Stack\Stack\Stack.c" /> | ||
| <ClCompile Include="Main.c" /> | ||
| <ClCompile Include="Postfix.c" /> | ||
| <ClCompile Include="PostfixFormTest.c" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ClInclude Include="..\..\Stack\Stack\Stack.h" /> | ||
| <ClInclude Include="Postfix.h" /> | ||
| <ClInclude Include="PostfixFormTest.h" /> | ||
| </ItemGroup> | ||
| <PropertyGroup Label="Globals"> | ||
| <VCProjectVersion>16.0</VCProjectVersion> | ||
| <Keyword>Win32Proj</Keyword> | ||
| <ProjectGuid>{15009e36-9d86-441e-a9ea-947aac734f19}</ProjectGuid> | ||
| <RootNamespace>Postfixform</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>v142</PlatformToolset> | ||
| <CharacterSet>Unicode</CharacterSet> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||
| <ConfigurationType>Application</ConfigurationType> | ||
| <UseDebugLibraries>false</UseDebugLibraries> | ||
| <PlatformToolset>v142</PlatformToolset> | ||
| <WholeProgramOptimization>true</WholeProgramOptimization> | ||
| <CharacterSet>Unicode</CharacterSet> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
| <ConfigurationType>Application</ConfigurationType> | ||
| <UseDebugLibraries>true</UseDebugLibraries> | ||
| <PlatformToolset>v142</PlatformToolset> | ||
| <CharacterSet>Unicode</CharacterSet> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
| <ConfigurationType>Application</ConfigurationType> | ||
| <UseDebugLibraries>false</UseDebugLibraries> | ||
| <PlatformToolset>v142</PlatformToolset> | ||
| <WholeProgramOptimization>true</WholeProgramOptimization> | ||
| <CharacterSet>Unicode</CharacterSet> | ||
| </PropertyGroup> | ||
| <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||
| <ImportGroup Label="ExtensionSettings"> | ||
| <Import Project="$(VCTargetsPath)\BuildCustomizations\ImageContentTask.props" /> | ||
| <Import Project="$(VCTargetsPath)\BuildCustomizations\lc.props" /> | ||
| <Import Project="$(VCTargetsPath)\BuildCustomizations\marmasm.props" /> | ||
| <Import Project="$(VCTargetsPath)\BuildCustomizations\masm.props" /> | ||
| <Import Project="$(VCTargetsPath)\BuildCustomizations\MeshContentTask.props" /> | ||
| <Import Project="$(VCTargetsPath)\BuildCustomizations\ShaderGraphContentTask.props" /> | ||
| </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> | ||
| <ProjectReference> | ||
| <UseLibraryDependencyInputs>false</UseLibraryDependencyInputs> | ||
| </ProjectReference> | ||
| </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>true</SDLCheck> | ||
| <PreprocessorDefinitions>_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(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> | ||
| <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
| <ImportGroup Label="ExtensionTargets"> | ||
| <Import Project="$(VCTargetsPath)\BuildCustomizations\ImageContentTask.targets" /> | ||
| <Import Project="$(VCTargetsPath)\BuildCustomizations\lc.targets" /> | ||
| <Import Project="$(VCTargetsPath)\BuildCustomizations\marmasm.targets" /> | ||
| <Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" /> | ||
| <Import Project="$(VCTargetsPath)\BuildCustomizations\MeshContentTask.targets" /> | ||
| <Import Project="$(VCTargetsPath)\BuildCustomizations\ShaderGraphContentTask.targets" /> | ||
| </ImportGroup> | ||
| </Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.