Advertisement
SteelK

Untitled

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