Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct Polinom {
  4.     int pow;
  5.     float cof;
  6.     Polinom* next;
  7. };
  8.  
  9. void Summ(Polinom** R, Polinom* P, Polinom* Q);
  10. int read(const char* filePath, Polinom* P);
  11. void write(const char* filePath, Polinom* R);
  12.  
  13. int main() {
  14.  
  15.     Polinom* P, * Q, * R;
  16.     R = nullptr;
  17.     P = new Polinom;
  18.     Q = new Polinom;
  19.     if (!read("1.txt", P)) // 1.txt - файл с полиномом P в формате a b, a - степень, b - коэффициент
  20.         return 1;
  21.     if (!read("2.txt", Q)) // 2.txt - файл с полиномом Q
  22.         return 1;
  23.  
  24.     Summ(&R, P, Q); // Вычисление R
  25.  
  26.     write("result.txt", R);
  27.  
  28.     return 0;
  29. }
  30.  
  31. void Summ(Polinom** R, Polinom* P, Polinom* Q) {
  32.  
  33.     if ((P || Q) && !(*R)) {
  34.         *R = new Polinom;
  35.         (*R)->next = nullptr;
  36.     }
  37.  
  38.     while (P || Q) {
  39.         if (P && Q) {
  40.             if (P->pow > Q->pow) {
  41.                 (*R)->pow = P->pow;
  42.                 (*R)->cof = P->cof;
  43.                 P = P->next;
  44.                 (*R) = (*R)->next;
  45.             }
  46.             else if (Q->pow > P->pow) {
  47.                 (*R)->pow = Q->pow;
  48.                 (*R)->cof = Q->cof;
  49.                 Q = Q->next;
  50.                 (*R) = (*R)->next;
  51.             }
  52.             else if (Q->cof != -(P->cof)) {
  53.                 (*R)->pow = Q->pow;
  54.                 (*R)->cof = P->cof + Q->cof;
  55.                 P = P->next;
  56.                 Q = Q->next;
  57.                 (*R) = (*R)->next;
  58.             }
  59.             else {
  60.                 P = P->next;
  61.                 Q = Q->next;
  62.             }
  63.            
  64.         }
  65.         else if (P) {
  66.             (*R)->pow = P->pow;
  67.             (*R)->cof = P->cof;
  68.             P = P->next;
  69.             (*R) = (*R)->next;
  70.         }
  71.         else if (Q) { // Только Q не просмотрен
  72.             (*R)->pow = Q->pow;
  73.             (*R)->cof = Q->cof;
  74.             P = P->next;
  75.             (*R) = (*R)->next;
  76.         }
  77.     }
  78.  
  79.  
  80.    /* if (P && Q) { // P и Q не просмотрены
  81.  
  82.         if (P->pow > Q->pow) {
  83.             (*R)->pow = P->pow;
  84.             (*R)->cof = P->cof;
  85.             Summ(&(*R)->next, P->next, Q);
  86.         }
  87.         else if (Q->pow > P->pow) {
  88.             (*R)->pow = Q->pow;
  89.             (*R)->cof = Q->cof;
  90.             Summ(&(*R)->next, P, Q->next);
  91.         }
  92.         else if (Q->cof != -(P->cof)) {
  93.             (*R)->pow = Q->pow;
  94.             (*R)->cof = P->cof + Q->cof;
  95.             Summ(&(*R)->next, P->next, Q->next);
  96.         }
  97.         else {
  98.             Summ(R, P->next, Q->next);
  99.         }
  100.     }
  101.     else if (P) { // Только P не просмотрен
  102.         (*R)->pow = P->pow;
  103.         (*R)->cof = P->cof;
  104.         Summ(&(*R)->next, P->next, Q);
  105.     }
  106.     else if (Q) { // Только Q не просмотрен
  107.         (*R)->pow = Q->pow;
  108.         (*R)->cof = Q->cof;
  109.         Summ(&(*R)->next, P, Q->next);
  110.     }*/
  111. }
  112.  
  113. int read(const char* filePath, Polinom* P) {
  114.     FILE* file;
  115.     errno_t error = fopen_s(&file, filePath, "r");
  116.  
  117.     if (error) {
  118.         printf("Failed to open the file \"%s\".", filePath);
  119.         return 0;
  120.     }
  121.     int i;
  122.     fscanf_s(file, "%d", &i);
  123.     for (int j = 0; j < i - 1; j++) {
  124.         fscanf_s(file, "%d", &P->pow);
  125.         fscanf_s(file, "%f", &P->cof);
  126.         P->next = new Polinom;
  127.         P = P->next;
  128.     }
  129.     fscanf_s(file, "%d", &P->pow);
  130.     fscanf_s(file, "%f", &P->cof);
  131.     P->next = nullptr;
  132.     fclose(file);
  133.     return 1;
  134. }
  135.  
  136. void write(const char* filePath, Polinom* R) {
  137.     FILE* file;
  138.     errno_t error = fopen_s(&file, filePath, "w+");
  139.     if (error) {
  140.         printf("Failed to open the file \"%s\".", filePath);
  141.         return;
  142.     }
  143.     while (R) {
  144.         fprintf(file, "Power: %d, coefficient: %.3f\n", R->pow, R->cof);
  145.         R = R->next;
  146.     }
  147.     fclose(file);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement