Advertisement
Guest User

sfg

a guest
Sep 22nd, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. //Buffon's problem-C/C++ coding
  2.  
  3. //header
  4. #include<iostream>
  5. #include<cstdlib>
  6. #include<conio.h>
  7. #include<cmath>
  8. #include<ctime>
  9.  
  10. using namespace std;
  11.  
  12. //constants
  13. #define pi 3.1415926536 //p
  14.  
  15. //macros
  16. #define randomize() srand((unsigned)time(NULL))
  17.  
  18. //global variables
  19. double dist; //distance between two lines
  20. int ScreenMode; //screenmode
  21.  
  22. //needle class
  23. class needle{
  24. public:
  25. double len; //length of the needle
  26. double angle; //angle-RANDOM between 0 ~ p
  27. double x; //distance between needle and line-RANDOM between (dist-len*a) ~ dist
  28. bool check(); //whether the needle meets the line or not
  29. bool needleShort(); //whether len<=dist
  30. };
  31.  
  32. bool needle::needleShort(){
  33. if(len<=dist) return true;
  34. else return false;
  35. }
  36.  
  37. bool needle::check(){
  38. double b=len*sin(angle);
  39. if(needleShort()){
  40. if(x+b>=dist) return true;
  41. else return false;
  42. }
  43. else{
  44. if(b>=dist) return true;
  45. else{
  46. if(x+b>=dist) return true;
  47. else return false;
  48. }
  49. }
  50. }
  51.  
  52. void getScreenMode(){
  53. int sc;
  54. cin.clear();
  55. sc=_getch();
  56. switch(sc){
  57. case 's': //show all steps
  58. ScreenMode=1;
  59. break;
  60. case 'p': //step by step
  61. ScreenMode=2;
  62. break;
  63. case 'a': //just see final result
  64. ScreenMode=3;
  65. break;
  66. default:
  67. getScreenMode();
  68. break;
  69. }
  70. }
  71.  
  72. struct prob{
  73. unsigned int tot; //total
  74. unsigned int num; //number of success
  75. };
  76.  
  77. int main(){
  78. unsigned int n; //number of trying
  79. unsigned int i; //loop index
  80. needle L; //needle "L"
  81. register bool* ifNeedle; //whether the needle meets the line or not
  82. prob p; //probability
  83. double res;
  84. double piCal;
  85. int ch;
  86.  
  87. for(;;){
  88. //
  89. randomize();
  90.  
  91. //
  92. cout<<"Buffon's needle problem - C++ simulation"<<endl<<endl;
  93. cin.clear();
  94. cout<<"Set : distance between lines"<<endl; //Input dist
  95. cin>>dist;
  96.  
  97. cin.clear();
  98. cout<<"Set : length of a needle"<<endl; //Input len
  99. cin>>L.len;
  100.  
  101. cin.clear();
  102. cout<<"How many times do you want to throw needle?"<<endl; //Input n
  103. cin>>n;
  104. ifNeedle=(bool *)malloc(n*sizeof(bool));
  105.  
  106. cin.clear();
  107. cout<<endl<<"s : view all steps, p: step by step, a : just see total result"<<endl<<endl; //Input ScreenMode
  108. getScreenMode();
  109.  
  110. cout<<"Throwing..."<<endl<<endl;
  111.  
  112. //
  113. /*when len<=dist*/
  114. if(L.needleShort()){
  115. p.tot=1; //as prob must not be 0;
  116. p.num=1, ifNeedle[0]=true;
  117. for(i=1;i<n;i++){
  118. p.tot++;
  119.  
  120. L.angle=(rand()%30000)/29999.0*pi;
  121. L.x=(rand()%30000)/29999.0*dist;
  122.  
  123. if(L.check()){
  124. p.num++;
  125. ifNeedle[i]=true;
  126. }
  127. else ifNeedle[i]=false;
  128. }
  129. }
  130. /*when len>dist*/
  131. else{
  132. p.tot=1; //as prob must not be 1;
  133. p.num=0, ifNeedle[0]=false;
  134. for(i=1;i<n;i++){
  135. p.tot++;
  136.  
  137. L.angle=(rand()%30000)/29999.0*pi;
  138. L.x=(rand()%30000)/29999.0*dist;
  139.  
  140. if(L.check()){
  141. p.num++;
  142. ifNeedle[i]=true;
  143. ifNeedle[i]=false;
  144. }
  145. }
  146. }
  147.  
  148. //
  149. switch(ScreenMode){
  150. case 1:
  151. for(i=0;i<n;i++){
  152. cout<<" Trying "<<i;
  153. if(ifNeedle[i]) cout<<" : "<<"good"<<endl;
  154. else cout<<" : "<<"no"<<endl;
  155. }
  156. cout<<endl<<endl;
  157. break;
  158. case 2:
  159. for(i=0;i<n;i++){
  160. _getch();
  161. cout<<" Trying "<<i;
  162. if(ifNeedle[i]) cout<<" : "<<"good"<<endl;
  163. else cout<<" : "<<"no"<<endl;
  164. }
  165. cout<<endl<<endl;
  166. break;
  167. default:
  168. break;
  169. }
  170.  
  171. //
  172. res=p.num/(p.tot*1.0);
  173. if(L.len<=dist) piCal=2*L.len/(dist*res);
  174. else piCal=(1/(res-1))*((2*L.len/dist)-(2/dist)*(sqrt(L.len*L.len-dist*dist)+dist*asin(dist/L.len)));
  175.  
  176. //
  177. cout<<"Total numbers of trying : ";
  178. cout<<p.tot<<endl<<endl;
  179. cout<<"Number of success : ";
  180. cout<<p.num<<endl<<endl;
  181. cout<<"Probability : ";
  182. cout<<res<<endl<<endl<<endl;
  183. cout<<"Calculated pi ";
  184. cout<<piCal<<endl<<endl;
  185.  
  186. cout<<"press q to exit, otherwise restart"<<endl;
  187.  
  188. //
  189. cin.clear();
  190. ch=_getch();
  191. switch(ch){
  192. case 'q':
  193. exit(0);
  194. break;
  195. default:
  196. system("cls");
  197. break;
  198. }
  199.  
  200. free(ifNeedle);
  201. }
  202.  
  203. return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement