From e034e1a6ff29f3a463b47ca1c543a85bc0489794 Mon Sep 17 00:00:00 2001 From: Dnyanesh Fulsundar Date: Wed, 11 Dec 2024 21:17:26 +0530 Subject: [PATCH] Add files via upload --- Class_Publication.cpp | 70 +++++++++++++++++ Complex.cpp | 68 ++++++++++++++++ Constructor.cpp | 179 ++++++++++++++++++++++++++++++++++++++++++ Employee_File.cpp | 57 ++++++++++++++ Map.cpp | 53 +++++++++++++ Searching_Sorting.cpp | 117 +++++++++++++++++++++++++++ Template.cpp | 67 ++++++++++++++++ 7 files changed, 611 insertions(+) create mode 100644 Class_Publication.cpp create mode 100644 Complex.cpp create mode 100644 Constructor.cpp create mode 100644 Employee_File.cpp create mode 100644 Map.cpp create mode 100644 Searching_Sorting.cpp create mode 100644 Template.cpp diff --git a/Class_Publication.cpp b/Class_Publication.cpp new file mode 100644 index 0000000..5df7745 --- /dev/null +++ b/Class_Publication.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +class publication { +public: + string title; + float prize; + + void getdata() { + cout << "Enter Name of Publication: "; + cin >> title; + cout << "Enter Price of Publication: "; + cin >> prize; + cout << endl; + } + + void putdata() { + cout << "Publication Title: " << title << endl; + cout << "Publication Price: " << prize << endl; + } +}; + +class book : public publication { +public: + int pagecount; + + void getdata() { + publication::getdata(); + cout << "Enter Book Page Count: "; + cin >> pagecount; + cout << endl; + } + + void putdata() { + publication::putdata(); + cout << "Book Page Count: " << pagecount << endl; + cout << endl; + } +}; + +class tape : public publication { +private: + float ptime; + +public: + void getdata() { + publication::getdata(); + cout << "Enter Tape Playing Time (Minutes): "; + cin >> ptime; + cout << endl; + } + + void putdata() { + publication::putdata(); + cout << "Tape Playing Time: " << ptime << " min" << endl; + cout << endl; + } +}; + +int main() { + book b; + tape t; + + b.getdata(); + t.getdata(); + b.putdata(); + t.putdata(); + + return 0; +} diff --git a/Complex.cpp b/Complex.cpp new file mode 100644 index 0000000..0ed7dba --- /dev/null +++ b/Complex.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; + +class complex { + float realp, imagp; + +public: + complex() { + realp = 0; + imagp = 0; + } + + complex(float x, float y) { + realp = x; + imagp = y; + } + + complex operator+(complex &); + complex operator*(complex &); + + friend istream &operator>>(istream &, complex &); + friend ostream &operator<<(ostream &, const complex &); +}; + +istream &operator>>(istream &din, complex &c) { + cout << "Enter real part of complex number: "; + din >> c.realp; + cout << "Enter imaginary part of complex number: "; + din >> c.imagp; + return din; +} + +ostream &operator<<(ostream &dout, const complex &c) { + dout << c.realp << " + " << c.imagp << "i"; + return dout; +} + +complex complex::operator+(complex &c) { + complex temp; + temp.realp = realp + c.realp; + temp.imagp = imagp + c.imagp; + return temp; +} + +complex complex::operator*(complex &c) { + complex mul; + mul.realp = (realp * c.realp) - (imagp * c.imagp); + mul.imagp = (imagp * c.realp) + (realp * c.imagp); + return mul; +} + +int main() { + complex c1, c2, c3; + + cout << "Enter complex number 1" << endl; + cin >> c1; + + cout << "Enter complex number 2" << endl; + cin >> c2; + + c3 = c1 + c2; + cout << "Addition of two complex numbers is: " << c3 << endl; + + c3 = c1 * c2; + cout << "Multiplication of two complex numbers is: " << c3 << endl; + + return 0; +} diff --git a/Constructor.cpp b/Constructor.cpp new file mode 100644 index 0000000..834c589 --- /dev/null +++ b/Constructor.cpp @@ -0,0 +1,179 @@ +#include +#include +#include +using namespace std; + +class db { + int roll; + char name[20]; + char Class[10]; + char Div[10]; + char dob[10]; + char bg[3], contact[10]; + char phone[10], license[12]; + +public: + static int stdno; + + static void count() { + cout << "No. of objects created: " << stdno << endl; + } + + void fin() { cout << "Inline Function!" << endl; } + + db() { + roll = 0; + strcpy(name, "Sachin"); + strcpy(Class, "I"); + strcpy(Div, "A"); + strcpy(dob, "11/11/1111"); + strcpy(bg, "A"); + strcpy(contact, "city"); + strcpy(phone, "9000000000"); + strcpy(license, "A0101010"); + ++stdno; + } + + db(db* ob) { + strcpy(name, ob->name); + strcpy(dob, ob->dob); + strcpy(Class, ob->Class); + strcpy(Div, ob->Div); + strcpy(bg, ob->bg); + strcpy(contact, ob->contact); + strcpy(phone, ob->phone); + strcpy(license, ob->license); + ++stdno; + } + + void getdata() { + cout << "Enter Name Of Student: "; + cin >> name; + cout << "Enter Roll Number: "; + cin >> roll; + cout << "Enter Class: "; + cin >> Class; + cout << "Enter Division: "; + cin >> Div; + cout << "Enter Date Of Birth: "; + cin >> dob; + cout << "Enter Blood Group: "; + cin >> bg; + cout << "Enter Address: "; + cin >> contact; + cout << "Enter Contact Number: "; + cin >> phone; + cout << "Enter License No.: "; + cin >> license; + } + + friend void display(const db& d); + + ~db() { + cout << this->name << "(Object) is destroyed!" << endl; + } +}; + +void display(const db& d) { + cout << setw(12) << "Index" << setw(12) << "Name" << setw(12) << "Roll" + << setw(10) << "Class" << setw(8) << "Div" << setw(12) << "DOB" + << setw(5) << "Blood Group" << setw(12) << "Contact" << setw(12) << "Phone" + << setw(12) << "License" << endl; + + cout << setw(12) << 0 << setw(12) << d.name << setw(12) << d.roll + << setw(10) << d.Class << setw(8) << d.Div << setw(12) << d.dob + << setw(5) << d.bg << setw(12) << d.contact << setw(12) << d.phone + << setw(12) << d.license << endl; +} + + + +int db::stdno = 0; + +int main() { + db defaultObj; + cout << "Default values of the object: "; + cout<> n; + db** ptr = new db*[n]; + for (i = 0; i < n; i++) { + ptr[i] = new db(); + ptr[i]->getdata(); + } + cout << setw(12) << "Index" << setw(12) << "Name" << setw(5) << "Roll" << setw(4) << "Class" << setw(4) << "Div" << setw(12) << + "DOB" << setw(4) << "Blood Group" << setw(12) << "Contact" << setw(12) << "Phone" << setw(12) << "License" << endl; + for (i = 0; i < n; i++) { + cout << setw(12) << i; + display(*ptr[i]); + } + db::count(); + + char choice; + do { + cout << "**Menu**"; + cout << "1. Add new object"; + cout << "2. Delete an object"; + cout << "3. Exit"; + cout << "Enter your choice: "; + cin >> choice; + switch (choice) { + case '1': { + db** newPtr = new db*[n + 1]; + for (i = 0; i < n; i++) { + newPtr[i] = ptr[i]; + } + newPtr[n] = new db(); + newPtr[n]->getdata(); + ptr = newPtr; + n++; + cout << setw(12) << "Index" << setw(12) << "Name" << setw(5) << "Roll" << setw(4) << "Class" << setw(4) << "Div" << setw(12) << + "DOB" << setw(4) << "Blood Group" << setw(12) << "Contact" << setw(12) << "Phone" << setw(12) << "license" << endl; + for (i = 0; i < n; i++) { + cout << setw(12) << i; + display(*ptr[i]); + } + db::count(); + break; + } + case '2': { + cout << "Enter the index of the object you want to delete (0 to " << n - 1 << "): "; + cin >> delIndex; + if (delIndex >= 0 && delIndex < n) { + delete ptr[delIndex]; + ptr[delIndex] = NULL; + cout << "Object at index " << delIndex << " deleted!" << endl; + } else { + cout << "Invalid index!" << endl; + } + + cout << "Remaining objects:" << endl; + for (i = 0; i < n; i++) { + if (ptr[i] != NULL) { + cout << setw(12) << i; + display(*ptr[i]); + } + } + break; + } + case '3': { + cout << "Exiting program." << endl; + break; + } + default: + cout << "Invalid choice!" << endl; + break; + } + } while (choice != '3'); + + for (i = 0; i < n; i++) { + if (ptr[i] != NULL) { + delete ptr[i]; + } + } + delete[] ptr; + return 0; +} diff --git a/Employee_File.cpp b/Employee_File.cpp new file mode 100644 index 0000000..927c543 --- /dev/null +++ b/Employee_File.cpp @@ -0,0 +1,57 @@ +#include +#include +using namespace std; + +class employee { + char name[20]; + int ID; + double salary; + +public: + void accept() { + cout << "Enter name: "; + cin >> name; + cout << "Enter ID: "; + cin >> ID; + cout << "Enter salary: "; + cin >> salary; + cout<> n; + + f.open("employee.txt", ios::out | ios::binary); + + cout << "Enter information of Employees:" << endl; + for(i = 0; i < n; i++) { + cout << "Enter information of Employee " << (i + 1) << ":" << endl; + o[i].accept(); + f.write((char*)&o[i], sizeof(o[i])); + } + f.close(); + + f.open("employee.txt", ios::in | ios::binary); + + cout << "Information of employees is as follows:" << endl; + for(i = 0; i < n; i++) { + f.read((char*)&o[i], sizeof(o[i])); + o[i].display(); + } + f.close(); + + return 0; +} \ No newline at end of file diff --git a/Map.cpp b/Map.cpp new file mode 100644 index 0000000..893cfe9 --- /dev/null +++ b/Map.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +using namespace std; + +int main() { + string state; + int population; + char ans = 'y'; + int choice; + map m; + map::iterator i; + + do { + cout << "**Main Menu**"<> choice; + + switch (choice) { + case 1: + cout << "Enter the name of state: "; + cin >> state; + cout << "Enter the population (in Cr): "; + cin >> population; + m.insert(pair(state, population)); + break; + + case 2: + cout << "State and Populations are: "; + for (i = m.begin(); i != m.end(); i++) + cout << "[" << (*i).first << ", " << (*i).second << "] "; + break; + + case 3: + cout << "Enter the name of state for searching its population: "; + cin >> state; + if (m.count(state) != 0) + cout << "Population is " << m.find(state)->second << " Cr"; + else + cout << "State is not present in the list" << endl; + break; + } + + cout << "Do you want to continue? (y/n): "<> ans; + } while (ans == 'y' || ans == 'Y'); + + return 0; +} diff --git a/Searching_Sorting.cpp b/Searching_Sorting.cpp new file mode 100644 index 0000000..907b09a --- /dev/null +++ b/Searching_Sorting.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +using namespace std; +typedef struct rec +{ + char name[20]; + char birthdt[20]; + char phone[11]; +}node; +node temp; +vector rec; +vector::iterator ptr; +bool compare(node &r1, node &r2) +{ + if(strcmp(r1.name, r2.name) < 0) + { + return true; + } + else + { + return false; + } +} +void create() +{ + int n, i; + cout << "How many elements do you want to insert: "; + cin >> n; + cout << "Enter the elements in the database:"<> temp.name; + cout << "Birthdt(dd-mm-yy): "; + cin >> temp.birthdt; + cout << "Phone number: "; + cin >> temp.phone; + rec.push_back(temp); + } +} +void display() +{ + cout << "\tThe contents of the database are:" << endl; + cout << "--------------------------------------------------" << endl; + cout << "NAME DATE OF BIRTH PHONE NUMBER" << endl; + cout << "------------------------------------------------------" << endl; + for(ptr = rec.begin(); ptr != rec.end(); ptr++) + { + cout << (*ptr).name << " " << (*ptr).birthdt << " " << (*ptr).phone << endl; + } +} +void searching() +{ + char key[20]; + int flag = 0; + cout << "Enter the name which you want to search: "; + cin >> key; + for(ptr = rec.begin(); ptr != rec.end(); ptr++) + { + if(strcmp((*ptr).name, key) == 0) + { + flag = 1; + break; + } + else + { + flag = 0; + } + } + if(flag == 1) + { + cout << "Desired element is present in the database:" << endl; + } + else + { + cout << "Desired element is not present in the database:" << endl; + } +} +void sorting() +{ + sort(rec.begin(), rec.end(), compare); + cout << "Record is sorted!" << endl; +} +int main() +{ + char ans = 'y'; + int choice; + cout << "\t Program for searching and sorting" << endl; + do + { + cout << "Main Menu" << endl; + cout << "1. Create a database" << endl; + cout << "2. Display the database" << endl; + cout << "3. Search the element in the database" << endl; + cout << "4. Sort the database (Name)" << endl; + cout << "Enter the choice what do you want: "; + cin >> choice; + switch(choice) + { + case 1: create(); + break; + case 2: display(); + break; + case 3: searching(); + break; + case 4: sorting(); + display(); + break; + } + cout << "Do you want to go back to the menu(y): "; + cin >> ans; + } while(ans == 'y'); + return 0; +} diff --git a/Template.cpp b/Template.cpp new file mode 100644 index 0000000..cbf0cf9 --- /dev/null +++ b/Template.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; + +const int MAX_SIZE = 100; +int n; + +template +void selection(T A[]) { + 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 << "The sorted list is: "; + for (i = 0; i < n; i++) { + cout << " " << A[i]; + } + cout << endl; +} + +int main() { + int i, A[MAX_SIZE]; + float B[MAX_SIZE]; + + cout << "**Selection Sort**"<> n; + + if (n > MAX_SIZE) { + cout << "Error: Maximum size exceeded. "; + return 1; + } + + cout << "Enter the integer elements: "; + for (i = 0; i < n; i++) { + cin >> A[i]; + } + selection(A); + + cout << "How many elements are there? "; + cin >> n; + + if (n > MAX_SIZE) { + cout << "Error: Maximum size exceeded. "; + return 1; + } + + cout << "Enter the float elements: "; + for (i = 0; i < n; i++) { + cin >> B[i]; + } + selection(B); + + return 0; +} \ No newline at end of file