Compare commits

..

No commits in common. "c27a71a56fa0646b6e04339d9443fe32100f6c00" and "ef2bb1ddc65ae62f65f99927fc4620b593bc3926" have entirely different histories.

122
Main.cpp
View File

@ -1,4 +1,3 @@
#include <fstream>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
@ -100,89 +99,17 @@ typedef struct contacts{
} Kontaktdaten; } Kontaktdaten;
void addContact(Kontaktdaten* person, std::vector<Kontaktdaten*> *Telefonbuch) { void addContact(Kontaktdaten* person, std::vector<Kontaktdaten*> &Telefonbuch) {
Telefonbuch->push_back(person); Telefonbuch.push_back(person);
// std::vector<Kontaktdaten *> buch = *Telefonbuch;
// buch.push_back(person);
} }
void printAllContacts(std::vector<Kontaktdaten*> *Telefonbuch) { void printAllContacts(std::vector<Kontaktdaten*> Telefonbuch) {
for (Kontaktdaten * personpointer: *Telefonbuch) { for (Kontaktdaten * personpointer: Telefonbuch) {
Kontaktdaten person = *personpointer; Kontaktdaten person = *personpointer;
std::cout << person.firstName << " " << person.lastName << " " << person.number << std::endl; std::cout << person.firstName << " " << person.lastName << " " << person.number << std::endl;
} }
} }
void saveAllContacts(std::vector<Kontaktdaten*> *Telefonbuch, std::string *fileName) {
std::ofstream outfile;
outfile.open(*fileName);
for (Kontaktdaten * personpointer: *Telefonbuch) {
Kontaktdaten person = *personpointer;
outfile << person.firstName << std::endl << person.lastName << std::endl << person.number << std::endl;
}
outfile.close();
}
int exponentiate(int base, int exponent) {
int result = 1;
while (exponent > 0) {
result *= base;
exponent--;
}
return result;
}
int intStringToInt(std::string input) {
int result = 0;
int size = input.length();
for(int i = 0; i < size; i++) {
int digit = input[size - 1 - i] - '0'; // -1 to consider 0-index // -'0' to get ascii indicies right
result += digit * exponentiate(10, i); // shift digit
}
return result;
}
void readContactsFromFile(std::vector<Kontaktdaten*> *Telefonbuch, std::string *fileName) {
std::ifstream infile;
infile.open(*fileName);
if (!infile.is_open()) {
std::cout << "File does not exist" << std::endl;
}
std::string line;
int counter = 0;
Kontaktdaten * personpointer = new Kontaktdaten;
while (std::getline(infile, line)) {
int attribute = counter % 3;
counter++;
switch (attribute) {
case 0:
personpointer->firstName = line;
break;
case 1:
personpointer->lastName = line;
break;
case 2:
personpointer->number = intStringToInt(line);
addContact(personpointer, Telefonbuch);
personpointer = new Kontaktdaten;
break;
default:
std::cout << "This should never happen" << std::endl;
}
}
infile.close();
}
/* /*
Kontaktdaten * createContact(std::string firstName, std::string lastName, int number) { Kontaktdaten * createContact(std::string firstName, std::string lastName, int number) {
Kontaktdaten person = new Kontaktdaten; Kontaktdaten person = new Kontaktdaten;
@ -196,17 +123,11 @@ Kontaktdaten * createContact(std::string firstName, std::string lastName, int nu
*/ */
void TelefonbuchMain() { void TelefonbuchMain() {
std::string fileName = "../IO-Files/Telefonbuch.txt";
std::vector<Kontaktdaten*> Telefonbuch; std::vector<Kontaktdaten*> Telefonbuch;
for (int i=0; i<1; i++) { Kontaktdaten * Robin = new Kontaktdaten;
readContactsFromFile(&Telefonbuch, &fileName); Kontaktdaten * Saxo = new Kontaktdaten;
std::cout << "Done with reading " << i << std::endl; Kontaktdaten * Jesus = new Kontaktdaten;
}
auto * Robin = new Kontaktdaten;
auto * Saxo = new Kontaktdaten;
auto * Jesus = new Kontaktdaten;
Robin->number = 123456; Robin->number = 123456;
Robin->firstName = "Robin"; Robin->firstName = "Robin";
@ -220,30 +141,15 @@ void TelefonbuchMain() {
Jesus->firstName = "Jesus"; Jesus->firstName = "Jesus";
Jesus->lastName = "Nazarett"; Jesus->lastName = "Nazarett";
/* addContact(Robin, Telefonbuch);
*Robin->number = 123456; addContact(Saxo, Telefonbuch);
Robin->firstName = "Robin der Neue"; addContact(Jesus, Telefonbuch);
Robin->lastName = "Schnaiter";
Saxo->number = 987654; printAllContacts(Telefonbuch);
Saxo->firstName = "Saxobastian";
Saxo->lastName = "Broschianer";
Jesus->number = 6666666; for (Kontaktdaten * personpointer: Telefonbuch) {
Jesus->firstName = "Jesus"; delete personpointer;
Jesus->lastName = "Nazarett"; }
*/
addContact(Robin, &Telefonbuch);
addContact(Saxo, &Telefonbuch);
addContact(Jesus, &Telefonbuch);
// printAllContacts(&Telefonbuch);
saveAllContacts(&Telefonbuch, &fileName);
delete Robin;
delete Saxo;
delete Jesus;
} }
#pragma endregion #pragma endregion