0% found this document useful (0 votes)
10 views

CPP LABPGM 1- 12

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

CPP LABPGM 1- 12

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

//CPP1 LABPGM1 Complex Number

#include <iostream.h>
#include<conio.h>
class complexno
{
private:
int real,img;
public:
complexno()
{
real=0;
img=0;
}
void get()
{
cout<<"\nreal: ";
cin>>real;
cout<<"imaginary: ";
cin>>img;
}
void disp()
{
cout<<real<<"+i"<<img;
}
complexno add(complexno c1,complexno c2)
{
complexno c3;
c3.real = c1.real+c2.real;
c3.img=c1.img+c2.img;
return c3;
}
complexno sub(complexno c1,complexno c2)
{
complexno c3;
c3.real = c1.real-c2.real;
c3.img=c1.img-c2.img;
return c3;
}
complexno multiply(complexno c1,complexno c2)
{
complexno c3;
c3.real = (c1.real*c2.real)-(c1.img*c2.img);
c3.img=(c1.real*c2.img)+(c1.img*c2.real);
return c3;
}
complexno scalar(complexno c1,int n1)
{
complexno c3;
c3.real = c1.real*n1;
c3.img=c1.img*n1;
return c3;
}
};

void main()
{
cout<<"Complex Numbers";
int n;
complexno c1,c2,c3;
cout<<"\nFirst Complex Number";
c1.get();
cout<<"\nSecond Complex Number";
c2.get();
c3=c3.add(c1,c2);
cout<<"\nFirst Complex Number: ";
c1.disp();
cout<<"\nSecond Complex Number: ";
c2.disp();
cout<<"\nAddition of 2 Complex Numbers: ";
c3.disp();

c3=c3.sub(c1,c2);
cout<<"\nSubtraction of 2 Complex Numbers: ";
c3.disp();

c3=c3.multiply(c1,c2);
cout<<"\nMultiplication of 2 Complex Numbers: ";
c3.disp();

cout<<"\nScalar Multiplication";
cout<<"\n n= ";
cin>>n;

c3=c3.scalar(c1,n);
cout<<"\n Scalar Multiplication of first complex no: ";
c3.disp();

}
//CPP2 LABPGM2 2D-point
#include <iostream>
#include<math.h>
using namespace std;
class point2d
{
public:
int x,y;
point2d()
{
x=0;
y=0;
}
void get()
{
cout<<"\nx: ";
cin>>x;
cout<<"y: ";
cin>>y;
}
void disp()
{
cout<<"("<<x<<" , "<<y<<")";
}
float dist(point2d p2)
{
return sqrt(pow(p2.x - x, 2) + pow(p2.y - y, 2) * 1.0);
}
void equ(point2d p2)
{
if((x==p2.x)&&(y==p2.y))
cout<<"\nBoth the points are Equal";
else
cout<<"\nBoth the points are not Equal";
}
};

int main()
{
cout<<"\nPoint 2D";
int n;
point2d p1,p2;
cout<<"\nFirst Coordinates";
p1.get();
cout<<"\nSecond Coordinates";
p2.get();

cout<<"\nFirst Coordinates: ";


p1.disp();
cout<<"\nSecond Coordinates: ";
p2.disp();
cout<<"\nDistance between two points is: "<<p1.dist(p2);
p1.equ(p2);

return 0;
}

// CPP LABPRGM 3
//Harmonic Progression
#include <iostream>
using namespace std;
class hp
{
public:
int a,n,d;
void get()
{
cout<<"\nFirst Term: ";
cin>>a;
cout<<"n: ";
cin>>n;
cout<<"Difference: ";
cin>>d;
}
void disp(){
for(float i = 1; i <= n; i++){
cout<<"1/"<<(float)(a+(i-1)*d);
if(i!=n)cout<<"+";

}
}
float findSeriesSum(){
float sumVal = 0;
float term = 0;
for(float i = 1; i <= n; i++){
term = (1.0)/(float)(a + (i-1)*d);
sumVal += term;
}
return sumVal;
}
};
int main(){
hp h1;
h1.get();
h1.disp();
cout<<"\nThe sum of HP is "<<h1.findSeriesSum();
return 0;
}

//CPP4 LABPRGM 4 Volume & TSA of 3Dshapes


#include <iostream.h>
#include<conio.h>
class solidobj
{
private:
int l,r,h;
float v,sa;
public:
void get()
{
cout<<"\nRadius: ";
cin>>r;
cout<<"Height";
cin>>h;
cout<<"Length";
cin>>l;
}
void cube()
{
v=l*l*l;
sa=6*l*l;
}
void cubiod()
{
v=l*r*h;
sa=2*(1*r+r*h+l*h);
}
void cone()
{
v=(0.33)*(3.14)*(r*r*h);
sa=(3.14)*(r)*(l+r);
}
void cylinder()
{
v=(3.14)*(r*r*h);
sa=(3.14)*(2*r)*(h+r);
}
void sphere()
{
v=(4/3)*(3.14)*(r*r*r);
sa=4*(3.14)*r*r;
}
void disp()
{
cout<<"\nvolume: "<<v;
cout<<"\nSurface Area: "<<sa;
}
};
void main()
{
printf("Volume and Surface Area for Solid Objects ");
solidobj s1;
s1.get();
cout<<"\nCUBE";
s1.cube();
s1.disp();
cout<<"\nCUBIOD";
s1.cubiod();
s1.disp();
cout<<"\nCONE";
s1.cone();
s1.disp();
cout<<"\nCYLINDER";
s1.cylinder();
s1.disp();
cout<<"\nSPHERE";
s1.sphere();
s1.disp();
}

//CPP5 LABPRGM 5
//time program
#include <iostream>

using namespace std;


class hour
{
public:
int hh,mm,ss,min;
void get()
{
cout<<"\nHour: ";
cin>>hh;
cout<<"Minute: ";
cin>>mm;
cout<<"Seconds: ";
cin>>ss;
}
void disp()
{
cout<<hh<<" : "<<mm<<" : "<<ss;
}
hour diff(hour h1,hour h2)
{
hour h3;
h3.hh=h1.hh-h2.hh;
h3.mm=h1.mm-h2.mm;
h3.ss=h1.ss=h2.ss;
return h3;
}
void add()
{
cout<<"\nExtra Mins: ";
cin>>min;
mm=mm+min;
}
int conv()
{
int sec=(hh*60*60)+(mm*60)+ss;
return sec;
}

};
int main()
{
cout<<"Time Implementation";
hour h1,h2,h3;
h1.get();
cout<<"First Time: ";
h1.disp();
h2.get();
cout<<"Second Time: ";
h2.disp();
h3=h3.diff(h1,h2);
cout<<"\nTime Difference: ";
h3.disp();
h1.add();
h1.disp();
int secs=h1.conv();
cout<<"\nTotal Seconds: "<<secs;
return 0;
}

//CPP Labpgm 6 //CPP Labpgm 6


3X3 Matrix

#include <iostream>

using namespace std;

class Matrix3x3 {
private:
static int objectCount; // Static member variable to count the number of objects
int matrix[3][3];

public:
// Constructor
Matrix3x3() {
// Increment the object count when a new object is created
++objectCount;

// Initialize matrix elements to 0


for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
matrix[i][j] = 1;
}
}
}
// Destructor
~Matrix3x3() {
// Decrement the object count when an object is destroyed
--objectCount;
}

// Overloaded addition operator


Matrix3x3 operator+(const Matrix3x3& other) const {
Matrix3x3 result;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
result.matrix[i][j] = matrix[i][j] + other.matrix[i][j];
}
}
return result;
}

// Overloaded multiplication operator


Matrix3x3 operator*(const Matrix3x3& other) const {
Matrix3x3 result;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 3; ++k) {
result.matrix[i][j] += matrix[i][k] * other.matrix[k][j];
}
}
}
return result;
}

// Function to display the matrix


void display() const {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
}

// Static member function to get the count of created objects


static int getObjectCount() {
return objectCount;
}
};

// Initialize the static member variable


int Matrix3x3::objectCount = 0;

int main() {
// Create two matrix objects
Matrix3x3 matrix1;
Matrix3x3 matrix2;

// Display the initial count of objects


std::cout << "Initial Object Count: " << Matrix3x3::getObjectCount() << std::endl;

// Perform addition and display the result


Matrix3x3 resultAddition = matrix1 + matrix2;
std::cout << "Matrix Addition Result:" << std::endl;
resultAddition.display();

// Perform multiplication and display the result


Matrix3x3 resultMultiplication = matrix1 * matrix2;
std::cout << "Matrix Multiplication Result:" << std::endl;
resultMultiplication.display();

// Display the final count of objects


std::cout << "Final Object Count: " << Matrix3x3::getObjectCount() << std::endl;

return 0;
}

//CPP7 LABPGM 7 – C String Operations


#include <iostream>
#include <cstring>

using namespace std;


class CString {
private:
char data[100];

public:
//Default Constructor
CString() {
// Initialize the string to an empty string
data[0] = '\0';
}
CString(char* s)
{
strcpy(data,s);
}
// Copy Constructor
CString(const CString& other) {
strcpy(data, other.data);
}

// Function to concatenate two strings


CString concatenate(const CString& other) const {
CString result;
strcat(result.data, data);
strcat(result.data, other.data);
return result;
}

// Function to find the length of the string


int length() const {
return strlen(data);
}

// Function to reverse the string


CString reverse() const {
CString reversed;
int len = length();
for (int i = len - 1; i >= 0; --i) {
reversed.data[len - 1 - i] = data[i];
}
reversed.data[len] = '\0';
return reversed;
}

// Function to compare two strings


int compare(const CString& other) const {
return strcmp(data, other.data);
}

// Function to display the string


void display() const {
std::cout << data << std::endl;
}
};
int main() {
// Create instances of the CString class
CString str1,str2,str3;
str1="Hello";
str2="world";

// Copy Constructor
CString str4 = str2;

// Concatenate two strings


CString result = str2.concatenate(str3);

// Find the length of a string


int length = result.length();
std::cout << "Length of the string: " << length << std::endl;

// Reverse a string
CString reversed = result.reverse();
std::cout << "Reversed string: ";
reversed.display();

// Compare two strings


int comparisonResult = str2.compare(str3);
std::cout << "Comparison result: " << comparisonResult << std::endl;

return 0;
}

//CPP 9 LABPRGM 9
Run Time Polymorphism
#include<iostream>
using namespace std;
class Shape
{
public: double a,b;
void get_adata ()
{
cin>>a;
}
void get_bdata ()
{
cin>>b;
}
virtual void display_area () = 0;
};

class Triangle:public Shape


{
public: void display_area ()
{
cout<<"Area of triangle "<<0.5*a*b<<endl;
}
};

class Rectangle:public Shape


{
public: void display_area ()
{
cout<<"Area of rectangle "<<a*b<<endl;
}
};

class circle:public Shape


{
public: void display_area ()
{
cout<<"Area of Triangle "<<(3.14)*a*a<<endl;
}
};

int main()
{
Triangle t;
Shape *st = &t;
cout<<"Enter base and altitude: ";
st->get_adata();
st->get_bdata();
st->display_area();

Rectangle r;
Shape *sr = &r;
cout<<"Enter length and breadth: ";
sr->get_adata();
sr->get_bdata();
sr->display_area();

circle c;
Shape *sc = &c;
cout<<"Enter radius ";
sc->get_adata();
sc->display_area();
return 0;
}

//CPP10 Labpgm10 Array Sort


#include <iostream>
#include <stdexcept>

using namespace std;

template <typename T>


class DynamicArray {
private:
T* arr;
size_t size;

public:
// Constructor
DynamicArray(size_t initialSize) : size(initialSize) {
if (initialSize > 0) {
arr = new T[size];
} else {
arr = nullptr;
}
}

// Destructor
~DynamicArray() {
delete[] arr;
}

// Accessor to get the size of the array


size_t getSize() const {
return size;
}

// Accessor to get the element at a specific index


T& operator[](size_t index) {
if (index >= size) {
throw std::out_of_range("Array index out of bounds");
}
return arr[index];
}

// Function to sort the array elements


void sort() {
for (size_t i = 0; i < size - 1; ++i) {
for (size_t j = 0; j < size - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
// Swap if the elements are in the wrong order
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
};

int main() {
try {
// Create a dynamic array of integers
DynamicArray<int> intArray(5);

// Initialize array elements


intArray[0] = 5;
intArray[1] = 2;
intArray[2] = 8;
intArray[3] = 1;
intArray[4] = 3;

// Display unsorted array


std::cout << "Unsorted Array: ";
for (size_t i = 0; i < intArray.getSize(); ++i) {
std::cout << intArray[i] << " ";
}
std::cout << std::endl;

// Sort the array


intArray.sort();

// Display sorted array


std::cout << "Sorted Array: ";
for (size_t i = 0; i < intArray.getSize(); ++i) {
std::cout << intArray[i] << " ";
}
std::cout << std::endl;

// Access an out-of-bounds element to demonstrate exception handling


// Uncommenting the line below will result in an exception being thrown
// std::cout << intArray[10] << std::endl;
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}

return 0;
}

//CPP11 lab pgm 11 vector STL


#include <iostream>
#include <vector>

using namespace std;

int main() {
// Declare a vector to store integers
vector<int> numbers;

// Add elements to the vector


cout << "Adding elements to the vector:" << endl;
for (int i = 1; i <= 5; ++i) {
numbers.push_back(i * 10);
}

// Display the elements in the vector


cout << "Vector elements: ";
for (int number : numbers) {
cout << number << " ";
}
cout << endl;

// Modify an element in the vector


numbers[2] = 100;

// Display the modified vector


cout << "Modified vector elements: ";
for (int number : numbers) {
cout << number << " ";
}
cout << endl;

// Accessing elements using iterators


cout << "Accessing elements using iterators: ";
for (auto it = numbers.begin(); it != numbers.end(); ++it) {
cout << *it << " ";
}
cout << endl;

// Erase an element from the vector


numbers.erase(numbers.begin() + 3);

// Display the vector after erasing an element


cout << "Vector after erasing an element: ";
for (int number : numbers) {
cout << number << " ";
}
cout << endl;

// Check if the vector is empty


cout << "Is the vector empty? " << (numbers.empty() ? "Yes" : "No") << endl;

// Clear all elements from the vector


numbers.clear();

// Check if the vector is empty after clearing


cout << "Is the vector empty after clearing? " << (numbers.empty() ? "Yes" : "No") << endl;

return 0;
}

//CPP12 Labpgm 12 Telephone Directory


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Contact {
string name;
string phoneNumber;
};

void addContactToFile(const Contact& contact) {


ofstream file("telephone_directory.txt", ios::app);

if (file.is_open()) {
file << contact.name << "," << contact.phoneNumber << endl;
file.close();
cout << "Contact added successfully!" << endl;
} else {
cerr << "Unable to open file for writing." << endl;
}
}

void displayAllContactsFromFile() {
ifstream file("telephone_directory.txt");

if (file.is_open()) {
string line;
cout << "Telephone Directory:" << endl;

while (getline(file, line)) {


size_t commaPos = line.find(",");
string name = line.substr(0, commaPos);
string phoneNumber = line.substr(commaPos + 1);

cout << "Name: " << name << "\tPhone Number: " << phoneNumber << endl;
}

file.close();
} else {
cerr << "Unable to open file for reading." << endl;
}
}

void searchContact(const string& name) {


ifstream file("telephone_directory.txt");

if (file.is_open()) {
string line;
bool found = false;

while (getline(file, line)) {


size_t commaPos = line.find(",");
string contactName = line.substr(0, commaPos);

if (contactName == name) {
found = true;
string phoneNumber = line.substr(commaPos + 1);
cout << "Contact found:" << endl;
cout << "Name: " << contactName << "\tPhone Number: " << phoneNumber << endl;
break;
}
}

if (!found) {
cout << "Contact not found." << endl;
}

file.close();
} else {
cerr << "Unable to open file for reading." << endl;
}
}

int main() {
int choice;
Contact newContact;

do {
cout << "Telephone Directory Menu:" << endl;
cout << "1. Add a new contact" << endl;
cout << "2. Display all contacts" << endl;
cout << "3. Search for a contact" << endl;
cout << "4. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter name: ";
cin.ignore();
getline(cin, newContact.name);
cout << "Enter phone number: ";
getline(cin, newContact.phoneNumber);
addContactToFile(newContact);
break;
case 2:
displayAllContactsFromFile();
break;

case 3:
cout << "Enter name to search: ";
cin.ignore();
getline(cin, newContact.name);
searchContact(newContact.name);
break;

case 4:
cout << "Exiting the program. Goodbye!" << endl;
break;

default:
cout << "Invalid choice. Please try again." << endl;
}

} while (choice != 4);

return 0;
}

You might also like