Advertisement
SteelK

Untitled

Jun 26th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.22 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <fstream>
  6. #define SIZE 1024
  7.  
  8. using namespace std;
  9.  
  10. long double **new_mas();
  11. void del_mas(long double **mas);
  12.  
  13. bool cacheExist();
  14. void goInitializer(long double** cache1, long double** cache2, bool cacheExistAnswer);
  15. void initializerMasZero(long double** mas);
  16. void initializerMasCache(ifstream& cache, long double** mas);
  17.  
  18. void goCopy(long double** cache1, long double** cache2);
  19. void copyInCache(ofstream& cache, long double** mas);
  20.  
  21. void start_while(long double **cache1, long double **cache2);
  22. void io_user_send(size_t *t, size_t *s);
  23. void cin_parametr(size_t *in_t, size_t *in_s);
  24. void work_func(size_t t, size_t s, long double *in_func, long double **cache1, long double **cache2);
  25. long double formul(long double **cache2, size_t t, size_t s);
  26.  
  27. int main()
  28. {
  29.     setlocale(0, "");
  30.     bool cacheExistAnswer = cacheExist();
  31.  
  32.     long double **cache1 = new_mas();
  33.     long double **cache2 = new_mas();
  34.  
  35.     goInitializer(cache1, cache2, cacheExistAnswer);
  36.  
  37.     start_while(cache1, cache2);
  38.  
  39.     goCopy(cache1, cache2);
  40.  
  41.     del_mas(cache1);
  42.     del_mas(cache2);
  43.  
  44.     return 0;
  45. }
  46.  
  47.  
  48. bool cacheExist()
  49. {
  50.     ifstream fileCache1("cache1.bin", ios_base::binary);
  51.     ifstream fileCache2("cache2.bin", ios_base::binary);
  52.     bool answer = !(fileCache1 && fileCache1);
  53.     if (answer)
  54.         cout << "   [Отсутствуют файлы кеша, будут созданы новые...]" << endl;
  55.     else
  56.         cout << "   [Файлы кеша найдены...]" << endl;
  57.     fileCache1.close();
  58.     fileCache2.close();
  59.     return answer;
  60. }
  61.  
  62.  
  63. long double **new_mas()
  64. {
  65.     long double **mas = new long double *[SIZE];
  66.     for (size_t y = 0; y < SIZE; y++)
  67.         mas[y] = new long double[SIZE];
  68.     return mas;
  69. }
  70.  
  71.  
  72. void goInitializer(long double** cache1, long double** cache2, bool cacheExistAnswer)
  73. {
  74.     if (cacheExistAnswer)
  75.     {
  76.         ifstream fileCache1("cache1.bin", ios_base::binary);
  77.         ifstream fileCache2("cache2.bin", ios_base::binary);
  78.         initializerMasCache(fileCache1, cache1);
  79.         initializerMasCache(fileCache2, cache2);
  80.         fileCache1.close();
  81.         fileCache1.close();
  82.     }
  83.     else
  84.     {
  85.         initializerMasZero(cache1);
  86.         initializerMasZero(cache2);
  87.     }
  88. }
  89.  
  90.  
  91. void initializerMasZero(long double** mas)
  92. {
  93.     for(size_t i = 0; i < SIZE; i++)
  94.         for (size_t j = 0; j < SIZE; j++)
  95.             mas[i][j] = 0;
  96. }
  97.  
  98.  
  99. void initializerMasCache(ifstream& cache, long double** mas)
  100. {
  101.     for (size_t i = 0; i < SIZE; i++)
  102.         for (size_t j = 0; j < SIZE; j++)
  103.             cache >> mas[i][j];
  104. }
  105.  
  106.  
  107. void goCopy(long double** cache1, long double** cache2)
  108. {
  109.     ofstream copyCache1("cache1.bin", ios_base::binary);
  110.     ofstream copyCache2("cache2.bin", ios_base::binary);
  111.     copyInCache(copyCache1, cache1);
  112.     copyInCache(copyCache2, cache2);
  113.     copyCache1.close();
  114.     copyCache2.close();
  115. }
  116.  
  117.  
  118. void copyInCache(ofstream& cache, long double** mas)
  119. {
  120.     for (size_t i = 0; i < SIZE; i++)
  121.         for (size_t j = 0; j < SIZE; j++)
  122.             cache << mas[i][j];
  123. }
  124.  
  125.  
  126. void del_mas(long double **mas)
  127. {
  128.     for (size_t y = 0; y < SIZE; y++)
  129.         delete[] mas[y];
  130.     delete[] mas;
  131. }
  132.  
  133.  
  134. void start_while(long double **cache1, long double **cache2)
  135. {
  136.     size_t t, s;
  137.     long double func;
  138.     bool answer = 1;
  139.     while (answer)
  140.     {
  141.         io_user_send(&t, &s);
  142.         work_func(t, s, &func, cache1, cache2);
  143.         cout << "Результат = " << func << endl;
  144.  
  145.         cout << endl << "Ввести новые значения? (1/0)" << endl;
  146.         cin >> answer;
  147.     }
  148. }
  149.  
  150.  
  151. void io_user_send(size_t *t, size_t *s)
  152. {
  153.     cout << "Введите число t и число s" << endl;
  154.     cin_parametr(t, s);
  155. }
  156.  
  157. void cin_parametr(size_t *in_t, size_t *in_s)
  158. {
  159.     cin >> *in_t;
  160.     cin >> *in_s;
  161.     if (*in_t > 1024 || *in_s > 1024)
  162.     {
  163.         cout << "Введенное число больше 1024" << endl;
  164.         exit(-1);
  165.     }
  166. }
  167.  
  168.  
  169. void work_func(size_t t, size_t s, long double *in_func, long double **cache1, long double **cache2)
  170. {
  171.     if (!cache1[t][s])
  172.         cache1[t][s] = *in_func = formul(cache2, t, s);
  173.     else
  174.         *in_func = cache1[t][s];
  175. }
  176.  
  177.  
  178. long double formul(long double **cache2, size_t t, size_t s)
  179. {
  180.     long double argument;
  181.     if (!cache2[t][s])
  182.         cache2[t][s] = argument = log(1 + powl((t + s), 5)) * cosl(powl((t + s), 2));
  183.     else
  184.         argument = cache2[t][s];
  185.     return argument * t / s;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement