[go: up one dir, main page]

0% found this document useful (0 votes)
15 views5 pages

Lab7 3

The document contains code for a C++ program that allows a user to store notes with names, phone numbers, and birthdates. The program allows adding notes, sorting by name or birthdate, searching by name or phone number, and displaying notes by birth month.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Lab7 3

The document contains code for a C++ program that allows a user to store notes with names, phone numbers, and birthdates. The program allows adding notes, sorting by name or birthdate, searching by name or phone number, and displaying notes by birth month.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include "Struct.

h"

void NOTE::input_data()
{
fullName = new char[50];
phoneNumber = new char[20];

cin.ignore();
cout << "Enter full name: ";
cin.getline(fullName, 50);

bool validPhoneNumber = false;


while (!validPhoneNumber) {
cout << "Enter phone number (numbers only): ";
cin.getline(phoneNumber, 20);
validPhoneNumber = true;
for (size_t j = 0; j < strlen(phoneNumber); ++j) {
if (!isdigit(phoneNumber[j])) {
validPhoneNumber = false;
break;
}
}
if (!validPhoneNumber) {
cout << "Invalid phone number! Please enter only numbers." <<
endl;
}
}

cout << "Enter birth date (day month year): ";

for (int j = 0; j < 3; ++j) {


cin >> birthDate[j];

}
}
void addNotes(NOTE* notes, int& numNotes) {
int numToAdd;
cout << "Enter the number of notes you want to add: ";
cin >> numToAdd;

for (int i = numNotes; i < numNotes + numToAdd; ++i) {


notes[i].input_data();
}

numNotes += numToAdd;
}

void NOTE::show_data()
{
cout << "Full name" << fullName << ", phone number:" << phoneNumber << ",
Birth date: " << birthDate[0] << "-" << birthDate[1] << "-" << birthDate[2] <<
endl;
}
void showPeople( NOTE* notes, int numNotes)
{
cout << "All people: " << endl;
for (int i = 0; i < numNotes; i++)
{
notes[i].show_data();
}
}

void SortBySurname(NOTE* notes, int numNotes) {


for (int i = 0; i < numNotes - 1; ++i) {
for (int j = 0; j < numNotes - i - 1; ++j) {
char* lastName1 = strrchr(notes[j].fullName, ' ');
char* lastName2 = strrchr(notes[j + 1].fullName, ' ');

if (lastName1 && lastName2 && strcmp(lastName1 + 1, lastName2 +


1) > 0) {

char* tempName = notes[j].fullName;


notes[j].fullName = notes[j + 1].fullName;
notes[j + 1].fullName = tempName;

char* tempPhone = notes[j].phoneNumber;


notes[j].phoneNumber = notes[j + 1].phoneNumber;
notes[j + 1].phoneNumber = tempPhone;

for (int k = 0; k < 3; ++k) {


int temp = notes[j].birthDate[k];
notes[j].birthDate[k] = notes[j + 1].birthDate[k];
notes[j + 1].birthDate[k] = temp;
}
}
}
}

cout << "Sorted list by Surname:\n";


for (int i = 0; i < numNotes; ++i) {
cout << "Full Name: " << notes[i].fullName << ", Phone Number: " <<
notes[i].phoneNumber << ", Birth Date: "
<< notes[i].birthDate[0] << "-" << notes[i].birthDate[1] << "-"
<< notes[i].birthDate[2] << endl;
}
}

void SortByBirthDate(NOTE* notes, int numNotes) {


for (int i = 0; i < numNotes - 1; ++i) {
for (int j = 0; j < numNotes - i - 1; ++j) {
for (int k = 0; k < 3; ++k) {
if (notes[j].birthDate[k] > notes[j + 1].birthDate[k]) {

for (int l = 0; l < 3; ++l) {


int temp = notes[j].birthDate[l];
notes[j].birthDate[l] = notes[j +
1].birthDate[l];
notes[j + 1].birthDate[l] = temp;
}

char* tempName = notes[j].fullName;


notes[j].fullName = notes[j + 1].fullName;
notes[j + 1].fullName = tempName;
char* tempPhone = notes[j].phoneNumber;
notes[j].phoneNumber = notes[j + 1].phoneNumber;
notes[j + 1].phoneNumber = tempPhone;
}
else if (notes[j].birthDate[k] < notes[j + 1].birthDate[k])
{
break;
}
}
}
}

cout << "Sorted list by Birth Date:\n";


for (int i = 0; i < numNotes; ++i) {
cout << "Full Name: " << notes[i].fullName << ", Phone Number: " <<
notes[i].phoneNumber << ", Birth Date: "
<< notes[i].birthDate[0] << "-" << notes[i].birthDate[1] << "-"
<< notes[i].birthDate[2] << endl;
}
}

void searchBySurname(NOTE* notes, int numNotes) {


const int MAX_SURNAME_LENGTH = 50; // Define the maximum length for the
surname
char searchSurname[MAX_SURNAME_LENGTH]; // Create an array to store the
surname

cout << "Enter the surname to search: ";


cin >> searchSurname;

bool found = false;

cout << "People with the surname '" << searchSurname << "':\n";
for (int i = 0; i < numNotes; ++i) {
char* lastName = strrchr(notes[i].fullName, ' ');
if (lastName && strcmp(lastName + 1, searchSurname) == 0) {
cout << "Full Name: " << notes[i].fullName << ", Phone Number: "
<< notes[i].phoneNumber << ", Birth Date: "
<< notes[i].birthDate[0] << "-" << notes[i].birthDate[1] <<
"-" << notes[i].birthDate[2] << endl;
found = true;
}
}

if (!found) {
cout << "No people found with the surname '" << searchSurname << "'.\
n";
}
}

void searchByPhoneNumber(NOTE* notes, int numNotes) {


char searchPhoneNumber[20];
cout << "Enter phone number to search: ";
cin.ignore();
cin.getline(searchPhoneNumber, 20);

bool found = false;


for (int i = 0; i < numNotes; ++i) {
if (strcmp(notes[i].phoneNumber, searchPhoneNumber) == 0) {
cout << "Full Name: " << notes[i].fullName << ", Birth Date: "
<< notes[i].birthDate[0] << "-" << notes[i].birthDate[1] <<
"-" << notes[i].birthDate[2] << endl;
found = true;
break;
}
}

if (!found) {
cout << "No person found with the provided phone number." << endl;
}
}

void displayByBirthMonth(NOTE* notes, int numNotes) {


int searchMonth;
cout << "Enter birth month (1-12) to display: ";
cin >> searchMonth;

bool found = false;


for (int i = 0; i < numNotes; ++i) {
if (notes[i].birthDate[1] == searchMonth) {
cout << "Full Name: " << notes[i].fullName << ", Phone Number: "
<< notes[i].phoneNumber << endl;
found = true;
}
}

if (!found) {
cout << "No people found with birthdays in the provided month." <<
endl;
}
}
#pragma once
#include <iostream>
#include<iomanip>
using namespace std;

struct NOTE {
char* fullName;
char* phoneNumber;
int birthDate[3];
void input_data();
void show_data();

};
void SortBySurname(NOTE* , int );
void SortByBirthDate(NOTE*, int);
void searchBySurname(NOTE* , int);
void searchByPhoneNumber(NOTE* , int );
void displayByBirthMonth(NOTE*, int );
void showPeople( NOTE*, int);
void addNotes(NOTE*, int&);
#include<iostream>
#include"Struct.h"
#include"voids.h"
using namespace std;

int main() {
NOTE notes[100];
int numNotes = 0;
int choice;

do {
cout << "\n1. Add Notes\n2. Sort Notes by Name\n3. Sort Notes by Birth
Date\n4. Search by Name\n5. Search by Phone Number\n6. Display People by Birth
Month\n7. Show all notes\n0. Exit\nEnter your choice: ";
cin >> choice;

switch (choice) {
case 1:
addNotes(notes, numNotes);
break;
case 2:
SortBySurname(notes, numNotes);
cout << "Notes sorted by Full Name." << endl;
break;
case 3:
SortByBirthDate(notes, numNotes);
cout << "Notes sorted by Date of Birth." <<endl;
break;
case 4:
searchBySurname(notes, numNotes);
break;
case 5:
searchByPhoneNumber(notes, numNotes);
break;
case 6:
displayByBirthMonth(notes, numNotes);
break;
case 7:
showPeople(notes, numNotes);
break;
case 8:
cout << "Exiting the program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice." << endl;
break;
}
} while (choice != 0);
system("pause");
return 0;
}

You might also like