Added all codes.
This commit is contained in:
parent
f994f8bdb4
commit
7cb4ea2e3a
79
Codes/Code-A1 (Complex Numbers).cpp
Normal file
79
Codes/Code-A1 (Complex Numbers).cpp
Normal file
@ -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 <iostream>
|
||||
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<<endl<<"Enter first complex number (first real part then imaginary):\t";
|
||||
cin>>c1;
|
||||
cout<<"First complex number is:\t"<<c1<<endl;
|
||||
|
||||
cout<<endl<<"Enter first complex number (first real part then imaginary):\t";
|
||||
cin>>c2;
|
||||
cout<<"Second complex number is:\t"<<c2<<endl;
|
||||
|
||||
result = c1 + c2; // addition of c1 and c2
|
||||
cout<<endl<<"----------"<<endl<<"Addition is:\t"<<result;
|
||||
|
||||
result = c1 * c2; // multiplication of c1 and c2
|
||||
cout<<endl<<"Multiplication is:\t"<<result<<endl<<"----------"<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
// END OF CODE
|
242
Codes/Code-A2 (Student Database).cpp
Normal file
242
Codes/Code-A2 (Student Database).cpp
Normal file
@ -0,0 +1,242 @@
|
||||
/*
|
||||
THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
|
||||
|
||||
Problem Statement: Write a C++ program create a calculator for an arithmetic operator (+, -, *, /). The program should take two operands from user and performs the operation on those two operands depending upon the operator entered by user. Use a switch statement to select the operation. Finally, display the result.
|
||||
|
||||
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<iostream>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
|
||||
#define max 100;
|
||||
class student
|
||||
{
|
||||
string lic, dob, bg;
|
||||
public:
|
||||
student();
|
||||
student(student &);
|
||||
~student()
|
||||
{
|
||||
cout<<"\nDESTRUCTOR IS CALLED!!!!!"<<endl;
|
||||
cout<<"RECORD DELETED SUCCESSFULLY";
|
||||
}
|
||||
friend class info;
|
||||
};
|
||||
class info
|
||||
{
|
||||
string name, address, cls;
|
||||
char div;
|
||||
int roll_no;
|
||||
long c_number,*tel;
|
||||
static int cnt;
|
||||
public:
|
||||
void create(student &);
|
||||
void display(student &);
|
||||
|
||||
inline static void inccnt()
|
||||
|
||||
{
|
||||
cnt++;
|
||||
}
|
||||
inline static void showcnt()
|
||||
|
||||
{
|
||||
cout<<"\nTOTAL NO OF RECORDS ARE : "<<cnt;
|
||||
}
|
||||
info();
|
||||
info(info &);
|
||||
~info()
|
||||
{
|
||||
cout<<"\nDESTRUCTOR IS CALLED!!!"<<endl;
|
||||
cout<<"STUDENT DELETED SUCCESSFULLY";
|
||||
}
|
||||
};
|
||||
|
||||
int info::cnt;
|
||||
info::info()
|
||||
{
|
||||
name="Student Name";
|
||||
cls="SE-COMP";
|
||||
address="Student Address";
|
||||
div='A';
|
||||
roll_no=0000;
|
||||
c_number=8010059559;
|
||||
tel = new long;
|
||||
}
|
||||
student::student()
|
||||
{
|
||||
lic="LICENCE NUMBER";
|
||||
dob="dd/mm/yyyy";
|
||||
bg="A+";
|
||||
}
|
||||
info::info(info &obj)
|
||||
{
|
||||
this->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 *********************** " <<endl;
|
||||
cout<< "\nNAME OF STUDENT : " <<name<<endl;
|
||||
cout<< "\nCLASS OF STUDENT : " <<cls<<endl;
|
||||
cout<< "\nADDRESS OF STUDENT : " <<address<<endl;
|
||||
cout<< "\nDATE OF BIRTH : " <<obj.dob<<endl;
|
||||
cout<< "\nDIVISION : " <<div<<endl;
|
||||
cout<< "\nROLL NO : " <<roll_no<<endl;
|
||||
cout<< "\nBLOOD GROUP : " <<obj.bg<<endl;
|
||||
cout<< "\nLICEINCE NUMBER : " <<obj.lic<<endl;
|
||||
cout<< "\nPHONE NUMBER(CONTACT) : " <<c_number<<endl;
|
||||
cout << "\n TELEPHONE NUMBER : " << *tel << endl;
|
||||
cout<<"\n *********************** "<<endl;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int ch;
|
||||
char ans;
|
||||
|
||||
cout<<"\nENTER NUMBER OF STUDENTS :\t";
|
||||
cin>>n;
|
||||
cout<<"\n***********************"<<endl;
|
||||
info *sobj=new info[n];
|
||||
student *pobj=new student[n];
|
||||
|
||||
do
|
||||
{
|
||||
cout<<"\n #####...MENU...##### \n 1. CREATE STUDENT DATABASE \n 2. DISPLAY STUDENT DATABASE \n 3. COPY CONSTRUCTOR \n 4. DEFAULT CONSTRUCTOR \n 5. DELETE (Destructor) \n 6. EXIT";
|
||||
cout<<endl<<" Enter your Choice(1-6):\t ";
|
||||
cin>>ch;
|
||||
switch(ch)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
sobj[i].create(pobj[i]);
|
||||
sobj[i].inccnt();
|
||||
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
{
|
||||
sobj[0].showcnt();
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
sobj[i].display(pobj[i]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
info obj1;
|
||||
student obj2;
|
||||
obj1.create(obj2);
|
||||
info obj3(obj1);
|
||||
|
||||
student obj4(obj2);
|
||||
cout<<endl<<"\n Copy Constructor is called ";
|
||||
obj3.display(obj4);
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
{
|
||||
info obj1;
|
||||
student obj2;
|
||||
cout<<endl<<"\n Default Constructor is called ";
|
||||
obj1.display(obj2);
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
{
|
||||
delete [] sobj;
|
||||
delete [] pobj;
|
||||
}
|
||||
break;
|
||||
case 6:
|
||||
{
|
||||
// Exit the program
|
||||
cout<<" END OF THE PROGRAM ";
|
||||
//return 0;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
cout << "Invalid choice...... Please try again." << endl;
|
||||
}
|
||||
cout<<"\n Want to Continue(y/n): ";
|
||||
cin>>ans;
|
||||
}while(ans=='y') ;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
// END OF CODE
|
173
Codes/Code-A3 (Book Publication).cpp
Normal file
173
Codes/Code-A3 (Book Publication).cpp
Normal file
@ -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 <iostream>
|
||||
#include <limits> // 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<<endl<<"----- ENTER ACCURATE BOOK INFORMATION BELOW -----";
|
||||
cout<<endl<<"Title of your book:\t";
|
||||
cin>>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 -----"<<endl;
|
||||
}
|
||||
}
|
||||
catch (const invalid_argument& invalArg) {
|
||||
cerr << invalArg.what();
|
||||
cin.clear();
|
||||
cin.ignore(numeric_limits<streamsize>::max(), '\n');
|
||||
title="";
|
||||
price=0.0;
|
||||
bookPages=0;
|
||||
}
|
||||
}
|
||||
|
||||
void displayBook() {
|
||||
// Displaying information about the book.
|
||||
cout<<endl<<"---- INFORMATION ABOUT YOUR BOOK: -----";
|
||||
cout<<endl<<"Title of your book:\t"<<title;
|
||||
cout<<endl<<"Price of your book:\tRs. "<<price;
|
||||
cout<<endl<<"Number of pages in your book:\t"<<bookPages;
|
||||
cout<<endl<<"----- END OF INFORMATION -----"<<endl;
|
||||
}
|
||||
};
|
||||
|
||||
// ----- TAPE -----
|
||||
class TapePublication : protected KPublications {
|
||||
// Derived class (from base class KPublications) for tape.
|
||||
private:
|
||||
float tapeLength;
|
||||
public:
|
||||
TapePublication() {
|
||||
// Displaying information about the book.
|
||||
tapeLength=0.0;
|
||||
}
|
||||
|
||||
void aboutTape() {
|
||||
// Storing information about the tape.
|
||||
cout<<endl<<"---- ENTER ACCURATE TAPE INFORMATION ----";
|
||||
cout<<endl<<"Title of your tape:\t";
|
||||
cin>>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 -----"<<endl;
|
||||
}
|
||||
}
|
||||
catch (const invalid_argument& invalArg) {
|
||||
cerr<<invalArg.what()<<endl;
|
||||
cin.clear();
|
||||
cin.ignore(numeric_limits<streamsize>::max(), '\n');
|
||||
title="";
|
||||
price=0.0;
|
||||
tapeLength = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
void displayTape() {
|
||||
// Displaying information about the tape.
|
||||
cout<<endl<<"---- INFORMATION ABOUT YOUR TAPE: -----";
|
||||
cout<<endl<<"Title of your tape:\t"<<title;
|
||||
cout<<endl<<"Price of your tape:\tRs. "<<price;
|
||||
cout<<endl<<"Length of your tape (in minutes):\t"<<tapeLength;
|
||||
cout<<endl<<"----- END OF INFORMATION -----"<<endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
BookPublication pubBook;
|
||||
TapePublication pubTape;
|
||||
int optn;
|
||||
|
||||
while (1) {
|
||||
// Diplaying main option menu.
|
||||
cout<<endl<<"---- MAIN MENU ----";
|
||||
cout<<endl<<"1 -> STORE information about BOOK";
|
||||
cout<<endl<<"2 -> STORE information about TAPE";
|
||||
cout<<endl<<"3 -> SHOW information about BOOK";
|
||||
cout<<endl<<"4 -> SHOW information about TAPE";
|
||||
cout<<endl<<"5 -> Exit";
|
||||
|
||||
// User input for option.
|
||||
cout<<endl<<"Choose an option (1-5):\t";
|
||||
cin>>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<<endl<<"## END OF CODE\n\n";
|
||||
exit(0);
|
||||
default:
|
||||
cout<<endl<<"Please choose a valid option (1-5)"<<endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// END OF CODE
|
96
Codes/Code-B2 (File Operation).cpp
Normal file
96
Codes/Code-B2 (File Operation).cpp
Normal file
@ -0,0 +1,96 @@
|
||||
/*
|
||||
THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
|
||||
|
||||
Problem Statement: Write a C++ program that creates an output file, writes information to it, closes the file and open it again as an input file and read the information from the file.
|
||||
|
||||
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<iostream>
|
||||
#include<fstream>
|
||||
using namespace std;
|
||||
|
||||
class Employee {
|
||||
string name;
|
||||
int identity;
|
||||
double salary;
|
||||
|
||||
public:
|
||||
void accept() {
|
||||
cout<<endl<<"Enter name:\t";
|
||||
cin.ignore();
|
||||
getline(cin,name);
|
||||
cout<<"Enter ID:\t";
|
||||
cin>>identity;
|
||||
cout<<"Enter salary (in Rs.):\t";
|
||||
cin>>salary;
|
||||
}
|
||||
|
||||
void display() {
|
||||
cout<<endl<<"Name is:\t"<<name<<endl;
|
||||
cout<<"ID is:\t"<<identity<<endl;
|
||||
cout<<"Salary is (in Rs.):\t"<<salary<<endl;
|
||||
}
|
||||
};
|
||||
|
||||
void openFun() {
|
||||
Employee o[5];
|
||||
fstream f;
|
||||
int i,n;
|
||||
|
||||
cout<<endl<<"FILE OPERATION USING open() FUNCTION."<<endl;
|
||||
|
||||
f.open("info.txt",ios::out);
|
||||
cout<<endl<<"Enter total employees:\t";
|
||||
cin>>n;
|
||||
for(i=0;i<n;i++) {
|
||||
cout<<endl<<"Enter information of employee no. "<<i+1<<endl;
|
||||
o[i].accept();
|
||||
f.write((char*)&o[i],sizeof o[i]);
|
||||
}
|
||||
|
||||
f.close();
|
||||
|
||||
f.open("info.txt",ios::in);
|
||||
cout<<endl<<"Information stored in:\n";
|
||||
for(i=0;i<n;i++) {
|
||||
cout<<endl<<"Information of employee "<<i+1<<" is:\n";
|
||||
f.write((char*)&o[i],sizeof o[i]);
|
||||
o[i].display();
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
|
||||
void constFun() {
|
||||
Employee o[5];
|
||||
fstream f;
|
||||
int i,n;
|
||||
|
||||
cout<<endl<<"FILE OPERATION USING CONSTRUCTOR FUNCTION."<<endl;
|
||||
|
||||
ofstream outf("info.txt");
|
||||
cout<<endl<<"Enter total employees:\t";
|
||||
cin>>n;
|
||||
for(i=0;i<n;i++) {
|
||||
cout<<endl<<"Enter information of employee no. "<<i+1<<endl;
|
||||
o[i].accept();
|
||||
outf.write((char*)&o, sizeof o[i]);
|
||||
}
|
||||
|
||||
ifstream inf("info.txt");
|
||||
cout<<endl<<"Information stored is:\n";
|
||||
for(i=0;i<n;i++) {
|
||||
cout<<endl<<"Information of employee "<<i+1<<" is:\n";
|
||||
o[i].display();
|
||||
}
|
||||
inf.close();
|
||||
}
|
||||
|
||||
int main() {
|
||||
openFun();
|
||||
constFun();
|
||||
cout<<"\n## END OF FILE";
|
||||
return 0;
|
||||
}
|
||||
// END OF CODE
|
88
Codes/Code-B3 (Template - Selection Sort).cpp
Normal file
88
Codes/Code-B3 (Template - Selection Sort).cpp
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
THIS CODE HAS BEEN TESTED AND IS FULLY OPERATIONAL.
|
||||
|
||||
Problem Statement:Write a function template selection Sort. Write a program that inputs, sorts and outputs an integer array and a float array.
|
||||
|
||||
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<iostream>
|
||||
using namespace std;
|
||||
#define max 10
|
||||
|
||||
int n;
|
||||
template <class T>
|
||||
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
|
@ -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 <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
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: "<<name<<")(Telephone: "<<telephone<<")(DOB: "<<dob<<")";
|
||||
}
|
||||
};
|
||||
|
||||
bool compareDate(Person p1,Person p2){
|
||||
return ((p1.year==p2.year)?
|
||||
((p1.month==p2.month)?
|
||||
((p1.day<=p2.day)? true :false)
|
||||
:((p1.month<p2.month)?true:false ))
|
||||
:((p1.year<p2.year)?true:false ));
|
||||
|
||||
}
|
||||
bool compareTelephone(Person p1,Person p2){
|
||||
return (p1.telephone<p2.telephone);
|
||||
}
|
||||
bool compareName(Person p1,Person p2){
|
||||
return (p1.name<p2.name);
|
||||
}
|
||||
|
||||
Person return_obj(){
|
||||
Person a;
|
||||
a.input();
|
||||
return a;
|
||||
}
|
||||
void srch(vector<Person> vs1){
|
||||
Person p1;
|
||||
cout<<"Enter name of the person you want to find: "<<endl;
|
||||
cin>>p1.name;
|
||||
vector<Person>::iterator ir;
|
||||
ir=find(vs1.begin(),vs1.end(),p1);
|
||||
if(ir==vs1.end())
|
||||
cout<<"The person with name "<<p1.name<<"is not in the vector."<<endl;
|
||||
else{
|
||||
cout<<"Person found!"<<endl;
|
||||
ir->display();cout<<endl;
|
||||
}
|
||||
}
|
||||
void deletevec(vector<Person>& vs1){
|
||||
Person p1;
|
||||
cout<<"Enter name of the person you want to delete: "<<endl;
|
||||
cin>>p1.name;
|
||||
vector<Person>::iterator ir;
|
||||
ir=find(vs1.begin(),vs1.end(),p1);
|
||||
if(ir==vs1.end())
|
||||
cout<<"The person with name "<<p1.name<<"is not in the vector."<<endl;
|
||||
else
|
||||
{ ir->display();cout<<"\n";
|
||||
vs1.erase(ir);
|
||||
cout<<"Deleted\n";
|
||||
}
|
||||
}
|
||||
|
||||
void vec_display(vector<Person> vs1){
|
||||
cout<<"[";
|
||||
for (auto elem:vs1){
|
||||
elem.display();
|
||||
cout<<",";
|
||||
}
|
||||
cout<<"]\n";
|
||||
}
|
||||
int main(){
|
||||
|
||||
vector<Person> v1;
|
||||
|
||||
cout<<"Enter number of people: ";
|
||||
int n,ch,ch2;
|
||||
cin>>n;
|
||||
|
||||
for (int i=0;i<n;i++){
|
||||
v1.push_back(return_obj());
|
||||
}
|
||||
|
||||
while(1){
|
||||
cout<<"\t\tMenu\n1.Add Person\n";
|
||||
cout<<"2.Delete Person\n";
|
||||
cout<<"3.Sort\n";
|
||||
cout<<"4.Search\n";
|
||||
cout<<"5.Display\n";
|
||||
cout<<"6. Exit\n";
|
||||
cout<<"Choose an option (1-6): ";
|
||||
cin>>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
|
@ -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 <iostream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
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<i1.code)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
vector<Item> 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<<endl<<"Please choose a valid option."<<endl;
|
||||
}
|
||||
} while(ch!=7);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void insert() {
|
||||
Item i1;
|
||||
cout<<"\nEnter Item Name: ";
|
||||
cin>>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: "<<i1.name;
|
||||
cout<<"\nItem Quantity: "<<i1.quantity;
|
||||
cout<<"\nItem Cost: "<<i1.cost;
|
||||
cout<<"\nItem Code: "<<i1.code;
|
||||
}
|
||||
|
||||
void search() {
|
||||
vector<Item>::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."<<endl;
|
||||
cout<<"Item Name: "<<p ->name<<endl;
|
||||
cout<<"Item Quantity: "<<p ->quantity<<endl;
|
||||
cout<<"Item Cost: "<<p ->cost<<endl;
|
||||
cout<<"Item Code: "<<p ->code<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
void dlt() {
|
||||
vector<Item>::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
|
120
Codes/Code-C2 (Associative Container - Map).cpp
Normal file
120
Codes/Code-C2 (Associative Container - Map).cpp
Normal file
@ -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 <iostream>
|
||||
#include <map>
|
||||
#include <iterator> // THIS IS NOT NEEDED FOR NEWER C++ COMPILER, BUT SINCE COLLEGES USE OLD STUFF, HAD TO INCLUDE
|
||||
using namespace std;
|
||||
|
||||
map<string, int> 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<<endl<<"--- ENTER STATE INFORMATION ---\n";
|
||||
for (int i=0; i<total; i++) {
|
||||
cout<<endl<<"--- STATE "<<i+1<<" ---";
|
||||
cout<<endl<<"State name:\t";
|
||||
cin>>key;
|
||||
cout<<"State population:\t";
|
||||
cin>>value;
|
||||
m1.insert({key, value});
|
||||
cout<<"--- STATE SAVED ---"<<endl;
|
||||
}
|
||||
cout<<endl<<"--- ALL STATES SAVED ---"<<endl;
|
||||
}
|
||||
|
||||
void delState() {
|
||||
string key;
|
||||
cout<<endl<<"Specify the state name to delete:\t";
|
||||
cin>>key;
|
||||
|
||||
auto p = m1.find(key); // auto automatically determines the datatype (i.e. int, float, etc.)
|
||||
if (p == m1.end()) {
|
||||
cout<<endl<<"State ["<<key<<"] not found.\n";
|
||||
}
|
||||
else {
|
||||
cout<<endl<<"State ["<<p->first<<"] with population of ["<<p->second<<"] deleted.\n";
|
||||
m1.erase(p);
|
||||
}
|
||||
}
|
||||
|
||||
void displayState() {
|
||||
string key;
|
||||
cout<<endl<<"Specify state name to search:\t";
|
||||
cin>>key;
|
||||
|
||||
auto p = m1.find(key);
|
||||
cout<<endl<<"--- SEARCH RESULT ---";
|
||||
if (p == m1.end()) {
|
||||
cout<<endl<<"State ["<<key<<"] not found.\n";
|
||||
}
|
||||
else {
|
||||
|
||||
cout<<endl<<"State ["<<p->first<<"] has a population of:\t ["<<p->second<<"]"<<endl;
|
||||
}
|
||||
cout<<"--- END OF SEARCH ---"<<endl;
|
||||
}
|
||||
|
||||
void displayAll() {
|
||||
map<string, int>::iterator p;
|
||||
|
||||
p = m1.begin();
|
||||
while (p != m1.end()) {
|
||||
cout<<endl<<"--- STATE INFORMATION BEGINS ---";
|
||||
cout<<endl<<"State name:\t"<<p->first; // first = first value, i.e. key (state name)
|
||||
cout<<endl<<"State population:\t"<<p->second; // second = second value, i.e. value (state population)
|
||||
cout<<endl<<"--- STATE INFORMATION ENDS ---"<<endl;
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
int optn;
|
||||
|
||||
while (1) {
|
||||
cout<<endl<<"--- MAIN MENU ---";
|
||||
cout<<endl<<"1 -> Add states";
|
||||
cout<<endl<<"2 -> Delete state";
|
||||
cout<<endl<<"3 -> Search for a state";
|
||||
cout<<endl<<"4 -> Display all states";
|
||||
cout<<endl<<"5 -> Exit";
|
||||
cout<<endl<<"Choose an option (1-5):\t";
|
||||
cin>>optn;
|
||||
|
||||
switch (optn) {
|
||||
case 1:
|
||||
cout<<endl<<"Total states to add:\t";
|
||||
cin>>total;
|
||||
addState();
|
||||
break;
|
||||
case 2:
|
||||
delState();
|
||||
break;
|
||||
case 3:
|
||||
displayState();
|
||||
break;
|
||||
case 4:
|
||||
displayAll();
|
||||
break;
|
||||
case 5:
|
||||
cout<<endl<<"## DESIGNED AND ENGINEERED BY KSHITIJ\n## END OF CODE\n\n";
|
||||
exit(1);
|
||||
break;
|
||||
default:
|
||||
cout<<endl<<"Please choose a valid option (1-5)\n\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// END OF CODE
|
11
README.md
11
README.md
@ -6,6 +6,17 @@ Discover the essence of Object-Oriented Programming (OOP) in this repository. Ex
|
||||
|
||||
## Index
|
||||
|
||||
### Codes
|
||||
1. [OOP - Code A1 (Complex Numbers)](https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming/src/branch/main/Codes/Code-A1%20%28Complex%20Numbers%29.cpp)
|
||||
2. [OOP - Code A2 (Student Database)](https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming/src/branch/main/Codes/Code-A2%20%28Student%20Database%29.cpp)
|
||||
3. [OOP - Code A3 (Book+Casette Publication)](https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming/src/branch/main/Codes/Code-A3%20%28Book%20Publication%29.cpp)
|
||||
4. [OOP - Code B2 (File Operation)](https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming/src/branch/main/Codes/Code-B2%20%28File%20Operation%29.cpp)
|
||||
5. [OOP - Code B3 (Templates - Selection Sort)](https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming/src/branch/main/Codes/Code-B3%20%28Template%20-%20Selection%20Sort%29.cpp)
|
||||
6. Code C1 - Template Selection and Sorting
|
||||
- [Code-C1-1 (Person)](https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming/src/branch/main/Codes/Code-C1-1%20%28Template%20Selection%20Sort%20and%20Searching%20for%20Person%29.cpp)
|
||||
- [Code-C1-2 (Item)](https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming/src/branch/main/Codes/Code-C1-2%20%28Template%20Selection%20Sort%20and%20Searching%20for%20Item%29.cpp)
|
||||
7. [Code C2 - Associative Container - Map](https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming/src/branch/main/Codes/Code-C2%20%28Associative%20Container%20-%20Map%29.cpp)
|
||||
|
||||
### Write-ups
|
||||
1. [Write-up A1 - Complex Numbers](https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming/src/branch/main/Write-ups/OOP%20-%20Write-up%20A1%20%28Complex%20Numbers%29.pdf)
|
||||
2. [Write-up A2 - Student Database](https://git.kska.io/sppu-se-comp-content/ObjectOrientedProgramming/src/branch/main/Write-ups/OOP%20-%20Write-up%20A2%20%28Student%20Database%29.pdf)
|
||||
|
Loading…
Reference in New Issue
Block a user