Category: CSE
Computer Science and Engineering
Tree Search
Tree Traversal, In-order, Pre-order, Post-order
OOP Lab: File operation
/* Write to a file */
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream of(“test”,ios::app);
char name[30];
cout<<“Enter name:”<<endl;
cin>>name;
of<<name<<“\n”;
of.close();
return 0;
}
/* Read file */
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream ifs(“test”);
char name[30];
cout<<“File data:” <<endl;
ifs>>name;
while(ifs.eof() == 0){
cout << name <<” “;
ifs>>name;
}
ifs.close();
return 0;
}
/* Read as line */
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream ifs(“test”);
int SIZE = 80;
char name[SIZE];
cout<<“File data:” <<endl;
do{
ifs.getline(name,SIZE);
cout<< name <<endl;
}while(ifs.eof() == 0);
ifs.close();
return 0;
}
/* cursor movement */
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream of(“test”,ios::app);
char name[30];
cout<<“Enter name:”<<endl;
cin>>name;
of.seekp(-5,ios::cur);
of<<name<<“\n”;
of.close();
return 0;
}
Draw translated and rotated hierarchical models
/*
* robot.c
* This program shows how to composite modeling transformations
* to draw translated and rotated hierarchical models.
* Interaction: pressing the s and e keys (shoulder and elbow)
* alters the rotation of the robot arm.
*/
#include <GL/glut.h>
#include <stdlib.h>
static int shoulder = 0, elbow = 0;
void init()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
//glShadeModel (GL_FLAT);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef (-1.0, 0.0, 0.0);
glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix();
glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);
glTranslatef (1.0, 0.0, 0.0);
glPushMatrix();
glScalef (2.0, 0.4, 1.0);
glutWireCube (1.0);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0);
}
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case ‘s’:
shoulder = (shoulder + 5) % 360;
glutPostRedisplay();
break;
case ‘S’:
shoulder = (shoulder – 5) % 360;
glutPostRedisplay();
break;
case ‘e’:
elbow = (elbow + 5) % 360;
glutPostRedisplay();
break;
case ‘E’:
elbow = (elbow – 5) % 360;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
Demonstrates modeling transformations
/*
* model.c
* This program demonstrates modeling transformations
*/
#include <GL/glut.h>
#include <stdlib.h>
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
//glShadeModel (GL_FLAT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void draw_triangle(void)
{
glBegin (GL_LINE_LOOP);
glVertex2f(0.0, 25.0);
glVertex2f(25.0, -25.0);
glVertex2f(-25.0, -25.0);
glEnd();
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity ();
glColor3f (1.0, 1.0, 1.0);
draw_triangle ();
glEnable (GL_LINE_STIPPLE);
glLineStipple (1, 0xF0F0);
glLoadIdentity ();
glTranslatef (-20.0, 0.0, 0.0);
draw_triangle ();
glLineStipple (1, 0xF00F);
glLoadIdentity ();
glScalef (1.5, 0.5, 1.0);
draw_triangle ();
glLineStipple (1, 0x8888);
glLoadIdentity ();
glRotatef (90.0, 0.0, 0.0, 1.0);
draw_triangle ();
glDisable (GL_LINE_STIPPLE);
glFlush ();
}
/*void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
//glutReshapeFunc(reshape);
// glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}
Recent Comments