Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <time.h>
  4. #include <windows.h>
  5. using namespace std;
  6.  
  7. void Traspasos();
  8. void Contabilizar();
  9. void Proceso1();
  10. void Proceso2();
  11. bool bandera_proceso1,bandera_proceso2;
  12. int turno;
  13. int x[100];
  14.  
  15. int main()
  16. {
  17.     int i,sum=0;
  18.     for(i=0;i<100;i++){
  19.         x[i]=100;
  20.         sum=sum+x[i];
  21.     }
  22.     cout<<"Dinero en el banco: "<<sum<<endl;
  23.     system("pause");
  24.     thread pro1(Proceso1);
  25.     thread pro2(Proceso2);
  26.     pro1.join();
  27.     pro2.join();
  28.     return 0;
  29. }
  30. void Proceso1()
  31. {
  32.     while (true){
  33.         bandera_proceso1=true;
  34.         turno=1;
  35.             while(bandera_proceso2=true && turno==1){
  36.                 Contabilizar();
  37.             }
  38.         bandera_proceso1=false;
  39.         Sleep(1000);
  40.     }
  41. }
  42. void Proceso2()
  43. {
  44.     while(true){
  45.         bandera_proceso2=true;
  46.         turno=0;
  47.             while(bandera_proceso1=true && turno==0){
  48.                 Traspasos();
  49.                 Sleep(1000);
  50.             }
  51.         bandera_proceso2=false;
  52.     }
  53. }
  54. void Traspasos()
  55. {
  56.     srand(time(NULL));
  57.     int t1,t2,can;
  58.     t1=rand()%99;
  59.     t2=rand()%99;
  60.     can=rand()%9;
  61.     x[t1]=x[t1]+can;
  62.     x[t2]=x[t2]-can;
  63. }
  64. void Contabilizar()
  65. {
  66.     int sum=0,i;
  67.     for(i=0;i<100;i++){
  68.         cout<<"Cuenta "<<(i+1)<<" "<<x[i]<<endl;
  69.         sum=sum+x[i];
  70.     }
  71.     cout<<"Dinero en el banco: "<<sum<<endl;
  72.     system("pause");
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement