Skip to content

Commit 5a66467

Browse files
committed
Initial commit
0 parents  commit 5a66467

File tree

204 files changed

+7935
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

204 files changed

+7935
-0
lines changed

00/ex00/Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
NAME = megaphone
2+
3+
CC = clang++
4+
FLAGS = -Wall -Wextra -Werror -std=c++98
5+
GCC = $(CC) $(FLAGS)
6+
7+
SRCS = megaphone.cpp
8+
9+
all: $(NAME)
10+
11+
$(NAME):
12+
$(GCC) $(SRCS) -o $(NAME)
13+
14+
clean:
15+
rm $(NAME)
16+
17+
fclean: clean
18+
19+
re: fclean all
20+

00/ex00/megaphone.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <string>
2+
#include <iostream>
3+
4+
int main(int argc, char **argv){
5+
if (argc == 1){
6+
std::cout << "* LOUD AND UNBEARABLE FEEDBACK NOISE *" << std::endl;
7+
return 0;
8+
}
9+
10+
std::string input;
11+
for (int i = 1; i < argc; ++i){
12+
input = argv[i];
13+
for (size_t j = 0; j < input.size(); ++j){
14+
std::cout << (char)std::toupper(input[j]);
15+
}
16+
}
17+
std::cout << std::endl;
18+
return (0);
19+
}

00/ex01/Contact.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "Contact.hpp"
2+
3+
Contact::Contact(){
4+
is_empty = true;
5+
}
6+
7+
Contact::Contact(std::string first_name, std::string last_name, std::string nickname,
8+
std::string phone_number, std::string darkest_secret)
9+
: first_name(first_name), last_name(last_name), nickname(nickname),
10+
phone_number(phone_number), darkest_secret(darkest_secret) {
11+
is_empty = false;
12+
}
13+
14+
std::string Contact::GetFirstName(){
15+
return first_name;
16+
}
17+
18+
std::string Contact::GetLastName(){
19+
return last_name;
20+
}
21+
22+
std::string Contact::GetNickname(){
23+
return nickname;
24+
}
25+
26+
std::string Contact::GetPhoneNumber(){
27+
return phone_number;
28+
}
29+
30+
std::string Contact::GetDarkestSecret(){
31+
return darkest_secret;
32+
}
33+
34+
bool Contact::IsEmpty(){
35+
return is_empty;
36+
}

00/ex01/Contact.hpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef CONTACT_HPP
2+
# define CONTACT_HPP
3+
4+
# include <string>
5+
6+
class Contact{
7+
public:
8+
Contact();
9+
Contact(std::string first_name, std::string last_name, std::string nickname,
10+
std::string phone_number, std::string darkest_secret);
11+
12+
std::string GetFirstName();
13+
std::string GetLastName();
14+
std::string GetNickname();
15+
std::string GetPhoneNumber();
16+
std::string GetDarkestSecret();
17+
18+
bool IsEmpty();
19+
20+
private:
21+
std::string first_name;
22+
std::string last_name;
23+
std::string nickname;
24+
std::string phone_number;
25+
std::string darkest_secret;
26+
bool is_empty;
27+
28+
};
29+
30+
#endif

00/ex01/Makefile

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
NAME = phonebook
2+
3+
CC = clang++
4+
FLAGS = -Wall -Wextra -Werror -std=c++98
5+
GCC = $(CC) $(FLAGS)
6+
7+
SRCS = PhoneBook.cpp Contact.cpp main.cpp
8+
HDRS = PhoneBook.hpp Contact.hpp
9+
OBJS = $(SRCS:.cpp=.o)
10+
11+
all: $(NAME)
12+
13+
%.o: %.cpp $(HDRS)
14+
$(GCC) -c -o $@ $<
15+
16+
$(NAME): $(OBJS) $(HDRS)
17+
$(GCC) $(OBJS) -o $(NAME)
18+
19+
clean:
20+
rm $(OBJS)
21+
22+
fclean: clean
23+
rm -f $(NAME)
24+
25+
re: fclean all
26+

00/ex01/PhoneBook.cpp

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "PhoneBook.hpp"
2+
3+
PhoneBook::PhoneBook() {
4+
last_added = -1;
5+
users_count = 0;
6+
}
7+
8+
void PhoneBook::Add(Contact contact){
9+
if (!contact.IsEmpty()){
10+
if (users_count < USERS_COUNT)
11+
users_count++;
12+
if (last_added == USERS_COUNT - 1)
13+
last_added = -1;
14+
last_added++;
15+
contacts[last_added] = contact;
16+
}
17+
}
18+
19+
void PhoneBook::Search(){
20+
if (users_count == 0){
21+
std::cout << "Phonebook is empty" << std::endl;
22+
return;
23+
}
24+
25+
std::string tmp;
26+
int id_to_show;
27+
28+
PrintString_("index");
29+
std::cout << '|';
30+
PrintString_("first name");
31+
std::cout << '|';
32+
PrintString_("last name");
33+
std::cout << '|';
34+
PrintString_("nickname");
35+
std::cout << '|' << std::endl;
36+
37+
for (int i = 0; i < USERS_COUNT; ++i){
38+
if (contacts[i].IsEmpty()){
39+
continue;
40+
}
41+
42+
PrintString_(std::to_string(i));
43+
std::cout << '|';
44+
45+
PrintString_(contacts[i].GetFirstName());
46+
std::cout << '|';
47+
48+
PrintString_(contacts[i].GetLastName());
49+
std::cout << '|';
50+
51+
PrintString_(contacts[i].GetNickname());
52+
std::cout << std::endl;
53+
}
54+
55+
id_to_show = -1;
56+
std::cout << "enter id: ";
57+
std::cin >> id_to_show;
58+
59+
if (id_to_show < 0 || id_to_show >= users_count){
60+
std::cin.clear();
61+
std::cout << "Invalid id" << std::endl;
62+
return;
63+
}
64+
65+
std::cout << "First name: " << contacts[id_to_show].GetFirstName() << std::endl;
66+
std::cout << "Last name: " << contacts[id_to_show].GetLastName() << std::endl;
67+
std::cout << "Nickname: " << contacts[id_to_show].GetNickname() << std::endl;
68+
std::cout << "Phone number: " << contacts[id_to_show].GetPhoneNumber() << std::endl;
69+
std::cout << "Darkest secret: " << contacts[id_to_show].GetDarkestSecret() << std::endl;
70+
}
71+
72+
void PhoneBook::PrintString_(std::string str){
73+
if (str.size() == STR_LEN){
74+
std::cout << str;
75+
} else if (str.size() < STR_LEN){
76+
std::cout << std::string(STR_LEN - str.size(), ' ') << str;
77+
} else {
78+
std::cout << str.substr(0, STR_LEN - 1) << '.';
79+
}
80+
}

00/ex01/PhoneBook.hpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef PHONEBOOK_HPP
2+
# define PHONEBOOK_HPP
3+
4+
# define USERS_COUNT 8
5+
# define STR_LEN 10
6+
7+
# include <iostream>
8+
9+
# include "Contact.hpp"
10+
11+
class PhoneBook{
12+
public:
13+
PhoneBook();
14+
15+
void Add(Contact contact);
16+
void Search();
17+
18+
private:
19+
int last_added;
20+
int users_count;
21+
Contact contacts[USERS_COUNT];
22+
23+
void PrintString_(std::string str);
24+
25+
};
26+
27+
#endif

00/ex01/main.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
#include "PhoneBook.hpp"
5+
6+
int main(){
7+
PhoneBook phone_book;
8+
std::string input, first_name, second_name,
9+
nickname, phone_number, darkest_secret;
10+
11+
while(1){
12+
std::cout << "phonebook> ";
13+
std::cin >> input;
14+
if (input == "ADD"){
15+
std::cout << "First name: ";
16+
std::cin >> first_name;
17+
18+
std::cout << "Second name: ";
19+
std::cin >> second_name;
20+
21+
std::cout << "Nickname: ";
22+
std::cin >> nickname;
23+
24+
std::cout << "Phone number: ";
25+
std::cin >> phone_number;
26+
27+
std::cout << "Darkest secret: ";
28+
std::cin >> darkest_secret;
29+
30+
Contact contact(first_name, second_name,
31+
nickname, phone_number, darkest_secret);
32+
33+
phone_book.Add(contact);
34+
} else if (input == "SEARCH"){
35+
phone_book.Search();
36+
} else if (input == "EXIT") {
37+
return (0);
38+
} else {
39+
std::cout
40+
<< "Bad command"
41+
<< std::endl
42+
<< "Available commands: ADD, SEARCH, EXIT"
43+
<< std::endl;
44+
}
45+
std::cin.clear();
46+
}
47+
}

0 commit comments

Comments
 (0)