Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <math.h>
- #include <string>
- #include <vector>
- #include <iomanip>
- using namespace std;
- string input = "";
- int dotCount;
- double read(string startMes, string strErr, string dotErr) { //Checks variable for correct input (fractional number)
- cout << startMes << ": ";
- getline(cin, input);
- dotCount = 0;
- bool onlyDigits = true;
- string dummy;
- while (!onlyDigits || (dotCount >= 0 && dotCount < 2)) { //Cycle for checking each char in input string
- for (int i = input.length() - 1; i >= 0; i--) {
- if (!isdigit(input[i])) {
- if (input[i] == '.') dotCount += 1; //Adds number if it counted dot. Reacts to wrong input with multiple dots
- else onlyDigits = false; //Changes bool if there is something except dots and digits
- }
- }
- if (onlyDigits && dotCount < 2) { //Checks if all requirements are met and return double variable from string
- return stod(input.c_str());
- }
- else { //Cycle if something is wrong, reseting all values and, saying what is wrong
- if (!onlyDigits) { cout << strErr << ", type again: "; }
- else if (dotCount > 1) { cout << dotErr << ", type again: "; }
- getline(cin, input);
- onlyDigits = true;
- dotCount = 0;
- }
- }
- }
- char readChar(string request) { //Function that reads char symbol for further input Y or N, also works with not capital letters
- do {
- string input = "";
- cout << request << ": ";
- getline(cin, input);
- if (input.length() == 1) return input[0]; //Check if user wrote more than 1 letter and asks him again
- else {
- cout << "Press Y or N" << endl;
- }
- } while (input.length() != 1);
- }
- unsigned long long int double_fact(int i) { //Function for calculating double factorial sum
- unsigned long long int factSum; factSum = 1;
- int j = 1;
- while (2 * j <= i + 1) {//Cycle for double factorial if factorial is doe
- if (i % 2 == 0) {
- factSum *= 2 * j;//Formula for even factorial
- ++j;
- }
- else { //Cycle if factorial is odd
- factSum *= (2 * j) - 1;//Formula for odd factorial
- ++j;
- }
- }
- return factSum;
- }
- int main() {
- cout << setprecision(10);//Sets the amount of integers that will be printed
- int n = 0;
- double x, alpha;
- long double sum, a_n, epsilon; sum = 0;
- do {do {//Do while cycle for main function, also do while cycle for x input
- x = read("Write X number, -1 < X > 1", "You wrote incorrect number with letters", "You wrote number with several dots");
- } while (!(x < 1) || !(x > -1));
- alpha = read("Write aplha number", "You wrote incorrect number with letters", "You wrote number with several dots");
- cout << setw(2) << "n" << setw(15) << "a_n" << setw(15) << "sum" << setw(15) << "epsilon" << endl;//Starts the table input
- if (dotCount == 0) {
- while (n <= alpha) { //Cycle for count a_n, sum and epsilon if alpha is integer
- a_n = double_fact(2 * n + 1)*pow(x, 2 * n) / double_fact(2 * n);
- sum += a_n;
- epsilon = (double_fact(2 * (n + 1) + 1)*pow(x, 2 * (n + 1)) / double_fact(2 * (n + 1))) / sum;
- cout << setw(2) << n << setw(15) << a_n << setw(15) << sum << setw(15) << epsilon << endl;
- n++;
- }
- }
- else {
- do {//Cycle for counting elements if alpha is fractional
- a_n = double_fact(2 * n + 1)*pow(x, 2 * n) / double_fact(2 * n);
- sum += a_n;
- epsilon = (double_fact(2 * (n + 1) + 1)*pow(x, 2 * (n + 1)) / double_fact(2 * (n + 1))) / sum;
- cout << setw(2) << n << setw(15) << a_n << setw(15) << sum << setw(15) << epsilon << endl;
- n++;
- } while (alpha < epsilon);
- }
- n = 0, sum = 0, epsilon = 0;
- }while (abs(readChar("Continue (Y/N)?") - 105) == 16);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement