- /* This program shows how to pick up the scan codes from a keyboard */
- /* These define the scan codes(IBM) for the keys. All numbers are in decimal.*/
- #define PAGE_UP 73
- #define HOME 71
- #define END 79
- #define PAGE_DOWN 81
- #define UP_ARROW 72
- #define LEFT_ARROW 75
- #define DOWN_ARROW 80
- #define RIGHT_ARROW 77
- #define F1 59
- #define F2 60
- #define F3 61
- #define F4 62
- #define F5 63
- #define F6 64
- #define F7 65
- #define F8 66
- #define F9 67
- #define F10 68
- #include <iostream>
- #include <conio.h>
- using namespace std;
- int main()
- {
- char KeyStroke;
- cout << "Press Escape to quit." << endl;
- do
- {
- KeyStroke = getch();
- if (KeyStroke == 0)
- {
- KeyStroke = getch(); // Even though there are 2 getch() it reads one keystroke
- switch (KeyStroke)
- {
- case PAGE_UP:
- cout << "PAGE UP" << endl;
- break;
- case PAGE_DOWN:
- cout << "PAGE DOWN" << endl;
- break;
- case HOME:
- cout << "HOME" << endl;
- break;
- case END:
- cout << "END" << endl;
- break;
- case UP_ARROW:
- cout << "UP ARROW" << endl;
- break;
- case DOWN_ARROW:
- cout << "DOWN ARROW" << endl;
- break;
- case LEFT_ARROW:
- cout << "LEFT_ARROW" << endl;
- break;
- case RIGHT_ARROW:
- cout << "RIGHT_ARROW" << endl;
- break;
- case F1:
- cout << "F1" << endl;
- break;
- case F2:
- cout << "F2" << endl;
- break;
- case F3:
- cout << "F3" << endl;
- break;
- case F4:
- cout << "F4" << endl;
- break;
- case F5:
- cout << "F5" << endl;
- break;
- case F6:
- cout << "F6" << endl;
- break;
- case F7:
- cout << "F7" << endl;
- break;
- case F8:
- cout << "F8" << endl;
- break;
- case F9:
- cout << "F9" << endl;
- break;
- case F10:
- cout << "F10" << endl;
- break;
- default:
- cout << "Some other key." << endl;
- }
- }
- else
- cout << KeyStroke << endl;
- }
- while (KeyStroke != 27); // 27 = Escape key
- }
A blog on programming and software design interview problems and solutions. Also there are some application development related stuff.
Pages
Tuesday, December 6, 2011
C++ Program to pick the scan codes from a keyboard
Wednesday, March 30, 2011
The Twin Towers(Longest Common Subsequence)
using namespace std;
int main()
{
int A[102],B[102],C[102][102],m,n,count=1,i,j;
void fun(int[],int[],int[][102],int,int);
for(i=0;i<102;i++)
{
C[0][i]=0;
C[i][0]=0;
}
while(cin>>m)
{
if(m==0)
{
cin>>n;
if(n==0)
{
break;
}
}
else
{
cin>>n;
for(i=1;i<=m;i++)
{
cin>>A[i];
}
for(i=1;i<=n;i++)
{
cin>>B[i];
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(A[i]==B[j])
{
C[i][j]=C[i-1][j-1]+1;
}
else
{
if(C[i][j-1]>C[i-1][j])
{
C[i][j]=C[i][j-1];
}
else
{
C[i][j]=C[i-1][j];
}
}
}
}
cout<<"Twin Towers #"<<count<<endl;
cout<<"Number of Tiles : "<<C[m][n]<<endl;
count++;
fun(A,B,C,m,n);
cout<<endl;
}
}
return 0;
}
void fun(int A[],int B[],int C[][102],int i,int j)
{
if(i==0 || j==0)
{
return;
}
else
{
if(C[i-1][j-1]==C[i][j]-1)
{
fun(A,B,C,i-1,j-1);
cout<<A[i]<<" ";
}
else if(C[i-1][j]==C[i][j])
{
fun(A,B,C,i-1,j);
}
else
{
fun(A,B,C,i,j-1);
}
}
}
Sunday, January 9, 2011
Point in Triangle
You are given the coordinates of the vertices of a triangle and also the coordinates of a point. You have to determine whether the point lies in the triangle or the it lies outside the triangle.
Logic Used
Distance of the point from a vertex is compared with the distances of the vertex from the other two vertices. If the distance of the point is found to be greater than any of the distances from other vertices, then the point lies outside the triangle. This is repeated for all the three vertices of the triangle.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float dist(float,float,float,float);//Function to find the distance between two points.
float x1,x2,x3,y1,y2,y3,px,py,distance,length1,length2;
int out=0;
while(cin>>x1)
{
cin>>y1;
cin>>x2>>y2;
cin>>x3>>y3;
cin>>px>>py;
out=0;
length1=dist(x1,y1,x2,y2);
length2=dist(x1,y1,x3,y3);
distance=dist(x1,y1,px,py);
if(distance>length1 || distance>length2)
{
out=1;
}
length1=dist(x2,y2,x1,y1);
length2=dist(x2,y2,x3,y3);
distance=dist(x2,y2,px,py);
if(distance>length1 || distance>length2)
{
out=1;
}
length1=dist(x3,y3,x1,y1);
length2=dist(x3,y3,x2,y2);
distance=dist(x3,y3,px,py);
if(distance>length1 || distance>length2)
{
out=1;
}
if(out==0)
{cout<<"Inside"<<endl;}
else
{cout<<"Outside"<<endl;}
}
return 0;
}
float dist(float x1,float y1,float x2,float y2)
{
return sqrt(pow((x2-x1),2)+pow((y2-y1),2));
}
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float dist(float,float,float,float);//Function to find the distance between two points.
float x1,x2,x3,y1,y2,y3,px,py,distance,length1,length2;
int out=0;
while(cin>>x1)
{
cin>>y1;
cin>>x2>>y2;
cin>>x3>>y3;
cin>>px>>py;
out=0;
length1=dist(x1,y1,x2,y2);
length2=dist(x1,y1,x3,y3);
distance=dist(x1,y1,px,py);
if(distance>length1 || distance>length2)
{
out=1;
}
length1=dist(x2,y2,x1,y1);
length2=dist(x2,y2,x3,y3);
distance=dist(x2,y2,px,py);
if(distance>length1 || distance>length2)
{
out=1;
}
length1=dist(x3,y3,x1,y1);
length2=dist(x3,y3,x2,y2);
distance=dist(x3,y3,px,py);
if(distance>length1 || distance>length2)
{
out=1;
}
if(out==0)
{cout<<"Inside"<<endl;}
else
{cout<<"Outside"<<endl;}
}
return 0;
}
float dist(float x1,float y1,float x2,float y2)
{
return sqrt(pow((x2-x1),2)+pow((y2-y1),2));
}