Sie sind auf Seite 1von 32

Practical No 1

Code:
#include <iostream>

using namespace std;


class fraction{
public:
int num,dec;
fraction(int n,int d)
{
num=n;
dec=d;
}

};
class height{
public:
int feet,inches;
height(int f,int i)
{
feet=f;
while(i>12)
{
i-=12;
inches=i;
feet++;
}
inches=i;
}

};
class dist{
public:
int kms,meters;
dist(int k,int m)
{
kms=k;
while(m>1000)
{

m-=1000;
meters=m;
kms++;
}
meters=m;
}

};
float convert(fraction f)
{
return float(f.num)/float(f.dec);
}

float convert(height h)
{
return h.feet*30.48+h.inches*2.54;
}
float convert(dist d)
{
return d.kms*1000+d.meters;
}
int main()
{
int i,num,dec,mtr,choice;
float kms,ft,inches;

start: cout<<"\n\n1] Fraction\n2] Height\n3] Distance\n4] Exit\n\nEnter your choice


: ";
cin>>choice;
switch(choice)
{
case 1:{ cout<<"Enter Numerator ";
cin>>num;
cout<<"Enter Denominator ";
cin>>dec;
fraction f(num,dec);
cout<<convert(f)<<"\n";
break;}
case 2:{
cout<<"Enter feet ";
cin>>ft;
cout<<"Enter inches ";
cin>>inches;
height h(ft,inches);
cout<<convert(h)<<" centimeters\n";
break;}
case 3:{
cout<<"Enter kms ";
cin>>kms;
cout<<"Enter meter ";
cin>>mtr;
dist d(kms,mtr);
cout<<convert(d)<<" meters\n";
break;}
default:{
return(0);}
}
goto start;
return 0;
}

Output
Practical No 2
Code:

#include <iostream>
using namespace std;
struct date
{
int DD,MM,YY;
}obj;
bool isLeap(int yr)
{
return (((yr % 4 == 0) &&
(yr % 100 != 0)) ||
(yr % 400 == 0));
}
bool isValidDate(int d, int m, int y)
{
if (m < 1 || m > 12)
return false;
if (d < 1 || d > 31)
return false;

if (m == 2)
{
if (isLeap(y))
return (d <= 29);
else
return (d <= 28);
}

if (m == 4 || m == 6 ||
m == 9 || m == 11)
return (d <= 30);

return true;
}
void oneaddDay(int day,int month,int yr)
{
if(month==2)
{
if(isLeap(yr))
{
if(day==29)
{
day=1;
month+=1;
}
else
day+=1;
}
else
{
if(day==28)
{

day=1;
month+=1;
}
else
day+=1;
}
}
else
if(month==1||month==3||month==5||month==7||month==8||month==10||mont
h==12)
{
if(day==31)
{
day=1;
if(month==12)
{
month=1;
yr+=1;
}
else
month+=1;
}
else
day+=1;
}
else if(month==4||month==6||month==9||month==11)
{

if(day==30)
{
day=1;
month+=1;
}
else
day+=1;
}

cout<<"the date after 1 day is :"<<day<<"/"<<month<<"/"<<yr<<"\n";

}
void tenaddDay(int day,int month,int yr)
{
if(month==2)
{
if(isLeap(yr))
{
if(day>=20)
{
day = 0 + (day + 10 - 29);
month+=1;
}
else
day+=10;
}
else
{
if(day>=19)
{
day = 0 + (day + 10 - 28);
month+=1;
}
else
day+=10;
}
}
else
if(month==1||month==3||month==5||month==7||month==8||month==10||mont
h==12)
{
if(day>21)
{
day = 0 + (day + 10 - 31);
if(month==12)
{
month=1;
yr+=1;
}
else
month+=1;
}
else
day+=10;
}
else if(month==4||month==6||month==9||month==11)
{
if(day>20)
{
day = 0 + (day + 10 - 30);
month+=1;
}
else
day+=1;
}

cout<<"the date after 10 day is :"<<day<<"/"<<month<<"/"<<yr<<"\n";

}
void onesubDay(int day,int month,int yr)
{
if(month==3)
{
if(isLeap(yr))
{
if(day==1)
{
day=29;
month-=1;
}
else
day-=1;
}
else
{
if(day==1)
{
day=28;
month+=1;
}
else
day-=1;
}
}
else
if(month==1||month==3||month==5||month==7||month==8||month==10||mont
h==12)
{
if(day==1)
{
day=31;
if(month==1)
{
month=12;
yr-=1;
}
else
month-=1;
}
else
day-=1;
}
else if(month==4||month==6||month==9||month==11||month==2)
{
if(day==1)
{
day=30;
month-=1;
}
else
day-=1;

cout<<"the date before 1 day is :"<<day<<"/"<<month<<"/"<<yr<<"\n";

}
void tensubDay(int day,int month,int yr)
{
if(month==3)
{
if(isLeap(yr))
{
if(day>10){
day-=10;
}
else{
day = 29 + (day - 10);
month--;
}
}
else
{
if(day>10){
day-=10;
}
else{
day = 28 + (day - 9);
month--;
}
}
}
else
if(month==1||month==3||month==5||month==7||month==8||month==10||mont
h==12)
{
if(day>10)
{
day-=10;
}
else{
if(month==1)
{
month=12;
yr-=1;
day = 31 + (day - 10);
}
else{
month-=1;
day = 30 + (day - 10);
}
}
}
else if(month==4||month==6||month==9||month==11||month==2)
{
if(day>10){
day-=10;
}
else{
month--;
day = 31 + (day - 10);
}
}

cout<<"the date before 10 day is :"<<day<<"/"<<month<<"/"<<yr<<"\n";

int main()
{

cout<<"\n Enter the Year: ";


cin>>obj.YY;

cout<<"\n Enter the Month: ";


cin>>obj.MM;

cout<<"\n Enter the Day : ";


cin>>obj.DD;

if(isValidDate(obj.DD,obj.MM,obj.YY))
{
cout<<"The Date "<<obj.DD<<"/"<<obj.MM<<"/"<<obj.YY<<" is a valid date\n";
}
else
cout<<"The Date "<<obj.DD<<"/"<<obj.MM<<"/"<<obj.YY<<" is an invalid
date\n";
if(isLeap(obj.YY))
{
cout<<"The Year "<<obj.YY<<" is a Leap year \n";
}
else
cout<<"The Year "<<obj.YY<<" is not a Leap year \n";

oneaddDay(obj.DD,obj.MM,obj.YY);
tenaddDay(obj.DD,obj.MM,obj.YY);
onesubDay(obj.DD,obj.MM,obj.YY);
tensubDay(obj.DD,obj.MM,obj.YY);

return 0;
}

Output:
Practical 3
Code:
#include <iostream>
using namespace std;
class Time{
int hr,mn,sec;
bool am,pm;
public:
bool validate(int hr,int mn,int sec){
if(hr<0 || hr>12 ||mn<0||mn>59||sec<0||sec>59)
return false;

}
void addmin(int hr, int mn,int sec){
if(mn==59){
mn=0;
hr+=1;
}
else
mn+=1;
cout<<"Time after 1 min will be :"<<hr<<":"<<mn<<":"<<sec<<"\n";
}
void submin(int hr,int mn,int sec){
if(mn==0){
mn=59;
hr-=1;
}
else
mn-=1;
cout<<"Time before 1 min was :"<<hr<<":"<<mn<<":"<<sec<<"\n";
}
void addhour(int hr,int mn,int sec){
if(hr==12)
hr=1;
else
hr+=1;
cout<<"Time after 1 hour will be :"<<hr<<":"<<mn<<":"<<sec<<"\n";
}
void subhour(int hr,int mn,int sec){
if(hr==1)
hr=12;
else
hr-=1;
cout<<"Time before 1 hour was :"<<hr<<":"<<mn<<":"<<sec<<"\n";
}
void convert(int hr,int mn,int sec,bool am,bool pm){
if(pm){
if(hr < 12)
hr+=12;
}
else{
if(hr == 12)
hr = 0;
}
cout<<"\n Time after conversion is : "<<hr<<":"<<mn<<":"<<sec;
}
};
int main(){
Time t;
bool a=false,p=false;
int h,m,s,ap;
cout<<"Enter Hour : ";
cin>>h;
cout<<"Enter minute : ";
cin>>m;
cout<<"Enter second : ";
cin>>s;
cout<<"1)Am or 2)Pm\nEnter your choice : ";
cin>>ap;
switch(ap)
{
case 1:
a=true;
break;
case 2: p=true;
break;
default:
break;

}
t.addmin(h,m,s);
t.submin(h,m,s);
t.addhour(h,m,s);
t.subhour(h,m,s);
t.convert(h,m,s,a,p);
return 0;
}
Output:
Practical no 4
Code
#include <iostream>
using namespace std;
static int totalnoofpeople;
static int amt1=0,amt2=0;
class TicketCounter
{
public:
void display()
{
cout<<"Total NO oF visitors are :"<<totalnoofpeople<<"\n";
cout<<"Total Cash Collected is: "<<amt1+amt2;
}

};
class Hall13
{
public:
void cal(bool adult,bool child)
{
totalnoofpeople+=1;

if(adult)
{
amt1+=350;
}
if(child)
{
amt1+=200;
}
}
};
class Hall24
{
public:
void cal(bool adult,bool child)
{
totalnoofpeople+=1;
if(adult)
{
amt2+=550;
}
if(child)
{
amt2+=300;
}
}
};
int main()
{
Hall13 h13;
Hall24 h24;
bool adult,child;
int screen,age;
start: cout<<"\n1]Adult or 2]child \n";
cin>>age;
switch(age)
{
case 1: adult=true;
child=false;
break;
case 2: child=true;
adult=false;
break;

cout<<"Choose your Screen(Hall) [1,2,3,4] :\n";


cin>>screen;
switch(screen)
{
case 1:
case 3:
h13.cal(adult,child);
adult=false;child=false;
break;
case 2:
case 4:
h24.cal(adult,child);
adult=false;child=false;
break;
default:
break;
}
TicketCounter tc;
tc.display();
goto start;
return 0;
}
Output
Practical 5

Code:
#include <iostream>
#include <fstream>
using namespace std;
static int cnt=0;
struct name
{
string fname[100],mname,lname;

}n;
struct dob
{
int birth_year,birth_month,birth_day;
}b;
struct doj
{
int joining_day[100],joining_month[100],joining_year[100];
}j;
struct address
{
string area,city,street;
}a;
class employee
{
public:
int eid,salary[100],joining_day[100],joining_month[100],joining_year[100];
string fname[100],dept[100],designation[100];

void getData()
{
cout<<"Enter Emp Id\n";
cin>>eid;

cout<<"Enter Name 1]F Name\n";


cin>>n.fname[cnt];
fname[cnt]=n.fname[cnt];

cout<<"Enter 2]Mname\n";
cin>>n.mname;
string mname=n.mname;
cout<<"Enter 3]lname\n";
cin>>n.lname;
string lname=n.lname;
cout<<"Enter DOB : 1]Day ";
cin>>b.birth_day;
int birth_day=b.birth_day;
cout<<"Enter DOB : 2]Month ";
cin>>b.birth_month;
int birth_month=b.birth_month;
cout<<"Enter DOB : 3]Year ";
cin>>b.birth_year;
int birth_year=b.birth_year;
cout<<"Enter DOJ : 1]Day ";
cin>>j.joining_day[cnt];
joining_day[cnt]=j.joining_day[cnt];
cout<<"Enter DOJ : 2]Month ";
cin>>j.joining_month[cnt];
joining_month[cnt]=j.joining_month[cnt];
cout<<"Enter DOJ : 3]Year ";
cin>>j.joining_year[cnt];
joining_year[cnt]=j.joining_year[cnt];
cout<<"Enter Address : 1]area ";
cin>>a.area;
string area=a.area;
cout<<"Enter Address : 1]city ";
cin>>a.city;
string city=a.city;
cout<<"Enter Address : 1]street ";
cin>>a.street;
string steet=a.street;
cout<<"Enter designation ";
cin>>designation[cnt];
cout<<"enter salary ";
cin>>salary[cnt];
cout<<"Enter department ";
cin>>dept[cnt];
cnt++;
}
};
employee emp;
void WriteData(){

ofstream fout;
fout.open("Employee.dat",ios::binary);
fout.write((char*)&emp,sizeof(emp));
fout.close();
}
void readData()
{
char ch;
ifstream fin("Employee.dat",ios::binary);
cout<<"View : \nA]Employee on a particular Joining Date\nB]Employee of a
particular department\nC]Employee of a particular designation\nD]Employee above
a particular salary limit\n";
cin>>ch;
switch(ch){
case 'A':
while(fin.read((char*)&emp,sizeof(emp)))
{
int d,m,y;
cout<<"Enter the joining Date Day:";
cin>>d;
cout<<"Enter the Date Month:";
cin>>m;
cout<<"Enter the Date Year:";
cin>>y;

for(int i=0;i<cnt;i++)
{
if(emp.joining_day[i]==d
&&emp.joining_month[i]==m&&emp.joining_year[i]==y)
{
cout<<emp.fname[i]<<" has joined on this date\n";
}
// if(int(emp.joining_year)<2008)
//{
// cout<<emp.fname[i]<<" has completed 10 years of service\n";
//}
}
}
break;
case 'B': while(fin.read((char*)&emp,sizeof(emp)))
{
string dep;
cout<<"Enter department ";
cin>>dep;
for(int i=0;i<cnt;i++)
{
if(emp.dept[i]==dep)
{
cout<<emp.fname[i]<<" is of department "<<dep<<"\n" ;
}
}
}
break;
case 'C': while(fin.read((char*)&emp,sizeof(emp)))
{
string de;
cout<<"Enter designation ";
cin>>de;
for(int i=0;i<cnt;i++)
{
if(emp.designation[i]==de)
{
cout<<emp.fname[i]<<" is "<<de <<"\n";
}
}
}
break;
case 'D': while(fin.read((char*)&emp,sizeof(emp)))
{
int sal;
cout<<"Enter Salary Limit ";
cin>>sal;
for(int i=0;i<cnt;i++)
{
if(emp.salary[i]>sal)
{
cout<<emp.fname[i]<<" has salary "<<emp.salary[i]<<"\n";
}
}
}
break;
default:
break;
}
}
int main(){
int choice;
start:
cout<<"\n\n1]Enter Records\n2]Read Records\nEnter your Choice : ";
cin>>choice;
switch(choice)
{
case 1:
emp.getData();
WriteData();
break;
case 2:
readData();
break;
}goto start;
}
Output
Practical 6
Code:
#include <iostream>
using namespace std;
static int cnt=0;
class TrainTicket{
public:
int t_no[100],pnrno[100],age[100];
string tname[100],source[100],destination[100],name[100],birthalloted[100];
bool cnf[100];
void putDetails(){
cout<<"Enter Train No ";
cin>>t_no[cnt];
cout<<"Enter Train Name ";
cin>>tname[cnt];
cout<<"Enter Pnr No ";
cin>>pnrno[cnt];
cout<<"Enter Source ";
cin>>source[cnt];
cout<<"Enter destination ";
cin>>destination[cnt];
cout<<"Enter Name ";
cin>>name[cnt];
cout<<"Enter age ";
cin>>age[cnt];
cout<<"Enter birth alloted 1]upper 2]lower 3]middle ";
int a;
cin>>a;
switch(a){
case 1:birthalloted[cnt]={"upper"};
break;
case 2:birthalloted[cnt]={"lower"};
break;
case 3:birthalloted[cnt]={"middle"};
break;
}
cout<<"Is ticket CNF? 1]Yes 2]No ";
int b;
cin>>b;
switch(b){
case 1:cnf[cnt]=true;
break;
case 2:cnf[cnt]=false;
break;
}
cnt++;
}
void viewInfo(){
int cnt1=0,cnt2=0,cnt3=0,cnt4=0;
string sour;
cout<<"Enter the source (for view)";
cin>>sour;
for(int i=0;i<sizeof(source)/sizeof(source[0]);i++){
if(sour==source[i]){
cnt1++;
}
}
cout<<"Total No of people from source "<<sour<<" are "<<cnt1<<"\n";
string tn;
cout<<"Enter the Train Name (for view)";
cin>>tn;
for(int i=0;i<sizeof(tname)/sizeof(tname[0]);i++){
if(tn==tname[i]){
if(age[i]<18){
cnt4++;
}
}
}
cout<<"Total No of childrens in train "<<tn<<" are "<<cnt4<<"\n";
string des;
cout<<"Enter the destination (for view)";
cin>>des;
for(int i=0;i<sizeof(destination)/sizeof(destination[0]);i++){
if(des==destination[i]){
cnt2++;
}
}
cout<<"Total No of people from destination "<<des<<" are "<<cnt2<<"\n";
int l=sizeof(name)/sizeof(name[0]);
for(int i=0;i<l;i++){
if(!(cnf[i])){
cnt3++;
}
}
cout<<"Total No of people without CNF are "<<cnt3;
}
};
int main(){
TrainTicket t;
int choice;
start: cout<<"\nChoose 1]Enter Details 2]View Info\n";
cin>>choice;
switch(choice){
case 1: t.putDetails();
break;

case 2:t.viewInfo();
break;
}
goto start;
return 0;
}

Output
Practical 7
Code
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
void scoreDisplay(int sc){
cout<<"\n\n\t\t\t -----------Your score is "<<sc<<"!";
}
int main()
{
int arr[]= {1,2,3,4,5,6,7,8,9,10};
int score=0,marks=10,quest,qno=0;
char ans;
ifstream readquest;
readquest.open("Questions.txt");
ifstream readans;
readans.open("Ans.txt");
string p;
srand(time(0));
random_shuffle(&arr[0],&arr[9]);
cout<<"\t\t\t\t\t\tGood luck for the Quiz!\n\n\n";
for(int i=0; i<10; i++)
{
int lineno1=0;
quest=arr[i];
while(!readquest.eof())
{
lineno1++;
if(quest!=lineno1)
{
getline(readquest,p);
continue;
}
getline(readquest,p,'|');
cout<<"\n\t"<<i+1<<"."<<p<<"\n";
for(int i=97; i<101; i++)
{
getline(readquest,p,'|');
cout<<"\t "<<(char)i<<". "<<p<<"\n";
}
cout<<"\t Enter your response: ";
cin>>ans;
int lineno2=0;
char c;
while(!readans.eof())
{
lineno2++;
readans>>c;
if(lineno1==lineno2)
{
(ans==c)?score+=marks:score=score;
break;
}
}
readans.seekg(0,ios::beg);
int choice=0;
cout<<"\n\t Please select one of the options:";
cout<<"\n\t 1. Continue Quiz";
cout<<"\n\t 2. Exit Quiz\n\t ";
cin>>choice;
switch(choice)
{
case 1:
break;

case 2:
goto Exit;

default:
cout<<"\n\tInvalid Input";
}
qno++;
if(qno%3==0)
{
marks=marks*2;
}
break;
}
readquest.seekg(0,ios::beg);
}
Exit:
scoreDisplay(score);
readquest.close();
readans.close();
}
Output

Das könnte Ihnen auch gefallen