Guest User

Untitled

a guest
Feb 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <windows.h>
  2. #include <conio.h>
  3. #include <string>
  4. #include <iostream>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. float* number;
  10. int size;
  11.  
  12. STARTUPINFO si1,si2;
  13. PROCESS_INFORMATION pi1,pi2;
  14.  
  15. void load()
  16. {
  17.     fstream file;
  18.     file.open("wejscie.txt",ios::in);
  19.  
  20.     size=0;
  21.     file>>size;
  22.  
  23.     number = new float[size];
  24.    
  25.     int i=0;
  26.     char buf[1];
  27.     while(!file.eof())
  28.     {
  29.         file>>number[i];
  30.         i++;
  31.     }
  32.  
  33.     file.close();
  34.    
  35. }
  36.  
  37. void save(char what, float result)
  38. {
  39.     fstream file;
  40.  
  41.     if(what==1)
  42.     {
  43.         file.open("1.txt",ios::out);
  44.     }
  45.     else
  46.     {
  47.         file.open("2.txt",ios::out);
  48.     }
  49.     file<<result;
  50.  
  51.     file.close();
  52. }
  53.  
  54. float operation(char what)
  55. {
  56.     float result=0;
  57.  
  58.     if(what=='+')
  59.     {
  60.         for(int i=0;i<size;i++)
  61.             result +=number[i];
  62.     }
  63.     else
  64.     {
  65.         for(int i=0;i<size;i++)
  66.             result -=number[i];
  67.     }
  68.  
  69.     return result;
  70. }
  71.  
  72. void compare()
  73. {
  74.     fstream file1;
  75.     fstream file2;
  76.  
  77.     file1.open("1.txt",ios::in);
  78.     file2.open("2.txt",ios::in);
  79.  
  80.     float result1=0;
  81.     float result2=0;
  82.  
  83.     file1>>result1;
  84.     file2>>result2;
  85.  
  86.     file1.close();
  87.     file2.close();
  88.     //------
  89.     cout<<"\n--------------------------------------------";
  90.     cout<<"\n              R E S U L T S:";
  91.     cout<<"\n--------------------------------------------";
  92.     cout<<"\n     dadawanie:   "<<result1<<endl;
  93.     cout<<"\n     odejmowanie: "<<result2<<endl;
  94.     cout<<"\n--------------------------------------------";
  95. }
  96.  
  97. int main(int argc, const char* argv[])
  98. {
  99.  
  100.  
  101.     if(argc==1)//proces glowny
  102.     {
  103.         cout<<"Uruchomiono proces glowny"<<endl;
  104.  
  105.         memset(&si1,0,sizeof(si1));
  106.         memset(&si2,0,sizeof(si2));
  107.         si1.cb = sizeof(si1);
  108.         si2.cb = sizeof(si2);
  109.         TCHAR path1[MAX_PATH] = TEXT("LAB3_MechanizmWspoldzielenia.exe 1");
  110.         TCHAR path2[MAX_PATH] = TEXT("LAB3_MechanizmWspoldzielenia.exe 2");
  111.  
  112.         CreateProcess(NULL,path1,NULL,NULL,false,0,NULL,NULL,&si1,&pi1);
  113.         CreateProcess(NULL,path2,NULL,NULL,false,0,NULL,NULL,&si2,&pi2);
  114.         WaitForSingleObject(pi1.hProcess,INFINITE);
  115.         WaitForSingleObject(pi2.hProcess,INFINITE);
  116.         compare();
  117.         CloseHandle(pi1.hProcess);
  118.         CloseHandle(pi2.hProcess);
  119.         getch();
  120.     }
  121.     else if(argc==2)//procesy poboczny
  122.     {
  123.         if(atoi(argv[1])==1) //dodawanie
  124.         {
  125.             cout<<"Uruchomiono proces poboczny - dodawanie"<<endl;
  126.             load();
  127.             float result = operation('+');
  128.             save(1,result);
  129.         }
  130.         else //odejmowanie
  131.         {
  132.             cout<<"Uruchomiono proces poboczny - odejmowanie"<<endl;
  133.             load();
  134.             float result = operation('-');
  135.             save(2,result);
  136.         }
  137.     }
  138.     else
  139.         cout<<"Blad programu";
  140.  
  141.     delete number;
  142.    
  143.     return 0;
  144. }
Add Comment
Please, Sign In to add comment