cg programs

51
NAME--HARSHAL PAWALE ROLL NO.—2053 #include<iostream.h> #include<conio.h> #include<process.h> #include<dos.h> class weather_report { private: float hightemp,lowtemp; float amount_rain,amount_snow; public: static int day_of_month; weather_report() { //default constructor hightemp=999; //which initialise the data lowtemp=-999; //members for dafault values amount_rain=0; amount_snow=0; } void getdata(char *); //function to get the data from user inline void display(int); //function to display the monthly report static int increment(); //function to increment the day of month friend void average(weather_report*); ~weather_report(); //destructor }; int weather_report::day_of_month; //static member declaration void hline() { cout<<"----------------------------------------------------------------------- ---------"; } void weather_report::getdata(char *m) { cout<<"\n\t\t### ENTER THE REPORT Of "<<increment()<<","<<m<<" 2010 ###\n\ n"; cout<<"\n Enter the high temp:"; cin>>hightemp; cout<<"\n Enter the low temp:"; cin>>lowtemp; cout<<"\n Enter the amount rain:"; cin>>amount_rain; cout<<"\n Enter the amount_snow:"; cin>>amount_snow; cout<<"\n\n\tPress Any Key To Continue...."; getch(); } void weather_report::display(int day) { gotoxy(3,10+day); cout<<day; gotoxy(22,10+day);

Upload: avnish-singh

Post on 26-Mar-2015

32 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cg Programs

NAME--HARSHAL PAWALEROLL NO.—2053

#include<iostream.h>#include<conio.h>#include<process.h>#include<dos.h>class weather_report{ private: float hightemp,lowtemp;

float amount_rain,amount_snow; public: static int day_of_month;

weather_report() { //default constructor

hightemp=999; //which initialise the datalowtemp=-999; //members for dafault valuesamount_rain=0;amount_snow=0;

} void getdata(char *); //function to get the data from user inline void display(int); //function to display the monthly report static int increment(); //function to increment the day of month friend void average(weather_report*); ~weather_report(); //destructor

};int weather_report::day_of_month; //static member declarationvoid hline(){ cout<<"--------------------------------------------------------------------------------";}void weather_report::getdata(char *m){ cout<<"\n\t\t### ENTER THE REPORT Of "<<increment()<<","<<m<<" 2010 ###\n\n"; cout<<"\n Enter the high temp:"; cin>>hightemp; cout<<"\n Enter the low temp:"; cin>>lowtemp; cout<<"\n Enter the amount rain:"; cin>>amount_rain; cout<<"\n Enter the amount_snow:"; cin>>amount_snow; cout<<"\n\n\tPress Any Key To Continue...."; getch();}void weather_report::display(int day){ gotoxy(3,10+day); cout<<day; gotoxy(22,10+day); cout<<hightemp<<" øc"; gotoxy(39,10+day); cout<<lowtemp<<" øc"; gotoxy(56,10+day); cout<<amount_rain<<" mm"; gotoxy(72,10+day); cout<<amount_snow<<" mm"; delay(25);}void disp(char *m)

Page 2: Cg Programs

{ gotoxy(10,3); cout<<"\t\t~~~~ THE WEATHER REPORT ~~~~"; gotoxy(35,5); cout<<m<<" , 2010"; gotoxy(1,7); hline(); gotoxy(1,8); cout<<"1.DAY_Of_MONTH"; gotoxy(18,8); cout<<"2.HIGHEST TEMP."; gotoxy(35,8); cout<<"3.LOWEST TEMP."; gotoxy(52,8); cout<<"4.AMOUNT RAIN"; gotoxy(68,8); cout<<"5.AMOUNT SNOW"; gotoxy(1,9); hline();}int weather_report::increment(){ day_of_month++; return(day_of_month);}void average(weather_report *w1){ float avg_hightemp=0,avg_lowtemp=0; float avg_amount_rain=0,avg_amount_snow=0; for(int i=1;i<=w1->day_of_month;i++) { avg_hightemp=avg_hightemp+w1[i].hightemp; avg_lowtemp=avg_lowtemp+w1[i].lowtemp; avg_amount_rain=avg_amount_rain+w1[i].amount_rain; avg_amount_snow=avg_amount_snow+w1[i].amount_snow; } avg_hightemp=avg_hightemp/w1->day_of_month; avg_lowtemp=avg_lowtemp/w1->day_of_month; avg_amount_rain=avg_amount_rain/w1->day_of_month; avg_amount_snow=avg_amount_snow/w1->day_of_month; cout<<"\nAverage_High_Temp= "<<avg_hightemp; cout<<"\tAverage_Low_Temp= "<<avg_lowtemp; cout<<"\n\nAverage_amount_Rain= "<<avg_amount_rain; cout<<"\tAverage_amount_Snow= "<<avg_amount_snow;}weather_report::~weather_report(){ weather_report *destruct=this; delete [] destruct;}int main(){ clrscr(); int month,days,ch,i; char *months[]={"Jan","Feb","March","April","May","June","July","August","Sept","Oct","Nov","Dec"}; int days_in_month[]={31,28,31,30,31,30,31,31,30,31,30,31}; do { cout<<"\n\n\tENTER THE MONTH Of REPORT: ";

Page 3: Cg Programs

cin>>month; days=days_in_month[month-1]; if(month>12) {

cout<<"\nYou have entered wrong month...Entered month between 1 to 12"; continue;

} else

break; }while(1); weather_report *w; w=new weather_report[days_in_month[month-1]+1]; do { clrscr(); cout<<"\n\t\t$$$$$$ DAILY WEATHER REPORT INFO $$$$$$"; cout<<"\n\n1.DEFAULT VALUES\n2.ACCEPT REPORT\n3.DISPLAY REPORT\n4.EXIT"; cout<<"\n\nEnter the choice: "; cin>>ch; switch(ch) {

case 1: clrscr(); disp(months[month-1]); for(i=1;i<=days;i++) { w[i].display(i); } cout<<"\n\n"; hline(); cout<<"\n\n\tPress Any Key To Continue....."; getch(); break;

case 2: clrscr(); char ch; for(i=1;i<=days;i++) { w[i].getdata(months[month-1]); } break;

case 3: clrscr(); disp(months[month-1]); for(i=1;i<=w->day_of_month;i++) { w[i].display(i); } average(w++); cout<<"\n\n"; hline(); cout<<"\n\tPress Any Key To Continue....."; getch(); break;

case 4: exit(0); } }while(1);}

NAME--HARSHAL PAWALEROLL NO.—2053

Page 4: Cg Programs

#include<iostream.h>#include<conio.h>#include<stdlib.h>class complex{ float real; float imagi;

public: complex() //default constructor { real=imagi=0.0; }

complex(float x,float y) { //parameterisred constructor real=x; imagi=y; } complex operator+(complex); //overloading '+' operator for // addition of complex no. using member fn complex operator-(complex);//overloading '-' for subtraction using member fn friend complex operator*(complex,complex);//overloading '*'for multiplication using friend fn complex operator/(complex);//overloading '/'for division purpose void disp_complex(); //function for display complex

}; //end of class definitioncomplex complex::operator+(complex c2){ complex temp; temp.real=real+c2.real; temp.imagi=imagi+c2.imagi;

return(temp);}complex complex::operator-(complex c2){ complex temp; temp.real=real-c2.real; temp.imagi=imagi-c2.imagi;

return(temp);}

complex operator*(complex c1,complex c2){ complex temp; temp.real=c1.real*c2.real+c1.real*c2.imagi; temp.imagi=c1.imagi*c2.real+c1.imagi*c2.imagi;

return(temp);}

complex complex::operator/(complex c2){ complex temp(c2.real,-c2.imagi); //calculates rationalised no.to multiplied with num. & deno.

float ratio=(c2.real*temp.real)+(-c2.imagi*temp.imagi);//calc product of denominator complex c1=*this; //returns the invoking object

Page 5: Cg Programs

complex product_num=c1*temp; //calc product of numerator product_num.real=product_num.real/ratio; //divides real & imagi. part by product_num.imagi=product_num.imagi/ratio; //denominator's ratio

return(product_num);}void complex::disp_complex(){ if(imagi<0) {

float i=-imagi; cout<<" "<<real<<" - i "<<i;

} else

cout<<" "<<real<<" + i "<<imagi;}

int main(){ clrscr(); int ch; float R,I; complex c1,c2,c3;//create objects with default constructor do {

cout<<"\n\n\t\tComplex No. Operations";cout<<"\n\n1.Accept Complex No's\n2.Addition\n3.Subtraction";cout<<"\n4.Multiplication\n5.Division\n6.Exit";cout<<"\n\nEnter the choice: ";cin>>ch;switch(ch){ case 1:

cout<<"\n\nEnter the 1st Complex no.: "; cin>>R>>I; complex c1(R,I); //calls parameterised constructor cout<<"\n\nEnter the 2nd Complex no.: "; cin>>R>>I; complex c2(R,I);//calls parameterised constructor break;

case 2: cout<<"\nThe 1st Complex no.: "; c1.disp_complex(); cout<<"\n\nThe 2nd Complex no.: "; c2.disp_complex(); cout<<"\n\nThe Addition of two Complex no. is: "; c3=c1+c2; //similar to c1.operator+(c2); c3.disp_complex(); break;

case 3: cout<<"\nThe 1st Complex no.: "; c1.disp_complex(); cout<<"\n\nThe 2nd Complex no.: "; c2.disp_complex(); cout<<"\n\nThe Subtraction of two Complex no. is: "; c3=c1-c2; //similar to c1.operator-(c2); c3.disp_complex(); break;

case 4:

Page 6: Cg Programs

cout<<"\nThe 1st Complex no.: "; c1.disp_complex(); cout<<"\n\nThe 2nd Complex no.: "; c2.disp_complex(); cout<<"\n\nThe Multiplication of two Complex no. is: "; c3=c1*c2; //similar to operator*(c1,c2); c3.disp_complex(); break;

case 5: cout<<"\nThe 1st Complex no.: "; c1.disp_complex(); cout<<"\n\nThe 2nd Complex no.: "; c2.disp_complex(); cout<<"\n\nThe Division of two Complex no.: is: "; c3=c1/c2;//similar to c1.operator/(c2); c3.disp_complex(); break;

case 6: exit(0);

} }while(1);}

NAME--HARSHAL PAWALEROLL NO.—2053

TITLE:WRITE A PROGRAM TO IMPLEMENT 2D TRANSFORMATION.

Page 7: Cg Programs

#include<stdlib.h>#include<dos.h>#include<conio.h>#include<graphics.h>#include<math.h>#include<snap.h>

class POLYGON{

private:int p[10][10];

public:int accept_poly(int [][10]);void draw_poly(int [][10],int);void draw_polyfloat(float [][10],int);void matmult(int [][10],int [][10],int,int,int,int [][10]);void matmultfloat(float [][10],int [][10],int,int,int,float [][10]);void shearing(int [][10],int);void scaling(int [][10],int);void rotation(int [][10],int);void translation(int [][10],int);void reflection(int [][10],int);

};int POLYGON :: accept_poly(int p[][10]){

int i,n;cout << "\n\n\t\tEnter no.of vertices:";cin >> n;for(i=0;i<n;i++){

cout << "\n\n\t\tEnter (x,y)Co-ordinate of point P" << i << ": ";cin >> p[0][i] >> p[1][i];p[2][i] = 1;

}p[0][n] = p[0][0];p[1][n] = p[1][0];p[2][n] = 1;return n;

}void POLYGON :: draw_poly(int p[][10], int n){

int i,gd = DETECT,gm;initgraph(&gd,&gm,"C:\\TC\\BGI");line(320,0,320,480);line(0,240,640,240);for(i=0;i<n;i++){

if(i!=n-1)line(p[0][i]+320,240-p[1][i],p[0][i+1]+320,240-p[1][i+1]);

elseline(p[0][i]+320,240-p[1][i],p[0][0]+320,240-p[1][0]);

}getch();closegraph();

}void POLYGON :: draw_polyfloat(float p[][10], int n){

int i,gd = DETECT,gm;initgraph(&gd,&gm,"C:\\TC\\BGI");

Page 8: Cg Programs

line(320,0,320,480);line(0,240,640,240);for(i=0;i<n;i++){

if(i!=n-1)line(p[0][i]+320,-p[1][i]+240,p[0][i+1]+320,-p[1][i+1]+240);

elseline(p[0][i]+320,-p[1][i]+240,p[0][0]+320,-p[1][0]+240);

}getch();closegraph();

}void POLYGON :: matmult(int mat1[][10],int mat2[][10],int r1,int c1,int c2,int mat3[][10]){

int i,j,k;for(i=0;i<10;i++)

for(j=0;j<10;j++)mat3[i][j] = 0;

for(i=0;i<r1;i++)for(j=0;j<c2;j++)

for(k=0;k<c1;k++)mat3[i][j] = mat3[i][j]+(mat1[i][k] * mat2[k][j]);

mat3[c2][0] = mat3[0][0];mat3[c2][1] = mat3[0][1];mat3[c2][2] = mat3[0][2];

}void POLYGON :: matmultfloat(float mat1[][10],int mat2[][10],int r1,int c1,int c2,float mat3[][10]){

int i,j,k;for(i=0;i<10;i++)

for(j=0;j<10;j++)mat3[i][j] = 0;

for(i=0;i<r1;i++)for(j=0;j<c2;j++)

for(k=0;k<c1;k++)mat3[i][j] = mat3[i][j]+(mat1[i][k] * mat2[k][j]);

mat3[c2][0] = mat3[0][0];mat3[c2][1] = mat3[0][1];mat3[c2][2] = mat3[0][2];

}void POLYGON :: translation(int p[10][10],int n){

int tx,ty,i,j,translate[10][10],Result[10][10];cout << "\n\n\t\tEnter X-Translation tx: ";cin >> tx;cout << "\n\n\t\tEnter Y-Translation ty: ";cin >> ty;for(i=0;i<3;i++)

for(j=0;j<3;j++)translate[i][j] = 0;

translate[0][0] = translate[1][1] = translate[2][2] = 1;translate[0][2] = tx;translate[1][2] = ty;matmult(translate,p,3,3,n,Result);cout << "\n\n\t\tPolygon after Translation...";draw_poly(Result,n);

}void POLYGON :: reflection(int p[][10],int n){

Page 9: Cg Programs

int type,i,j,reflect[10][10],Result[10][10];cout << "\n\t************************Reflection Types************************";cout << "\n\n\t\t1.About X-Axis \n\n\t\t2.About Y-Axis \n\n\t\t3.About Origin \

\n\n\t\t4.About Line y = x \n\n\t\t5.About Line y = -x \ \n\n\t\tEnter your choice(1-5): ";

cin >> type;for(i=0;i<3;i++)

for(j=0;j<3;j++){

if(i == j)reflect[i][j] = 1;

elsereflect[i][j] = 0;

}switch(type){

case 1: reflect[1][1] = -1;break;

case 2: reflect[0][0] = -1;break;

case 3: reflect[1][1] = -1;reflect[0][0] = -1;break;

case 4: reflect[1][0] = reflect[0][1] = 1;reflect[1][1] = 0;reflect[0][0] = 0;break;

case 5: reflect[1][0] = reflect[0][1] = -1;reflect[1][1] = 0;reflect[0][0] = 0;break;

}matmult(reflect,p,3,3,n,Result);cout << "\n\n\t\tPolygon after Reflection...";draw_poly(Result,n);

}void POLYGON :: scaling(int p[][10],int n){

float Sx,Sy,result[10][10],scale[10][10],i,j;cout << "\n\n\t\tEnter X-Scaling Sx: ";cin >> Sx;cout << "\n\n\t\tEnter Y-Scaling Sy: ";cin >> Sy;for(i=0;i<3;i++)

for(j=0;j<3;j++)scale[i][j] = 0;

scale[0][0] = Sx;scale[1][1] = Sy;scale[2][2] = 1;matmultfloat(scale,p,3,3,n,result);cout << "\n\n\t\tPolygon after Scaling...";draw_polyfloat(result,n);

}void POLYGON :: rotation(int p[][10],int n){

float type,rotate[10][10],result[10][10],i,j,Ang,Sinang,Cosang;cout << "\n\n\t\tEnter the angle of rotation in degrees: ";cin >> Ang;cout << "\n\t************************Rotation Types************************";

Page 10: Cg Programs

cout << "\n\n\t\t1.Clockwise Rotation \n\n\t\t2.Anti-Clockwise Rotation ";cout << "\n\n\t\tEnter your choice(1-2): ";cin >> type;Ang = (Ang * 6.2832)/360;Sinang = sin(Ang);Cosang = cos(Ang);for(i=0;i<3;i++)

for(j=0;j<3;j++)rotate[i][j] = 0;

rotate[0][0] = rotate[1][1] = Cosang;rotate[0][1] = rotate[1][0] = Sinang;rotate[2][2] = 1;if(type == 1)

rotate[1][0] = -Sinang;rotate[0][1] = Sinang;matmultfloat(rotate,p,3,3,n,result);cout << "\n\n\t\tPolygon after Rotation...";draw_polyfloat(result,n);

}void POLYGON :: shearing(int p[][10],int n){

int Sx,Sy,type,i,j,shear[10][10],Result[10][10];for(i=0;i<3;i++)

for(j=0;j<3;j++){

if(i == j)shear[i][j] = 1;

elseshear[i][j] = 0;

}cout << "\n\t************************Shearing Types************************";cout << "\n\n\t\t1.X-Direction Shear \n\n\t\t2.Y-Direction Shear ";cout << "\n\n\t\tEnter your choice(1-2): ";cin >> type;if(type == 1){

cout << "\n\n\t\tEnter X-Shear Sx: ";cin >> Sx;shear[0][1] = Sx;

}else{

cout << "\n\n\t\tEnter Y-Shear Sy: ";cin >> Sy;shear[1][0] = Sy;

}matmult(shear,p,3,3,n,Result);cout << "\n\n\t\tPolygon after Shearing...";draw_poly(Result,n);

}

int menu(){

int ch;clrscr();cout << "\n\t***************************2-D TRANSFORMATION************************";cout << "\n\n\t\t1.Translation \n\n\t\t2.Scaling \n\n\t\t3.Rotation \

\n\n\t\t4.Reflection \n\n\t\t5.Shearing \n\n\t\t6.Exit \ \n\n\t\tEnter your choice(1-6): ";

Page 11: Cg Programs

cin >> ch;return ch;

}void main(){

int ch,n,p[10][10];POLYGON ob;clrscr();n = ob.accept_poly(p);clrscr();cout << "\n\n\t\tOriginal Polygon ...";ob.draw_poly(p,n);do{

ch = menu();switch(ch){

case 1:ob.translation(p,n);capture("t1.bmp");break;

case 2:ob.scaling(p,n);capture("t2.bmp");break;

case 3:ob.rotation(p,n);capture("t3.bmp");break;

case 4: ob.reflection(p,n);capture("t4.bmp");break;

case 5: ob.shearing(p,n);capture("t5.bmp");break;

case 6: exit(0);}

}while(ch!=6);getch();

}

NAME--HARSHAL PAWALEROLL NO.—2053

#include<fstream.h>#include<process.h>#include<stdio.h>#include<conio.h>#include<iostream.h>class stud

Page 12: Cg Programs

{protected:

char name[15];int roll;stud(){}

};class subc:virtual protected stud{

protected:int subcode;subc(){}

};class subn:virtual protected stud{

protected:char subname[10];subn(){}

};class marks:private subn,private subc{

int internal;int external;

public:int retroll(){

return roll;}void getdata();void dispall();void display();

};void marks::getdata(){

cout<<"\n\n\tName : ";gets(name);cout<<"\n\tRoll no. : ";cin>>roll;cout<<"\n\tSub code : ";cin>>subcode;cout<<"\n\tSub name : ";gets(subname);cout<<"\n\tInternal marks : ";cin>>internal;cout<<"\n\tUniversity marks : ";cin>>external;

}void marks::dispall(){

cout<<name<<"\t"<<roll<<"\t"<<subcode<<"\t"<<subname<<"\t"<<internal<<"\t"

Page 13: Cg Programs

<<external<<"\t";}void marks::display(){

cout<<"\n\tName : "<<name<<"\n\tRoll no. : "<<roll<<"\n\tSub code : "<<subcode<<"\n\tSub name : "<<subname<<"\n\tInternal marks : "<<internal<<"\n\tUniversity marks : "<<external;

}class file{

marks m;fstream fio;

public:void create(int);void read();void write();void search(int );void del(int);void modify(int);

};void file::create(int n){

fio.open("stud",ios::out|ios::binary);for(int i=0;i<n;i++){

cout<<"\nEnter details for entry "<<i+1;m.getdata();fio.write((char*)&m,sizeof(m));

}fio.close();

}void file::read(){

int flag=0;fio.open("stud",ios::in|ios::binary);while(fio.read((char*)&m,sizeof(m))){

m.dispall();cout<<"\n";flag=1;

}if(!flag){

cout<<"\nFile empty!!!";}fio.close();

}void file::write(){

fio.open("stud",ios::app|ios::binary);m.getdata();fio.write((char*)&m,sizeof(m));fio.close();

}void file::search(int no){

Page 14: Cg Programs

fio.open("stud",ios::in|ios::binary);int flag=0;while(fio.read((char *)&m,sizeof(m))){

if(no==m.retroll()){

cout<<"\nsearch found!!!!\n";m.display();flag=1;break;

}}if(flag==0)

cout<<"\n\tRecord not found!!!";fio.close();

}void file::modify(int no){

ofstream fout;fout.open("temp",ios::out|ios::binary);fio.open("stud",ios::in|ios::binary);int flag=0;while(fio.read((char *)&m,sizeof(m))){

if(no==m.retroll()){

cout<<"\nEnter new details :-";m.getdata();fout.write((char*)&m,sizeof(m));flag=1;

}else

fout.write((char*)&m,sizeof(m));

}if(flag==0)

cout<<"\n\tRecord not found!!!";else

cout<<"\n\tRecord modified!!!";fio.close();fout.close();remove("stud");rename("temp","stud");

}void file::del(int no){

ofstream fout;fout.open("temp",ios::out|ios::binary);fio.open("stud",ios::in|ios::binary);int flag=0;while(fio.read((char *)&m,sizeof(m))){

if(no!=m.retroll())fout.write((char*)&m,sizeof(m));

elseflag=1;

}if(flag==0)

cout<<"\n\tRecord not found!!!";

Page 15: Cg Programs

elsecout<<"\n\tRecord deleted!!!";

fout.close();fio.close();remove("stud");rename("temp","stud");

}void main(){

file f;int ch;while(1){

clrscr();cout<<"\n1.Create master file"

<<"\n2.Insert a record"<<"\n3.Read All"<<"\n4.Read one"<<"\n5.Modify"<<"\n6.Delete"<<"\n7.Exit\n\n";

cin>>ch;int n;clrscr();switch(ch){

case 1: cout<<"\nEnter the no. of records : ";cin>>n;f.create(n);break;

case 2: cout<<"\nEnter details :-";f.write();break;

case 3: f.read();getch();break;

case 4: cout<<"enter the roll no.to be seacrh: ";cin>>n;f.search(n);getch();break;

case 5: cout<<"enter the roll no.to be modified : ";cin>>n;f.modify(n);getch();break;

case 6: cout<<"enter the roll no.to be deleted : ";cin>>n;f.del(n);getch();break;

case 7: exit(0);}

}}

Page 16: Cg Programs

NAME--HARSHAL PAWALEROLL NO.—2053

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

class television{public:

int model_no,screen_size,prize;

void operator<<(char);void operator>>(char);

Page 17: Cg Programs

};void television::operator<<(char a){

cout<<"\n\tEnter the model no. : ";cin>>model_no;try{

if(model_no>999){

throw(1);}

}catch(int i){

cout<<"\n\tEnter the valid model no.";model_no=0;

}cout<<"\n\n\tEnter the screen size : ";cin>>screen_size;try{

if(screen_size<=12||screen_size>=17){

throw(1);}

}catch(int i)

{cout<<"\n\tEnter the valid screen size .";screen_size=0;

}cout<<"\n\n\tEnter the prize : ";cin>>prize;try{

if(prize<0||prize>5000){

throw(1);}

}catch(int i)

{cout<<"\n\tEnter the valid prize .";prize=0;

}}

void television::operator>>(char b){

cout<<"\n\tEnter the model no. : "<<model_no;cout<<"\n\tEnter the screen size : "<<screen_size;cout<<"\n\tEnter the prize : "<<prize;cout<<"\n";

}void main(){

int ch;television t;do{t<<('\t');

Page 18: Cg Programs

t>>('\t');cout<<"\n\tDo you want to continue (1/0) : ";cin>>ch;}while(ch!=0);getch();

}

NAME--HARSHAL PAWALEROLL NO.—2053

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

class str_opn{ char str[20];

public: str_opn(){};

Page 19: Cg Programs

str_opn(char *s) { strcpy(str,s); }

int operator=(str_opn &); //for equality void operator==(str_opn &); //string copy void operator+(str_opn &); //concatnation

friend void operator<<(ostream &,str_opn &); //display string void operator>>(str_opn &); //reverse string

int str_len(char*); //calulates the length of string void str_palin(str_opn &);//for palindrome void substring(str_opn &);

};int str_opn::str_len(char *s){ int len=0; for(int i=0;s[i]!=NULL;i++) {

len++; } return(len);}int str_opn::operator=(str_opn &s2){ int l1=str_len(str); int l2=str_len(s2.str);

if(l1==l2) { for(int i=0;str[i]!=NULL;i++) {

if(str[i]==s2.str[i]) {

continue; } else {

return 0; }

} if(i==l1) {

return 1; } } return 0;}void str_opn::operator==(str_opn &s2){ for(int i=0;str[i]!=NULL;i++) {

s2.str[i]=str[i]; } s2.str[i]=NULL;}void str_opn::operator+(str_opn &s2)

Page 20: Cg Programs

{ int l1=str_len(str); for(int i=0;s2.str[i]!=NULL;i++) {

str[l1]=s2.str[i]; l1++;

} str[l1]=NULL;}

void str_opn::operator>>(str_opn &s2){ int l1=str_len(str); for(int i=0;str[i]!=NULL;i++) {

s2.str[i]=str[l1-1]; l1--;

} s2.str[i]=NULL;}void operator<<(ostream & disp_str,str_opn & s){ disp_str<<"\t"<<s.str;}void str_opn::str_palin(str_opn &s2){ int l1=str_len(str);

for(int i=0;str[i]!=NULL;i++) {

if(str[i]==s2.str[i]) { continue; } else { cout<<" is not Palindrome"; break; }

} if(i==l1) {

cout<<" is Palindrome"; }}

void str_opn::substring(str_opn &s2){ int l1=str_len(str); int l2=str_len(s2.str); int j,k; for(int i=0;i<=l1-l2;i++) {

j=0;k=i;while((str[k]==s2.str[j])&&(j<l2)){ k++; j++;

Page 21: Cg Programs

}if(j==l2) cout<<"\nThe pattern string is found at position: "<<i;

} if(k==i)

cout<<"\nThe pattern string is not found";}int main(){ clrscr(); int ch; char str1[20],str2[20],str3[20]; str_opn s1,s2,s3,s4; do { cout<<"\n\n\tString Operations Using Operator Overloading"; cout<<"\n\n1.Accept\n2.Equality\n3.String Copy\n4.Display String"; cout<<"\n5.Reverse String\n6.Palindrome\n7.Concatenation\n8.Substring\n9.Exit"; cout<<"\n\nEnter the choice: "; cin>>ch; switch(ch) {

case 1: //for accepting string cout<<"\nEnter the String_1: "; cin>>str1; str_opn s1(str1);

cout<<"\n\nEnter the String_2: "; cin>>str2; str_opn s2(str2);

break; case 2: //for comparing string

cout<<"\nThe first String is:"; cout<<s1; //invoke operator<<(s1)

cout<<"\nThe second String is:"; cout<<s2; //invoke the function operator<<(s2) int result=(s1=s2); //invoke the function s1.operator=(s2) if(result==1) cout<<"\nThe given Strings are Equal"; else cout<<"\nThe given Strings are Not Equal"; break;

case 3: //for string copy cout<<"\nThe Original String is: "; cout<<s1; s1==s3; //invoke s1.operator==(s3) cout<<"\nThe Copied String is: "; cout<<s3; break;

case 4: //for displaying string cout<<"\nThe Entered Strings are: "; cout<<"\nString_1 :"; cout<<s1; cout<<"\nString_2 :"; cout<<s2; break;

case 5: //for string reverse

Page 22: Cg Programs

cout<<"\nThe Original String is:"; cout<<s1; s1>>s3; //invoke s1.operator>>(s3) cout<<"\nThe Reversed String is:"; cout<<s3; break;

case 6: //for palindrome s1>>s3; //invoke s1.operator>>(s3) cout<<"\n\nThe String \" "; cout<<s1; cout<<" \""; s1.str_palin(s3); break;

case 7: //for concatenation cout<<"\nThe first String is:"; cout<<s1; cout<<"\nThe Second String is:"; cout<<s2;

s1==s3; s3+s2; //invoke s1.operator+(s2) cout<<"\n\nThe Concatenated String is: "; cout<<s3; break;

case 8: //for substring cout<<"\nThe Original String is :"; cout<<s1;

cout<<"\n\nEnter the substring: "; cin>>str3; str_opn s4(str3); s1.substring(s4); break;

case 9: exit(0);

} }while(1);}

NAME--HARSHAL PAWALEROLL NO.—2053

#include<iostream.h>#include<dos.h>#include<graphics.h>#include<conio.h>#include<math.h>#include<snap.h>

class LINE_CIRCLE{ public :

void lineBresenham(int,int,int,int); //draws a line using Bresenham's Algorithm void ddaline(int,int,int,int); //draws a line using DDA Algorithm void Bresenham_circle(int,int,int); //draws a circle using Bresenham's Algorithm void mid_point(int,int,int); //draws a circle using Mid Point Method

Page 23: Cg Programs

};void LINE_CIRCLE::lineBresenham(int xa,int ya,int xb,int yb){ int x,y,dx,dy,p,xend; dx=abs(xb-xa); //difference of x coordinates dy=abs(yb-ya); //difference of y coodinates

p=(2*dy)-dx; //p is decision parameter which is initialized if(xa < xb) { x=xa; y=ya; xend=xb; } else { x=xb; y=yb; xend=xa; } putpixel(x,y,15); while(x<=xend) { x++; if(p<0) {

p=p+(2*dy); } else {

y++; p=p+(2*dy)-(2*dx);

} putpixel(x,y,15); }}void LINE_CIRCLE::ddaline(int xa,int ya,int xb,int yb){ int i,dx,dy,steps; float x,y,xinc,yinc; dx=abs(xb-xa); //difference of x coordinates dy=abs(yb-ya); //difference of y coodinates

if(dx>dy) steps=dx; else steps=dy;

xinc=dx/(float)steps; yinc=dy/(float)steps;

x=xa+0.5; //0.5 is added to round the values y=ya+0.5;

putpixel(x,y,15); //putpixel lits the pixel at (x,y) pt using color specified by no. 15 for(i=0;i<steps;i++) { x=x+xinc; y=y+yinc;

Page 24: Cg Programs

putpixel(x,y,15); }}void LINE_CIRCLE::Bresenham_circle(int xc,int yc,int r){ int x,y,d; x=0; y=r;

d=3-2*r; //initialization of decision value d

do {

delay(20); putpixel(xc+x,yc+y,15); putpixel(xc+y,yc+x,15); putpixel(xc+y,yc-x,15); putpixel(xc-x,yc+y,15); putpixel(xc-x,yc-y,15); putpixel(xc-y,yc-x,15); putpixel(xc-y,yc+x,15); putpixel(xc+x,yc-y,15);

if(d<=0) {

d=d+4*x+6; } else {

d=d+4*(x-y)+10;y=y-1;

} x=x+1;

}while(x<y);}

void LINE_CIRCLE::mid_point(int xc,int yc,int r){ int x,y,d; x=0; y=r;

d=1.25-r; //initilizes the decision parameter d do {

delay(30); putpixel(xc+x,yc+y,15); putpixel(xc+y,yc+x,15); putpixel(xc+y,yc-x,15); putpixel(xc-x,yc+y,15); putpixel(xc-x,yc-y,15); putpixel(xc-y,yc-x,15); putpixel(xc-y,yc+x,15); putpixel(xc+x,yc-y,15);

if(d<0) {

x=x+1; y=y;

Page 25: Cg Programs

d=d+2*x+2; } else {

x=x+1; y=y-1; d=d+2*(x-y)+1;

} }while(x<y);}

int main(){ clrscr(); int gd=DETECT,gm,x1,y1,x2,y2,rad; //autodetection for driver's int ch;

LINE_CIRCLE lc;

initgraph(&gd,&gm,"c:\\tc\\bgi"); //initialization of graphics mode

do { cleardevice(); gotoxy(2,2); cout<<">>>>>>>>>>>>>> LINE & CIRCLE DRAWING ALGORITHM's <<<<<<<<<<<<<<<"; cout<<"\n\n\t1. DDA Line\n\t2.BRESENHAM's Line\n\t3.BRESENHAM's Circle\n\t4.MID_POINT Circle\n\t5.EXIT"; cout<<"\n\nENTER THE CHOICE : "; cin>>ch; switch(ch) {

case 1: gotoxy(2,11); cout<<"Enter The Co-ordinates of Line : "; cin>>x1>>y1>>x2>>y2; lc.ddaline(x1,y1,x2,y2); capture("lc1.bmp"); getch(); break;

case 2: gotoxy(2,11); cout<<"Enter The Co-ordinates of Line : "; cin>>x1>>y1>>x2>>y2; lc.lineBresenham(x1,y1,x2,y2); capture("lc2.bmp"); getch(); break;

case 3: gotoxy(2,11); cout<<"Enter The Centre of Circle : "; cin>>x1>>y1; cout<<"\nEnter The Radius of Circle : "; cin>>rad; lc.Bresenham_circle(x1,y1,rad); capture("lc3.bmp"); getch(); break;

case 4:

Page 26: Cg Programs

gotoxy(2,11); cout<<"Enter The Centre of Circle : "; cin>>x1>>y1; cout<<"\nEnter The Radius of Circle : "; cin>>rad; lc.mid_point(x1,y1,rad); capture("lc4.bmp"); getch(); break;

case 5: gotoxy(2,11); cout<<"Exiting..............!!!"; getch();

} }while(ch!=5); capture("lc5.bmp"); return 0;}

NAME--HARSHAL PAWALEROLL NO.—2053

#include<iostream.h>#include<conio.h>#include<stdlib.h>template <class T>class matrix{ T mat[10][10]; //declare matrix of type T int row,col;

public: matrix(int r,int c) { //constructor initialises row & col row=r; col=c; }

void acceptmat(); //function to accept matrix

Page 27: Cg Programs

void displaymat(); //function to display matrix void addmat(matrix<T>,matrix<T>); //for addition of two matrices void subtractmat(matrix<T>,matrix<T>); //for subtraction void mulmat(matrix<T>,matrix<T>); //for multiplication

};template <class T>void matrix<T>::acceptmat(){ for(int i=0;i<row;i++) { for(int j=0;j<col;j++) {

cin>>mat[i][j]; } }}template <class T>void matrix<T>::displaymat(){ for(int i=0;i<row;i++) { for(int j=0;j<col;j++) {

cout<<mat[i][j]<<"\t"; } cout<<"\n\n"; }}template <class T>void matrix<T>::addmat(matrix<T> m1,matrix<T> m2){ for(int i=0;i<m1.row;i++) { for(int j=0;j<m1.col;j++) {

mat[i][j]=m1.mat[i][j]+m2.mat[i][j]; } }}template <class T>void matrix<T>::subtractmat(matrix<T> m1,matrix<T> m2){ for(int i=0;i<m1.row;i++) { for(int j=0;j<m1.col;j++) {

mat[i][j]=m1.mat[i][j]-m2.mat[i][j]; } }}template<class T>void matrix<T>::mulmat(matrix<T> m1,matrix<T> m2){ for(int i=0;i<m1.row;i++) {

for(int j=0;j<m2.col;j++) { mat[i][j]=0; for(int k=0;k<m1.col;k++)

Page 28: Cg Programs

mat[i][j]=mat[i][j]+m1.mat[i][k]*m2.mat[k][j]; }

}}int main(){ clrscr(); int r,c,ch; do { cout<<"\n\tMATRIX OPERATIONS"; cout<<"\n\n1.Using integer matrix\n\n2.Using float matrix\n\n3.Exit"; cout<<"\n\nEnter The Choice :"; cin>>ch; switch(ch) {

case 1:clrscr(); //case for integer matrix operation int ch1; do {

clrscr(); cout<<"\n\t\tMATRIX OPERATION ON INTEGER MATRIX"; cout<<"\n1.Accept\n2.Addition\n3.Subtraction\n4.Multiplication\n5.Stop"; cout<<"\nEnter the choice :"; cin>>ch1; switch(ch1) { case 1: clrscr(); //to accept matrices

cout<<"\nEnter Row & Col of 1st Matrix :"; cin>>r>>c; matrix <int> m1(r,c); //create integer matrix object 1 cout<<"\nEnter Row & Col of 2nd Matrix :"; cin>>r>>c; matrix <int> m2(r,c); //create integer matrix object 2 matrix <int> m3(r,c); //create integer matrix object 3 cout<<"\nEnter 1st Matrix :"; m1.acceptmat(); //invoke acceptmat() with m1 cout<<"\n\nEnter 2nd Matrix :"; m2.acceptmat(); //invoke acceptmat() with m2 break;

case 2: clrscr(); //to add matrices cout<<"\n1st Matrix is :\n\n" ; m1.displaymat(); cout<<"\n\n2nd Matrix is :\n\n"; m2.displaymat(); cout<<"\n\nAddition of Matrix is :\n\n" ; m3.addmat(m1,m2); //invoke addmat() with m3 & passes m1,m2 as parameter m3.displaymat(); //display addition getch(); clrscr(); break;

case 3: clrscr(); //to subtract matrices cout<<"\n1st Matrix is :\n\n" ; m1.displaymat(); cout<<"\n\n2nd Matrix is :\n\n"; m2.displaymat(); cout<<"\n\nSubtraction of Matrix is :\n\n" ; m3.subtractmat(m1,m2 m3.displaymat(); //display subtraction

Page 29: Cg Programs

getch(); clrscr(); break;

case 4: clrscr(); //to multiply matrices cout<<"\n1st Matrix is :\n\n" ; m1.displaymat(); cout<<"\n\n2nd Matrix is :\n\n"; m2.displaymat(); cout<<"\n\nMultiplication of Matrix is :\n\n" ; m3.mulmat(m1,m2); //invoke mulmat() with m3 & passes m1,m2 as,parameter m3.displaymat(); //display multiplication getch(); clrscr(); break;

} }while(ch1!=5); //end case for integer matrix clrscr(); break;case 2:clrscr(); //case for floating matrix operation int ch2; do {

clrscr(); cout<<"\n\t\tMATRIX OPERATION ON FLOAT MATRIX"; cout<<"\n1.Accept\n2.Addition\n3.Subtraction\n4.Multiplication\n5.Stop"; cout<<"\nEnter the choice :"; cin>>ch2; switch(ch2) { case 1: clrscr(); //to accept matrices

cout<<"\nEnter Row & Col of 1st Matrix :"; cin>>r>>c; matrix <float> m1(r,c); //create floating matrix object 1 cout<<"\nEnter Row & Col of 2nd Matrix :"; cin>>r>>c; matrix <float> m2(r,c); //create floating matrix object 2 matrix <float> m3(r,c); //create floating matrix object 3 cout<<"\nEnter 1st Matrix :"; m1.acceptmat(); cout<<"\n\nEnter 2nd Matrix :"; m2.acceptmat(); break;

case 2: clrscr(); //to add matrices cout<<"\n1st Matrix is :\n\n" ; m1.displaymat(); cout<<"\n\n2nd Matrix is :\n\n"; m2.displaymat(); cout<<"\n\nAddition of Matrix is :\n\n" ; m3.addmat(m1,m2); //invoke addmat() with m3 & passes m1,m2 as parameter m3.displaymat(); //display addition getch(); clrscr(); break;

case 3: clrscr(); //to subtract matrices cout<<"\n1st Matrix is :\n\n" ; m1.displaymat(); cout<<"\n\n2nd Matrix is :\n\n"; m2.displaymat(); cout<<"\n\nSubtraction of Matrix is :\n\n" ;

Page 30: Cg Programs

m3.subtractmat(m1,m2); m3.displaymat(); //display subtraction getch(); clrscr(); break;

case 4: clrscr(); //to multiply matrices cout<<"\n1st Matrix is :\n\n" ; m1.displaymat(); cout<<"\n\n2nd Matrix is :\n\n"; m2.displaymat(); cout<<"\n\nMultiplication of Matrix is :\n\n" ; m3.mulmat(m1,m2); //invoke mulmat() with m3 & passes m1,m2 as parameter m3.displaymat(); //display multiplication getch(); clrscr(); break;

} }while(ch2!=5); clrscr(); break; //end case for floating matrixcase 3: exit(0);

} }while(1);}

NAME--HARSHAL PAWALEROLL NO.—2053

#include<iostream.h>#include<conio.h>#include<stdlib.h>class personal_info1{ protected:

char name[20]; char date_of_birth[15]; char blood_group[5];

public: void getdata(); void display();

};

void personal_info1::getdata() //gets data of base class 1 members{ cout<<"\t\nName :"; cin>>name; cout<<"\nDate of birth :";

Page 31: Cg Programs

cin>>date_of_birth; cout<<"\nBlood group :"; cin>>blood_group;}

void personal_info1::display() //displays data of base class 1 members{ gotoxy(22,14); cout<<"NAME : "<<name; gotoxy(22,16); cout<<"D_O_B : "<<date_of_birth; gotoxy(43,16); cout<<"BLOOD_GROUP : "<<blood_group;}class personal_info2{ protected:

float weight,height;

public: void getdata(); void display();

};

void personal_info2::getdata() //gets data of base class 2 members{ cout<<"\nWeight:"; cin>>weight; cout<<"\nHeight:"; cin>>height;}

void personal_info2::display() //displays data of base class 2 members{

gotoxy(22,18); cout<<"HEIGHT : "<<height<<" cm"; gotoxy(43,18); cout<<"WEIGHT : "<<weight<<" kg";}class personal_info3{ protected:

char contact_address[20]; long int insurance_policy;

public: void getdata(); void display();

};

void personal_info3::getdata() //gets data of base class 3 members{ cout<<"\nContact address:"; cin>>contact_address; cout<<"\nInsurance policy:"; cin>>insurance_policy;}

Page 32: Cg Programs

void personal_info3::display() //displays data of base class 3 members{ gotoxy(22,20); cout<<"CONTACT_ADDRESS : "<<contact_address; gotoxy(22,24); cout<<"INSURANCE_POLICY_No.: "<<insurance_policy;}class personal_info:public personal_info1,public personal_info2,public personal_info3{ protected:

char telephone_no[15]; //derived class's members long driving_licence;

public: int id; static int cnt; //static member declared static int incre() { //increments the cnt for keeping track

return(++cnt); //no. of records created } static void decre() //decrement cnt in case of deletion of record {

cnt--; }

void getdata(); //gets data of derived class members void display(); //displays data of derived class members int deleteREC(int); //deletes record int modifyREC(int); // modify record int search(int); //searches for record

};

int personal_info::cnt; //define static member cnt

void personal_info::getdata(){ cout<<"\nEnter The Record: #"<<incre(); cout<<"\nId of Person :"; cin>>id; personal_info1::getdata(); //invoke getdata() of base class 1 personal_info2::getdata(); //invoke getdata() of base class 2 personal_info3::getdata(); //invoke getdata() of base class 3 cout<<"\nTelephone no:"; cin>>telephone_no; cout<<"\nDriving licence:"; cin>>driving_licence;}

void personal_info::display(){ gotoxy(18,10); cout<<"PERSONAL INFORMATION SYSTEM DATABASE RECORD"; gotoxy(22,12); cout<<"ID No.: "<<id;

personal_info1::display(); personal_info2::display(); personal_info3::display(); gotoxy(22,22); cout<<"TELEPHONE_No.: "<<telephone_no;

Page 33: Cg Programs

gotoxy(22,26); cout<<"DRIVING_LICENCE_No.: "<<driving_licence; gotoxy(35,35); cout<<"Press Any Key To Continue......."; getch(); clrscr();}

int personal_info::deleteREC(int x){ personal_info *p=this; for(int i=0;i<cnt;i++) {

if(p[i].id==x){ clrscr(); cout<<"\nThe Following Record is Deleted Successfully......."; p[i].display();

for(int m=i;m<cnt;m++) p[m]=p[m+1]; //swaps the data of object p[m+1] to object p[m] decre();

return 0;}

} if(i==p->cnt)

cout<<"\nThe record is not found in the database"; getch(); return 0;}

int personal_info::modifyREC(int x){ if(id==x) { personal_info1::getdata(); personal_info2::getdata(); cout<<"\n\nThe record is modified successfully...!!!"; getch(); return(1); } return(-1);}

int personal_info::search(int id){ personal_info *P=this; for(int i=0;i<P->cnt;i++) { if(P[i].id==id) {

clrscr(); cout<<"\nThe Record is found in database successfully.......!!!"; cout<<"\nThe Record is As Follows: "; P[i].display(); return 0;

} } if(i==P->cnt)

Page 34: Cg Programs

cout<<"\nThe Record is not Found in the database......"; getch(); return 0;}int main(){ clrscr(); personal_info p[20]; int ch,i=0; do {

clrscr();cout<<"\n\n\t\tPROGRAM_MENU FOR DATABASE";cout<<"\n\n\t1.Accept_Record\n\t2.Display_Record\n\t3.Insert_Entry\n\t4.Delete_Record";cout<<"\n\t5.Modify_Record\n\t6.Search_Record\n\t7.Exit";cout<<"\n\nEnter The Choice :";cin>>ch;

switch(ch){ case 1: //case for accepting record

do {

clrscr();p[i].getdata();i++;cout<<"\n\nDo You Want To Add More Record[y/n]? ";

}while(getche()=='y'||getche()=='Y'); break;

case 2: //for displaying record clrscr(); for(int j=0,m=1;j<p->cnt;j++) {

gotoxy(5,5); cout<<"Displaying Record......#"<<(m); m++; p[j].display(); getch();

} break;

case 3: //for inserting record clrscr(); p[p->cnt].getdata(); cout<<"\n\nRecord Inserted Successfully......!!!\n\n\tPress any key to continue..."; getch(); break;

case 4: //for deletion clrscr(); int id; cout<<"\nEnter the Id of Record to be deleted:"; cin>>id; p[0].deleteREC(id); getch(); break;

case 5: //for modification cout<<"\nEnter the Id person whose record is to be modify:"; cin>>id; int result; for(int i=0;i<p->cnt;i++)

Page 35: Cg Programs

{result=p[i].modifyREC(id);if(result==1) break;else continue;

} if(i==(p->cnt))

cout<<"\nThe record is not found"; getch();

break; case 6: //for searching record

cout<<"\nEnter The Id To Searched:";cin>>id; p[0].search(id); break;

case 7: // stop exit(0);

} }while(1);}

NAME--HARSHAL PAWALEROLL NO.—2053

#include<iostream.h>#include<conio.h>#include<process.h>#include<string.h>class personal_info_system{ private: char name[20],d_o_b[15],blood_group[5];

float height,weight; long int insu_policy_no; char telephone_no[15]; long int driv_license_no; char contact_add[30]; int id_no;

public: static int ID; personal_info_system() //default constructor { //to initialise strcpy(name,"zzz"); //the data members strcpy(d_o_b,"999"); strcpy(blood_group,"aa-"); height=9999; weight=8888; insu_policy_no=9009; strcpy(telephone_no,"00000"); driv_license_no= -9999; strcpy(contact_add,"zzz_zzz");

Page 36: Cg Programs

id_no=0; } void getdata(); //function to getdata from user inline void display_info(); //function to display the record static int incre_id(){ return(++ID); } //function to increment id of person static void decre_id(){ ID--; } //function to decrement the id of person in case of delete opn friend int search(personal_info_system*,int);//friend function for seraching of records int deleteREC(int); //function for deleting reords int modifyREC(int); //function for modifying records ~personal_info_system(); //destructor for deleting whole records

};int personal_info_system::ID; //declaration of static variablesvoid personal_info_system::getdata(){ int x=incre_id(); cout<<"\n\tEnter The Information Of Person :#"<<x; cout<<"\nEnter the id of person: ";cin>>id_no; cout<<"\nEnter the Name :";cin>>name; cout<<"\n\nEnter the Date Of Birth : ";cin>>d_o_b; cout<<"\n\nEnter the Blood Group :";cin>>blood_group; cout<<"\n\nEnter the Height :";cin>>height; cout<<"\n\nEnter the Weight :";cin>>weight; cout<<"\n\nEnter the Contact Address :";cin>>contact_add; cout<<"\n\nEnter the Telephone No.: ";cin>>telephone_no; cout<<"\n\nEnter the Insurance Policy No. :";cin>>insu_policy_no; cout<<"\n\nEnter the Driving License No.:";cin>>driv_license_no; cout<<"\n\nRecord accepted successfully...!!!"; getch(); clrscr();}void window(){ int i=15,j1=7,j2=30; for(;i<=65;i++) { gotoxy(i,j1); cout<<"*"; gotoxy(i,j2); cout<<"*"; gotoxy(i,11); cout<<"-"; } for(;j1<=30;j1++) { gotoxy(15,j1); cout<<"*"; gotoxy(65,j1); cout<<"*"; }}void personal_info_system::display_info(){

gotoxy(18,10); cout<<"PERSONAL INFORMATION SYSTEM DATABASE RECORD"; gotoxy(22,12); cout<<"ID No.: "<<id_no; gotoxy(22,14); cout<<"NAME : "<<name; gotoxy(22,16);

Page 37: Cg Programs

cout<<"D_O_B : "<<d_o_b; gotoxy(43,16); cout<<"BLOOD_GROUP : "<<blood_group; gotoxy(22,18); cout<<"HEIGHT : "<<height<<" cm"; gotoxy(43,18); cout<<"WEIGHT : "<<weight<<" kg"; gotoxy(22,20); cout<<"CONTACT_ADDRESS : "<<contact_add; gotoxy(22,22); cout<<"TELEPHONE_No.: "<<telephone_no; gotoxy(22,24); cout<<"INSURANCE_POLICY_No.: "<<insu_policy_no; gotoxy(22,26); cout<<"DRIVING_LICENCE_No.: "<<driv_license_no; window(); gotoxy(35,35); cout<<"Press Any Key To Continue......."; getch(); clrscr();

}int search(personal_info_system *P,int id){ for(int i=0;i<P->ID;i++) { if(P[i].id_no==id) {

clrscr(); cout<<"\nThe Record is found in database successfully.......!!!"; cout<<"\nThe Record is As Follows: "; P[i].display_info(); return 0;

} } if(i==P->ID) cout<<"\nThe Record is not Found in the database......"; return 0;}int personal_info_system::deleteREC(int x){ personal_info_system *p=this; for(int i=0;i<p->ID;i++) {

if(p[i].id_no==x){ clrscr(); cout<<"\nThe Following Record is Deleted Successfully......."; p[i].display_info();

for(int m=i;m<p->ID;m++){ p[m]=p[m+1];}decre_id();

return 0;}

} if(i==p->ID)

cout<<"\nThe record is not found in the database";return 0;

Page 38: Cg Programs

}int personal_info_system::modifyREC(int x){ if(id_no==x) { cout<<"\nEnter the Name :";cin>>name; cout<<"\n\nEnter the Date Of Birth : ";cin>>d_o_b; cout<<"\n\nEnter the Blood Group :";cin>>blood_group; cout<<"\n\nEnter the Height :";cin>>height; cout<<"\n\nEnter the Weight :";cin>>weight; cout<<"\n\nEnter the Contact Address :";cin>>contact_add; cout<<"\n\nEnter the Telephone No.: ";cin>>telephone_no; cout<<"\n\nThe record is modified successfully...!!!"; return(1); } return(-1);}personal_info_system::~personal_info_system(){ delete [] this;}int main(){ clrscr(); int ch,rec_no,i; personal_info_system *PIS; cout<<"\n\nEnter The No. Of Records You Want :";cin>>rec_no; PIS=new personal_info_system[rec_no]; clrscr(); do { cout<<"\n\t\t THE PERSONAL INFORMATION SYSTEM DATABASE"; cout<<"\n1.Create Database\n2.Display Database\n3.Search Record\n4.Delete Record\n5.Modify Record\n6.Exit"; cout<<"\nEnter the choice: ";cin>>ch; switch(ch) {

case 1: for(i=0;i<rec_no;i++) { PIS[i].getdata(); getch(); } break;

case 2: if(PIS->ID!=0) { rec_no=PIS->ID; } for(i=0;i<rec_no;i++) { clrscr(); PIS[i].display_info(); getch(); } break;

case 3: int id;cout<<"\nEnter The Id To Searched:";cin>>id;search(PIS,id);

Page 39: Cg Programs

break;

case 4: cout<<"\nEnter the Id of Record to be deleted:";cin>>id;PIS[0].deleteREC(id);break;

case 5: cout<<"\nEnter the Id person whose record is to be modify:";cin>>id;int result;for(int i=0;i<PIS->ID;i++){ result=PIS[i].modifyREC(id); if(result==1)

break; else

continue;}if(i==rec_no) cout<<"\nThe record is not found";break;

case 6: exit(0); } }while(1);}

NAME--HARSHAL PAWALEROLL NO.—2053

#include<math.h>#include<conio.h>#include<graphics.h>#include<process.h>#include<dos.h>#include<iostream.h>#include<snap.h> struct edge{ int x1,x2,y1,y2; int flag;};class scanline{ private: float dx,dy,m[10],x_int[10],inter_x[10];

struct edge ed[10] ; public: void display();

void get();

};void scanline::get(){ int x[10],y[10],ymax=0,ymin=480,temp1; int n,i,j,k,yy; float dx,dy,m[10],x_int[10],inter_x[10]; struct edge ed[10]; int temp; cout<<"\nEnter no of vertices:";

Page 40: Cg Programs

cin>>n; cout<<"Enter vertices:"; for(i=0;i<n;i++) { cout<<" \nx["<<i<<"]="; cin>>x[i]; cout<<"y["<<i<<"]="; cin>>y[i]; if(y[i]>ymax) ymax=y[i]; if(y[i]<ymin) ymin=y[i]; ed[i].x1=x[i]; ed[i].y1=y[i]; } for(i=0;i<n-1;i++) { ed[i].x2=ed[i+1].x1; ed[i].y2=ed[i+1].y1; ed[i].flag=0; } ed[i].x2=ed[0].x1; ed[i].y2=ed[0].y1; ed[i].flag=0; for(i=0;i<n;i++) { if(ed[i].y1<ed[i].y2) { temp1=ed[i].x1; ed[i].x1=ed[i].x2; ed[i].x2=temp1; temp1=ed[i].y1; ed[i].y1=ed[i].y2; ed[i].y2=temp1; }}for(i=0;i<n;i++) { line(ed[i].x1,ed[i].y1,ed[i].x2,ed[i].y2); }for(i=0;i<n;i++){

dx=ed[i].x2-ed[i].x1; dy= ed[i].y2-ed[i].y1;

if(dy==0){ m[i]=0; }

else{ m[i]=dx/dy;}

inter_x[i]=ed[i].x1;}yy=ymax; while(yy>ymin) { for(i=0;i<n;i++) {

Page 41: Cg Programs

if(yy>ed[i].y2&&yy<=ed[i].y1){

ed[i].flag=1;}

else{

ed[i].flag=0;}

} j=0;

for(i=0;i<n;i++){ if(ed[i].flag==1) { if(yy==ed[i].y1)

{x_int[j]=ed[i].x1;j++;

if(ed[i-1].y1==yy&&ed[i-1].y1<yy) { x_int[j]=ed[i].x1; j++; } if(ed[i+1].y1==yy&&ed[i+1].y1<yy) { x_int[j]=ed[i].x1; j++; }}else{ x_int[j]=inter_x[i]+(-m[i]); inter_x[i]=x_int[j]; j++;}

}}for(i=0;i<j;i++){ for(k=0;k<j-1;k++) { if(x_int[k]>x_int[k+1]) { temp1=x_int[k]; x_int[k]=x_int[k+1]; x_int[k+1]=temp1; } }}for(i=0;i<j;i=i+2){line(x_int[i],yy,x_int[i+1],yy);}yy--;delay(50);}}int main() {

Page 42: Cg Programs

scanline s; int gd=DETECT,gm,ch; struct edge ed[10],temp; initgraph(&gd,&gm,"c:\\tc\\bgi"); do { clrscr(); cleardevice(); cout<<"\n ---------MENU--------"; cout<<"\n 1.Create polygon\n 2.Exit"; cout<<"\n Enter your chioce:"; cin>>ch; switch(ch) {

case 1:s.get();capture("d1.bmp"); getch(); break;

} }while(ch<2); getch(); closegraph(); return 0; }

NAME--HARSHAL PAWALEROLL NO.—2053

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

class Shape //BASE CLASS FOR SHAPE{ public:

virtual void draw() //declaration & definition of virtual function { cout<<"\n\n\n\tTHIS IS THE PROGRAM FOR DRAWING DIFFERENT SHAPES\n\nYOUR NOW IN Shape "; } virtual void getdata() { cout<<"\n\n\The Background color & Line Style is Changed In Shape"; cout<<"\n\n\n\n\tPress Any Key To Continue......."; setbkcolor(BLUE); setlinestyle(SOLID_LINE,1,3); }

};

class Circle:public Shape //DERIVED CLASS FOR CIRCLE{ float x,y,rad;

public: void getdata() { gotoxy(2,9);

cout<<"Enter the center (x,y) of circle and radius :"; cin>>x>>y>>rad;

Page 43: Cg Programs

// cleardevice();}void draw(){ setcolor(RED); circle(x,y,rad); getch(); //cleardevice(); }

};

class Line:public Shape //DERIVED CLASS FOR LINE{ float x1,y1,x2,y2;

public: void getdata() { gotoxy(2,9);

cout<<"Enter starting & ending co-ordinates of line :"; cin>>x1>>y1>>x2>>y2; //cleardevice();

} void draw() {

setcolor(RED); line(x1,y1,x2,y2); getch(); // cleardevice();

}};

class Rectangle:public Shape //DERIVED CLASS FOR RECTANGLE{ float x1,y1,x2,y2;

public: void getdata() { gotoxy(2,9);

cout<<"\nEnter upper left & lower right co-ordinates of rectangle :"; cin>>x1>>y1>>x2>>y2; // cleardevice();

} void draw() {

setcolor(RED); rectangle(x1,y1,x2,y2); getch(); //cleardevice();

}};

int main(){

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\tc\\bgi"); //Initialiasation of graphics mode

Shape S; //Create object for shape

Page 44: Cg Programs

Circle C; //Create object for circle Rectangle R; //Create object for rectangle Line L; //Create object for line

Shape *s; //Pointer creation for base class

s=&S; s->draw(); s->getdata(); capture("c:\\v1.bmp"); getch(); cleardevice();

int ch; do {

cleardevice(); gotoxy(5,1); cout<<"\n\tHeirarchical Inheritance"; cout<<"\n1.Draw Circle\n2.Draw Rectangle\n3.Draw Line\n4.Exit"; cout<<"\n\nEnter the choice :"; cin>>ch; switch(ch) { case 1:

//cleardevice(); s=&C; s->getdata(); gotoxy(2,13); cout<<"\n\t\tThe Circle :"; s->draw(); capture("c:\\v2.bmp"); break;

case 2: //cleardevice(); s=&R; s->getdata(); gotoxy(2,13); cout<<"\n\t\tThe Rectangle :"; s->draw(); capture("c:\\v3.bmp"); break;

case 3: //cleardevice(); s=&L; s->getdata(); gotoxy(2,13); cout<<"\n\t\tThe Line :"; s->draw(); capture("c:\\v4.bmp"); break;

} }while(ch!=4); capture("c:\\v5.bmp"); closegraph(); restorecrtmode(); return 0;}

Page 45: Cg Programs