Skip to content

Commit bd19e11

Browse files
committed
solve 1106
1 parent 59bdd1b commit bd19e11

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

Diff for: 1106/parserBoolExprestion.sln

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "parserBoolExprestion", "parserBoolExprestion\parserBoolExprestion.csproj", "{7865BDE0-652B-4E0E-84E5-21348AC72BB1}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(SolutionProperties) = preSolution
14+
HideSolutionNode = FALSE
15+
EndGlobalSection
16+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
17+
{7865BDE0-652B-4E0E-84E5-21348AC72BB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{7865BDE0-652B-4E0E-84E5-21348AC72BB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{7865BDE0-652B-4E0E-84E5-21348AC72BB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{7865BDE0-652B-4E0E-84E5-21348AC72BB1}.Release|Any CPU.Build.0 = Release|Any CPU
21+
EndGlobalSection
22+
EndGlobal

Diff for: 1106/parserBoolExprestion/Program.cs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Solution sol = new Solution();
2+
sol.ParseBoolExpr("|(&(t,f,t),!(t))");
3+
Console.WriteLine("finished");
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

Diff for: 1106/parserBoolExprestion/solution.cs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
3+
public class Solution {
4+
public bool ParseBoolExpr(string expression) {
5+
return Parse(expression);
6+
7+
8+
}
9+
10+
private bool Parse(string expression)
11+
{
12+
switch (expression[0])
13+
{
14+
case 't':
15+
return true;
16+
case 'f':
17+
return false;
18+
case '&':
19+
foreach(string exp in ParseSubExpresions(expression.Substring(2, expression.Length - 3)))
20+
{
21+
if (!Parse(exp))
22+
{
23+
return false;
24+
}
25+
}
26+
return true;
27+
28+
case '|':
29+
foreach(string exp in ParseSubExpresions(expression.Substring(2, expression.Length - 3)))
30+
{
31+
if (Parse(exp))
32+
{
33+
return true;
34+
}
35+
}
36+
return false;
37+
case '!':
38+
return !Parse(expression.Substring(2, expression.Length - 3));
39+
}
40+
return true;
41+
}
42+
43+
private IEnumerable<string> ParseSubExpresions(string v)
44+
{
45+
int lvl = 0;
46+
string currentString = "";
47+
foreach(char c in v){
48+
if (c==','&&lvl==0){
49+
yield return currentString;
50+
currentString = "";
51+
}
52+
else{
53+
currentString+=c;
54+
}
55+
if (c=='(')
56+
{
57+
lvl++;
58+
}
59+
if (c==')'){
60+
lvl--;
61+
}
62+
}
63+
yield return currentString;
64+
65+
}
66+
}

0 commit comments

Comments
 (0)