CG And OOPs Practicals for 2023-24 For Computer Science Student By Karan Rathod

JIT COLLEGE CG AND OOP PRACTICAL BY                                  KARAN RATHOD                          

                                    💛


                                                                               (Aram Se copy kar bhai)                                                                    

 CG Practicals 

1


#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <math.h>

void drawcircle(int xc, int yc, int r) {
    int d = 3 - 2 * r;
    int x = 0;
    int y = r;
    while (y >= x) {
        putpixel(xc + x, yc + y, WHITE);
        putpixel(xc + y, yc + x, WHITE);
        putpixel(xc + y, yc - x, WHITE);
        putpixel(xc + x, yc - y, WHITE);
        putpixel(xc - x, yc - y, WHITE);
        putpixel(xc - y, yc - x, WHITE);
        putpixel(xc - y, yc + x, WHITE);
        putpixel(xc - x, yc + y, WHITE);
        x++;
        if (d > 0) {
            y--;
            d = d + 4 * (x - y) + 10;
        } else {
            d = d + 4 * x + 6;
        }
        delay(50);
    }
}

void drawline(int x1, int y1, int x2, int y2) {
    int dx = abs(x2 - x1);
    int dy = abs(y2 - y1);
    int steps = dx > dy ? dx : dy;
    float xinc = float(x2 - x1) / float(steps);
    float yinc = float(y2 - y1) / float(steps);
    float x = x1;
    float y = y1;
    for(int k = 0; k < steps; k++) {
        putpixel(x, y, WHITE);
        x = x + xinc;
        y = y + yinc;
    }
}

void main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
    int x, y, r;
    int x1, y1, x2, y2, x3;
    cout << "ENTER COORDINATES : ";
    cout << "X1 : ";
    cin >> x1;
    cout << "Y1 : ";
    cin >> y1;
    cout << "X2 : ";
    cin >> x2;
    x3 = (x2 + x1) / 2;
    y2 = y1 - sqrt(pow((x2 - x1), 2) - pow((x2 - x1) / 2, 2));
    drawline(x1, y1, x2, y1);
    drawline(x2, y1, x3, y2);
    drawline(x1, y1, x3, y2);
    x = x3;
    y = y2 + 2 * (y1 - y2) / 3;
    r = (y1 - y2) / 3;
    drawcircle(x, y, r);
    r = 2 * (y1 - y2) / 3;
    drawcircle(x, y, r);
    getch();
    closegraph();
}



2


#include <graphics.h>
#include <dos.h>
#include <conio.h>
#include <math.h>
#include <iostream.h>

class Coordinate {
public:
    int x, y;
    char code[4];
};

class Lineclip {
public:
    Coordinate PT;
    void drawwindow();
    void drawline(Coordinate p1, Coordinate p2);
    Coordinate setcode(Coordinate p);
    int visibility(Coordinate p1, Coordinate p2);
    Coordinate resetendpt(Coordinate p1, Coordinate p2);
};

void Lineclip::drawwindow() {
    line(150, 100, 450, 100);
    line(450, 100, 450, 350);
    line(450, 350, 150, 350);
    line(150, 350, 150, 100);
}

void Lineclip::drawline(Coordinate p1, Coordinate p2) {
    line(p1.x, p1.y, p2.x, p2.y);
}

Coordinate Lineclip::setcode(Coordinate p) {
    Coordinate ptemp;
    ptemp.code[0] = p.y < 100 ? '1' : '0';
    ptemp.code[1] = p.y > 350 ? '1' : '0';
    ptemp.code[2] = p.x > 450 ? '1' : '0';
    ptemp.code[3] = p.x < 150 ? '1' : '0';
    ptemp.x = p.x;
    ptemp.y = p.y;
    return ptemp;
}

int Lineclip::visibility(Coordinate p1, Coordinate p2) {
    for(int i = 0; i < 4; i++) {
        if(p1.code[i] != p2.code[i]) return 2;
        if(p1.code[i] == '1') return 1;
    }
    return 0;
}

Coordinate Lineclip::resetendpt(Coordinate p1, Coordinate p2) {
    Coordinate temp;
    int x, y;
    float m, k;
    if(p1.code[3] == '1' || p1.code[2] == '1') {
        x = p1.code[3] == '1' ? 150 : 450;
        m = (float)(p2.y - p1.y) / (p2.x - p1.x);
        k = (p1.y + (m * (x - p1.x)));
        temp.y = k;
        temp.x = x;
        for(int i = 0; i < 4; i++)
            temp.code[i] = p1.code[i];
        if(temp.y <= 350 && temp.y >= 100)
            return temp;
    }
    if(p1.code[0] == '1' || p1.code[1] == '1') {
        y = p1.code[0] == '1' ? 100 : 350;
        m = (float)(p2.y - p1.y) / (p2.x - p1.x);
        k = (float)p1.x + (float)(y - p1.y) / m;
        temp.x = k;
        temp.y = y;
        for(int i = 0; i < 4; i++)
            temp.code[i] = p1.code[i];
        return temp;
    }
    return p1;
}

void main() {
    int gd = DETECT, gm;
    Coordinate p1, p2, p3, p4;
    clrscr();
    cout << "\n Enter x1 and y1\n";
    cin >> p1.x >> p1.y;
    cout << "\n Enter x2 and y2\n";
    cin >> p2.x >> p2.y;
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
    Lineclip lc;
    lc.drawwindow();
    delay(2000);
    lc.drawline(p1, p2);
    delay(2000);
    cleardevice();
    delay(2000);
    p1 = lc.setcode(p1);
    p2 = lc.setcode(p2);
    int v = lc.visibility(p1, p2);
    delay(2000);
    switch(v) {
        case 0: lc.drawwindow();
                delay(2000);
                lc.drawline(p1, p2);
                break;
        case 1: lc.drawwindow();
                delay(2000);
                break;
        case 2: p3 = lc.resetendpt(p1, p2);
                p4 = lc.resetendpt(p2, p1);
                lc.drawwindow();
                delay(2000);
                lc.drawline(p3, p4);
                break;
    }
    delay(2000);
    closegraph();
    getch();
}


3


#include <graphics.h>
#include <dos.h>
#include <conio.h>
#include <math.h>
#include <iostream.h>

class kochCurve {
public:
    void koch(int it, int x1, int y1, int x5, int y5) {
        int x2, y2, x3, y3, x4, y4;
        int dx, dy;
        if (it == 0) {
            line(x1, y1, x5, y5);
        } else {
            delay(10);
            dx = (x5 - x1) / 3;
            dy = (y5 - y1) / 3;
            x2 = x1 + dx;
            y2 = y1 + dy;
            x3 = (int)(0.5 * (x1 + x5) + sqrt(3) * (y1 - y5) / 6);
            y3 = (int)(0.5 * (y1 + y5) + sqrt(3) * (x5 - x1) / 6);
            x4 = 2 * dx + x1;
            y4 = 2 * dy + y1;
            koch(it - 1, x1, y1, x2, y2);
            koch(it - 1, x2, y2, x3, y3);
            koch(it - 1, x3, y3, x4, y4);
            koch(it - 1, x4, y4, x5, y5);
        }
    }
};

void main() {
    int gd = DETECT, gm;
    kochCurve k;
    int it;
    clrscr();
    cout << "Enter Number Of Iterations : ";
    cin >> it;
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
    k.koch(it, 150, 20, 20, 280);
    k.koch(it, 280, 280, 150, 20);
    k.koch(it, 20, 280, 280, 280);
    getch();
    closegraph();
}


4


#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<conio.h>

class transform
{
public:
int m,a[20][20],c[20][20];
int i,j,k;
public:
void object();
void accept();
void operator *(float b[20][20]);
};

void transform::object()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line(300,0,300,600);
line(0,300,600,300);
for( i=0;i<m-1;i++)
{
line(300+a[i][0],300-a[i][1],300+a[i+1][0],300-a[i+1][1]);
}
line(300+a[0][0],300-a[0][1],300+a[i][0],300-a[i][1]);
for( i=0;i<m-1;i++)
{
line(300+c[i][0],300-c[i][1],300+c[i+1][0],300-c[i+1][1]);
}
line(300+c[0][0],300-c[0][1],300+c[i][0],300-c[i][1]);
int temp;
cout << "Press 1 to continue";
cin >> temp;
closegraph();
}

void transform::accept()
{
cout<<"\n";
cout<<"Enter the Number Of Edges:";
cin>>m;
cout<<"\nEnter The Coordinates :";
for(int i=0;i<m;i++)
{
for(int j=0;j<3;j++)
{
if(j>=2)
 a[i][j]=1;
else
cin>>a[i][j];
}
}
}

void transform::operator *(float b[20][20])
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
 c[i][j]=0;
for(int k=0;k<m;k++)
{
 c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
}

int main()
{
int ch,tx,ty,sx,sy;
float deg,theta,b[20][20];
transform t;
t.accept();
cout<<"\nEnter your choice";
cout<<"\n1.Translation"
"\n2.Scaling"
"\n3.Rotation\n";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nTRANSLATION OPERATION\n";
cout<<"Enter value for tx and ty:";
cin>>tx>>ty;
b[0][0]=b[2][2]=b[1][1]=1;
b[0][1]=b[0][2]=b[1][0]=b[1][2]=0;
b[2][0]=tx;
b[2][1]=ty;
t * b;
t.object();
break;
case 2: cout<<"\nSCALING OPERATION\n";
cout<<"Enter value for sx,sy:";
cin>>sx>>sy;
b[0][0]=sx;
b[1][1]=sy;
b[0][1]=b[0][2]=b[1][0]=b[1][2]=0;
b[2][0]=b[2][1]=0;
b[2][2] = 1;
t * b;
t.object();
break;
case 3: cout<<"\nROTATION OPERATION\n";
cout<<"Enter value for angle:";
cin>>deg;
theta=deg*(3.14/100);
b[0][0]=b[1][1]=cos(theta);
b[0][1]=sin(theta);
b[1][0]=sin(-theta);
b[0][2]=b[1][2]=b[2][0]=b[2][1]=0;
b[2][2]=1;
t * b;
t.object();
break;
default:
cout<<"\nInvalid choice";
}
getch();
return 0;
}


5


#include <graphics.h>
#include <dos.h>
#include <conio.h>
#include <math.h>
#include <iostream.h>
#include <stdlib.h> // Include this to fix the error

class walkingman {
public:
    void draw(int);
    void draw(int, int);
};

void walkingman::draw(int i) {
    line(20, 380, 580, 380);
    int base = 25 + i;
    int top = 35 + i;
    line(base, 380, top, 340);
    line(top + 10, 380, top, 340);
    line(top, 310, base, 330);
    delay(20);
    line(top, 340, top, 310);
    circle(top, 300, 10);
    line(top, 310, top + 15, 330);
    line(top + 15, 330, top + 15, 280);
    line(base - 10, 280, top + 50, 280);
    arc(top + 15, 280, 0, 180, 35);
    arc(top + 20, 330, 180, 360, 5);
}

void walkingman::draw(int x, int y) {
    char str[] = "|";
    for(int j = 0; j < 100; j++) {
        outtextxy(rand() % x, rand() % (y - 50), str);
        setcolor(WHITE);
    }
}

void main() {
    int gd = DETECT, gm;
    walkingman obj;
    clrscr();
    cout << "Enter x1 and y1 coordinates: ";
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
    for(int i = 0; i < 500; i++) {
        obj.draw(i);
        obj.draw(getmaxx(), getmaxy());
        delay(150);
        cleardevice();
    }
    getch();
    closegraph();
}


6


#include <graphics.h>
#include <conio.h> // Include this for getch()
#include <stdlib.h> // Include this for some other functions
#include <dos.h> // Include this for delay()

int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm,"C:\\TURBOC3\\BGI");
    int midx,midy,r=10;
    midx=getmaxx()/2;
    while(r<=50)
    {
        cleardevice();
        setcolor(WHITE);
        line(0,310,160,150);
        line(160,150,320,310);
        line(320,310,480,150);
        line(480,150,640,310);
        line(0,310,640,310);
        arc(midx,310,225,133,r);
        floodfill(midx,300,15);
        if(r>20)
        {
            setcolor(7);
            floodfill(2,2,15);
            setcolor(6);
            floodfill(150,250,15);
            floodfill(550,250,15);
            setcolor(2);
            floodfill(2,450,15);
        }
        delay(50);
        r+=2;
    }
    getch(); // Add this line to hold the graphics window open
    closegraph();
    return 0;
}


7


#include<iostream.h>
#include<graphics.h>
#include<conio.h> // Include this for clrscr() and getch()

class Polygon {
public:
    int xavg, yavg;
    void draw(int x1, int y1, int x2, int y2, int x3, int y3) {
        line(x1, y1, x2, y2);
        line(x2, y2, x3, y3);
        line(x3, y3, x1, y1);
        xavg = (x1 + x2 + x3) / 3;
        yavg = (y1 + y2 + y3) / 3;
    }
};

class Triangle : public Polygon {
public:
    void fill(int o_col, int n_col) {
        setfillstyle(SOLID_FILL, n_col);
        floodfill(xavg, yavg, o_col);
    }
};

int main() {
    int gd = DETECT, gm;
    int x1, y1, x2, y2, x3, y3;
    clrscr();
    cout << "\n\t Enter the points of triangle";
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
    Triangle t;
    t.draw(x1, y1, x2, y2, x3, y3);
    t.fill(WHITE, RED);
    getch(); // Add this line to hold the graphics window open
    closegraph();
    getch(); // Add this line to hold the console window open
    return 0;
}




OOPs Practacals 



1


#include<iostream>
using namespace std;

class Complex {
    int real;
    float imag;

public:
    Complex() : real(0), imag(0) {}

    Complex operator+(const Complex&);
    Complex operator*(const Complex&);

    friend istream& operator>>(istream&, Complex&);
    friend ostream& operator<<(ostream&, const Complex&);
};

Complex Complex::operator+(const Complex& obj) {
    Complex temp;
    temp.real = real + obj.real;
    temp.imag = imag + obj.imag;
    return temp;
}

Complex Complex::operator*(const Complex& obj) {
    Complex temp;
    temp.real = real * obj.real - imag * obj.imag;
    temp.imag = imag * obj.real + real * obj.imag;
    return temp;
}

istream& operator>>(istream& in, Complex& a) {
    cout << "\nEnter real part: ";
    in >> a.real;
    cout << "Enter imaginary part: ";
    in >> a.imag;
    return in;
}

ostream& operator<<(ostream& out, const Complex& a) {
    out << a.real << (a.imag >= 0 ? "+" : "") << a.imag << "i";
    return out;
}

int main() {
    Complex c1, c2, c3;
    int choice;

    do {
        cout << "\n1. Enter two complex numbers";
        cout << "\n2. Add two complex numbers";
        cout << "\n3. Multiply two complex numbers";
        cout << "\n4. Quit";
        cout << "\nEnter your choice: ";
        cin >> choice;

        switch (choice) {
            case 1:
                cout << "\nEnter first complex number";
                cin >> c1;
                cout << "Enter second complex number";
                cin >> c2;
                break;

            case 2:
                c3 = c1 + c2;
                cout << "\nResult after addition: " << c3 << endl;
                break;

            case 3:
                c3 = c1 * c2;
                cout << "\nResult after multiplication: " << c3 << endl;
                break;

            case 4:
                cout << "\nQuitting program.\n";
                break;

            default:
                cout << "\nInvalid choice. Try again.\n";
        }
    } while (choice != 4);

    return 0;
}


2


#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

class StudentDatabase {
    int roll;
    char name[20];
    char Class[10];
    char Div[10];
    char dob[12];
    char bg[5];
    char contact[10];
    char phone[12];
    char license[12];

public:
    static int stdCount;

    StudentDatabase() : roll(0) {
        strcpy(name, "Sachin");
        strcpy(Class, "I");
        strcpy(Div, "A");
        strcpy(dob, "11/11/2010");
        strcpy(bg, "A");
        strcpy(contact, "city");
        strcpy(phone, "9876543211");
        strcpy(license, "A0101010");
        ++stdCount;
    }

    StudentDatabase(const StudentDatabase &obj) {
        strcpy(name, obj.name);
        strcpy(dob, obj.dob);
        strcpy(Class, obj.Class);
        strcpy(Div, obj.Div);
        strcpy(bg, obj.bg);
        strcpy(contact, obj.contact);
        strcpy(phone, obj.phone);
        strcpy(license, obj.license);
        ++stdCount;
    }

    void getData() {
        cout << "\nEnter: name, roll, Class, Div, Dob, bg, contact, phone, license\n";
        cin >> name >> roll >> Class >> Div >> dob >> bg >> contact >> phone >> license;
    }

    friend void display(const StudentDatabase &d);

    ~StudentDatabase() {
        cout << "\n\n" << name << "(Object) is destroyed!";
    }

    static void count() {
        cout << "\nNo. of objects created: " << stdCount;
    }
};

void display(const StudentDatabase &d) {
    cout << "\n" << setw(12) << d.name << setw(5) << d.roll << setw(7) << d.Class
              << setw(3) << d.Div << setw(15) << d.dob << setw(6) << d.bg << setw(12)
              << d.contact << setw(15) << d.phone << setw(12) << d.license;
}

int StudentDatabase::stdCount;

int main() {
    int n, i;
    StudentDatabase d1, *ptr[5];
    cout << "\nDefault values:";
    display(d1);
    d1.getData();
    display(d1);
    StudentDatabase d2(d1);
    cout << "\n\nUse of copy constructor :\n";
    display(d2);
    cout << "\nHow many objects do you want to create?: ";
    cin >> n;

    for (i = 0; i < n; ++i) {
        ptr[i] = new StudentDatabase();
        ptr[i]->getData();
    }

    cout << "\n" << setw(12) << "Name" << setw(5) << "Roll" << setw(10) << "Class"
              << setw(5) << "Div" << setw(15) << "dob" << setw(6) << "bg" << setw(12)
              << "contact" << setw(15) << "phone" << setw(12) << "license";

    for (i = 0; i < n; ++i)
        display(*ptr[i]);

    StudentDatabase::count();

    for (i = 0; i < n; ++i)
        delete ptr[i];

    cout << "\nObjects deleted!";
    return 0;
}


3


#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

class StudentDatabase {
    int roll;
    char name[20];
    char Class[10];
    char Div[10];
    char dob[12];
    char bg[5];
    char contact[10];
    char phone[12];
    char license[12];

public:
    static int stdCount;

    StudentDatabase() : roll(0) {
        strcpy(name, "Sachin");
        strcpy(Class, "I");
        strcpy(Div, "A");
        strcpy(dob, "11/11/2010");
        strcpy(bg, "A");
        strcpy(contact, "city");
        strcpy(phone, "9876543211");
        strcpy(license, "A0101010");
        ++stdCount;
    }

    StudentDatabase(const StudentDatabase &obj) {
        strcpy(name, obj.name);
        strcpy(dob, obj.dob);
        strcpy(Class, obj.Class);
        strcpy(Div, obj.Div);
        strcpy(bg, obj.bg);
        strcpy(contact, obj.contact);
        strcpy(phone, obj.phone);
        strcpy(license, obj.license);
        ++stdCount;
    }

    void getData() {
        cout << "\nEnter: name, roll, Class, Div, Dob, bg, contact, phone, license\n";
        cin >> name >> roll >> Class >> Div >> dob >> bg >> contact >> phone >> license;
    }

    friend void display(const StudentDatabase &d);

    ~StudentDatabase() {
        cout << "\n\n" << name << "(Object) is destroyed!";
    }

    static void count() {
        cout << "\nNo. of objects created: " << stdCount;
    }
};

void display(const StudentDatabase &d) {
    cout << "\n" << setw(12) << d.name << setw(5) << d.roll << setw(7) << d.Class
              << setw(3) << d.Div << setw(15) << d.dob << setw(6) << d.bg << setw(12)
              << d.contact << setw(15) << d.phone << setw(12) << d.license;
}

int StudentDatabase::stdCount;

int main() {
    int n, i;
    StudentDatabase d1, *ptr[5];
    cout << "\nDefault values:";
    display(d1);
    d1.getData();
    display(d1);
    StudentDatabase d2(d1);
    cout << "\n\nUse of copy constructor :\n";
    display(d2);
    cout << "\nHow many objects do you want to create?: ";
    cin >> n;

    for (i = 0; i < n; ++i) {
        ptr[i] = new StudentDatabase();
        ptr[i]->getData();
    }

    cout << "\n" << setw(12) << "Name" << setw(5) << "Roll" << setw(10) << "Class"
              << setw(5) << "Div" << setw(15) << "dob" << setw(6) << "bg" << setw(12)
              << "contact" << setw(15) << "phone" << setw(12) << "license";

    for (i = 0; i < n; ++i)
        display(*ptr[i]);

    StudentDatabase::count();

    for (i = 0; i < n; ++i)
        delete ptr[i];

    cout << "\nObjects deleted!";
    return 0;
}


4




#include <fstreaam>
#include <iostream>
using namespace std;

void writetofile() {
    ofstream a_file("abcd.txt", ios::app);
    int n, id, sal;
    char name[10];

    cout << "Enter No of Records you want to enter:\n";
    cin >> n;

    for (int i = 0; i < n; ++i) {
        cout << "Enter Id, Name, and Salary of Emp:\n";
        cin >> id >> name >> sal;
        a_file << id << '\t' << name << '\t' << sal << '\n';
    }

    cout << endl;
}

void readfile() {
    ifstream b_file("abcd.txt", ios::in);
    int id, sal;
    char name[10];

    cout << "ID\tName\tSalary\n";

    while (b_file >> id >> name >> sal) {
        cout << id << '\t' << name << '\t' << sal << '\n';
    }

    cout << endl;
}

int main() {
    int ch;

    do {
        cout << "Main Menu for file\n";
        cout << "1. Write to file\n";
        cout << "2. Read from file\n";
        cout << "3. Exit\n";
        cout << "Enter your choice\n";
        cin >> ch;

        switch (ch) {
            case 1:
                writetofile();
                break;
            case 2:
                readfile();
                break;
            case 3:
                break;
            default:
                cout << "Wrong choice\n\n";
        }
    } while (ch != 3);

    return 0;
}


5


/* Title:Write a function template selection Sort. Write a program that inputs, sorts and outputs an int
array and a float array.
ASSIGNMENT NO. :
GROUP          :
ROLL NO.       :
BATCH          :
*/
#include <iostream>
using namespace std;
template <typename T>
//template <class T>
void sort()
{
int i, j,count;
T temp;
cout<<"\n How many no. u want to enter in array : ";
cin>>count;
T n[count];
cout<<"\n Enter" <<count<<" numbers : ";
for(i=0;i<count;i++)
{
cin>>n[i];
}
for(i=0;i<(count-1);i++)
{
for(j=i+1;j<count;j++)
{
if(n[i]>n[j])
{
temp=n[i];
n[i]=n[j];
n[j]=temp;
}
}
}
cout<<"\n The array in the sorted order is : "<<endl;
for(i=0;i<count;i++)
{
cout<<"\t"<<n[i];
}
}


int main()
{
int choice;
char ans;
do
{
cout<<"\n 1. Integer  sort. \n 2. Float sort.";
cout<<"\n Enter the input you want to sort : ";
cin>>choice;
switch(choice)
{
case 1 : sort<int>();
break;
case 2 : sort<float>();
break;
case 3 : cout<<"\n Invalid choice.";
break;
}
cout<<"\n Do u wish to continue (Y/N)?";
cin>>ans;
}while(ans=='Y' || ans=='y');
return 0;
}



6


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class Item {
public:
    char name[10];
    int quantity;
    int cost;
    string code;

    bool operator==(const Item& i1) {
        return code == i1.code;
    }

    bool operator<(const Item& i1) {
        return code < i1.code;
    }
};

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 *****\n1.Insert\n2.Display\n3.Search\n4.Sort\n5.Delete\n6.Exit\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);
        }
    } 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\nItem Name:"<<i1.name<<"\nItem Quantity:"<<i1.quantity<<"\nItem Cost:"<<i1.cost<<"\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.";
}

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."; }
}

 



..................................................{Best of Luck Akshay}.................................................................

Comments

Popular posts from this blog

Data Science Lab 2023/24 Practicals From Karan Rathod

25 Scientifically Proven Tips for More Effective Studying by KKR