WordPress.com



#include<stdafx.h>#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<glut.h>void *currentfont;int size,a[10],ch=1,key,loc=0,location;void binary_search();void input();void bitmap_output(int , int , char*, void* );void mymenu(int);void init();void keyboard(unsigned char,int,int);#define MAX 10int k=0,d;typedef struct circle{GLfloat x;//x axis of centerGLfloat y;//y axis of centerGLfloat r;// radius}circle;circle c[MAX];//array of circles to store the center and radius of each circleint b[MAX];// int array for sorting algoint initial[MAX]; //int array to restore random values after sortingGLfloat initial_x1,initial_x2;//to set the destiniation for swappingint global_i = 0, global_j = 0;//i and j values for bubble-sortint swapping = 0;//flag to check if circle are being swappedint sorting = 0;//start sorting only after user inputvoid draw_tab(){int i,h,kt;char s[20],*s1;for(i=0;i<size;i++){h=50+i*50;kt=100+i*50;glBegin(GL_LINE_LOOP);glColor3f(0,0,0);glVertex2i(h+50,280);glVertex2i(kt+50,280);glVertex2i(kt+50,330);glVertex2i(h+50,330);glEnd();s1=itoa(a[i],s,10);glColor3f(0,0,1);bitmap_output((h+70),305,s1,GLUT_BITMAP_HELVETICA_18);glColor3f(0,1,0);}glFlush();}void initialise(){global_i = global_j = swapping = 0; //reset all globalsfor (int i=0;i<MAX;i++){b[i] = initial[i];//if a[] is sorted restore from initial[] c[i].r = b[i]*4.0;//4 because to fit 10 circles in screenc[i].y = 300.0;//vertical center of windowif (i == 0)c[i].x = 45.0;// first circle starts from 45 offsetelsec[i].x = c[i-1].x + 90.0;//(c[i-1].r+c[i].r+10.0); //distance between circles = sum of 2 max readiiprintf("%f - %f - %f\n",c[i].x,c[i].y,c[i].r); //for testing purpose don worry}}//func to display text on screen char by charvoid bitmap_output(int x, int y, char *string, void *font){ int len, i; glRasterPos2f(x, y); len = (int) strlen(string); for (i = 0; i < len; i++) { glutBitmapCharacter(font, string[i]); }}//function to integer to stringvoid int_str(int rad,char r[]){switch(rad){case 1 : strcpy(r, "1"); break;case 2 : strcpy(r, "2"); break;case 3 : strcpy(r, "3"); break;case 4 : strcpy(r, "4"); break;case 5 : strcpy(r, "5"); break;case 6 : strcpy(r, "6"); break;case 7 : strcpy(r, "7"); break;case 8 : strcpy(r, "8"); break;case 9 : strcpy(r, "9"); break;case 10 : strcpy(r, "10"); break;}}//draw circle by drawing consecutive triangle fansvoid circle_draw(circle c){float i;glBegin(GL_TRIANGLE_FAN);glVertex2f(c.x, c.y);//center of circlefor (i=0;i<360;i+=1)glVertex2f(c.x + sin(i) * c.r, c.y + cos(i) * c.r);glEnd();//display the value of circle belowint x = c.x-2;int y = c.y-(c.r+10);int rad = c.r /4;char r[3] = "";int_str(rad,r);glColor3f(0.7,0.0,0.3);bitmap_output(x, y, r, GLUT_BITMAP_TIMES_ROMAN_10);}// swaps circles cc1 and cc2 by changing their centersvoid swap_circles(circle *cc1,circle *cc2){if (swapping == 0)//if circles are not being swapped set destination for each circles{initial_x1 = cc1->x; //center of circle in leftinitial_x2 = cc2->x; //center of circle in rightswapping = 1;//means cicle are being swappedprintf("%f - %f\n",cc1->r,cc2->r);}if (initial_x1 <= cc2->x)//decrease the center of circle in right till its > center of left circlecc2->x -= 3.0;//speed of animationif (initial_x2 >= cc1->x)//increase the center of circle in left till its < center of right circlecc1->x += 3.0;printf("one %f - %f\n",initial_x1,cc2->x);printf("two %f - %f\n",initial_x2,cc1->x);// if difference between destination and center < 0.3 then cicles are swappedif (abs(initial_x1-cc2->x) < 0.3 && abs(initial_x2-cc1->x) < 0.3) //set this to speed of animation{swapping = 0;int temp = cc1->x;cc1->x = cc2->x;cc2->x = temp;temp = cc1->y;cc1->y = cc2->y;cc2->y = temp;temp = cc1->r;cc1->r = cc2->r;cc2->r = temp;}}void sort()//bubble sort algo{if (!swapping) //if not in process of swappin 2 circles only then get 2 new circles to swap{while (global_i < 10){global_j = global_i;while (global_j < 9){if (b[global_j] > b[global_j+1]){printf("%d %d\n",global_j,b[global_j]);int temp = b[global_j];b[global_j] = b[global_j+1];b[global_j+1] = temp;goto SWAP;}global_j ++;}global_i ++;}}SWAP:swap_circles(&c[global_j],&c[global_j+1]);}void display_text(){bitmap_output(200, 565, "BUBBLE SORT IMPLEMENTATION",GLUT_BITMAP_TIMES_ROMAN_24);//title larger fontglBegin(GL_LINE_LOOP); //to underline the titleglVertex2f(180, 560);glVertex2f(600, 560);glEnd();//other text small fontbitmap_output(10, 525, "This program sorts a random set of numbers in ascending order displaying them graphically as ",GLUT_BITMAP_9_BY_15);bitmap_output(10, 505, "circles with varying radii(range 1-10).",GLUT_BITMAP_9_BY_15);if (sorting == 0)//if not sorting display menu{bitmap_output(10, 455, "MENU",GLUT_BITMAP_9_BY_15);bitmap_output(10, 435, "Press s to SORT",GLUT_BITMAP_9_BY_15);bitmap_output(10, 415, "Press r to RANDOMISE",GLUT_BITMAP_9_BY_15);bitmap_output(10, 395, "Esc to QUIT",GLUT_BITMAP_9_BY_15);}else if (sorting == 1){glColor3f(0.5,0.5,0.5);bitmap_output(10, 455, "Sorting in progress...",GLUT_BITMAP_9_BY_15);bitmap_output(10, 435, "Please do not quit",GLUT_BITMAP_9_BY_15);glColor3f(0.0,0.0,0.0);}}void front(){glColor3f(0.0,0.0,1.0);bitmap_output(330, 565, "BUBBLE SORT IMPLEMENTATION",GLUT_BITMAP_TIMES_ROMAN_24);glBegin(GL_LINE_LOOP); glVertex2f(328, 560);glVertex2f(720, 560);glEnd();glColor3f(1.0,0.0,5.0);bitmap_output(330, 300, "PRESS ENTER TO CONTINUE.........",GLUT_BITMAP_HELVETICA_18);}void display(void){glClear(GL_COLOR_BUFFER_BIT);if(d==2){glColor3f(1.0,0.5,0.5);bitmap_output(240,550,"BINARY SEARCH IMPLEMENTATION",GLUT_BITMAP_TIMES_ROMAN_24);bitmap_output(240,548,"__________________________________________",GLUT_BITMAP_HELVETICA_18);bitmap_output(220,350,"INPUT ARRAY a[ ]",GLUT_BITMAP_HELVETICA_18);glColor3f(1.0,0.5,0.5);bitmap_output(100, 200, "low-------->",GLUT_BITMAP_TIMES_ROMAN_24);bitmap_output(100, 180, "high------->",GLUT_BITMAP_TIMES_ROMAN_24);draw_tab();binary_search();goto menu;//glutSwapBuffers();}else if(d==1){glColor3f(1.0,0.5,0.5);if (k==0)front();else{display_text();glPointSize(2.0);for (int i=0;i<MAX;i++){glColor3f(0.5,1.0,0.5);circle_draw(c[i]);}if (global_j+1 < MAX && sorting == 1) // call sort only on key presssort();elsesorting = 0;goto menu;}menu:glFlush();//glutSwapBuffers();glutCreateMenu(mymenu);glutAddMenuEntry("Back to Prompt",1);glutAddMenuEntry("Quit",2);glutAttachMenu(GLUT_RIGHT_BUTTON);}}void mymenu(int id){switch(id){case 1:int l,i;printf("\t\t\t\tSELECT THE FUNCTIONALITY:\n\t\t\t\t1.SORTING\t\n\t\t\t\t2.SEARCHING\t(ascending order)\n\t\t\t\t");scanf("%d",&d);if(d==1){printf("\n\t\t\tYOU WOULD LIKE THE ELEMENTS TO BE....... \n\t\t\t\t1.ENTERED MANUALLY \t(NOTE:range 1-10) \n\t\t\t\t2.ENTER RANDOMLY");printf("\n\t\t\t\tplease enter.......");scanf("%d",&l);if(l==1){for(int i=0;i<10;i++){scanf("%d",&initial[i]);if(initial[i]>10){printf("\t(out of range-elements btw 1-10 only)\n");--i;continue;}}}else if (l==2){ for(int i=0;i<10;i++)initial[i]=1+(rand()%10);}else {printf("invalid choice");}}else if (d==2){printf("Enter the size of the array\n");scanf("%d",&size);printf("Enter the elements\n");for(i=0;i<size;i++){scanf("%d",&a[i]);}printf("Enter the element to be searched for\n");scanf("%d",&key);}glutInitDisplayMode(GLUT_RGB);glutInitWindowPosition(50,50);glutInitWindowSize(900,600);if(d==1)glutCreateWindow("BUBBLE SORT IMPLEMENTATION");elseglutCreateWindow("BINARY SEARCH IMPLEMENTATION");glutDisplayFunc(display);init();initialise();if(d==1){glutIdleFunc(display);glutKeyboardFunc(keyboard);}glutMainLoop();break;case 2:exit(0);}}void keyboard (unsigned char key, int x, int y) {if(key==13)k=1;if (k==1){switch (key){case 27 : exit (0); //27 is the ascii code for the ESC keycase 's' : sorting = 1; break;case 'r' : input();break;}}}void reshape(int w, int h){glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION); glLoadIdentity();if(w<=h) glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w, 2.0* (GLfloat) h / (GLfloat) w, -10.0, 10.0);else glOrtho(-2.0 * (GLfloat) w/ (GLfloat) h, 2.0* (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);glMatrixMode(GL_MODELVIEW);}void init(void){glClearColor(1.0,1.0,1.0,0.0);glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0,900.0,0.0,600.0);}void input(){for(int i=0;i<10;i++){initial[i]=1+(rand()%10);initialise();}}void binary_search(void){ int mid,low=0,high=size-1,x=260;char s3[3],s4[3],pos[3]; char *l,*h,*p;h=itoa(high,s3,10);bitmap_output(x, 180, h,GLUT_BITMAP_HELVETICA_18);l=itoa(low,s4,10);bitmap_output(x, 200, l,GLUT_BITMAP_HELVETICA_18);glFlush();{int u,v;for(u=0;u<50000;u++)for(v=0;v<5000;v++);} x=x+25;while(low<=high) { mid=(low+high)/2; if(key==a[mid]) { loc=1; location=mid; }if(key<a[mid]){ high=mid-1;h=itoa(high,s3,10);bitmap_output(x, 180, h,GLUT_BITMAP_HELVETICA_18);l=itoa(low,s4,10); bitmap_output(x, 200, l,GLUT_BITMAP_HELVETICA_18); glFlush();{int u,v;for(u=0;u<50000;u++)for(v=0;v<5000;v++);} x=x+25; }else {low=mid +1;l=itoa(low,s4,10);bitmap_output(x, 200, l,GLUT_BITMAP_HELVETICA_18);h=itoa(high,s3,10);bitmap_output(x, 180, h,GLUT_BITMAP_HELVETICA_18);glFlush();{int u,v;for(u=0;u<50000;u++)for(v=0;v<5000;v++);}x=x+25;} }if(loc==0)bitmap_output(330, 150, "NOT FOUND!!!!!!!",GLUT_BITMAP_HELVETICA_18);else{bitmap_output(330, 150, "FOUND AT--->",GLUT_BITMAP_HELVETICA_18); p=itoa((location+1),pos,10); bitmap_output(470,150,p,GLUT_BITMAP_HELVETICA_18);}}void main(int argc, char** argv){int l,i;printf("\t\t\t\tSELECT THE FUNCTIONALITY:\n\t\t\t\t1.SORTING\t\n\t\t\t\t2.SEARCHING\t(ascending order)\n\t\t\t\t");scanf("%d",&d);if(d==1){printf("\n\t\t\tYOU WOULD LIKE THE ELEMENTS TO BE....... \n\t\t\t\t1.ENTERED MANUALLY \t(NOTE:range 1-10) \n\t\t\t\t2.ENTER RANDOMLY");printf("\n\t\t\t\tplease enter.......");scanf("%d",&l);if(l==1){for(int i=0;i<10;i++){scanf("%d",&initial[i]);if(initial[i]>10){printf("\t(out of range-elements btw 1-10 only)\n");--i;continue;}}}else if (l==2){ for(int i=0;i<10;i++)initial[i]=1+(rand()%10);}else {printf("invalid choice");}}else if (d==2){printf("Enter the size of the array\n");scanf("%d",&size);printf("Enter the elements\n");for(i=0;i<size;i++){scanf("%d",&a[i]);}printf("Enter the element to be searched for\n");scanf("%d",&key);}glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB);glutInitWindowPosition(50,50);glutInitWindowSize(900,600);if(d==1)glutCreateWindow("BUBBLE SORT IMPLEMENTATION");elseglutCreateWindow("BINARY SEARCH IMPLEMENTATION");glutDisplayFunc(display);init();initialise();if(d==1){glutIdleFunc(display);glutKeyboardFunc(keyboard);}glutMainLoop();} ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download