From 025e205b808c560b6dd2f9edcf3c125da5165278 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 15 Nov 2025 22:13:00 +0000 Subject: [PATCH] intrat to gitea --- ex00/Makefile | 29 ++++++++++++ ex00/megaphone.cpp | 31 +++++++++++++ ex01/Contact.cpp | 59 +++++++++++++++++++++++++ ex01/Contact.hpp | 30 +++++++++++++ ex01/Makefile | 29 ++++++++++++ ex01/PhoneBook.cpp | 108 +++++++++++++++++++++++++++++++++++++++++++++ ex01/PhoneBook.hpp | 22 +++++++++ ex01/main.cpp | 36 +++++++++++++++ 8 files changed, 344 insertions(+) create mode 100644 ex00/Makefile create mode 100644 ex00/megaphone.cpp create mode 100644 ex01/Contact.cpp create mode 100644 ex01/Contact.hpp create mode 100644 ex01/Makefile create mode 100644 ex01/PhoneBook.cpp create mode 100644 ex01/PhoneBook.hpp create mode 100644 ex01/main.cpp diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..048ca59 --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,29 @@ +NAME = megaphone + +SRCS = megaphone.cpp + +CC = clang++ +CFLAGS = -Wall -Wextra -Werror + +RM = rm -f + +OBJS = $(SRCS:.cpp=.o) + +%.o: %.cpp + @$(CC) $(FLAGS) -c $< -o $@ + +all : $(NAME) + +$(NAME) : $(OBJS) + @$(CC) $(CFLAGS) $^ -o $(NAME) + @echo '$(NAME) compiled!' + +fclean : clean + @$(RM) $(NAME) + +clean : + @$(RM) $(OBJS) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/ex00/megaphone.cpp b/ex00/megaphone.cpp new file mode 100644 index 0000000..ee6432e --- /dev/null +++ b/ex00/megaphone.cpp @@ -0,0 +1,31 @@ +#include +#include + +int main(int ac, char **av) +{ + int i = 1; + int j = 0; + + if (ac == 1) + { + std::cout << "* LOUD AND UNBEARABLE FEEDBACK NOISE *" << std::endl; + return 0; + } + while (av[i]) + { + while (av[i][j]) + { + if (av[i][j] >= 'a' && av[i][j] <= 'z') + { + av[i][j] = toupper(av[i][j]); + j++; + } + else + j++; + } + std::cout << av[i]; + i++; + j = 0; + } + std::cout << std::endl; +} \ No newline at end of file diff --git a/ex01/Contact.cpp b/ex01/Contact.cpp new file mode 100644 index 0000000..09efd74 --- /dev/null +++ b/ex01/Contact.cpp @@ -0,0 +1,59 @@ +#include "PhoneBook.hpp" +#include "Contact.hpp" + +Contact::Contact() { +} + +Contact::~Contact() { +} + +void Contact::setFirstName(const std::string& firstName) +{ + _firstName = firstName; +} + +void Contact::setLastName(const std::string& lastName) +{ + _lastName = lastName; +} + +void Contact::setNickname(const std::string& nickname) +{ + _nickname = nickname; +} + +void Contact::setPhoneNumber(const std::string& phoneNumber) +{ + _phoneNumber = phoneNumber; +} + +void Contact::setDarkestSecret(const std::string& darkestSecret) +{ + _darkestSecret = darkestSecret; +} + +std::string Contact::getFirstName() const { + return _firstName; +} + +std::string Contact::getLastName() const { + return _lastName; +} + +std::string Contact::getNickname() const { + return _nickname; +} + +std::string Contact::getPhoneNumber() const { + return _phoneNumber; +} + +std::string Contact::getDarkestSecret() const { + return _darkestSecret; +} + +bool Contact::isEmpty() const { + return _firstName.empty(); +} + + diff --git a/ex01/Contact.hpp b/ex01/Contact.hpp new file mode 100644 index 0000000..597bd5a --- /dev/null +++ b/ex01/Contact.hpp @@ -0,0 +1,30 @@ +#pragma once +#include + +class Contact { +private: + std::string _firstName; + std::string _lastName; + std::string _nickname; + std::string _phoneNumber; + std::string _darkestSecret; + +public: + Contact(); + ~Contact(); + // Setters + void setFirstName(const std::string& firstName); + void setLastName(const std::string& lastName); + void setNickname(const std::string& nickname); + void setPhoneNumber(const std::string& phoneNumber); + void setDarkestSecret(const std::string& darkestSecret); + + // Getters + std::string getFirstName() const; + std::string getLastName() const; + std::string getNickname() const; + std::string getPhoneNumber() const; + std::string getDarkestSecret() const; + + bool isEmpty() const; +}; \ No newline at end of file diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..687942a --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,29 @@ +NAME = PhoneBook + +SRCS = main.cpp PhoneBook.cpp Contact.cpp + +CC = clang++ +CFLAGS = -Wall -Wextra -Werror -std=c++98 + +RM = rm -f + +OBJS = $(SRCS:.cpp=.o) + +%.o: %.cpp + @$(CC) $(CFLAGS) -c $< -o $@ + +all: $(NAME) + +$(NAME): $(OBJS) + @$(CC) $(CFLAGS) $^ -o $(NAME) + @echo '$(NAME) compiled!' + +fclean: clean + @$(RM) $(NAME) + +clean: + @$(RM) $(OBJS) + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/ex01/PhoneBook.cpp b/ex01/PhoneBook.cpp new file mode 100644 index 0000000..8139786 --- /dev/null +++ b/ex01/PhoneBook.cpp @@ -0,0 +1,108 @@ +#include "PhoneBook.hpp" + +PhoneBook::PhoneBook() { + _currentIndex = 0; + _totalContacts = 0; +} + +PhoneBook::~PhoneBook() {} +void PhoneBook::addContact() { + Contact tempContact; + std::string input; + + while (true) { + std::cout << "Enter first name: "; + if (!std::getline(std::cin, input)) exit(1); + if (!input.empty()) break; + std::cout << "Field cannot be empty!" << std::endl; + } + tempContact.setFirstName(input); + while (true) { + std::cout << "Enter last name: "; + if (!std::getline(std::cin, input)) exit(1); + if (!input.empty()) break; + std::cout << "Field cannot be empty!" << std::endl; + } + tempContact.setLastName(input); + while (true) { + std::cout << "Enter nickname: "; + if (!std::getline(std::cin, input)) exit(1); + if (!input.empty()) break; + std::cout << "Field cannot be empty!" << std::endl; + } + tempContact.setNickname(input); + while (true) { + std::cout << "Enter phone number: "; + if (!std::getline(std::cin, input)) exit(1); + if (!input.empty()) break; + std::cout << "Field cannot be empty!" << std::endl; + } + tempContact.setPhoneNumber(input); + while (true) { + std::cout << "Enter Darkest Secret: "; + if (!std::getline(std::cin, input)) exit(1); + if (!input.empty()) break; + std::cout << "Field cannot be empty!" << std::endl; + } + tempContact.setDarkestSecret(input); + + _contacts[_currentIndex] = tempContact; + _currentIndex = (_currentIndex + 1) % 8; + if (_totalContacts < 8) _totalContacts++; + + std::cout << "Contact added successfully!" << std::endl; +} + +void PhoneBook::searchContacts() const { + std::string input; + int index; + + if (_totalContacts == 0) { + std::cout << "Phone book is empty!" << std::endl; + return; + } + std::cout << std::setw(10) << "Index" << "|" + << std::setw(10) << "First Name" << "|" + << std::setw(10) << "Last Name" << "|" + << std::setw(10) << "Nickname" << std::endl; + std::cout << "----------------------------------------" << std::endl; + int i = 0; + while (i < _totalContacts) { + std::cout << std::setw(10) << i << "|" + << std::setw(10) << truncateString(_contacts[i].getFirstName()) << "|" + << std::setw(10) << truncateString(_contacts[i].getLastName()) << "|" + << std::setw(10) << truncateString(_contacts[i].getNickname()) << std::endl; + i++; + } + while (true) { + std::cout << "Enter the index of the contact to display: "; + if (!std::getline(std::cin, input)) { + exit(1); + } + if (input.length() == 1 && input[0] >= '0' && input[0] <= '7') { + index = input[0] - '0'; + if (index < _totalContacts) { + break; + } + } + std::cout << "Invalid index! Please enter a valid contact index." << std::endl; + } + displayContact(index); +} + +std::string PhoneBook::truncateString(const std::string& str) const { + if (str.length() > 10) { + return str.substr(0, 9) + "."; + } + return str; +} + +void PhoneBook::displayContact(int index) const { + std::cout << std::endl << "=== Contact Details ===" << std::endl; + std::cout << "First Name: " << _contacts[index].getFirstName() << std::endl; + std::cout << "Last Name: " << _contacts[index].getLastName() << std::endl; + std::cout << "Nickname: " << _contacts[index].getNickname() << std::endl; + std::cout << "Phone Number: " << _contacts[index].getPhoneNumber() << std::endl; + std::cout << "Darkest Secret: " << _contacts[index].getDarkestSecret() << std::endl; + std::cout << "======================" << std::endl; +} diff --git a/ex01/PhoneBook.hpp b/ex01/PhoneBook.hpp new file mode 100644 index 0000000..42dc9a3 --- /dev/null +++ b/ex01/PhoneBook.hpp @@ -0,0 +1,22 @@ +#include "Contact.hpp" +#include +#include +#include +#include +#include + +class PhoneBook { +private: + Contact _contacts[8]; + int _currentIndex; + int _totalContacts; + +public: + PhoneBook(); + ~PhoneBook(); + + void addContact(); + void searchContacts() const; + void displayContact(int index) const; + std::string truncateString(const std::string& str) const; +}; \ No newline at end of file diff --git a/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..8d86231 --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,36 @@ +# include +# include +# include +#include "PhoneBook.hpp" + +int main() +{ + std::string command; + PhoneBook phoneBook; + + while (42) + { + std::cout << "Enter a command > "; + if (!std::getline(std::cin, command)) + { + std::cout << "\nBye!" << std::endl; + break; + } + else if (command == "ADD") + { + phoneBook.addContact(); + } + else if (command == "SEARCH") + { + phoneBook.searchContacts(); + } + else if (command == "EXIT") + { + std::cout << "EXIT" << std::endl; + break; + } + else + std::cout << "Invalid command. Please use ADD, SEARCH, or EXIT." << std::endl; + } + return 0; +} \ No newline at end of file