Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6.  
  7. #define FIFO "my_fifo"
  8.  
  9. void policz(FILE*output, float a, float b, float c)
  10. {
  11. if(a!=0)//kwadratowe
  12. {
  13. fprintf(output, "%.2fx^2 + %.2fx + %.2f = 0 - ",a,b,c);
  14. float delta = b*b-4*a*c;
  15.  
  16. if(delta<0)
  17. fprintf(output, "Brak rozwiazan(rzeczywistych)");
  18. else if(delta == 0)
  19. fprintf(output, "x1 = x2 = %.2f", -b/(2*a) );
  20. else
  21. {
  22. delta = sqrtf(delta);
  23. fprintf(output, "x1 = %.2f x2 = %.2f", (-b-delta)/(2*a), (-b+delta)/(2*a));
  24. }
  25. }
  26. else if(b!=0)//liniowe
  27. {
  28. fprintf(output, "%.2fx + %.2f = 0 - ",b,c);
  29. fprintf(output,"x = %f", -c/b);
  30. }
  31. else
  32. {
  33. fprintf(output, "%.2f = 0 - ",c);
  34. if(c==0)
  35. fprintf(output,"x nalezy do R");
  36. else
  37. fprintf(output,"Brak rozwiazan");
  38. }
  39.  
  40. fprintf(output, "\n");
  41.  
  42. }
  43.  
  44.  
  45. void PP1(FILE*input, int ilosc)
  46. {
  47. FILE*fp;
  48. for(int i = 0;i<ilosc;i++)
  49. {
  50. fp = fopen(FIFO, "w");
  51. float a,b,c;
  52. fscanf(input, "%f %f %f", &a, &b, &c);
  53. printf("%f %f %f out from PP1\n", a,b,c);
  54. fflush(stdout);
  55. fprintf(fp, "%f %f %f", a,b,c);
  56. fclose(fp);
  57.  
  58. sleep(1);
  59. }
  60. }
  61.  
  62.  
  63. void PP2(FILE*output, int ilosc)
  64. {
  65. sleep(1);
  66. FILE*fp;
  67. for(int i = 0;i<ilosc;i++)
  68. {
  69. fp = fopen(FIFO, "r");
  70. float a,b,c;
  71. fscanf(fp, "%f %f %f", &a,&b,&c);
  72. printf("%f %f %f in PP2\n", a,b,c);
  73. policz(output, a,b,c);
  74. fclose(fp);
  75. }
  76. }
  77.  
  78. int main(int argc, char *argv[])
  79. {
  80. if(argc!=2)
  81. {
  82. printf("Zla ilosc argumentow!\n");
  83. return 1;
  84. }
  85. FILE*input = fopen(argv[1], "r+");
  86. if(input == NULL) {
  87. printf("Blad przy odczytywaniu pliku!\n");
  88. return 2;
  89. }
  90. else if(access ( argv[1], F_OK ) == -1)
  91. {
  92. printf("Brak dostepu!\n");
  93. return 3;
  94. }
  95. FILE* output = fopen("out.txt", "w+");
  96. if(output == NULL) {
  97. printf("Blad przy otwieraniu pliku do zapisu!\n");
  98. return 4;
  99. }
  100.  
  101. umask(0);
  102. mkfifo(FIFO, 0666); //0 z przodu oznacza notacje ósemkowa
  103.  
  104.  
  105. int ilosc;
  106. fscanf(input, "%d", &ilosc);
  107.  
  108. if(fork())
  109. {
  110. PP1(input, ilosc);
  111. }
  112. else
  113. {
  114. PP2(output, ilosc);
  115. }
  116.  
  117.  
  118.  
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement