Advertisement
Mike_be

Wake my up 2 14 var

Dec 8th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <math.h>
  4. #include <string>
  5. #include <vector>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. string input = "";
  11. int dotCount;
  12.  
  13. double read(string startMes, string strErr, string dotErr) { //Checks variable for correct input (fractional number)
  14.     cout << startMes << ": ";
  15.     getline(cin, input);
  16.     dotCount = 0;
  17.     bool onlyDigits = true;
  18.     string dummy;
  19.     while (!onlyDigits || (dotCount >= 0 && dotCount < 2)) { //Cycle for checking each char in input string
  20.         for (int i = input.length() - 1; i >= 0; i--) {
  21.             if (!isdigit(input[i])) {
  22.                 if (input[i] == '.') dotCount += 1; //Adds number if it counted dot. Reacts to wrong input with multiple dots
  23.                 else onlyDigits = false; //Changes bool if there is something except dots and digits
  24.             }
  25.         }
  26.         if (onlyDigits && dotCount < 2) { //Checks if all requirements are met and return double variable from string
  27.             return stod(input.c_str());
  28.         }
  29.         else { //Cycle if something is wrong, reseting all values and, saying what is wrong
  30.             if (!onlyDigits) { cout << strErr << ", type again: "; }
  31.             else if (dotCount > 1) { cout << dotErr << ", type again: "; }
  32.             getline(cin, input);
  33.             onlyDigits = true;
  34.             dotCount = 0;
  35.         }
  36.     }
  37. }
  38.  
  39.  
  40. char readChar(string request) { //Function that reads char symbol for further input Y or N, also works with not capital letters
  41.     do {
  42.         string input = "";
  43.         cout << request << ": ";
  44.         getline(cin, input);
  45.  
  46.         if (input.length() == 1) return input[0]; //Check if user wrote more than 1 letter and asks him again
  47.         else {
  48.             cout << "Press Y or N" << endl;
  49.         }
  50.     } while (input.length() != 1);
  51. }
  52.  
  53. unsigned long long int double_fact(int i) { //Function for calculating double factorial sum
  54.     unsigned long long int factSum; factSum = 1;
  55.     int j = 1;
  56.     while (2 * j <= i + 1) {//Cycle for double factorial if factorial is doe
  57.         if (i % 2 == 0) {
  58.             factSum *= 2 * j;//Formula for even factorial
  59.             ++j;
  60.         }
  61.         else { //Cycle if factorial is odd
  62.             factSum *= (2 * j) - 1;//Formula for odd factorial
  63.             ++j;
  64.         }
  65.     }
  66.     return factSum;
  67. }
  68.  
  69. int main() {
  70.     cout << setprecision(10);//Sets the amount of integers that will be printed
  71.     int n = 0;
  72.     double x, alpha;
  73.     long double sum, a_n, epsilon; sum = 0;
  74.     do {do {//Do while cycle for main function, also do while cycle for x input
  75.         x = read("Write X number, -1 < X > 1", "You wrote incorrect number with letters", "You wrote number with several dots");
  76.     } while (!(x < 1) || !(x > -1));
  77.     alpha = read("Write aplha number", "You wrote incorrect number with letters", "You wrote number with several dots");
  78.     cout << setw(2) << "n" << setw(15) << "a_n" << setw(15) << "sum" << setw(15) << "epsilon" << endl;//Starts the table input
  79.     if (dotCount == 0) {
  80.         while (n <= alpha) { //Cycle for count a_n, sum and epsilon if alpha is integer
  81.             a_n = double_fact(2 * n + 1)*pow(x, 2 * n) / double_fact(2 * n);
  82.             sum += a_n;
  83.             epsilon = (double_fact(2 * (n + 1) + 1)*pow(x, 2 * (n + 1)) / double_fact(2 * (n + 1))) / sum;
  84.             cout << setw(2) << n << setw(15) << a_n << setw(15) << sum << setw(15) << epsilon << endl;
  85.             n++;
  86.         }
  87.     }
  88.     else {
  89.         do {//Cycle for counting elements if alpha is fractional
  90.             a_n = double_fact(2 * n + 1)*pow(x, 2 * n) / double_fact(2 * n);
  91.             sum += a_n;
  92.             epsilon = (double_fact(2 * (n + 1) + 1)*pow(x, 2 * (n + 1)) / double_fact(2 * (n + 1))) / sum;
  93.             cout << setw(2) << n << setw(15) << a_n << setw(15) << sum << setw(15) << epsilon << endl;
  94.             n++;
  95.         } while (alpha < epsilon);
  96.     }
  97.     n = 0, sum = 0, epsilon = 0;
  98.     }while (abs(readChar("Continue (Y/N)?") - 105) == 16);
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement