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

Plc C-- Lab Programs

Uploaded by

Kavin C Krishnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Plc C-- Lab Programs

Uploaded by

Kavin C Krishnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Program 1: Electricity Bill

#include<iostream>
using namespace std;

class Electric {
float unit;
char name[20];

public:
void accept() {
cout<<"\nEnter Name : ";
cin>>name;
cout<<"\nNo. Of Units : ";
cin>>unit;
}
void print_bill();
};

void Electric::print_bill() {
float bill=50;
if(unit<=100)
bill=bill+1.5*unit;
else if(unit>100 && unit<=200)
bill=bill+150+1.8*(unit-100);
else
bill=bill+150+180+2.5*(unit-200);
if(bill>300)
bill=bill+(bill*0.15);
cout<<"\nBill = "<<bill<<"\t"<<name;
}

int main() {
Electric e[10];
int i,cnt;
cout<<"\nEnter How Many Customers You Want? : ";
cin>>cnt;
for(i=0; i<cnt; i++)
e[i].accept();
for(i=0; i<cnt; i++)
e[i].print_bill();
return 0;
}

Output:
Program 2: Student Marksheet
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

class student_marksheet {
string name;
float test[3];
float average_value;
float sum;

public:
static int roll_no;
void input();
void average();
void display();
};

int student_marksheet::roll_no = 0;

void student_marksheet::input() {
cout<<"Roll No: "<<(++roll_no)<<endl;
cout<<"Name: ";
cin>>name;
for(int i=0; i<3; i++) {
cout<<"Test "<<i+1<<": ";
cin>>test[i];
}
}

void student_marksheet::average() {
float min = 100; int min_id;
sum = 0;
for(int i=0; i<3; i++) {
if(test[i]<=min) {
min = test[i];
min_id = i;
}
}
for(int i=0; i<3; i++) {
if(i!=min_id)
sum = sum + test[i];
}
average_value = sum/2;
}

void student_marksheet::display() {
cout<<"|"<<setw(10)<<(++roll_no)<<setw(20)<<name<<setw(5)<<test[0]<<set
w(10)<<test[1]<<setw(10)<<test[2]<<setw(10)<<average_value<<setw(6)<<"|\n";
}

int main() {
int i, n;
cout<<"\nSTUDENT RECORD\n\n";
cout<<"Enter the number of students\n";
cin>>n;
student_marksheet student[n];
for(i=0; i<n; i++) {
cout<<"\n";
student[i].input();
student[i].average();
cout<<"\n";
}

cout<<"\n|-------------------------------------------------------------------
--|\n";
cout<<"|"<<setw(10)<<"Roll No"<<setw(16)<<"Name"<<setw(10)<<"Test
1"<<setw(10)<<"Test 2"<<setw(10)<<"Test
3"<<setw(10)<<"Average"<<setw(5)<<"|\n";
student_marksheet::roll_no = 0;

cout<<"|---------------------------------------------------------------------
|\n";
for(i=0; i<n; i++)
student[i].display();

cout<<"|---------------------------------------------------------------------
|\n";
return 0;
}
Output:
Program 3: Shopping List
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

struct item {
int code;
string name_of_item;
float rate;
float total;
int quantity;
}menu[5];

class user {
string name;
struct item purchased[5];
float total_amount;

public:
user(){for(int i=0; i<5; i++) purchased[i]=menu[i];}
void input();
void shop();
void add();
void del();
void display_items();
void display_bill();
float billing();
};

void user::input() {
cout<<"Name: ";
cin>>name;
}

void user::shop() {
int choice;
cout<<"WELCOME "<<name<<"!!\n\n";
while(1)
{
cout<<"Enter \n1. Buy an item\n2. Remove an item from the
cart\n3. Display items purchased\n4. Exit\n";
cin>>choice;
switch(choice)
{
case 1:
add();
break;
case 2:
del();
break;
case 3:
display_items();
break;
case 4:
display_bill();
return;
default:
cout<<"Enter Valid Choice\n";
break;
}
}
}

void user::add() {
int temp_code, temp_quantity;
cout<<"\nEnter the code of the item to be purchased\n";
cin>>temp_code;
if(temp_code>0 && temp_code<6) {
cout<<"Enter the quantity\n";
cin>>temp_quantity;
purchased[temp_code-1].quantity+=temp_quantity;
purchased[temp_code-1].total=(purchased[temp_code-1].quantity)*(p
urchased[temp_code-1].rate);
}
else {
cout<<"\nEnter Valid Code!!\n";
add();
}
}

void user::del() {
int temp_code, temp_quantity;
cout<<"\nEnter the code of the item to be removed\n";
cin>>temp_code;
if(temp_code>0 && temp_code<6) {
if(purchased[temp_code-1].quantity <= 0)
cout<<"\nItem is not present in the cart\n";
else {
cout<<"Enter the quantity\n";
cin>>temp_quantity;
purchased[temp_code-1].quantity-=temp_quantity;
purchased[temp_code-1].total=(purchased[temp_code-1].quanti
ty)*(purchased[temp_code-1].rate);
}
}
else {
cout<<"\nEnter Valid Code!!\n";
del();
}
}

void user::display_items() {
cout<<"|---------------------------------------------------------------
--------------|\n";

cout<<"|"<<setw(10)<<"CODE"<<setw(10)<<"NAME"<<setw(10)<<"RATE"<<setw(1
0)<<"QUANTITY"<<setw(15)<<"TOTAL"<<setw(24)<<"|\n";

cout<<"|---------------------------------------------------------------
--------------|\n";
for(int i = 0 ; i < 5; i++) {
if(purchased[i].quantity>0) {
cout<<"|"<<setw(10)<<purchased[i].code<<setw(10)<<pur
chased[i].name_of_item<<setw(10)<<purchased[i].rate<<setw(1
0)<<purchased[i].quantity<<setw(15)<<purchased[i].total<<se
tw(24)<<"|\n";
}
}

cout<<"|---------------------------------------------------------------
--------------|\n";
}

float user::billing() {
float total = 0;
for(int i = 0 ; i < 5; i++) {
if(purchased[i].quantity>0)
total += purchased[i].total;
}
return total;
}

void user::display_bill() {
cout<<"\n|-------------------------------------------------------------
----------------|\n";
cout<<"\n\nDATE: 27.11.2017\n";
cout<<"NAME: "<<name<<"\n\n";
display_items();
cout<<"\n\nTOTAL: "<<setw(60)<<billing();

cout<<"\n|-------------------------------------------------------------
----------------|\n";
}

int main() {
int n;
cout<<"DEPARTMENTAL STORE\n\n";
for(int i=0; i<5;i++) {
menu[i].code = i+1;
menu[i].quantity = 0;
menu[i].total = 0;
}
menu[0].name_of_item = " A ";
menu[1].name_of_item = " B ";
menu[2].name_of_item = " C ";
menu[3].name_of_item = " D ";
menu[4].name_of_item = " E ";
menu[0].rate = 50;
menu[1].rate = 125;
menu[2].rate = 150;
menu[3].rate = 100;
menu[4].rate = 75;
cout<<"ITEMS AVAILABLE\n\n";
cout<<"|--------------------------------------------------|\n";
cout<<"|"<<setw(10)<<"CODE"<<setw(10)<<"NAME"<<setw(10)<<"RATE"<<setw(2
2)<<"|\n";
for(int i=0; i<5; i++) {
cout<<"|"<<setw(10)<<menu[i].code<<setw(10)<<menu[i].name_of_item
<<setw(10)<<menu[i].rate<<setw(22)<<"|\n";
}
cout<<"|--------------------------------------------------|\n";
cout<<"\n";
cout<<"\n\nEnter the number of users\n";
cin>>n;
user users[n];
for(int i=0; i<n; i++) {

cout<<"\n********************************************************
*******************\n";
users[i].input();
users[i].shop();

cout<<"\n********************************************************
*******************\n";
}
cout<<"\n";
return 0;
}

Output:
Program 4: Polynomial Operations
#include<iostream>
#include<stdlib.h>
#include<cmath>
using namespace std;

class Poly {
float *p;
int deg;
float x;
float value;

public:
Poly();
void setn(int n);
void valuecalc();
friend ostream& operator<<(ostream& out, Poly& a);
friend istream& operator>>(istream& in, Poly& a);
Poly& operator+(Poly& p2);
Poly& operator-(Poly& p2);
Poly& operator*(Poly& p2);
Poly& operator=(Poly& p2);
}p3;//The thing is we can't return a local Objects refrence because as soon
as the stack is thrown, Its all gone, so mine is p3!
//https://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-importan
t-const/

Poly::Poly() {
p=NULL;
deg=0;
x=0;
value=0;
}

void Poly::setn(int n) {
deg=n;
int x=n+1;
p=(float*)calloc(x,sizeof(float));//allocate the thing!
}

ostream& operator<<(ostream& out, Poly& a) {


int i;
out<<"The Polynomail is: ";
for(i=a.deg;i>=0;i--) {
out<<"("<<a.p[i]<<"x^"<<i<<")";
if(i>0)
out<<"+";
}
return out;
}

istream& operator>>(istream& in, Poly& a) {


int index;
float temp;
char c1='+',c='k';
cout<<"Enter the Poly in the form: anx^n+a(n-1)x^n-1+....+a0x^0\nEnter:
";
while(1) {
in>>temp>>c>>c>>index;
if(index<0||index>a.deg) {
cout<<"Invalid Entry!\n";
exit(0);
}
if(c1=='-')//Oh this handles the sign for number!
a.p[index]=-temp;
else
a.p[index]=temp;
in>>c1;
if(c1!='+'&&c1!='-')
break;//handles termination!
}
if(a.p[a.deg]==0) {
cout<<"Invalid Entry, degree mismatch!\n";
exit(0);
}
return in;
}

Poly& Poly::operator+(Poly& p2) {


int n,i;
n=(this->deg)>p2.deg?(this->deg):(p2.deg);
p3.setn(n);
i=0;
while(i<=(this->deg)&&i<=(p2.deg)) {
p3.p[i]=this->p[i]+p2.p[i];
i++;
}//Now the common power and done, rest copy the Others!
while(i<=(this->deg)) {
p3.p[i]=this->p[i];
i++;
}
while(i<=(p2.deg)) {
p3.p[i]=p2.p[i];
i++;
}
return p3;
}

Poly& Poly::operator-(Poly& p2) {


int n,i;
n=this->deg>p2.deg?(this->deg):(p2.deg);
p3.setn(n);
i=0;
while(i<=(this->deg)&&i<=(p2.deg)) {
p3.p[i]=this->p[i]-p2.p[i];
I++;
}
while(i<=(this->deg)) {
p3.p[i]=this->p[i];
i++;
}
while(i<=(p2.deg)) {
p3.p[i]=-p2.p[i];
i++;
}
return p3;
}

Poly& Poly::operator=(Poly& p2) {


int i;
this->deg=p2.deg;
this->setn(p2.deg);
for(i=0;i<=p2.deg;i++)
this->p[i]=p2.p[i];
return *this;
}

Poly& Poly::operator*(Poly& p2) {


int i,j;
p3.setn((this->deg)+(p2.deg));//deg add
for(i=0;i<=(p2.deg);i++) {
for(j=0;j<=(this->deg);j++)
p3.p[i+j]=(p3.p[i+j])+(this->p[j])*(p2.p[i]);
}
return p3;
}

void Poly::valuecalc() {
int i;
cout<<"x: ";
cin>>x;
if(p==NULL) {//So that It won't print for Empty OBJ
cout<<"No Calc term!\n";
return;
}
for(i=0;i<=deg;i++)
value=value+p[i]*pow(x,i);
cout<<"Value of the Polynomial is :"<<value<<endl;
value=0;
}

int main() {
int j,n;
Poly p1,p2,p4;
cout<<"Enter the degree of 1st polynomial: ";
cin>>n;
p1.setn(n);
cin>>p1;
cout<<"Enter the degree of 2nd polynomial: ";
cin>>n;
p2.setn(n);
cin>>p2;
cout<<"Select the Following option:\n1)ADD\n2)SUB\n3)MULT\n4)Value
Calc\nX)EXIT\n";
while(1) {
cout<<"Enter option: ";
cin>>j;
switch(j) {
case 1:
{p4=p1+p2;
cout<<(p4)<<endl;
break;
}
case 2:
{p4=p1-p2;
cout<<(p4)<<endl;
break;
}
case 3:
{p4=p1*p2;
cout<<(p4)<<endl;
break;
}
case 4:
{cout<<"Poly 1: ";
p1.valuecalc();
cout<<"\n Poly 2: ";
p2.valuecalc();
cout<<"\n Poly 3: ";
p4.valuecalc();
break;
}
default:
exit(0);
}
}
}

Output:
Program 5: Inheritance
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;

#define PI (3.14159265)

class shape {
protected:
float area;

public:
virtual void setdata()=0;
virtual float calculate()=0;
virtual void display()=0;
};

class rectangle: public shape {


float len;
float wid;
public:
rectangle();

void setdata() {
cout<<"\nEnter the length of the rectangle\n";
cin>>len;
cout<<"Enter the breadth of the rectangle\n";
cin>>wid;
}

float calculate() {
area=len*wid;
return area;
}

void display() {
cout<<"\nLength :"<<len;
cout<<"\nBreadth :"<<wid;
cout<<"\n____________________"<<endl;
cout<<"AREA :"<<area;
cout<<"\n____________________\n\n"<<endl;
}
};

rectangle::rectangle() {
cout<<"____________________________________________________"<<endl;
cout<<"RECTANGLE"<<endl;
}

class square: public shape {


float len;

public:
square();

void setdata() {
cout<<"\nEnter the length of the square\n";
cin>>len;
}

float calculate() {
area=len*len;
return area;
}

void display() {
cout<<"\nLength :"<<len;
cout<<"\n____________________"<<endl;
cout<<"AREA :"<<area;
cout<<"\n____________________\n\n"<<endl;
}
};

square::square() {
cout<<"____________________________________________________"<<endl;
cout<<"SQUARE"<<endl;
}

class circle: public shape {


float radius;

public:
circle();
void setdata() {
cout<<"\nEnter the radius of the circle\n";
cin>>radius;
}

float calculate() {
area=PI*radius*radius;
return area;
}

void display() {
cout<<"\nRadius :"<<radius;
cout<<"\n____________________"<<endl;
cout<<"AREA :"<<area;
cout<<"\n____________________\n\n"<<endl;
}
};

circle::circle() {
cout<<"____________________________________________________"<<endl;
cout<<"CIRCLE"<<endl;
}

int main() {
int choice;
shape* s;
cout<<"1- Rectangle\n2- Square\n3- Circle\n";
cout<<"\n____________________\n\n"<<endl;
while(1) {
cout<<"Enter your choice\n";
cin>>choice;
switch(choice) {
case 1:
{rectangle r;
s = &r;}
break;
case 2:
{square sq;
s = &sq;}
break;
case 3:
{circle c;
s = &c;}
break;
default:
exit(1);
}
s->setdata();
s->calculate();
s->display();
}
return 0;
}

Output:
Program 6: Generic Class
#include<iostream>
using namespace std;

#define MAX 5

template<typename T>
class queue {
T a[100];
int front;
int rear;

public:
void choice();
void insert();
void dequeue();
void display();
};
template<typename T>
void queue<T>::choice() {
int ch;
rear=-1;
front=0;
while(1) {
cout<<"Enter\n1:insert\n2:delete\n3:display\n4:quit\n";
cin>>ch;
switch(ch) {
case 1:insert();
break;
case 2:dequeue();
break;
case 3: display();
break;
default:return;
}
}
}

template<typename T>
void queue<T>::insert() {
T ele;
if(rear==MAX-1)
cout<<"Queue Full\n";
else {
cout<<"Enter the element to be added\n";
cin>>ele;
rear++;
a[rear]=ele;
}
}

template<typename T>
void queue<T>::dequeue() {
if (front>rear)
cout<<"Queue empty";
else {
cout<<"The deleted element is "<<a[front]<<"\n";
front++;
}
}

template<typename T>
void queue<T>::display() {
if(front>rear)
cout<<"Queue empty\n";
else {
int i;
for(i=front;i<=rear;i++)
cout<<a[i]<<" ";
cout<<"\n";
}
}

int main(){
queue<int> iq;
queue<float> fq;
queue<string> sq;
int ch;
while(1) {
cout<<"Enter\n1:integer queue\n2: string queue\n3:float
queue\n4:exit\n";
cin>>ch;
switch(ch) {
case 1:iq.choice();
break;
case 2: sq.choice();
break;
case 3: fq.choice();
break;
default: return 0;
}
}
}

Output:
Program 7: Vectors
#include<iostream>
#include<vector>
#include<list>
using namespace std;

int main() {
vector<int> a, b;
list<int> c;

vector<int>::iterator ptr1;
list<int>::iterator ptr;

int n, i=0, temp;


cout<<"Enter the number of elements\n";
cin>>n;
cout<<"Enter the elements\n";
for(i=0; i<n; i++) {
cin>>temp;
a.push_back(temp);
c.push_back(temp);
}
cout<<"The contents of the vector a are:\n";
for(i=0; i<n; i++)
cout<<a[i]<<"\t";
cout<<"\n";
cout<<"The contents of the list c are:\n";
for(ptr=c.begin(); ptr!=c.end(); ptr++)
cout<<*ptr<<"\t";
c.sort();
cout<<"\n";
cout<<"The contents of the list c after sorting are:\n";
for(ptr=c.begin(); ptr!=c.end(); ptr++)
cout<<*ptr<<"\t";
i=0;
for(ptr=c.begin(); ptr!=c.end(); ptr++) {
a[i]=*ptr;
i++;
}
for(i=0; i<n; i++) {
if(a[i]<=10)
b.push_back(a[i]);
else
break;
}
cout<<"\nThe contents of the vector b are\n";
for(ptr1=b.begin(); ptr1!=b.end(); ptr1++)
cout<<*ptr1<<"\t";
cout<<"\n";
}

Output:
Program 8: Templates
#include<iostream>
using namespace std;

template<typename T>
int search(T* a, T key, int first, int last) {
int mid;
if(first>last)
return -1;
mid = (first+last)/2;
if(a[mid]==key)
return mid;
if(a[mid]>key)
return(search(a, key, first, mid-1));
return(search(a, key, mid+1, last));
}

template<typename T>
class array {
public:
T* a;
int length;
array();
void search_element();
};

template<typename T>
array<T>::array() {
cout<<"Enter the length of the array\n";
cin>>length;
a = new T[length];
cout<<"Enter the elements of the array\n";
for(int i=0; i<length; i++)
cin>>a[i];
}

template<typename T>
void array<T>::search_element() {
T key;
cout<<"Enter the key\n";
cin>>key;
int index ;
index= search(a, key, 0, length-1);
if(index!= -1)
cout<<"The key is found at the index "<<index<<endl;
else
cout<<"The key is not found in the array"<<endl;
}

int main() {
cout<<"INT ARRAY\n";
array<int> a;
a.search_element();
cout<<"\nFLOAT ARRAY\n";
array<float> b;
b.search_element();
cout<<"\nCHAR ARRAY\n";
array<string> c;
c.search_element();
return 0;
}

Output:

You might also like