From 7cb4ea2e3a9898d73f5c74d4cc637fdc5490b474 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Mon, 19 Aug 2024 23:10:16 +0530 Subject: [PATCH] Added all codes. --- Codes/Code-A1 (Complex Numbers).cpp | 79 ++++++ Codes/Code-A2 (Student Database).cpp | 242 ++++++++++++++++++ Codes/Code-A3 (Book Publication).cpp | 173 +++++++++++++ Codes/Code-B2 (File Operation).cpp | 96 +++++++ Codes/Code-B3 (Template - Selection Sort).cpp | 88 +++++++ ...lection Sort and Searching for Person).cpp | 161 ++++++++++++ ...Selection Sort and Searching for Item).cpp | 145 +++++++++++ .../Code-C2 (Associative Container - Map).cpp | 120 +++++++++ README.md | 11 + 9 files changed, 1115 insertions(+) create mode 100644 Codes/Code-A1 (Complex Numbers).cpp create mode 100644 Codes/Code-A2 (Student Database).cpp create mode 100644 Codes/Code-A3 (Book Publication).cpp create mode 100644 Codes/Code-B2 (File Operation).cpp create mode 100644 Codes/Code-B3 (Template - Selection Sort).cpp create mode 100644 Codes/Code-C1-1 (Template Selection Sort and Searching for Person).cpp create mode 100644 Codes/Code-C1-2 (Template Selection Sort and Searching for Item).cpp create mode 100644 Codes/Code-C2 (Associative Container - Map).cpp diff --git a/Codes/Code-A1 (Complex Numbers).cpp b/Codes/Code-A1 (Complex Numbers).cpp new file mode 100644 index 0000000..2388201 --- /dev/null +++ b/Codes/Code-A1 (Complex Numbers).cpp @@ -0,0 +1,79 @@ +/* +THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL. + +Problem Statement: Implement a class Complex which represents the Complex Number data type. Implement the following: +1. Constructor (including a default constructor which creates the complex number 0+0i). +2. Overloaded operator+ to add two complex numbers. +3. Overloaded operator* to multiply two complex numbers. +4. Overloaded << and >> to print and read Complex Numbers. + +Code from Object Oriented Programming (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming +*/ + +// BEGINNING OF CODE +#include +using namespace std; + +class Complex { + private: + float real; // Store real part of complex number + float imag; // Store imaginary part of complex number + public: + Complex() { + // default constructor + real = 0.0; + imag = 0.0; + } + Complex(float r, float i) { + // parameterized constructor + real = r; + imag = i; + } + + Complex operator+(Complex& obj) { + // overloading + operator + return Complex(real + obj.real, imag + obj.imag); + } + + Complex operator*(Complex& obj) { + // overloading * operator + return Complex(real * obj.real - imag * obj.imag, real * obj.imag + imag * obj.real); + } + + friend ostream& operator<<(ostream& os, Complex& obj) { + // overloading << (output) operator + os << obj.real << " + " << obj.imag << "i"; + return os; + } + + friend istream& operator>>(istream& is, Complex& obj) { + // overloading >> (input) operator + is >> obj.real; // input real part + is >> obj.imag; // input imaginary part + return is; + } +}; + +int main() { + Complex c1, c2, result; + // c1 stores value for first complex number + // c2 stores value for second complex number + // result stores addition/muliplication of the two + + cout<>c1; + cout<<"First complex number is:\t"<>c2; + cout<<"Second complex number is:\t"< +#include +using namespace std; + +#define max 100; +class student +{ + string lic, dob, bg; + public: + student(); + student(student &); + ~student() + { + cout<<"\nDESTRUCTOR IS CALLED!!!!!"<name=obj.name; + this->cls=obj.cls; + this->address=obj.address; + this->div=obj.div; + this->roll_no=obj.roll_no; + this->c_number=obj.c_number; + this->tel=obj.tel; +} +student::student(student &obj) +{ + lic=obj.lic; + dob=obj.dob; + bg=obj.bg; +} + +void info::create(student &obj) +{ +cout<<"\nENTER NAME OF THE STUDENT:\t "; +cin>>name; +cout<<"\nENTER CLASS OF THE STUDENT:\t "; +cin>>cls; +cout<<"\nENTER ADDRESS OF THE STUDENT :\t "; +cin>>address; +cout<<"\nENTER DATE OF BIRTH(dd/mm/yyyy) :\t "; +cin>>obj.dob; +cout<<"\nENTER DIVISION OF STUDENT:\t "; +cin>>div; +cout<<"\nENTER ROLL NUMBER OF STUDENT :\t "; +cin>>roll_no; +cout<<"\nENTER BLOOD GROUP OF STUDENT :\t "; +cin>>obj.bg; +cout<<"\nENTER DRIVERS LICENCE NUMBER :\t "; +cin>>obj.lic; +cout<<"\nENTER PHONE NUMBER (CONTACT) :\t "; +cin>>c_number; +long no; + int total = 0, flag = 0; + while (flag != 1) + { + cout << "ENTER TELEPHONE NUMBER :\t "; + cin >> *tel; + no = *tel; + while (no > 0) + { + total++; + no = no / 10; + } + cout << "Total digits in telNo :\t " << total << " (required: 10)" << endl; + try + { + if (total == 10) + { + cout << " Correct number....." << endl; + flag = 1; + } + else + { + throw(*tel); + } + } + catch (long n) + { + cout << " Incorrect number..... " << n << endl; + total=0; + } + } +} + + +void info::display(student &obj) +{ +cout<<" \n *********************** " <>n; + cout<<"\n***********************"<>ch; + switch(ch) + { + case 1: + { + for(int i=0;i>ans; + }while(ans=='y') ; + + + return 0; +} +// END OF CODE diff --git a/Codes/Code-A3 (Book Publication).cpp b/Codes/Code-A3 (Book Publication).cpp new file mode 100644 index 0000000..167f61e --- /dev/null +++ b/Codes/Code-A3 (Book Publication).cpp @@ -0,0 +1,173 @@ +/* +THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL. + +Problem Statement: Imagine a publishing company which does marketing for book and audio cassette versions. Create a class publication that stores the title (a string) and price (type float) of a publication. +From this class derive two classes: book, which adds a page count (type int), and tape, which adds a playing time in minutes (type float). Write a program that instantiates the book and tape classes, allows user to enter data and displays the data members. If an exception is caught, replace all the data member values with zero values. + +Code from Object Oriented Programming (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming +*/ + +// BEGINNING OF CODE +#include +#include // For exception handling. +using namespace std; + +class KPublications { +// Base class that holds title and price for books. + protected: + string title; + float price; + public: + KPublications() { + // Constuctor to initialize zero values. + title=""; + price=0.0; + } +}; + +// ----- BOOK ----- +class BookPublication : protected KPublications { +// Derived class (from base class KPublications) for books. + private: + int bookPages; + public: + BookPublication() { + // Constuctor to initialize zero values. + bookPages=0; + } + + void aboutBook() { + // Storing information bout the book. + cout<>title; + cout<<"Price of your book:\tRs. "; + cin>>price; + + try { + // Exception handling for total pages in book. + cout<<"Total pages in your book:\t"; + cin>>bookPages; + + if (cin.fail()) { + throw invalid_argument("Invalid input. Please enter integer value.\n---- INFORMATION DISCARDED -----\n"); + } + else { + cout<<"----- INFORMATION SAVED -----"<::max(), '\n'); + title=""; + price=0.0; + bookPages=0; + } + } + + void displayBook() { + // Displaying information about the book. + cout<>title; + cout<<"Price of your tape:\tRs. "; + cin>>price; + + try { + // Exception handling for tape length input. + cout<<"Length of your tape (in minutes):\t"; + cin>>tapeLength; + + if (cin.fail()) { + throw invalid_argument("Invalid input. Please enter float value.\n----- INFORMATION DISCARDED -----\n"); + } + else { + cout<<"----- INFORMATION SAVED -----"<::max(), '\n'); + title=""; + price=0.0; + tapeLength = 0.0; + } + } + + void displayTape() { + // Displaying information about the tape. + cout< STORE information about BOOK"; + cout< STORE information about TAPE"; + cout< SHOW information about BOOK"; + cout< SHOW information about TAPE"; + cout< Exit"; + + // User input for option. + cout<>optn; + + // Switch case. + switch (optn) { + case 1: + pubBook.aboutBook(); + break; + case 2: + pubTape.aboutTape(); + break; + case 3: + pubBook.displayBook(); + break; + case 4: + pubTape.displayTape(); + break; + case 5: + cout< +#include +using namespace std; + +class Employee { + string name; + int identity; + double salary; + + public: + void accept() { + cout<>identity; + cout<<"Enter salary (in Rs.):\t"; + cin>>salary; + } + + void display() { + cout<>n; + for(i=0;i>n; + for(i=0;i +using namespace std; +#define max 10 + +int n; +template +void selection(T A[max]) +{ + int i, j, min; + T temp; + for (i = 0; i < n - 1; i++) + { + min = i; + for (j = i + 1; j < n; j++) + { + if (A[j] < A[min]) + { + min = j; + } + } + temp = A[i]; + A[i] = A[min]; + A[min] = temp; + } + cout << "\nSorted list : "; + for (i = 0; i < n; i++) + cout << A[i] << " "; +} + +int main() +{ + int i, A[max]; + float B[max]; + int choice; + bool flag1 = true; + while (flag1) + { + cout << "\nYOUR CHOICES ARE......\n"; + cout << "\nSelection sort"; + cout << "\n1. Integer Element: \n2. Float element: \n3. Exit: "; + cout << "\nEnter choice: "; + cin >> choice; + switch (choice) + { + case 1: + cout << "\nEnter desired number of elements: "; + cin >> n; + cout << "\nEnter integer elements: \n"; + for (i = 0; i < n; i++) + { + cin >> A[i]; + } + selection(A); + break; + + case 2: + cout << "\nFloat elements........"; + cout << "\nEnter desired number of elements: "; + cin >> n; + cout << "\nEnter float elements: \n"; + for (i = 0; i < n; i++) + { + cin >> B[i]; + } + selection(B); + break; + + case 3: + flag1 = false; + break; + + default: + cout << "\nEnter valid choice!!!"; + break; + } + } + return 0; +} +// END OF CODE diff --git a/Codes/Code-C1-1 (Template Selection Sort and Searching for Person).cpp b/Codes/Code-C1-1 (Template Selection Sort and Searching for Person).cpp new file mode 100644 index 0000000..675d5c1 --- /dev/null +++ b/Codes/Code-C1-1 (Template Selection Sort and Searching for Person).cpp @@ -0,0 +1,161 @@ +/* +THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL. + +Problem Statement: Write C++ program using STL for sorting and searching with user defined records such as person record (Name, DOB, Telephone number) using vector container. + +Code from Object Oriented Programming (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming +*/ + +// BEGINNING OF CODE +#include +#include +#include +#include +using namespace std; + +class Person +{ +public: +string name; +int day,month,year; +int telephone; +string dob; +Person(){ +name=""; +telephone=0; +day=1; +month=1; +year=2023; + +} +bool operator==(Person p2){ + return name==p2.name; +} +void input(){ +cout<<"Enter name: "; +cin>>name; +cout<<"Enter telephone: "; +cin>>telephone; +cout<<"Date of birth (first DD [ENTER], then MM [ENTER], then YYYY [ENTER] format): "; +cin>>day>>month>>year; +dob=""+ to_string(day)+"/"+to_string(month)+"/"+to_string(year); +} +void display () +{ +cout<<"(Name: "< vs1){ + Person p1; +cout<<"Enter name of the person you want to find: "<>p1.name; +vector::iterator ir; +ir=find(vs1.begin(),vs1.end(),p1); +if(ir==vs1.end()) +cout<<"The person with name "<display();cout<& vs1){ + Person p1; + cout<<"Enter name of the person you want to delete: "<>p1.name; + vector::iterator ir; + ir=find(vs1.begin(),vs1.end(),p1); + if(ir==vs1.end()) + cout<<"The person with name "<display();cout<<"\n"; + vs1.erase(ir); + cout<<"Deleted\n"; + } +} + +void vec_display(vector vs1){ + cout<<"["; + for (auto elem:vs1){ + elem.display(); + cout<<","; + } + cout<<"]\n"; +} +int main(){ + + vector v1; + + cout<<"Enter number of people: "; + int n,ch,ch2; + cin>>n; + + for (int i=0;i>ch; + switch(ch){ + case 1: + v1.push_back(return_obj()); + break; + case 2: + deletevec(v1); + break; + case 3: + cout<<"1.Sort by DOB , 2 Sort by Name , 3 Sort by Telephone no.\n"; + cin>>ch2; + switch(ch2){ + case 1: + sort(v1.begin(),v1.end(),compareDate); + break; + case 2: + sort(v1.begin(),v1.end(),compareName); + break; + case 3: + sort(v1.begin(),v1.end(),compareTelephone); + break; + } + vec_display(v1); + break; + case 4: + srch(v1); + break; + case 5: + vec_display(v1);break; + case 6: + exit(1); + default: + cout<<"Please choose a valid option.\n"; + } + } +} +// END OF CODE diff --git a/Codes/Code-C1-2 (Template Selection Sort and Searching for Item).cpp b/Codes/Code-C1-2 (Template Selection Sort and Searching for Item).cpp new file mode 100644 index 0000000..461fc12 --- /dev/null +++ b/Codes/Code-C1-2 (Template Selection Sort and Searching for Item).cpp @@ -0,0 +1,145 @@ +/* +THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL. + +Problem Statement: Write C++ program using STL for sorting and searching with user defined records such as item record (Item code, name, cost,quantity) using vector container + +Code from Object Oriented Programming (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming +*/ + +// BEGINNING OF CODE +#include +#include +#include +using namespace std; + +class Item { + public: + char name[10]; + int quantity; + int cost; + int code; + + bool operator==(const Item& i1) { + if(code==i1.code) + return 1; + return 0; + } + + bool operator<(const Item& i1) { + if(code o1; +void print(Item &i1); +void display(); +void insert(); +void search(); +void dlt(); + +bool compare(const Item &i1, const Item &i2) { + return i1.cost < i2.cost; +} + +int main() { + int ch; + do { + cout<<"\n*** Menu ***"; + cout<<"\n1.Insert"; + cout<<"\n2.Display"; + cout<<"\n3.Search"; + cout<<"\n4.Sort"; + cout<<"\n5.Delete"; + cout<<"\n6.Exit"; + cout<<"\nEnter your choice: "; + cin>>ch; + + switch(ch) { + case 1: + insert(); + break; + case 2: + display(); + break; + case 3: + search(); + break; + case 4: + sort(o1.begin(),o1.end(),compare); + cout<<"\n\n Sorted on Cost"; + display(); + break; + case 5: + dlt(); + break; + case 6: + exit(0); + default: + cout<>i1.name; + cout<<"\nEnter Item Quantity: "; + cin>>i1.quantity; + cout<<"\nEnter Item Cost: "; + cin>>i1.cost; + cout<<"\nEnter Item Code: "; + cin>>i1.code; + o1.push_back(i1); +} + +void display() { + for_each(o1.begin(),o1.end(),print); +} + +void print(Item &i1) { + cout<<"\n"; + cout<<"\nItem Name: "<::iterator p; + Item i1; + cout<<"\nEnter Item Code to search: "; + cin>>i1.code; + p=find(o1.begin(),o1.end(),i1); + + if(p==o1.end()) { + cout<<"\nNot found."; + } + else { + cout<<"\nFound."<name<quantity<cost<code<::iterator p; + Item i1; + cout<<"\nEnter Item Code to delete: "; + cin>>i1.code; + p=find(o1.begin(),o1.end(),i1); + + if(p==o1.end()) { + cout<<"\nNot found."; + } + else { + o1.erase(p); + cout<<"\nDeleted."; + } +} +// END OF CODE diff --git a/Codes/Code-C2 (Associative Container - Map).cpp b/Codes/Code-C2 (Associative Container - Map).cpp new file mode 100644 index 0000000..b67c296 --- /dev/null +++ b/Codes/Code-C2 (Associative Container - Map).cpp @@ -0,0 +1,120 @@ +/* +THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL. + +Problem Statement: Write a program in C++ to use map associative container. The keys will be the names of states, and the values will be the populations of the states. When the program runs, the user is prompted to type the name of a state. The program then looks in the map, using the state name as an index, and returns the population of the state. + +Code from Object Oriented Programming (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming +*/ + +// BEGINNING OF CODE +#include +#include +#include // THIS IS NOT NEEDED FOR NEWER C++ COMPILER, BUT SINCE COLLEGES USE OLD STUFF, HAD TO INCLUDE +using namespace std; + +map m1; // Creating an associative container (map) with string as key (state name) and int as value (state population) +int total; // Store total number of states + +void addState() { + string key; + int value; + + cout<>key; + cout<<"State population:\t"; + cin>>value; + m1.insert({key, value}); + cout<<"--- STATE SAVED ---"<>key; + + auto p = m1.find(key); // auto automatically determines the datatype (i.e. int, float, etc.) + if (p == m1.end()) { + cout<first<<"] with population of ["<second<<"] deleted.\n"; + m1.erase(p); + } +} + +void displayState() { + string key; + cout<>key; + + auto p = m1.find(key); + cout<first<<"] has a population of:\t ["<second<<"]"<::iterator p; + + p = m1.begin(); + while (p != m1.end()) { + cout<first; // first = first value, i.e. key (state name) + cout<second; // second = second value, i.e. value (state population) + cout< Add states"; + cout< Delete state"; + cout< Search for a state"; + cout< Display all states"; + cout< Exit"; + cout<>optn; + + switch (optn) { + case 1: + cout<>total; + addState(); + break; + case 2: + delState(); + break; + case 3: + displayState(); + break; + case 4: + displayAll(); + break; + case 5: + cout<