Beruflich Dokumente
Kultur Dokumente
#include <iostream.h>
#include<math.h>
#include<conio.h>
struct complex
{
float rel;
float img;
}s1,s2;
void main()
{
clrscr();
int ch;
float a,b;
cout<<"Enter real and imaginary part of 1st complex number:";
cin>>s1.rel>>s1.img;
cout<<"Enter real and imaginary part of 2nd complex number:";
cin>>s2.rel>>s2.img;
cout<<"For Addition Press 1\n";
cout<<"For Subtraction Press 2\n";
cout<<"For Multiplication Press 3\n";
cout<<"For Division Press 4\n";
cin>>ch;
if(ch==1)
{
a=(s1.rel)+(s2.rel);
b=(s1.img)+(s2.img);
cout<<"\nAddition: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
}
else if(ch==2)
{
a=(s1.rel)-(s2.rel);
b=(s1.img)-(s2.img);
cout<<"\nSubtraction: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
}
else if(ch==3)
{
a=((s1.rel)*(s2.rel))-((s1.img)*(s2.img));
b=((s1.rel)*(s2.img))+((s2.rel)*(s1.img));
2
cout<<"\nMultiplication: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
}
else if(ch==4)
{
a=(((s1.rel)*(s2.rel))+((s1.img)*(s2.img)))/(pow(s2.rel,2)+pow(s2.img,2));
b=(((s2.rel)*(s1.img))-((s1.rel)*(s2.img)))/(pow(s2.rel,2)+pow(s2.img,2));
cout<<"\nDivision: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
}
else
cout<<"INVALID INPUT";
getch();
}
3
4
/*Write a menu driven program to calculate the area of a circle, square and
rectangle using function overloading.*/
#include<iostream.h>
#include<math.h>
#include<conio.h>
float area(float a)
{
return a*a;
}
void main()
{
int op;
float ar, a,b,c;
cout<<"AREA CALCULATOR\n";
cout<<"1. SQUARE \t2. RECTANGLE \t3. TRIANGLE \t4. CIRCLE \n0. EXIT \n";
do
{
cout<<"\nMENU OPTION: ";
cin>>op;
switch(op)
{
case 1: cout<<"Enter Side: ";
cin>>a;
ar=area(a);
5
break;
case 0: break;
if((op>0)&&(op<5))
{
cout<<"Area : "<<ar<<endl;
getch();
}
}while(op>0);
6
7
/*Write a program to define a class Garments in C++*/
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
#include<string.h>
class Garments
{
char Gcode[10], Gtype[10], Gfabric[10];
int Gsize;
float Gprice;
void assign();
public: void input();
void output();
};
void Garments::assign()
{
if(strcmp(Gtype,"TROUSER")==0)
Gprice=1300;
else if(strcmp(Gtype,"SHIRT")==0)
Gprice=1100;
if(strcmp(Gfabric,"COTTON")!=0)
Gprice=Gprice-(Gprice/10);
}
void Garments::input()
{
cout<<"Enter The Garment CODE, TYPE, FABRIC and SIZE\n";
gets(Gcode);
gets(Gtype);
gets(Gfabric);
cin>>Gsize;
}
void Garments::output()
{
assign();
cout<<"Price of the above entry is "<<Gprice;
}
void main()
{
8
clrscr();
Garments g;
g.input();
g.output();
getch();
}
9
10
/*Imagine a publishing company that markets both books and audio-cassette
versions of its work. Create a class Publication that stores the title(a string)
and price (type float) of a publication.*/
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Publication
{
char title[50];
float price;
public: void getData()
{
cout<<"\nEnter Title: ";
gets(title);
cout<<"Enter Price: ";
cin>>price;
}
void putData()
{
cout<<"\nTitle: "<<title<<"\nPrice: "<<price;
}
};
void putData()
{
Publication::putData();
cout<<"\nPages: "<<pages;
}
};
11
float minutes;
public: void getData()
{
Publication::getData();
cout<<"Enter Minutes: ";
cin>>minutes;
}
void putData()
{
Publication::putData();
cout<<"\nMinutes: "<<minutes;
}
};
void main()
{
clrscr();
Book b;
Tape t;
b.getData();
t.getData();
b.putData();
cout<<endl;
t.putData();
cout<<endl;
getch();
}
12
13
/*Write a program to demonstrate the order in which base class & derived
class constructors are called.*/
#include<iostream.h>
#include<conio.h>
class Parent
{
public: Parent()
{
cout<<"Base class"<<endl;
}
};
void main()
{
clrscr();
Child obj;
getch();
}
14
15
/*Write a function in C++ to print the count of “the” word as an independent
word in a text file STORY.TXT.*/
#include<fstream.h>
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
#include<string.h>
void countword()
{
ifstream fin;
fin.open("STORY.TXT");
char word[30];
int count=0;
while(!fin.eof())
{
fin>>word;
if(strcmpi(word,"the")==0)
count++;
}
cout<<"\n\nNumber of the word in file are "<<count;
fin.close();
}
void display()
{
ifstream fin;
fin.open("STORY.TXT");
char word;
while(!fin.eof())
{
fin.get(word);
cout<<word;
}
fin.close();
}
void main()
{
clrscr();
display();
countword();
getch();
}
16
17
/*Write a program to delete a record from a binary file.*/
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class stu
{
int rollno;
char name[15],Class[4];
float marks;
char grade;
public:
void getdata();
void putdata();
int getrno()
{
return rollno;
}
}s1,stud;
void stu::getdata()
{
cout<<"enter roll no:";
cin>>rollno;
cout<<"enter name :";
gets(name);
cout<<"enter class :";
gets(Class);
cout<<"enter marks :";
cin>>marks;
cout<<"enter grade";
cin>>grade;
};
void stu::putdata()
{
cout<<"roll no:"<<rollno<<"\t Name:";
puts(name);
cout<<"\n Marks:"<<marks<<"\t grade :"<<grade;
};
void main()
{
clrscr();
ifstream fio("stu.dat",ios::in);
ofstream file("temp.dat",ios::out|ios::app);
18
int rno;
char found='f',confirm='n';
cout<<"enter rollno of student whose record is to be deleted \n";
cin>>rno;
while(!fio.eof())
{
fio.read((char*)&s1,sizeof(s1));
if(s1.getrno()==rno)
{
s1.putdata();
found='t';
cout<<"are you sure want to delete this record(y/n)";
cin>>confirm;
if(confirm=='y')
file.write((char*)&s1,sizeof(s1));
}
else
{
file.write((char*)&s1,sizeof(s1));
cout<<"Record successfully deleted";
}
}
if(found=='f')
cout<<"record not found!!\n";
fio.close();
file.close();
remove("stu.dat");
rename("temp.dat","stu.dat");
fio.open("stu.dat",ios::in);
cout<<"now file contains \n";
while(!fio.eof())
{
fio.read((char*)&stud,sizeof(stud));
if(fio.eof())
break;
stud.putdata();
}
fio.close();
getch();
}
19
20
/*Write a program using pointers to find the length of a string and print
reversed string.*/
#include<iostream.h>
#include<conio.h>
while(i>0)
{
strp--;
*resp=*strp;
resp++;
--i;
}
*resp='\0';
cout<<"Reverse of a string is:- ";
puts(res);
}
void main()
{
clrscr();
char str[50];
cout<<"Enter the String";
21
gets(str);
cout<<"Length of the string is "<<length(str);
reverse();
getch();
}
22
23
/*Write a program using pointers to find smallest & largest element in a
dynamically created array.*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *p;
int x;
cout<<"Enter the number of elements to enter: ";
cin >> x;
p=new int[x];
for(int i=0;i<x;i++)
{
cout << "Enter the element: ";
cin >> *(p + i);
}
int max = 0;
int min = 0;
max = *p;
min = *p;
for(i=0;i<x;i++)
{
if (min > *(p+i))
{
min = *(p+i);
}
}
for(i=0;i<x;i++)
{
if (max < *(p+i))
{
max = *(p+i);
}
}
cout << "Max num is : "<< max << "\n Min num is : " << min << endl;
getch();
}
24
25
/*Write a program to swap two integers using pointers (pass by reference).*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap(&a, &b);
cout << "\nAfter swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
getch();
}
26