Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <regex>
- #include <streambuf>
- #include <iomanip>
- #include <Windows.h>
- int matrix[4][4] =
- { //S, A1, A2, A3
- 1, 1, 2, 3, //A for 0-9
- 3, 2, 3, 3, //A for ".,"
- 3, 4, 4, 0, //S/F for fail or end
- 3, 3, 3, 3 //F - fail state for everything else
- };
- // 3 - fail state
- // 1 - state for digits
- // 2 - state for dot in number
- // 4 - print state
- /*
- * Most important function in auto-rifle. Add states or new conditions in it for new functionality
- * This one is for searching fractional numbers
- */
- int row(char& a)
- {
- if (a >= '0' && a <= '9')
- {
- return 0;
- }
- if (a == '.' || a == ',')
- {
- return 1;
- }
- if (a == ' ' || a == '\n')
- {
- return 2;
- }
- return 3;
- }
- /*
- Auto-rifle
- Usage: auto_rifle(string_you_want_to_check, vec_you_want_to_pt_results);
- */
- void auto_rifle(std::string& str, std::vector<double>& vec)
- {
- std::string num = "";
- int state = 0;
- for (unsigned long long k = 0; k < str.size(); k++)
- {
- if (state == 0)
- num.clear();
- state = matrix[(row(str[k]))][state];
- num.push_back(str[k]);
- if (state == 4 || ((state == 1 || state == 2) && k == str.size() - 1))
- {
- vec.push_back(std::stod(num));
- state = 0;
- }
- }
- return;
- }
- using namespace std;
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //Console address to change text color
- /*
- Printing color messages in console
- Usage: print_str(sring_you_want_to_print, color_of_text, time_between_letters);
- Colors: https://i.stack.imgur.com/ZG625.png
- */
- inline bool print_str(string str, int k, int n)
- {
- SetConsoleTextAttribute(hConsole, k);
- for (int l = 0; l < int(str.size()); l++)
- {
- cout << str[l];
- Sleep(n);
- }
- SetConsoleTextAttribute(hConsole, 14);
- return true;
- }
- /*
- * Read function, reads from given path/.../file
- * Usage: read(vec_you_want_to_fill, file)
- * Returns 1 if couldn't open file, can be used in cycles like "do{} while()" or common
- * Returns 0 if everything is okay (maybe)
- */
- int read(vector<double>& vec, string& file_path)
- {
- string file;
- ifstream in;
- in.open(file_path);
- if (!in.is_open())
- {
- print_str("Couldn't read the file, type again: ", 4, 10);
- return 1;
- }
- ofstream on("out.txt");
- file.assign((istreambuf_iterator<char>(in)),
- istreambuf_iterator<char>());
- in.close();
- auto_rifle(file, vec);
- return 0;
- }
- //Calcs some stuff for lab
- double calc(double& a, double& b)
- {
- if (a < b)
- {
- print_str("a < b\n", 11, 10);
- print_str("(b^3 - 2) / a = ", 10, 10);
- return (b*b*b - 2) / a;
- }
- else if (a == b)
- {
- print_str("a == b\n", 11, 10);
- print_str("Answer = ", 10, 10);
- return -3;
- }
- else
- {
- print_str("a > b\n", 11, 10);
- print_str("(3 * a - 5) / b = ", 10, 10);
- return (3 * a - 5) / b;
- }
- }
- int main()
- {
- string file, output, dummy;
- ofstream out;
- vector<double> Nums;
- double a, b, result;
- print_str("Write file name: ", 11, 10);
- do
- {
- getline(cin, file);
- } while (read(Nums, file));
- print_str("Write output file: ", 11, 10);
- getline(cin, output);
- out.open(output);
- for (unsigned int i = 1; i < Nums.size(); i = i + 2)
- {
- a = Nums[i-1];
- b = Nums[i];
- print_str("a = ", 15, 10);
- cout << a << " ";
- print_str("b = ", 15, 10);
- cout << b << endl;
- out << "a = " << a << " b = " << b << endl;
- result = calc(a, b);
- cout << setprecision(10) << result << endl << endl;
- out << "Result = " << setprecision(10) << result << endl << endl;
- }
- out.close();
- print_str("Press Enter to exit.", 15, 10);
- getline(cin, dummy);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement