Done Task 3 ish

master
Øyvind Skaaden 2019-02-13 13:08:28 +01:00
parent d24d0f7a1d
commit 23251dbf64
10 changed files with 143 additions and 14 deletions

View File

@ -136,6 +136,8 @@
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="Task1.cpp" />
<ClCompile Include="Task2.cpp" />
<ClCompile Include="Task3.cpp" />
</ItemGroup>
<ItemGroup>
<Text Include="grunnlov.txt" />
@ -144,6 +146,8 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="Task1.h" />
<ClInclude Include="Task2.h" />
<ClInclude Include="Task3.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -21,21 +21,33 @@
<ClCompile Include="Task1.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Task2.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Task3.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="grunnlov_iso-latin-1.txt">
<Filter>Source Files</Filter>
</Text>
<Text Include="temperatures.txt">
<Filter>Source Files</Filter>
<Filter>Resource Files</Filter>
</Text>
<Text Include="grunnlov.txt">
<Filter>Source Files</Filter>
<Filter>Resource Files</Filter>
</Text>
<Text Include="temperatures.txt">
<Filter>Resource Files</Filter>
</Text>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Task1.h">
<Filter>Source Files</Filter>
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Task2.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Task3.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -1,4 +1,5 @@
#include "Task1.h"
#include "std_lib_facilities.h"
void cinToFile(string path)
{
@ -22,27 +23,28 @@ void cinToFile(string path)
void addLineNumber(string inputPath, string outputPath)
{
string line;
ifstream ifs{ inputPath };
ifstream ifs{ inputPath }; // Try opening file
if (!ifs)
error("Cant open file ", inputPath);
else
{
vector<string> lines;
while (ifs.eof())
vector<string> lines; //Create vector of lines
while (!ifs.eof()) // Read all the lines
{
line.clear();
getline(ifs, line);
cout << line << endl;
lines.push_back(line);
}
ifs.close();
ofstream ofs{ outputPath };
if (!ifs || !ofs)
ofstream ofs{ outputPath }; // Prepeare to output to file
if (!ofs)
error("Cant open file ", outputPath);
else
{
for (int i = 0; i < lines.size(); ++i)
{
ofs << setw(lines.size()) << i + 1 << " " << lines[i] << endl;
ofs << setw(to_string(lines.size()).size()) << i + 1 << " " << lines[i] << endl; // Write all lines with linenumer with a specific width
}
}

37
Oving 6/Oving 6/Task2.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "Task2.h"
#include "std_lib_facilities.h"
void countCharInTxt(string path)
{
ifstream ifs{ path };
if (!ifs)
{
error("Cant open file ", path);
return;
}
string text, line;
while (!ifs.eof()) // Read all the lines
{
line.clear();
getline(ifs, line);
text += line;
}
vector<int> chars(26);
for (const auto c : text)
{
if ((c <= 255 && c > -1) && isalpha(c))
chars[tolower(c) - 'a'] += 1;
}
for (int i = 0; i < chars.size(); i += 3)
{
for (int j = i; j < i + 3 && j < chars.size(); ++j)
{
cout << (char)((j)+(int)'a') << ":\t" << chars[j] << "\t\t";
}
cout << endl;
}
}

4
Oving 6/Oving 6/Task2.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#include "std_lib_facilities.h"
void countCharInTxt(string path);

25
Oving 6/Oving 6/Task3.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "Task3.h"
CourseCatalog::CourseCatalog()
{
}
void CourseCatalog::addCourse(string code, string name)
{
courses.insert(code, name);
}
void CourseCatalog::removeCourse(string code)
{
courses.erase(code);
}
string CourseCatalog::getCourse(string code)
{
return courses[code];
}
ostream & operator<<(ostream &, const CourseCatalog &)
{
// TODO: insert return statement here
}

14
Oving 6/Oving 6/Task3.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include "std_lib_facilities.h"
class CourseCatalog
{
public:
CourseCatalog();
friend ostream& operator<<(ostream&, const CourseCatalog&);
void addCourse(string code, string name);
void removeCourse(string code);
string getCourse(string code);
private:
map<string, string> courses;
};

View File

@ -23,4 +23,4 @@ v
w
x
y
z
z

View File

@ -1,11 +1,16 @@
#include "std_lib_facilities.h"
#include "Task1.h"
#include "Task2.h"
#include "Task3.h"
int main(){
// Your code here
//cinToFile("inn.txt");
addLineNumber("inn.txt", "out.txt");
//addLineNumber("inn.txt", "out.txt");
countCharInTxt("grunnlov.txt");
return 0;
}

View File

@ -0,0 +1,26 @@
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i
10 j
11 k
12 l
13 m
14 n
15 o
16 p
17 q
18 r
19 s
20 t
21 u
22 v
23 w
24 x
25 y
26 z