Skip to content

Commit 97d65de

Browse files
committed
add challenge 2 cpp
1 parent d0c21ae commit 97d65de

File tree

18 files changed

+1169
-0
lines changed

18 files changed

+1169
-0
lines changed

cpp/02-exercices/Challenge2CsvProcessor/ChallengeInstruction.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Challenge instructions for CsvProcessor solution
2+
3+
## Prerequisites
4+
1. Open Visual Studio (Preview) 2022
5+
2. Open the solution `Code\CsvProcessor.sln`
6+
3. Press F5 to compile and run the solution
7+
8+
### Observations
9+
10+
Notice that the console shows multiple lines of a CSV file.
11+
Each line is broken down into fields. The algorithm correctly handles
12+
double-quotes fields and escaped double-quotes in fields.
13+
14+
Sample output:
15+
```
16+
Adrienne,,D'Alencourt,987432,adale@acme.com,+33 9 12 45 56 78,Sales,EMEA,105603,1/31/2019,"5'11"""
17+
[Adrienne] [] [D'Alencourt] [987432] [adale@acme.com] [+33 9 12 45 56 78] [Sales] [EMEA] [105603] [1/31/2019] [5'11"]
18+
````
19+
20+
## Step 1: Add comment on file
21+
22+
1. Open `CsvProcessor.cs` file
23+
2. Use CTRL+A to select all the content in the file
24+
3. Type `/doc` to add comments
25+
26+
### Observations
27+
28+
A comment is created at the top of the file.
29+
30+
31+
## Step 2: Use Copilot to explain some code
32+
33+
1. Open `CsvProcessor.cs` file
34+
2. Select the two `import` lines towards the top of the file
35+
3. Type `/explain`
36+
37+
### Observations
38+
39+
Copilot explains the purpose of the `import` statements -- a new keyword in C++ 20.
40+
Copilot also provide additional hyperlinks at the end of the explanation
41+
for further exploration.
42+
43+
## Step 3: Let Copilot generate code from a comment
44+
45+
1. Open `CsvProcessor.cs` file
46+
2. At the top of the `main` function, place the cusor at the end of the line
47+
that reads `//TODO3` and press ENTER
48+
3. Press TAB to accept the suggestion from Copilot
49+
50+
51+
### Observations
52+
53+
When you type comments before writing a section of code, Copilot can automatically
54+
generate the code for you. If you like the suggestion, you can accept it by pressing TAB.
55+
If not, keep typing and Copilot will continue to suggest code.
56+
That way, Copilot integrates seemlessly with your workflow.
57+
58+
## Step 4: Add comments to a block of code
59+
60+
1. Open `CsvProcessor.cs` file
61+
2. In the `main` function, select the entire `for` loop.
62+
Hint: You can press the Down arrow in the margin to "roll in" the loop
63+
in one line, then select the entire line
64+
3. Tell Copilot to "add comments to the selected code"
65+
66+
### Observations
67+
68+
Copilot comments each meaningful block of code. This is a great way to
69+
document existing code, or to help you understand code that you didn't write.
70+
71+
72+
## Step 5: Create automated tests
73+
74+
1. Open the Test Explorer window by selecting the menu item `View` > `Test Explorer`
75+
2. Run the tests by right-clicking on `LexerTest` and selecting `Run` in the context menu
76+
3. Open `LexerTest.cpp` file 4. Uncomment the lines `TEST_METHOD(...)` one by one and press ENTER to let Copilot generate the test code for you 5. Compile with F6 and run all tests again6. Create additional test methods with meaningful names in order to guide Copilot
77+
### Observations
78+
79+
Copilot generates the test code based on several assumptions. In most cases,
80+
the generated test code passes. However, you may need to adjust the test code
81+
for edge cases or to test specific scenarios.
82+
83+
## Step 6: Create more automated tests
84+
85+
1. Open the Test Explorer window by selecting the menu item `View` > `Test Explorer`
86+
2. Run the tests by right-clicking on `ParserTest` and selecting `Run` in the context menu
87+
3. Open `ParserTest.cpp` file 4. Uncomment the lines `TEST_METHOD(...)` one by one and press ENTER to let Copilot generate the test code for you 5. Compile with F6 and run all tests again6. Create additional test methods with meaningful names in order to guide Copilot
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.11.34929.205
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CsvProcessor", "CsvProcessor\CsvProcessor.vcxproj", "{4F188729-348D-436C-97B5-1E83682B6F17}"
7+
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9690C094-7CB0-4454-A7A4-0164AC57141F}"
9+
ProjectSection(SolutionItems) = preProject
10+
Employee.csv = Employee.csv
11+
EndProjectSection
12+
EndProject
13+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LexerTest", "LexerTest\LexerTest.vcxproj", "{40362D05-1A0E-4CA7-B56A-B12E656054EF}"
14+
EndProject
15+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ParserTest", "ParserTest\ParserTest.vcxproj", "{0F6227BA-7641-4D7C-92B0-AE72405E0DB1}"
16+
EndProject
17+
Global
18+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
19+
Debug|x64 = Debug|x64
20+
Debug|x86 = Debug|x86
21+
Release|x64 = Release|x64
22+
Release|x86 = Release|x86
23+
EndGlobalSection
24+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
25+
{4F188729-348D-436C-97B5-1E83682B6F17}.Debug|x64.ActiveCfg = Debug|x64
26+
{4F188729-348D-436C-97B5-1E83682B6F17}.Debug|x64.Build.0 = Debug|x64
27+
{4F188729-348D-436C-97B5-1E83682B6F17}.Debug|x86.ActiveCfg = Debug|Win32
28+
{4F188729-348D-436C-97B5-1E83682B6F17}.Debug|x86.Build.0 = Debug|Win32
29+
{4F188729-348D-436C-97B5-1E83682B6F17}.Release|x64.ActiveCfg = Release|x64
30+
{4F188729-348D-436C-97B5-1E83682B6F17}.Release|x64.Build.0 = Release|x64
31+
{4F188729-348D-436C-97B5-1E83682B6F17}.Release|x86.ActiveCfg = Release|Win32
32+
{4F188729-348D-436C-97B5-1E83682B6F17}.Release|x86.Build.0 = Release|Win32
33+
{40362D05-1A0E-4CA7-B56A-B12E656054EF}.Debug|x64.ActiveCfg = Debug|x64
34+
{40362D05-1A0E-4CA7-B56A-B12E656054EF}.Debug|x64.Build.0 = Debug|x64
35+
{40362D05-1A0E-4CA7-B56A-B12E656054EF}.Debug|x86.ActiveCfg = Debug|Win32
36+
{40362D05-1A0E-4CA7-B56A-B12E656054EF}.Debug|x86.Build.0 = Debug|Win32
37+
{40362D05-1A0E-4CA7-B56A-B12E656054EF}.Release|x64.ActiveCfg = Release|x64
38+
{40362D05-1A0E-4CA7-B56A-B12E656054EF}.Release|x64.Build.0 = Release|x64
39+
{40362D05-1A0E-4CA7-B56A-B12E656054EF}.Release|x86.ActiveCfg = Release|Win32
40+
{40362D05-1A0E-4CA7-B56A-B12E656054EF}.Release|x86.Build.0 = Release|Win32
41+
{0F6227BA-7641-4D7C-92B0-AE72405E0DB1}.Debug|x64.ActiveCfg = Debug|x64
42+
{0F6227BA-7641-4D7C-92B0-AE72405E0DB1}.Debug|x64.Build.0 = Debug|x64
43+
{0F6227BA-7641-4D7C-92B0-AE72405E0DB1}.Debug|x86.ActiveCfg = Debug|Win32
44+
{0F6227BA-7641-4D7C-92B0-AE72405E0DB1}.Debug|x86.Build.0 = Debug|Win32
45+
{0F6227BA-7641-4D7C-92B0-AE72405E0DB1}.Release|x64.ActiveCfg = Release|x64
46+
{0F6227BA-7641-4D7C-92B0-AE72405E0DB1}.Release|x64.Build.0 = Release|x64
47+
{0F6227BA-7641-4D7C-92B0-AE72405E0DB1}.Release|x86.ActiveCfg = Release|Win32
48+
{0F6227BA-7641-4D7C-92B0-AE72405E0DB1}.Release|x86.Build.0 = Release|Win32
49+
EndGlobalSection
50+
GlobalSection(SolutionProperties) = preSolution
51+
HideSolutionNode = FALSE
52+
EndGlobalSection
53+
GlobalSection(ExtensibilityGlobals) = postSolution
54+
SolutionGuid = {E911D580-46E9-4E93-9849-ABE6599C5831}
55+
EndGlobalSection
56+
EndGlobal
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// CsvProcessor.cpp : This file contains the 'main' function. Program execution begins and ends there.
2+
//
3+
//TODO1: Use /doc to document the file.
4+
5+
#include <iostream>
6+
#include <fstream>
7+
#include <filesystem>
8+
#include <format>
9+
#include <string>
10+
//TODO2: Select the next two import lines and use /explain.
11+
import Lexer;
12+
import Parser;
13+
14+
namespace fs = std::filesystem;
15+
16+
int main(int argc, char* argv[])
17+
{
18+
// Check number of arguments. At least a file name is required, several file names are supported.
19+
//TODO3: Press ENTER after this comment and let Copilot generate the code for you.
20+
21+
//TODO4: Select the entire for loop and tell Copilot to "add comments to the selected code".
22+
for (int pos = 1; pos < argc; pos++)
23+
{
24+
std::string_view filename = argv[pos];
25+
if (!fs::exists(filename))
26+
{
27+
std::cout << std::format(R"(WARNING: File "{}" does not exist. Skipping...)", argv[pos]) << std::endl;
28+
continue;
29+
}
30+
31+
if (fs::is_directory(filename))
32+
{
33+
std::cout << std::format(R"(WARNING: "{}" is a directory. Skipping)", argv[pos]) << std::endl;
34+
continue;
35+
}
36+
37+
std::ifstream file(argv[pos], std::ios::in);
38+
if (!file.is_open())
39+
{
40+
std::cout << std::format(R"(WARNING: Could not open file "{}")", argv[pos]) << std::endl;
41+
continue;
42+
}
43+
44+
std::string line;
45+
while (std::getline(file, line))
46+
{
47+
std::cout << line << std::endl;
48+
Parser parser(line);
49+
50+
while (parser.NextField())
51+
{
52+
std::cout << std::format("[{}] ", parser.Value());
53+
}
54+
std::wcout << std::endl;
55+
56+
if (parser.Error())
57+
{
58+
std::cout << std::format("ERROR: {}", parser.ErrorMessage()) << std::endl;
59+
}
60+
61+
std::cout << std::endl;
62+
}
63+
64+
file.close();
65+
}
66+
67+
return 0;
68+
}
69+
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>17.0</VCProjectVersion>
23+
<Keyword>Win32Proj</Keyword>
24+
<ProjectGuid>{4f188729-348d-436c-97b5-1e83682b6f17}</ProjectGuid>
25+
<RootNamespace>CsvProcessor</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>Application</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v143</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>Application</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v143</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>Application</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v143</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>Application</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v143</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
74+
<OutDir>$(SolutionDir)$(PlatformTarget)\$(Configuration)\</OutDir>
75+
<IntDir>$(PlatformTarget)\$(Configuration)\</IntDir>
76+
</PropertyGroup>
77+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
78+
<OutDir>$(SolutionDir)$(PlatformTarget)\$(Configuration)\</OutDir>
79+
<IntDir>$(PlatformTarget)\$(Configuration)\</IntDir>
80+
</PropertyGroup>
81+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
82+
<OutDir>$(SolutionDir)$(PlatformTarget)\$(Configuration)\</OutDir>
83+
<IntDir>$(PlatformTarget)\$(Configuration)\</IntDir>
84+
</PropertyGroup>
85+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
86+
<OutDir>$(SolutionDir)$(PlatformTarget)\$(Configuration)\</OutDir>
87+
<IntDir>$(PlatformTarget)\$(Configuration)\</IntDir>
88+
</PropertyGroup>
89+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
90+
<ClCompile>
91+
<WarningLevel>Level3</WarningLevel>
92+
<SDLCheck>true</SDLCheck>
93+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
94+
<ConformanceMode>true</ConformanceMode>
95+
<LanguageStandard>stdcpp20</LanguageStandard>
96+
</ClCompile>
97+
<Link>
98+
<SubSystem>Console</SubSystem>
99+
<GenerateDebugInformation>true</GenerateDebugInformation>
100+
</Link>
101+
</ItemDefinitionGroup>
102+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
103+
<ClCompile>
104+
<WarningLevel>Level3</WarningLevel>
105+
<FunctionLevelLinking>true</FunctionLevelLinking>
106+
<IntrinsicFunctions>true</IntrinsicFunctions>
107+
<SDLCheck>true</SDLCheck>
108+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
109+
<ConformanceMode>true</ConformanceMode>
110+
<LanguageStandard>stdcpp20</LanguageStandard>
111+
</ClCompile>
112+
<Link>
113+
<SubSystem>Console</SubSystem>
114+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
115+
<OptimizeReferences>true</OptimizeReferences>
116+
<GenerateDebugInformation>true</GenerateDebugInformation>
117+
</Link>
118+
</ItemDefinitionGroup>
119+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
120+
<ClCompile>
121+
<WarningLevel>Level3</WarningLevel>
122+
<SDLCheck>true</SDLCheck>
123+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
124+
<ConformanceMode>true</ConformanceMode>
125+
<LanguageStandard>stdcpp20</LanguageStandard>
126+
</ClCompile>
127+
<Link>
128+
<SubSystem>Console</SubSystem>
129+
<GenerateDebugInformation>true</GenerateDebugInformation>
130+
</Link>
131+
</ItemDefinitionGroup>
132+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
133+
<ClCompile>
134+
<WarningLevel>Level3</WarningLevel>
135+
<FunctionLevelLinking>true</FunctionLevelLinking>
136+
<IntrinsicFunctions>true</IntrinsicFunctions>
137+
<SDLCheck>true</SDLCheck>
138+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
139+
<ConformanceMode>true</ConformanceMode>
140+
<LanguageStandard>stdcpp20</LanguageStandard>
141+
</ClCompile>
142+
<Link>
143+
<SubSystem>Console</SubSystem>
144+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
145+
<OptimizeReferences>true</OptimizeReferences>
146+
<GenerateDebugInformation>true</GenerateDebugInformation>
147+
</Link>
148+
</ItemDefinitionGroup>
149+
<ItemGroup>
150+
<ClCompile Include="CsvProcessor.cpp" />
151+
<ClCompile Include="Lexer.ixx" />
152+
<ClCompile Include="Parser.ixx" />
153+
</ItemGroup>
154+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
155+
<ImportGroup Label="ExtensionTargets">
156+
</ImportGroup>
157+
</Project>

0 commit comments

Comments
 (0)