Tuesday, December 6, 2011

C++ Program to pick the scan codes from a keyboard




  1. /* This program shows how to pick up the scan codes from a keyboard */




  2. /* These define the scan codes(IBM) for the keys. All numbers are in decimal.*/


  3. #define PAGE_UP 73


  4. #define HOME 71


  5. #define END 79


  6. #define PAGE_DOWN 81


  7. #define UP_ARROW 72


  8. #define LEFT_ARROW 75


  9. #define DOWN_ARROW 80


  10. #define RIGHT_ARROW 77


  11. #define F1 59


  12. #define F2 60


  13. #define F3 61


  14. #define F4 62


  15. #define F5 63


  16. #define F6 64


  17. #define F7 65


  18. #define F8 66


  19. #define F9 67


  20. #define F10 68


  21. #include <iostream>


  22. #include <conio.h>




  23. using namespace std;




  24. int main()


  25. {


  26. char KeyStroke;




  27. cout << "Press Escape to quit." << endl;




  28. do


  29. {


  30. KeyStroke = getch();




  31. if (KeyStroke == 0)


  32. {


  33. KeyStroke = getch(); // Even though there are 2 getch() it reads one keystroke


  34. switch (KeyStroke)


  35. {


  36. case PAGE_UP:


  37. cout << "PAGE UP" << endl;


  38. break;


  39. case PAGE_DOWN:


  40. cout << "PAGE DOWN" << endl;


  41. break;


  42. case HOME:


  43. cout << "HOME" << endl;


  44. break;


  45. case END:


  46. cout << "END" << endl;


  47. break;


  48. case UP_ARROW:


  49. cout << "UP ARROW" << endl;


  50. break;


  51. case DOWN_ARROW:


  52. cout << "DOWN ARROW" << endl;


  53. break;


  54. case LEFT_ARROW:


  55. cout << "LEFT_ARROW" << endl;


  56. break;


  57. case RIGHT_ARROW:


  58. cout << "RIGHT_ARROW" << endl;


  59. break;


  60. case F1:


  61. cout << "F1" << endl;


  62. break;


  63. case F2:


  64. cout << "F2" << endl;


  65. break;


  66. case F3:


  67. cout << "F3" << endl;


  68. break;


  69. case F4:


  70. cout << "F4" << endl;


  71. break;


  72. case F5:


  73. cout << "F5" << endl;


  74. break;


  75. case F6:


  76. cout << "F6" << endl;


  77. break;


  78. case F7:


  79. cout << "F7" << endl;


  80. break;


  81. case F8:


  82. cout << "F8" << endl;


  83. break;


  84. case F9:


  85. cout << "F9" << endl;


  86. break;


  87. case F10:


  88. cout << "F10" << endl;


  89. break;


  90. default:


  91. cout << "Some other key." << endl;


  92. }


  93. }


  94. else


  95. cout << KeyStroke << endl;


  96. }


  97. while (KeyStroke != 27); // 27 = Escape key


  98. }


Wednesday, March 30, 2011

The Twin Towers(Longest Common Subsequence)

#include<iostream>
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

The Problem

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));


}