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
SRC = main.cpp
OBJ = main.o
CC = c++
CFLAGS = -std=c++98 -Wall -Werror -Wextra
CXXFLAGS = -std=c++98 -Wall -Werror -Wextra
RM = rm -rf
$(NAME): $(SRC)
$(CC) $(CFLAGS) $(SRC) -o $(NAME)
$(NAME): $(OBJ)
c++ $(OBJ) -o $(NAME)
all: $(NAME)
clean:
$(RM) $(OBJ)
fclean: clean
$(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 <locale.h>
int main(int argc, char **argv)
{
@ -30,5 +29,4 @@ int main(int argc, char **argv)
}
}
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)
{
this->id = id;
_id = id;
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::cin >> this->datas[LAST];
while (_datas[LAST] == "")
std::cin >> _datas[LAST];
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::cin >> this->datas[NUMBER];
while (_datas[NUMBER] == "")
std::cin >> _datas[NUMBER];
std::cout << "Enter new contact secret:\n";
std::cin >> this->datas[SECRET];
std::cout << std::endl;
while (_datas[SECRET] == "")
std::cin >> _datas[SECRET];
}
void Contact::summary()
{
std::cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";
std::cout << "|";
std::cout << std::setw(2) << this->id;
std::cout << std::setw(2) << _id;
std::cout << "|";
for (int i = 0; i <= SECRET; i++)
{
if (this->datas[i].length() < 10)
std::cout << std::setw(10) << this->datas[i] << "|";
if (_datas[i].length() < 10)
std::cout << std::setw(10) << _datas[i] << "|";
else
std::cout << this->datas[i].substr(0, 9) << "." << "|";
std::cout << _datas[i].substr(0, 9) << "." << "|";
}
std::cout << "\nvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv\n";
}
void Contact::get()
{
std::cout <<"First name : " << this->datas[FIRST] << std::endl;
std::cout <<"Last name : " << this->datas[LAST] << std::endl;
std::cout <<"Nick name : " << this->datas[NICK] << std::endl;
std::cout <<"Phone nb : " << this->datas[NUMBER] << std::endl;
std::cout <<"Dark secret : " << this->datas[SECRET] << std::endl;
std::cout <<"First name : " << _datas[FIRST] << std::endl;
std::cout <<"Last name : " << _datas[LAST] << std::endl;
std::cout <<"Nick name : " << _datas[NICK] << std::endl;
std::cout <<"Phone nb : " << _datas[NUMBER] << 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 */
/* */
/* ************************************************************************** */
#ifndef CONTACT_HPP
# define CONTACT_HPP
#pragma once
#include <iostream>
#include <string>
#include <iomanip>
@ -33,7 +31,6 @@ class Contact
void summary();
void get();
private:
int id;
std::string datas[5];
int _id;
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"
PhoneBook::PhoneBook()
{
this->id = 0;
PhoneBook::PhoneBook() {
_id = 0;
}
void PhoneBook::add()
{
int id = this->id % CONTACTS_AMOUNT;
if (this->id > 7)
void PhoneBook::add() {
int id = _id % CONTACTS_AMOUNT;
if (_id > CONTACTS_AMOUNT - 1)
{
std::cout << "You gonna remove the contact n." << id;
std::cout << "\nTo continue, type 'y': ";
@ -28,45 +26,37 @@ void PhoneBook::add()
std::cin >> confirm;
if (confirm != "y")
{
std::cout << "\nAbort.\n";
std::cout << std::endl << "Abort." << std::endl;
return ;
}
std::cout << std::endl;
}
this->contacts[id].set(id);
this->id++;
_contacts[id].set(id);
_id++;
}
void PhoneBook::search()
{
int id = 0;
Contact ctct;
ctct = this->contacts[0];
while (id++ < this->id % CONTACTS_AMOUNT)
{
ctct.summary();
ctct = this->contacts[id];
}
void PhoneBook::search() {
for (int id = 0; id < _id % CONTACTS_AMOUNT; id++)
_contacts[id].summary();
std::string sid;
std::cout << "Which contact do you wanna see [id?]:";
std::cin >> sid;
id = std::stoi(sid);
ctct = this->contacts[id];
ctct.get();
_contacts[(int)std::strtod(sid.c_str(), 0)].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 << "What do you want?\n";
std::cin >> this->cmd;
if (cmd == "EXIT")
return (EXIT_FAILURE);
if (cmd == "SEARCH")
this->search();
else if (cmd == "ADD")
std::cout << "=== PHONEBOOK ===" << std::endl;
std::cout << "Commands available : 1-ADD, 2-SEARCH, 3-EXIT.\n";
std::cout << "What do you want? ";
std::cin >> cmd;
if (cmd == "ADD" || cmd == "1")
this->add();
else
std::cout << "Unknown command.\n";
return (EXIT_SUCCESS);
else if (cmd == "SEARCH" || cmd == "2")
this->search();
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 */
/* */
/* ************************************************************************** */
#ifndef PHONEBOOK_HPP
# define PHONEBOOK_HPP
#pragma once
#include "Contact.hpp"
#include <cstdlib>
#define CONTACTS_AMOUNT 8
class PhoneBook
{
public:
PhoneBook();
int run();
void run();
private:
Contact contacts[CONTACTS_AMOUNT];
std::string cmd;
int id;
Contact _contacts[CONTACTS_AMOUNT];
int _id;
void add();
void search();
};
#endif

6
CPP00/ex01/main.cpp

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

13
CPP01/ex00/Zombie.cpp

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

1
CPP01/ex00/Zombie.hpp

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

1
CPP01/ex00/main.cpp

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

2
CPP01/ex01/Makefile

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

3
CPP01/ex01/Zombie.cpp

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

2
CPP01/ex01/Zombie.hpp

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

BIN
CPP01/ex01/apocalypse

Binary file not shown.

5
CPP01/ex01/main.cpp

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

2
CPP01/ex01/zombieHorde.cpp

@ -18,7 +18,7 @@ Zombie *zombieHorde(int N, string name)
int n = 0;
while (n < N)
{
ret[n].setName(name + std::to_string(n));
ret[n].setName(n);
n++;
}
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;
}
Converter::Converter (string str):_input(str){
startConv();
cout << "Converter parameter constructor called" << endl;
Converter::Converter (string str){
if (str[0] == '\'' || str[0] == '"')
{
_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) {
@ -36,27 +60,3 @@ Converter::~Converter (void) {
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 <iostream>
#include <cstdlib>
#include <stdexcept>
#include <cctype>
#include <limits>
using std::cout;
using std::endl;
using std::string;
@ -33,6 +36,4 @@ class Converter{
Converter(Converter const & src);
virtual ~Converter(void);
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);
string input = av[1];
Converter raw(input);
raw.display();
}

BIN
CPP06/ex00/main.o

Binary file not shown.
Loading…
Cancel
Save