Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ankush Mca-I (Sem Ii) 501

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

Ankush MCA-I(Sem=II) 501

/*WAP to perfrom Addition, Subtraction, Multiplication and Division of two


Complex Numbers using Structure*/

#include<iostream.h>
#include<conio.h>

struct ComplexNum
{
float x, y;
};

Add(struct ComplexNum c1, struct ComplexNum c2)


{
struct ComplexNum a;
a.x = c1.x+c2.x;
a.y = c1.y+c2.y;
cout<<a.x<<" + "<<a.y<<"i";
}

Sub(struct ComplexNum c1, struct ComplexNum c2)


{
struct ComplexNum s;
s.x = c1.x-c2.x;
s.y = c1.y-c2.y;
cout<<s.x<<" + "<<s.y<<"i";
}

Mul(struct ComplexNum c1, struct ComplexNum c2)


{
struct ComplexNum m;
m.x = c1.x*c2.x - c1.y*c2.y;
m.y = c1.x*c2.y + c2.x*c1.y;
cout<<m.x<<" + "<<m.y<<"i";
}

Div(struct ComplexNum c1, struct ComplexNum c2)


{
struct ComplexNum d;
d.x = c1.x*c2.x + c1.y*c2.y;
d.y = c2.x*c1.y - c1.x*c2.y;
cout<<d.x<<" + "<<d.y<<"i";
}

void main()
{
struct ComplexNum c1, c2, a, s, m, d;

PURCITM Page 1
Ankush MCA-I(Sem=II) 501

clrscr();
cout<<"Enter the First Number\n";
cout<<"Real Part = ";
cin>>c1.x;
cout<<"Imaginary Part = ";
cin>>c1.y;
cout<<"Enter the Second Number\n";
cout<<"Real Part = ";
cin>>c2.x;
cout<<"Imaginary Part = ";
cin>>c2.y;
cout<<"\nOperation on Two Complex Numbers :\n";
cout<<"\nAddition : ";
Add(c1,c2);
cout<<"\nSubtraction : ";
Sub(c1,c2);
cout<<"\nMultiplication : ";
Mul(c1,c2);
cout<<"\nDivision : ";
Div(c1,c2);
getch();
}

OUTPUT:

Enter the First Number


Real Part = 12
Imaginary Part = 10
Enter the Second Number
Real Part = 3
Imaginary Part = 2

Operation on Two Complex Numbers :

Addition : 15 + 12i
Subtraction : 9 + 8i
Multiplication : 16 + 54i
Division : 56 + 6i

PURCITM Page 2
Ankush MCA-I(Sem=II) 501

/*WAP to perfrom Addition, Subtraction, Multiplication and Division of two


ratios using Structure*/

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>

void add();
void sub();
void mul();
void div();
void simplify(int n,int d);

struct Ratio
{
int numr,denr;
}rat1,rat2;

void main()
{
int opt;
while(1)
{
int flag=0;
clrscr();
cout<<"\tMake your selection to perform operations on ratios:\n\n";
cout<<"\t\t1. Addition\n";
cout<<"\t\t2. Subtraction\n";
cout<<"\t\t3. Multiplication\n";
cout<<"\t\t4. Division\n";
cout<<"\t\t5. Exit";
cin>>opt;
if(opt>=1 && opt<=4)
{
cout<<"\tEnter First ratio:\n\n";
cout<<"\t\tNumerator: ";
cin>>rat1.numr;
cout<<"\t\tDenominator: ";
cin>>rat1.denr;
cout<<"\n\n\n\n\tEnter Second ratio:\n\n";
cout<<"\t\tNumerator: ";
cin>>rat2.numr;
cout<<"\t\tDenominator: ";
cin>>rat2.denr;
}

PURCITM Page 3
Ankush MCA-I(Sem=II) 501

switch(opt)
{
case 1:
{
add();
break;
}
case 2:
{
sub();
break;
}
case 3:
{
mul();
break;
}
case 4:
{
div();
break;
}
case 5:
{
exit(0);
}
default:
{
cout<<"Wrong input";
getch();
exit(0);
}
}
getch();
}

void add()
{
rat1.numr=((rat1.numr*rat2.denr)+(rat2.numr*rat1.denr));
rat1.denr=(rat1.denr*rat2.denr);
cout<<"\n\n\tAddition of two Ratios:\n\t\t";
simplify(rat1.numr,rat1.denr);

PURCITM Page 4
Ankush MCA-I(Sem=II) 501

void sub()
{
rat1.numr=((rat1.numr*rat2.denr)-(rat2.numr*rat1.denr));
rat1.denr=(rat1.denr*rat2.denr);
cout<<"\n\n\tSubtraction of two Ratios:\n\t\t";
simplify(rat1.numr,rat1.denr);
}

void mul()
{
rat1.numr=(rat1.numr*rat2.numr);
rat1.denr=(rat1.denr*rat2.denr);
cout<<"\n\n\tMultiplication of two Ratios:\n\t\t";
simplify(rat1.numr,rat1.denr);
}

void div()
{
rat1.numr=(rat1.numr*rat2.denr);
rat1.denr=(rat1.denr*rat2.numr);
cout<<"\n\n\tDivision of two Ratios:\n\t\t";
simplify(rat1.numr,rat1.denr);
}

void simplify(int n, int d)


{
int divisor,max_divisor;
n=abs(n); d=abs(d);
max_divisor=(n<d)?n:d;
for(int i=1;i<=max_divisor;i++)
{
if(n%i==0 && d%i==0)
divisor=i;
}
if((rat1.numr<0 && rat1.denr>0) || (rat1.denr<0 && rat1.numr>0))
cout<<"-"<<abs(rat1.numr/divisor)<<" : "<<abs(rat1.denr/divisor);
else if((rat1.numr<0 && rat1.denr<0) || (rat1.numr>0 && rat1.denr>0))
cout<<abs(rat1.numr/divisor)<<" : "<<abs(rat1.denr/divisor);
}

OUTPUT:
Make your selection to perform operations on ratios:

1. Addition
2. Subtraction

PURCITM Page 5
Ankush MCA-I(Sem=II) 501

3. Multiplication
4. Division
5. Exit3
Enter First ratio:

Numerator: 23
Denominator: 32

Enter Second ratio:

Numerator: 3
Denominator: 2

Multiplication of two Ratios:


69 : 64

PURCITM Page 6
Ankush MCA-I(Sem=II) 501

/*WAP for creating structure DATE holding three values for day, month and
year. Write function for:
a) Check the given date is valid or not
b) Finding the difference in two dates
c) Display the date in format mm/dd/yyyy, Month dd, yyyy and dd-mm-yyyy
d) Detremine whether the given year is a leap year or not. */

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>

struct DATE
{
int day, month, year;
}d;

void valid(DATE d, int val)


{
if((d.day>31) || (d.month>12) || (d.year>2011))
{
cout<<"You Entered an invalid date";
return;
}
else
{
if(d.month == 2)
{
if(val == 1)
{
if(d.day>30)
cout<<"You entered invalid date";
}
else if(d.day>29)
cout<<"You entered invalid date";
else
cout<<"You entered valid date";
}
else if(d.month <= 7)
{

PURCITM Page 7
Ankush MCA-I(Sem=II) 501

if(d.month%2 == 0)
{
if(d.day>30)
cout<<"You entered invalid date";
else
cout<<"You entered valid date";
}
else
cout<<"Date is valid";
}
else if(d.month > 7)
{
if(d.month%2 != 0)
{
if(d.day>30)
cout<<"You entered invalid date";
else
cout<<"You entered valid date";
}
else
cout<<"Date is valid";
}
}
}

int func1(int x) //x for month y for dd


{
int y=0;
switch(x)
{
case 1:
y=0; break;
case 2:
y=31; break;
case 3:
y=59; break;
case 4:
y=90; break;
case 5:
y=120;break;

PURCITM Page 8
Ankush MCA-I(Sem=II) 501

case 6:
y=151; break;
case 7:
y=181; break;
case 8:
y=212; break;
case 9:
y=243; break;
case 10:
y=273; break;
case 11:
y=304; break;
case 12:
y=334; break;
default:
cout<<"Error encountered";
exit(1);
}
return(y);

void diff(DATE d)
{
DATE d1, diff;
cout<<"\nEnter second values for DATE\nDay:\n";
cin>>d1.day;
cout<<"Month:\n";
cin>>d1.month;
cout<<"Year:\n";
cin>>d1.year;
int ref,dd1,dd2,i;
ref = d.year;
if(d1.year<d.year)
ref = d1.year;
dd1=0;
dd1=func1(d.month);
for(i=ref;i<d1.year;i++)
{
if(i%4==0)

PURCITM Page 9
Ankush MCA-I(Sem=II) 501

dd1+=1;
}
struct DATE temp;
if(d.year>d1.year)
{
temp = d;
d = d1;
d1 = temp;
}
dd1=dd1+d.day+(d.year-ref)*365;
cout<<"\nNo. of days of first date fronm the Jan 1 "<<d.year<<" = "<<dd1;
/* Count for additional days due to leap years*/
dd2=0;
for(i=ref;i<d1.year;i++)
{
if(i%4==0)
dd2+=1;
}
dd2=func1(d1.month)+dd2+d1.day+((d1.year-ref)*365);
cout<<"\nNo. of days from the reference year's first Jan = "<<dd2;
cout<<"\nTherefore, diff between the two dates is "<<abs(dd2-dd1);

getch();
}

void display(DATE d)
{
cout<<"Date in form of mm/dd/yyyy: "<<d.day<<"/"<<d.month<<"/"<<d.year;
cout<<"\nDate in form of month date,year: "<<d.day<<" "<<d.month<<","<<d.year;
cout<<"\nDate in form of dd-mm-yyyy: "<<d.month<<"-"<<d.day<<"-"<<d.year;
}

int leap(DATE d)
{
if(((d.year%4 == 0) && (d.year%100 != 0)) || (d.year%400 == 0))
{
cout<<"Year entered is a Leap year";
return 1;
}
else

PURCITM Page 10
Ankush MCA-I(Sem=II) 501

{
cout<<"Year entered is not a Leap Year";
return 0;
}
}
void main()
{
int val;
clrscr();
cout<<"Entering DATE\nEnter day:\n";
cin>>d.day;
cout<<"Entering month:\n";
cin>>d.month;
cout<<"Entering year:\n";
cin>>d.year;
cout<<"\nPerforming functions as:\n";
cout<<"Checking the given date is valid or not\n";
valid(d, val);
cout<<"\n\nFinding the difference in two dates\n";
diff(d);
cout<<"\n\nDisplay the date in format mm/dd/yyyy, Month dd, yyyy and dd-mm-
yyyy\n";
display(d);
cout<<"\n\nDetermine whether the given year is a leap year or not\n";
val = leap(d);
getch();
}

OUTPUT:

Entering DATE
Enter day:
12
Entering month:
4
Entering year:
2000

Performing functions as:


Checking the given date is valid or not

PURCITM Page 11
Ankush MCA-I(Sem=II) 501

You entered valid date

Finding the difference in two dates

Enter second values for DATE


Day:
9
Month:
5
Year:
2010

No. of days of first date fronm the Jan 1 2000 = 105


No. of days from the reference year's first Jan = 3782
Therefore, diff between the two dates is 3677

Display the date in format mm/dd/yyyy, Month dd, yyyy and dd-mm-yyyy
Date in form of mm/dd/yyyy: 12/4/2000
Date in form of month date,year: 12 4,2000
Date in form of dd-mm-yyyy: 4-12-2000

Determine whether the given year is a leap year or not


Year entered is a Leap year

PURCITM Page 12
Ankush MCA-I(Sem=II) 501

/*WAP to define structure EMPLOYEE with its members and declare a


structure array Emp[20]*/

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

int size = 0;

struct DATE
{
int date, month, year;
};

struct EMPLOYEE
{
int empCode;
char empName[25], dept[20], Rank[15];
float Salary;
struct DATE DOB, DOJ;
}Emp[20];

void insert(void)
{
cout<<"Insert a new record\n";
cout<<"Enter employee Code: ";
cin>>Emp[size].empCode;
cout<<"Enter employee Name: ";
cin>>Emp[size].empName;
cout<<"Enter employee Department ";
cin>>Emp[size].dept;
cout<<"Enter emplyee's DOB in form of dd mm yyyy ";
cin>>Emp[size].DOB.date;
cin>>Emp[size].DOB.month;
cin>>Emp[size].DOB.year;
cout<<"Enter emplyee's DOJ in form of dd mm yyyy ";
cin>>Emp[size].DOJ.date;
cin>>Emp[size].DOJ.month;

PURCITM Page 13
Ankush MCA-I(Sem=II) 501

cin>>Emp[size].DOJ.year;
cout<<"Enter employee's Rank ";
cin>>Emp[size].Rank;
cout<<"Enter emplyee's Salary ";
cin>>Emp[size].Salary;
size++;
}

void modify(int EmpCode)


{
for(int i = 0; i<size; i++)
{
if((Emp[i].empCode == EmpCode) || (i == size))
break;
}
if(i == size)
cout<<"Code was not found";
else
{
cout<<"\nModify existing record\n";
cout<<"Enter employee Code: ";
cin>>Emp[i].empCode;
cout<<"Enter employee Name: ";
cin>>Emp[i].empName;
cout<<"Enter employee Department ";
cin>>Emp[i].dept;
cout<<"Enter emplyee's DOB in form of dd mm yyyy ";
cin>>Emp[i].DOB.date;
cin>>Emp[i].DOB.month;
cin>>Emp[i].DOB.year;
cout<<"Enter emplyee's DOJ in form of dd mm yyyy ";
cin>>Emp[i].DOJ.date;
cin>>Emp[i].DOJ.month;
cin>>Emp[i].DOJ.year;
cout<<"Enter employee's Rank ";
cin>>Emp[i].Rank;
cout<<"Enter emplyee's Salary ";
cin>>Emp[i].Salary;
}
}

PURCITM Page 14
Ankush MCA-I(Sem=II) 501

void deletee(int EmpCode)


{
for(int i = 0; i<size; i++)
{
if((Emp[i].empCode == EmpCode) || (i == size))
break;
}
if(i == size)
{
cout<<"\nRecord was not found\n";
}
else
{
for(int j = i+1; j<size; j++)
{
Emp[j-1] = Emp[j];
}
size--;
}
}

void display(void)
{
if(size == 0)
cout<<"There is nothing to display\n";
else
{
cout<<”\n”;
for(int i = 0; i<size; i++)
{
cout<<"Record of "<<i+1<<" :\n";
cout<<"Employee Code : "<<Emp[i].empCode;
cout<<"\nEmplyee Name : "<<Emp[i].empName;
cout<<"\nEmplyee Department : "<<Emp[i].dept;
cout<<"\nEmployee DOB : "<<Emp[i].DOB.date;
cout<<"-"<<Emp[i].DOB.month;
cout<<"-"<<Emp[i].DOB.year;
cout<<"\nEmployee DOJ : "<<Emp[i].DOJ.date;
cout<<"-"<<Emp[i].DOJ.month;

PURCITM Page 15
Ankush MCA-I(Sem=II) 501

cout<<"-"<<Emp[i].DOJ.year;
cout<<"\nEmplyee Rank : "<<Emp[i].Rank;
cout<<"\nEmplyee Salary : "<<Emp[i].Salary;
cout<<"\n\n";
}
}
}

void sort(void)
{
struct EMPLOYEE temp;
int check1, check2, check3, i, j = 0, k = 0, l = 0;
for(i = 0; i<size; i++)
{
for(j = 1; j<size; j++)
{
check1 = strcmp(Emp[i].dept, Emp[j].dept);
if(check1>0)
{
temp = Emp[i];
Emp[i] = Emp[j];
Emp[j] = temp;
}
}
}
for(i = 0; i<size;)
{
for(; j<size; j++)
{
check2 = strcmp(Emp[i].dept, Emp[j].dept);
if(check2 > 0)
break;
else
{
continue;
j++;
}
}
for(; l<j; l++)
{

PURCITM Page 16
Ankush MCA-I(Sem=II) 501

for(; k<j; k++)


{
check3 = strcmp(Emp[l].Rank, Emp[k].Rank);
if(check3>0)
{
temp = Emp[l];
Emp[l] = Emp[k];
Emp[k] = temp;
}
}
}
i = j;
}
}

void main()
{
char chk = 'y';
int num, EmpCode;
clrscr();
while((chk == 'y') || (chk == 'Y'))
{
cout<<"\nMake your selection\n";
cout<<"Press 1 to enter new employee record\n";
cout<<"Press 2 to modify an existing record\n";
cout<<"Press 3 to delete an existing reord\n";
cout<<"Press 4 to display all the records\n";
cout<<"Press 5 to sort the records dept wise, then within a dept. rank wise\n";
cout<<"Press 6 to exit\n";
cin>>num;
if((num<1) || (num>6))
{
cout<<"\nYou made an invalid selection";
continue;
}
switch(num)
{
case 1:
insert();
break;

PURCITM Page 17
Ankush MCA-I(Sem=II) 501

case 2:
cout<<"Enter the Employee code you want to modify\n";
cin>>EmpCode;
modify(EmpCode);
break;
case 3:
cout<<"Enter the Employee code you want to delete\n";
cin>>EmpCode;
deletee(EmpCode);
break;
case 4:
display();
break;
case 5:
sort();
break;
case 6:
exit(0);
}
while(1)
{
fflush(stdin);
cout<<"\nDo you want to continue Y/N\n";
cin>>chk;
if((chk == 'Y') || (chk == 'y'))
break;
else if((chk == 'N') || (chk == 'n'))
exit(0);
else
{
cout<<"You made an invalid selection";
continue;
}
}
if((chk == 'Y') || (chk == 'y'))
continue;
}
getch();
}

PURCITM Page 18
Ankush MCA-I(Sem=II) 501

OUTPUT:

Make your selection


Press 1 to enter new employee record
Press 2 to modify an existing record
Press 3 to delete an existing reord
Press 4 to display all the records
Press 5 to sort the records dept wise, then within a dept. rank wise
Press 6 to exit
1
Insert a new record
Enter employee Code: 1
Enter employee Name: Varun
Enter employee Department Comp.Sc.
Enter emplyee's DOB in form of dd mm yyyy 21
3
1988
Enter emplyee's DOJ in form of dd mm yyyy 11
1
2000
Enter employee's Rank HOD
Enter emplyee's Salary 35000

Do you want to continue Y/N


y

Make your selection


Press 1 to enter new employee record
Press 2 to modify an existing record
Press 3 to delete an existing reord
Press 4 to display all the records
Press 5 to sort the records dept wise, then within a dept. rank wise
Press 6 to exit
1
Insert a new record
Enter employee Code: 2
Enter employee Name: Piyush
Enter employee Department Admin
Enter emplyee's DOB in form of dd mm yyyy 6 7
1987

PURCITM Page 19
Ankush MCA-I(Sem=II) 501

Enter emplyee's DOJ in form of dd mm yyyy 21 4 1999


Enter employee's Rank Client
Enter emplyee's Salary 20000

Do you want to continue Y/N


y

Make your selection


Press 1 to enter new employee record
Press 2 to modify an existing record
Press 3 to delete an existing reord
Press 4 to display all the records
Press 5 to sort the records dept wise, then within a dept. rank wise
Press 6 to exit
1
Insert a new record
Enter employee Code: 3
Enter employee Name: Nakul
Enter employee Department Admin
Enter emplyee's DOB in form of dd mm yyyy 4 3 1987
Enter emplyee's DOJ in form of dd mm yyyy 20 11 1998
Enter employee's Rank Administrator
Enter emplyee's Salary 40000

Do you want to continue Y/N


y

Make your selection


Press 1 to enter new employee record
Press 2 to modify an existing record
Press 3 to delete an existing reord
Press 4 to display all the records
Press 5 to sort the records dept wise, then within a dept. rank wise
Press 6 to exit
4

Record of 1 :
Employee Code : 1
Emplyee Name : Varun
Emplyee Department : Comp.Sc.

PURCITM Page 20
Ankush MCA-I(Sem=II) 501

Employee DOB : 21-3-1988


Employee DOJ : 11-1-2000
Emplyee Rank : HOD
Emplyee Salary : 35000

Record of 2 :
Employee Code : 2
Emplyee Name : Piyush
Emplyee Department : Admin
Employee DOB : 6-7-1987
Employee DOJ : 21-4-1999
Emplyee Rank : Client
Emplyee Salary : 20000

Record of 3 :
Employee Code : 3
Emplyee Name : Nakul
Emplyee Department : Admin
Employee DOB : 4-3-1987
Employee DOJ : 20-11-1998
Emplyee Rank : Administrator
Emplyee Salary : 40000

Do you want to continue Y/N


y

Make your selection


Press 1 to enter new employee record
Press 2 to modify an existing record
Press 3 to delete an existing reord
Press 4 to display all the records
Press 5 to sort the records dept wise, then within a dept. rank wise
Press 6 to exit
5

Do you want to continue Y/N


y

Make your selection

PURCITM Page 21
Ankush MCA-I(Sem=II) 501

Press 1 to enter new employee record


Press 2 to modify an existing record
Press 3 to delete an existing reord
Press 4 to display all the records
Press 5 to sort the records dept wise, then within a dept. rank wise
Press 6 to exit
4

Record of 1 :
Employee Code : 3
Emplyee Name : Nakul
Emplyee Department : Admin
Employee DOB : 4-3-1987
Employee DOJ : 20-11-1998
Emplyee Rank : Administrator
Emplyee Salary : 40000

Record of 2 :
Employee Code : 1
Emplyee Name : Varun
Emplyee Department : Comp.Sc.
Employee DOB : 21-3-1988
Employee DOJ : 11-1-2000
Emplyee Rank : HOD
Emplyee Salary : 35000

Record of 3 :
Employee Code : 2
Emplyee Name : Piyush
Emplyee Department : Admin
Employee DOB : 6-7-1987
Employee DOJ : 21-4-1999
Emplyee Rank : Client
Emplyee Salary : 20000

Do you want to continue Y/N


n

PURCITM Page 22

You might also like