Browse Source

fix:00 and cpp01ex00

master^2
narnaud 3 years ago
parent
commit
b60ba562d8
  1. 15
      CPP00/ex00/Makefile
  2. 2
      CPP00/ex00/main.cpp
  3. BIN
      CPP00/ex00/megaphone
  4. 36
      CPP00/ex01/Contact.cpp
  5. 9
      CPP00/ex01/Contact.hpp
  6. 20
      CPP00/ex01/Makefile
  7. 60
      CPP00/ex01/PhoneBook.cpp
  8. 12
      CPP00/ex01/PhoneBook.hpp
  9. 6
      CPP00/ex01/main.cpp
  10. 13
      CPP01/ex00/Zombie.cpp
  11. 1
      CPP01/ex00/Zombie.hpp
  12. 1
      CPP01/ex00/main.cpp
  13. 2
      CPP01/ex01/Makefile
  14. 3
      CPP01/ex01/Zombie.cpp
  15. 2
      CPP01/ex01/Zombie.hpp
  16. BIN
      CPP01/ex01/apocalypse
  17. 5
      CPP01/ex01/main.cpp
  18. 2
      CPP01/ex01/zombieHorde.cpp
  19. BIN
      CPP06/.main.cpp.swp
  20. 54
      CPP06/ex00/Converter.cpp
  21. 5
      CPP06/ex00/Converter.hpp
  22. BIN
      CPP06/ex00/Converter.o
  23. BIN
      CPP06/ex00/convert
  24. 2
      CPP06/ex00/main.cpp
  25. BIN
      CPP06/ex00/main.o

15
CPP00/ex00/Makefile

@ -1,16 +1,21 @@
NAME = megaphone NAME = megaphone
SRC = main.cpp SRC = main.cpp
OBJ = main.o
CC = c++ CXXFLAGS = -std=c++98 -Wall -Werror -Wextra
CFLAGS = -std=c++98 -Wall -Werror -Wextra
RM = rm -rf RM = rm -rf
$(NAME): $(SRC) $(NAME): $(OBJ)
$(CC) $(CFLAGS) $(SRC) -o $(NAME) c++ $(OBJ) -o $(NAME)
all: $(NAME) all: $(NAME)
clean: clean:
$(RM) $(OBJ)
fclean: clean
$(RM) $(NAME) $(RM) $(NAME)
.PHONY: all re re: fclean all
.PHONY: all clean fclean re

2
CPP00/ex00/main.cpp

@ -11,7 +11,6 @@
/* ************************************************************************** */ /* ************************************************************************** */
# include <iostream> # include <iostream>
# include <locale.h>
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@ -30,5 +29,4 @@ int main(int argc, char **argv)
} }
} }
std::cout << std::endl; std::cout << std::endl;
return (EXIT_SUCCESS);
} }

BIN
CPP00/ex00/megaphone

Binary file not shown.

36
CPP00/ex01/Contact.cpp

@ -15,41 +15,45 @@ Contact::Contact(){}
void Contact::set(int id) void Contact::set(int id)
{ {
this->id = id; _id = id;
std::cout << "Enter new contact first name:\n"; std::cout << "Enter new contact first name:\n";
std::cin >> this->datas[FIRST]; while (_datas[FIRST] == "")
std::cin >> _datas[FIRST];
std::cout << "Enter new contact last name:\n"; std::cout << "Enter new contact last name:\n";
std::cin >> this->datas[LAST]; while (_datas[LAST] == "")
std::cin >> _datas[LAST];
std::cout << "Enter new contact nickname:\n"; std::cout << "Enter new contact nickname:\n";
std::cin >> this->datas[NICK]; while (_datas[NICK] == "")
std::cin >> _datas[NICK];
std::cout << "Enter new contact number:\n"; std::cout << "Enter new contact number:\n";
std::cin >> this->datas[NUMBER]; while (_datas[NUMBER] == "")
std::cin >> _datas[NUMBER];
std::cout << "Enter new contact secret:\n"; std::cout << "Enter new contact secret:\n";
std::cin >> this->datas[SECRET]; while (_datas[SECRET] == "")
std::cout << std::endl; std::cin >> _datas[SECRET];
} }
void Contact::summary() void Contact::summary()
{ {
std::cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"; std::cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";
std::cout << "|"; std::cout << "|";
std::cout << std::setw(2) << this->id; std::cout << std::setw(2) << _id;
std::cout << "|"; std::cout << "|";
for (int i = 0; i <= SECRET; i++) for (int i = 0; i <= SECRET; i++)
{ {
if (this->datas[i].length() < 10) if (_datas[i].length() < 10)
std::cout << std::setw(10) << this->datas[i] << "|"; std::cout << std::setw(10) << _datas[i] << "|";
else else
std::cout << this->datas[i].substr(0, 9) << "." << "|"; std::cout << _datas[i].substr(0, 9) << "." << "|";
} }
std::cout << "\nvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv\n"; std::cout << "\nvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv\n";
} }
void Contact::get() void Contact::get()
{ {
std::cout <<"First name : " << this->datas[FIRST] << std::endl; std::cout <<"First name : " << _datas[FIRST] << std::endl;
std::cout <<"Last name : " << this->datas[LAST] << std::endl; std::cout <<"Last name : " << _datas[LAST] << std::endl;
std::cout <<"Nick name : " << this->datas[NICK] << std::endl; std::cout <<"Nick name : " << _datas[NICK] << std::endl;
std::cout <<"Phone nb : " << this->datas[NUMBER] << std::endl; std::cout <<"Phone nb : " << _datas[NUMBER] << std::endl;
std::cout <<"Dark secret : " << this->datas[SECRET] << std::endl; std::cout <<"Dark secret : " << _datas[SECRET] << std::endl;
} }

9
CPP00/ex01/Contact.hpp

@ -9,9 +9,7 @@
/* Updated: 2022/06/13 16:35:43 by narnaud ### ########.fr */ /* Updated: 2022/06/13 16:35:43 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef CONTACT_HPP #pragma once
# define CONTACT_HPP
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <iomanip> #include <iomanip>
@ -33,7 +31,6 @@ class Contact
void summary(); void summary();
void get(); void get();
private: private:
int id; int _id;
std::string datas[5]; std::string _datas[5];
}; };
#endif

20
CPP00/ex01/Makefile

@ -0,0 +1,20 @@
NAME = phonebook
SRCS = main.cpp PhoneBook.cpp Contact.cpp
OBJS = $(SRCS:.cpp=.o)
CXXFLAGS = -std=c++98 -Werror -Wextra -Wall
$(NAME): $(OBJS)
c++ $(OBJS) -o $(NAME)
all: $(NAME)
clean:
rm -rf $(OBJS)
fclean: clean
rm -rf $(NAME)
re: fclean all
.PHONY: all clean fclean re

60
CPP00/ex01/PhoneBook.cpp

@ -12,15 +12,13 @@
#include "PhoneBook.hpp" #include "PhoneBook.hpp"
PhoneBook::PhoneBook() PhoneBook::PhoneBook() {
{ _id = 0;
this->id = 0;
} }
void PhoneBook::add() void PhoneBook::add() {
{ int id = _id % CONTACTS_AMOUNT;
int id = this->id % CONTACTS_AMOUNT; if (_id > CONTACTS_AMOUNT - 1)
if (this->id > 7)
{ {
std::cout << "You gonna remove the contact n." << id; std::cout << "You gonna remove the contact n." << id;
std::cout << "\nTo continue, type 'y': "; std::cout << "\nTo continue, type 'y': ";
@ -28,45 +26,37 @@ void PhoneBook::add()
std::cin >> confirm; std::cin >> confirm;
if (confirm != "y") if (confirm != "y")
{ {
std::cout << "\nAbort.\n"; std::cout << std::endl << "Abort." << std::endl;
return ; return ;
} }
std::cout << std::endl; std::cout << std::endl;
} }
this->contacts[id].set(id); _contacts[id].set(id);
this->id++; _id++;
} }
void PhoneBook::search()
{
int id = 0;
Contact ctct;
ctct = this->contacts[0]; void PhoneBook::search() {
while (id++ < this->id % CONTACTS_AMOUNT) for (int id = 0; id < _id % CONTACTS_AMOUNT; id++)
{ _contacts[id].summary();
ctct.summary();
ctct = this->contacts[id];
}
std::string sid; std::string sid;
std::cout << "Which contact do you wanna see [id?]:"; std::cout << "Which contact do you wanna see [id?]:";
std::cin >> sid; std::cin >> sid;
id = std::stoi(sid); _contacts[(int)std::strtod(sid.c_str(), 0)].get();
ctct = this->contacts[id];
ctct.get();
} }
int PhoneBook::run() void PhoneBook::run() {
std::string cmd;
while (cmd != "EXIT" && cmd != "3")
{ {
std::cout << "PhoneBook.\n" << "Commands available : ADD, SEARCH, EXIT.\n"; std::cout << "=== PHONEBOOK ===" << std::endl;
std::cout << "What do you want?\n"; std::cout << "Commands available : 1-ADD, 2-SEARCH, 3-EXIT.\n";
std::cin >> this->cmd; std::cout << "What do you want? ";
if (cmd == "EXIT") std::cin >> cmd;
return (EXIT_FAILURE); if (cmd == "ADD" || cmd == "1")
if (cmd == "SEARCH")
this->search();
else if (cmd == "ADD")
this->add(); this->add();
else else if (cmd == "SEARCH" || cmd == "2")
std::cout << "Unknown command.\n"; this->search();
return (EXIT_SUCCESS); else if (cmd != "EXIT" && cmd != "3")
std::cout << "Unknown command." << std::endl;
}
} }

12
CPP00/ex01/PhoneBook.hpp

@ -9,21 +9,19 @@
/* Updated: 2022/06/13 15:35:30 by narnaud ### ########.fr */ /* Updated: 2022/06/13 15:35:30 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef PHONEBOOK_HPP #pragma once
# define PHONEBOOK_HPP
#include "Contact.hpp" #include "Contact.hpp"
#include <cstdlib>
#define CONTACTS_AMOUNT 8 #define CONTACTS_AMOUNT 8
class PhoneBook class PhoneBook
{ {
public: public:
PhoneBook(); PhoneBook();
int run(); void run();
private: private:
Contact contacts[CONTACTS_AMOUNT]; Contact _contacts[CONTACTS_AMOUNT];
std::string cmd; int _id;
int id;
void add(); void add();
void search(); void search();
}; };
#endif

6
CPP00/ex01/main.cpp

@ -14,9 +14,7 @@
int main(void) int main(void)
{ {
PhoneBook pb; PhoneBook phone;
while (pb.run() == EXIT_SUCCESS) phone.run();
;
return (EXIT_SUCCESS);
} }

13
CPP01/ex00/Zombie.cpp

@ -12,17 +12,14 @@
#include "Zombie.hpp" #include "Zombie.hpp"
Zombie::Zombie(string name):_name(name) Zombie::Zombie(string name):_name(name) {
{
cout << "*" << name << " died.*\n"; cout << "*" << name << " died.*\n";
} }
Zombie::~Zombie(void) Zombie::~Zombie(void) {
{ cout << "*Zombie " << _name << " really died.*\n";
cout << "*" << this->_name << " really died.*\n";
} }
void Zombie::announce(void) const void Zombie::announce(void) const {
{ cout << _name << ": BraiiiiiiinnnzzzZ...\n";
cout << this->_name << ": BraiiiiiiinnnzzzZ...\n";
} }

1
CPP01/ex00/Zombie.hpp

@ -17,6 +17,7 @@
# endif # endif
#include <iostream> #include <iostream>
#include <cstdlib>
using std::cin; using std::cin;
using std::cout; using std::cout;
using std::string; using std::string;

1
CPP01/ex00/main.cpp

@ -20,5 +20,4 @@ int main(void)
randomChump("Aurel"); randomChump("Aurel");
if (DEBUG) if (DEBUG)
system("leaks apocalypse"); system("leaks apocalypse");
return (EXIT_SUCCESS);
} }

2
CPP01/ex01/Makefile

@ -1,7 +1,7 @@
NAME = apocalypse NAME = apocalypse
SRCS = main.cpp Zombie.cpp zombieHorde.cpp SRCS = main.cpp Zombie.cpp zombieHorde.cpp
OBJS = $(SRCS:.cpp=.o) OBJS = $(SRCS:.cpp=.o)
CXXFLAGS = -g -std=c++98 -Werror -Wextra -Wall CXXFLAGS = -std=c++98 -Werror -Wextra -Wall
$(NAME): $(OBJS) $(NAME): $(OBJS)
c++ $(OBJS) -o $(NAME) c++ $(OBJS) -o $(NAME)

3
CPP01/ex01/Zombie.cpp

@ -21,7 +21,7 @@ Zombie::Zombie(string name):_name(name)
Zombie::~Zombie(void) Zombie::~Zombie(void)
{ {
cout << "*" << _name << " really died.*\n"; cout << "*Zombie " << _name << " really died.*\n";
} }
void Zombie::announce(void) const void Zombie::announce(void) const
@ -31,5 +31,6 @@ void Zombie::announce(void) const
void Zombie::setName(string name) void Zombie::setName(string name)
{ {
cout << "*" << name << " died.*\n";
_name = name; _name = name;
} }

2
CPP01/ex01/Zombie.hpp

@ -18,6 +18,8 @@
# define ZB_AMOUNT 8 # define ZB_AMOUNT 8
#include <iostream> #include <iostream>
#include <sstream>
#include <cstdlib>
using std::cin; using std::cin;
using std::cout; using std::cout;
using std::string; using std::string;

BIN
CPP01/ex01/apocalypse

Binary file not shown.

5
CPP01/ex01/main.cpp

@ -15,10 +15,9 @@
int main(void) int main(void)
{ {
Zombie *zombs = zombieHorde(ZB_AMOUNT, "Zombie"); Zombie *zombs = zombieHorde(ZB_AMOUNT, "Zombie");
int i = 0;
while (i < ZB_AMOUNT) for (int i = 0; i < ZB_AMOUNT; i++)
zombs[i++].announce(); zombs[i].announce();
delete [] zombs; delete [] zombs;
if (DEBUG) if (DEBUG)
system("leaks apocalypse"); system("leaks apocalypse");

2
CPP01/ex01/zombieHorde.cpp

@ -18,7 +18,7 @@ Zombie *zombieHorde(int N, string name)
int n = 0; int n = 0;
while (n < N) while (n < N)
{ {
ret[n].setName(name + std::to_string(n)); ret[n].setName(n);
n++; n++;
} }
return (ret); return (ret);

BIN
CPP06/.main.cpp.swp

Binary file not shown.

54
CPP06/ex00/Converter.cpp

@ -16,9 +16,33 @@ Converter::Converter (void) {
cout << "Converter default constructor called " << endl; cout << "Converter default constructor called " << endl;
} }
Converter::Converter (string str):_input(str){ Converter::Converter (string str){
startConv(); if (str[0] == '\'' || str[0] == '"')
cout << "Converter parameter constructor called" << endl; {
_c = str[1];
cout << "Char: " << _c << endl;
_d = static_cast<double>(_c);
}
else
{
cout << "Char: ";
_d = std::strtod(str.c_str(), 0);
_c = static_cast<char>(_d);
if (_d < 0 || _d > 255)
cout << "Invalid" << endl;
else if (std::isprint(_c))
cout << "Not Printable" << endl;
else
cout << _c << endl;
}
cout << "Double: " << _d << endl;
cout << "Integer: ";
_i = static_cast<int>(_d);
if (_d < std::numeric_limits<int>::min() || _d > std::numeric_limits<int>::max())
cout << "Off limits -> ";
cout << _i << endl;
_f = static_cast<float>(_d);
cout << "Float: " << _f << "f"<< endl;
} }
Converter::Converter (Converter const & src) { Converter::Converter (Converter const & src) {
@ -36,27 +60,3 @@ Converter::~Converter (void) {
cout << "Converter default destructor called" << endl; cout << "Converter default destructor called" << endl;
} }
void Converter::startConv(void) {
if (_input[0] == '\'' || _input[0] == '"')
{
_c = _input[1];
_d = static_cast<double>(_c);
}
else
{
_d = std::strtod(_input.c_str(), 0);
_c = static_cast<char>(_d);
}
_i = static_cast<int>(_d);
_f = static_cast<float>(_d);
}
void Converter::display(void) {
cout << "Input: " << _input << endl;
cout << "Char: " << _c << endl;
cout << "Double: " << _d << endl;
cout << "Float: " << _f << endl;
cout << "Integer: " << _i << endl;
}

5
CPP06/ex00/Converter.hpp

@ -15,6 +15,9 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
#include <stdexcept>
#include <cctype>
#include <limits>
using std::cout; using std::cout;
using std::endl; using std::endl;
using std::string; using std::string;
@ -33,6 +36,4 @@ class Converter{
Converter(Converter const & src); Converter(Converter const & src);
virtual ~Converter(void); virtual ~Converter(void);
Converter & operator= (Converter const & src); Converter & operator= (Converter const & src);
void startConv(void);
void display(void);
}; };

BIN
CPP06/ex00/Converter.o

Binary file not shown.

BIN
CPP06/ex00/convert

Binary file not shown.

2
CPP06/ex00/main.cpp

@ -23,6 +23,4 @@ int main(int ac, char **av) {
return (EXIT_FAILURE); return (EXIT_FAILURE);
string input = av[1]; string input = av[1];
Converter raw(input); Converter raw(input);
raw.display();
} }

BIN
CPP06/ex00/main.o

Binary file not shown.
Loading…
Cancel
Save