Commit 72883923 authored by KTKadyshev's avatar KTKadyshev
Browse files

Исходный код программы

parent fb737fe9
#include <iostream>
#include <cmath>
long factorial(long n) {
return (n==0) || (n==1) ? 1 : n*factorial(n-1);
}
double g(double x, int i) {
return std::pow(x, i)*factorial(i)/factorial(2*i+1);
}
double Fx(double x, int n)
{
double fx = 0;
for(int i = 1; i <= n; i++) {
fx = fx + g(x, i);
}
return fx;
}
const char* getTextFileName() {
return "file.txt";
}
const char* getBinaryFileName() {
return "file.bin";
}
bool writeToTextFile(double v) {
FILE* f = fopen(getTextFileName(), "wt+");
bool isOK = fprintf(f, "%lf", v) > 0;
fclose(f);
return isOK;
}
bool writeToBinaryFile(double v) {
FILE* f = fopen(getBinaryFileName(), "w+");
bool isOK = fwrite(&v, sizeof(v), 1, f) == 1;
fclose(f);
return isOK;
}
double readFromTextFile() {
FILE* f = fopen(getTextFileName(), "rt");
double v=0;
bool isOK = fscanf(f, "%lf", &v) > 0;
fclose(f);
return v;
}
double readFromBinaryFile() {
double v = 0;
FILE* f = fopen(getBinaryFileName(), "r");
bool isOK = fread(&v, sizeof(v), 1, f) == 1;
fclose(f);
return v;
}
int main(int argc, const char * argv[]) {
double x;
int count;
std::cout << "x=";
std::cin >> x;
std::cout << "count=";
std::cin >> count;
std::cout << Fx(x, count) << std::endl;
double fx = Fx(x, count);
writeToTextFile(fx);
writeToBinaryFile(fx);
double fxt = readFromTextFile();
double fxb = readFromBinaryFile();
std::cout << "from text = " << fxt << std::endl;
std::cout << "from binary = " << fxb << std::endl;
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment