//#include //#include //using namespace std; #include #include #include #include #include class lab1 { private: double x, y, yt, dx, eps; double x1, x2; double **valuesXY; int size; public: int factorial(int n) { if (n > 0) return n * this->factorial(n - 1); return 1; } double getFormula(int i) { /*cout << "x = " <x; cout << "i = " << i; cout << "y111 = " << (pow(-1, i) * ((pow(this->x, 2 * i)) / this->factorial(2 * i)));*/ return (pow(-1, i) * ((pow(this->x, 2 * i)) / this->factorial(2 * i))); } double calculateY() { double sum = 0; int i = 1; double ai = 0, lastAi = 0; do { lastAi = ai; ai = this->getFormula(i); sum += ai; //cout << "ai = " << ai; i++; } while (fabs(ai - lastAi) > eps); return sum; } void calculateXY() { int i = 0; for (this->x = this->x1; this->x < this->x2; this->x += this->dx, i++) { this->valuesXY[0][i] = this->x; this->valuesXY[1][i] = this->calculateY(); } } lab1(double x1, double x2, double dx, double eps) { this->x1 = x1; this->x2 = x2; this->dx = dx; this->eps = eps; this->size = (this->x2 - this->x1)/this->dx+1; this->valuesXY = (double**)malloc(sizeof(double*)*2); for(int i = 0; i < 2; i++) this->valuesXY[i] = (double*)malloc(sizeof(double*)*size); this->calculateXY(); } void calculateYt() { this->yt = cos(this->x) - 1; } double getMistake() { double y = 0; if(this->y != 0) y = (this->y - this->yt) / this->y * 100; return y; } void printTable() { int N = 0; cout << "+---------+-------------+-------------+--------------+--------+\n"; cout << "| x | y | yt | mistake | N |\n"; cout << "+---------+-------------+-------------+--------------+--------+\n"; for (int i = 0; i < (this->size)/2; i++, N++) { cout << "| "; cout.precision(2); cout.width(4); cout << this->valuesXY[0][i] << " | "; cout.precision(2); cout.width(7); cout << this->valuesXY[1][i] << " | "; cout.precision(2); cout.width(7); this->calculateYt(); cout << this->yt << " | "; cout.width(8); cout << this->getMistake() << " | "; cout.width(2); cout << N << " |\n"; //break; } cout << "+---------+-------------+-------------+--------------+--------+\n"; } int getSize() { return this->size; } double **getValuesXY() { return this->valuesXY; } }; class graph { private: double x,y,dx,dy,k; double **valuesXY; int size; void setK(double k) { this->k = k; } void initGraph() { int gDriver = DETECT, gMode; initgraph(&gDriver,&gMode,"d:/bc/bgi"); } public: graph(double **valuesXY, int size) { this->initGraph(); this->valuesXY = valuesXY; this->size = size; this->setK(1); } void printAxis() { int midX = getmaxx()/2; int midY = getmaxy()/2; setcolor(WHITE); // x line(0,midY,getmaxx(),midY); // y line(midX,0,midX,getmaxy()); } void printGraph() { double x; double y; double midX = getmaxx()/2; double midY = getmaxy()/2; double maxX = getmaxx(); double maxY = getmaxy(); moveto((*(this->valuesXY[0]+0)), (*(this->valuesXY[1]+0))); for(int i = 0; i < this->size; i++) { x = *(this->valuesXY[0]+i); y = *(this->valuesXY[1]+i); cout << "y = " < 0) && ((midY - y*k) > 0) ) { cout << "la"; */ //lineto(x,y); putpixel(x + midX,midY - y,4); //} } } }; int main() { clrscr(); lab1 obj(0, 1, 0.05, 0.001); obj.printTable(); cin.get(); graph g(obj.getValuesXY(),obj.getSize()); g.printAxis(); g.printGraph(); cin.get(); return 0; }