Skip to main content

Posts

Showing posts from May, 2010

Exception Handling

/* Problem Statement: Create a class named Television that has data members to hold the model number and the screen size in inches,and the price.Member functions include overloaded insertion and extraction operators. If more than four digits are entered for the model,if the screen size is smaller than 12 or greater than 70 inches, or if the price is negative or over $5000 then throw an integer. Write a main() function that instantiates a television object,allows user to enter data and displays the data members.If an exception is caught ,replace all the data member values with zero values. */ #include #include class television { int model; float size; float price; public: television(); friend void operator << (ostream &o,television &t); friend void operator >> (istream &i,television &t); }; television::television() { model=0; size=0; price=0; } void operator <<

Graphics Editor

#include #include #include void main() { int gm,gd=DETECT; int ch,draw,paint,text;//choices int x1,y1,x2,y2,r,sa,ea,xr,yr,poly[10],v,i,midx,midy,res[10],year[10]; char str[10]; initgraph(&gd,&gm,"c:\\tc\\bgi"); while(1) { x: cleardevice(); printf("\nGraphics Editor"); printf("\n1.DRAW\n2.TEXT\n3.PAINT\n4.EXIT\nEnter choice:"); scanf("%d",&ch); switch(ch) { case 1: while(1) { cleardevice(); printf("\nDrawing Different Polygons"); printf("\n1.Pixel\n2.Line\n3.Circle\n4.Ellipse\n5.Polygon"); printf("\n6.Line Styles\n7.Bar Graphs\n8.Pai Charts\n9.Histogram\n0.Main Menu\nEnter Choice: "); scanf("%d",&draw); switch(draw) { case 1://pixel printf("\nenter x1,y1: "); scanf("%d%d",&x1,&y1); putpixel(x1,y1,15)

Matrix - template

/* Problem Statement: Write a program in C++ using function template to read two matrices of different data types such as integers and floating point values and perform simple arithmetic operations on these matrices separately and display it. */ #include #include #include template //Template Definition class matrix { int m,n; T a[10][10]; public: matrix() //Default Constructor { m=n=0; } matrix(int x,int y,T c[10][10]); //Parameterised Constructor void insert(); void display(); matrix operator +(matrix ); matrix operator -(matrix ); matrix operator *(matrix ); }; //------------------- Constructor ---------- template matrix ::matrix(int x,int y,int z[10][10]) { int i,j; m=x; n=y; for(i=0;i for(j=0;j a[i][j]=z[i][j]; } //------------------ Insertion -------------- template void matrix ::ins

weather report

/* Problem Statement: Create a class named weather report that holds a daily weather report with data members day_of_month,hightemp,lowtemp,amount_rain and amount_snow. The constructor initializes the fields with default values: 99 for day_of_month, 999 for hightemp,-999 for low temp and 0 for amount_rain and amount_snow. Include a function that prompts the user and sets values for each field so that you can override the default values. Write a program that creates a monthly report. */ #include #include class weather { public: int day_of_month[50]; int high_temp[50]; int low_temp[50]; int amount_rain[50]; int amount_snow[50]; weather() //defination { day_of_month[0]=99; high_temp[0]=999; low_temp[0]=-999; amount_rain[0]=amount_snow[0]=0; } void get_data(int n); void put_data(int n); void average(int n); }; void weather:: average(int n) { int min ,max,total_rainfall,total_snowfall; total_rainfall=0; tot

Virtual

/* Problem Statement: Design a base class consisting of the data members such as name of the student,roll number and subject.The derived class consists of the data members subject code ,internal assessment and university examination marks.Construct a virtual base class for the item name of the student and roll number. The program should have these facilities:- i) Build a master table ii) List a table iii) Insert a new entry iv) Delete old entry v) Edit an entry vi) Search for a record */ Include: iostream.h conio.h fstream.h iomanip.h class base1 { public: char name[10],sub[10]; int rno; }; class derived: public base1 { protected: int subco,umark,iass; public: void getdata(derived ); void putdata(derived ); void insert(derived); derived edit(derived); void delet(derived); int search(int,derived); }; void derived::getdata(derived d) { fstream file; file.open(

Complex Nos

Include: iostream.h conio.h stdlib.h class complex { float x,y; public: complex() { } complex(float real,float imaginary) { x=real; y=imaginary; } complex operator+(complex c); complex operator-(complex c); complex operator*(complex c); complex operator/(complex c); void display() { cout< } }; // Overloading Operator " - " complex complex::operator-(complex c) { complex temp; temp.x=x-c.x; temp.y=y-c.y; return temp; } // Overloading Operator " * " complex complex::operator*(complex c) { complex temp; temp.x=x*c.x; temp.y=y*c.y; return temp; } // Overloading Operator " + " complex complex::operator+(complex c) { complex tem

Operator Overloading - String

Include: iostream.h conio.h process.h class string { public: char *str; void operator=(string); void operator==(string); void operator+(string); void operator<=(string); void operator>=(string); friend ostream* operator<<(ostream*,string *s1); friend istream& operator>>(istream&,string &s1); }; // String Comparison void string::operator=(string s2) { int i,j,flag=0; for(i=0;s2.str[i]!='\0'||str[i]!='\0';i++) { if(s2.str[i]!=str[i]) { flag=1; } } if(flag==0) cout<<"\nStrings are equal"; else cout<<"\nString are not equal"; } // String Copy void string::operator==(string s2) { int i,j,c=0; for(i=0;s2.str[i]!='\0';i++) {

Scan Line Algorithm

Include: stdio.h conio.h graphics.h // Store Edges struct edge { int x1; int y1; int x2; int y2; int flag; }; void main() { int gd = DETECT, gm, n, i, j, k; struct edge ed[10],temped; float dx,dy,m[10],x_int[10],inter_x[10]; int x[10],y[10],ymax = 0, ymin = 480, yy,temp; initgraph (&gd,&gm,"bgi"); printf("\n Enter Number Of Vertices : "); scanf("%d", &n); printf("\n Enter Vertices: \n"); for(i = 0; i < n; i++) { printf(" x[%d] : ", i); scanf("%d", &x[i]); printf(" y[%d] : ", i); scanf("%d", &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]; } // Store the edge info. for(i=0;i { ed[i].x2 = e

Line

Include: conio.h dos.h stdio.h process.h graphics.h math.h void DDA_Line(); void Bresenham_Line(); void main() { int x,y,x1,y1,x2,y2,i,dx,dy,len,e,r,d,gd,gm,ch; char ans; clrscr(); do { printf("\n\n-------------Menu-----------"); printf("\n 1. DDA Line"); printf("\n 2. Bresenham's Line"); printf("\n 3. Exit"); printf("\n\n Enter your choice : "); scanf("%d",&ch); switch(ch) { case 1: DDA_Line(); break; case 2: Bresenham_Line(); break; case 3: exit(1); } printf("\n\n Do You Want to Continue ? (Y/N) : "); ans=getche(); }while(ans=='Y'||ans=='y'); } //---------DDA Line Algorithm----------------- void DDA_Line() { int gd,gm,x1,x2,y1,y2,len,x,y,i,dx,dy; printf("\n Enter Values as (X1,Y1) & (X2,Y2) : "); scanf("%d %d %d %d",&x1,&y1,&x2,&y2); detectgraph(&gd,&gm); initgraph(&am

2D Transformations

Include: stdio.h math.h conio.h graphics.h float input[20][3]; float resM[20][3]; float scaleM[3][3]; float R[3][3],T[3][3]; int edges; // Plotting Center Of Screen void plot(float mat[20][3]) { int i; setcolor(10); line(0,240,639,240); line(320,0,320,479); outtextxy(295,243,"0,0"); setcolor(WHITE); for(i=0;i line(320+mat[i][0],240-mat[i][1],320+mat[i+1][0],240-mat[i+1][1]); line(320+mat[0][0],240-mat[0][1],320+mat[i][0],240-mat[i][1]); } // Accepting Co-ordinates for Polygon void accept() { int i,j; printf("\n Enter no of edges in fig:"); scanf("%d",&edges); printf("\n Enter co-ordinates of matrix:"); for(i=0;i { for(j=0;j<2;j++) { printf("\n A[%d][%d]:",i,j); scanf("%f",&input[i][j]); } } for(i=0;i input[i][2]=1; plot(input); getch(); }

C Code For Bresenham's Circle Drawing Algorithm

Bresenham’s Algorithm: C Code For Bresenham's Circle Drawing Algorithm Bresenham 's circle algorithm is derived from the midpoint circle algorithm We cannot display a continuous arc on the raster display. Instead, we have to choose the nearest pixel position to complete the arc. From the following illustration, you can see that we have put the pixel at (X, Y) location and now need to decide where to put the next pixel − at N (X+1, Y) or at S (X+1, Y-1) This can be decided by the decision parameter  d . If d <= 0, then N(X+1, Y) is to be chosen as next pixel. If d > 0, then S(X+1, Y-1) is to be chosen as the next pixel. Algorithm Step 1  − Get the coordinates of the center of the circle and radius, and store them in x, y, and R respectively. Set P=0 and Q=R. Step 2  − Set decision parameter D = 3 – 2R. Step 3  − Repeat through step-8 while P ≤ Q. Step 4  − Call Draw Circle (X, Y, P, Q). Step 5  − Increment the value of P. St