Advertisement
Mike_be

Base lab1

Feb 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <vector>
  6. #include <regex>
  7. #include <streambuf>
  8. #include <iomanip>
  9. #include <Windows.h>
  10.  
  11. int matrix[4][4] =
  12. { //S, A1, A2, A3
  13.     1, 1,  2,  3, //A for 0-9
  14.     3, 2,  3,  3, //A for ".,"
  15.     3, 4,  4,  0, //S/F for fail or end
  16.     3, 3,  3,  3  //F - fail state for everything else
  17. };
  18. // 3 - fail state
  19. // 1 - state for digits
  20. // 2 - state for dot in number
  21. // 4 - print state
  22.  
  23. /*
  24.  * Most important function in auto-rifle. Add states or new conditions in it for new functionality
  25.  * This one is for searching fractional numbers
  26.  */
  27. int row(char& a)
  28. {
  29.     if (a >= '0' && a <= '9')
  30.     {
  31.         return 0;
  32.     }
  33.     if (a == '.' || a == ',')
  34.     {
  35.         return 1;
  36.     }
  37.     if (a == ' ' || a == '\n')
  38.     {
  39.         return 2;
  40.     }
  41.     return 3;
  42. }
  43.  
  44.  
  45. /*
  46.  Auto-rifle
  47.  Usage: auto_rifle(string_you_want_to_check, vec_you_want_to_pt_results);
  48.  */
  49. void auto_rifle(std::string& str, std::vector<double>& vec)
  50. {
  51.     std::string num = "";
  52.     int state = 0;
  53.     for (unsigned long long k = 0; k < str.size(); k++)
  54.     {
  55.         if (state == 0)
  56.             num.clear();
  57.         state = matrix[(row(str[k]))][state];
  58.         num.push_back(str[k]);
  59.         if (state == 4 || ((state == 1 || state == 2) && k == str.size() - 1))
  60.         {
  61.             vec.push_back(std::stod(num));
  62.             state = 0;
  63.         }
  64.     }
  65.     return;
  66. }
  67.  
  68. using namespace std;
  69.  
  70. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //Console address to change text color
  71.  
  72. /*
  73.  Printing color messages in console
  74.  Usage: print_str(sring_you_want_to_print, color_of_text, time_between_letters);
  75.  Colors: https://i.stack.imgur.com/ZG625.png
  76. */
  77. inline bool print_str(string str, int k, int n)
  78. {
  79.     SetConsoleTextAttribute(hConsole, k);
  80.     for (int l = 0; l < int(str.size()); l++)
  81.     {
  82.         cout << str[l];
  83.         Sleep(n);
  84.     }
  85.     SetConsoleTextAttribute(hConsole, 14);
  86.     return true;
  87. }
  88.  
  89. /*
  90.  * Read function, reads from given path/.../file
  91.  * Usage: read(vec_you_want_to_fill, file)
  92.  * Returns 1 if couldn't open file, can be used in cycles like "do{} while()" or common
  93.  * Returns 0 if everything is okay (maybe)
  94.  */
  95. int read(vector<double>& vec, string& file_path)
  96. {
  97.     string file;
  98.     ifstream in;
  99.     in.open(file_path);
  100.     if (!in.is_open())
  101.     {
  102.         print_str("Couldn't read the file, type again: ", 4, 10);
  103.         return 1;
  104.     }
  105.     ofstream on("out.txt");
  106.     file.assign((istreambuf_iterator<char>(in)),
  107.         istreambuf_iterator<char>());
  108.     in.close();
  109.     auto_rifle(file, vec);
  110.     return 0;
  111. }
  112.  
  113. //Calcs some stuff for lab
  114. double calc(double& a, double& b)
  115. {
  116.     if (a < b)
  117.     {
  118.         print_str("a < b\n", 11, 10);
  119.         print_str("(b^3 - 2) / a = ", 10, 10);
  120.         return (b*b*b - 2) / a;
  121.     }
  122.     else if (a == b)
  123.     {
  124.         print_str("a == b\n", 11, 10);
  125.         print_str("Answer = ", 10, 10);
  126.         return -3;
  127.     }
  128.     else
  129.     {
  130.         print_str("a > b\n", 11, 10);
  131.         print_str("(3 * a - 5) / b = ", 10, 10);
  132.         return (3 * a - 5) / b;
  133.     }
  134. }
  135.  
  136. int main()
  137. {
  138.     string file, output, dummy;
  139.     ofstream out;
  140.     vector<double> Nums;
  141.     double a, b, result;
  142.     print_str("Write file name: ", 11, 10);
  143.     do
  144.     {
  145.         getline(cin, file);
  146.     } while (read(Nums, file));
  147.     print_str("Write output file: ", 11, 10);
  148.     getline(cin, output);
  149.     out.open(output);
  150.     for (unsigned int i = 1; i < Nums.size(); i = i + 2)
  151.     {
  152.         a = Nums[i-1];
  153.         b = Nums[i];
  154.         print_str("a = ", 15, 10);
  155.         cout << a << " ";
  156.         print_str("b = ", 15, 10);
  157.         cout << b << endl;
  158.         out << "a = " << a << " b = " << b << endl;
  159.         result = calc(a, b);
  160.         cout << setprecision(10) << result << endl << endl;
  161.         out << "Result = " << setprecision(10) << result << endl << endl;
  162.     }
  163.     out.close();
  164.     print_str("Press Enter to exit.", 15, 10);
  165.     getline(cin, dummy);
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement