Created
September 20, 2012 18:21
-
-
Save ernado/3757513 to your computer and use it in GitHub Desktop.
Lab04 by Zaikov
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <math.h> | |
| #include <windows.h> | |
| #include <iostream> | |
| using namespace std; | |
| ostream& operator <<(ostream& os, const char *out) | |
| { | |
| char *chbuff = new char[strlen(out) + 1]; | |
| CharToOem(out, chbuff); | |
| cout.write(chbuff, strlen(out)); | |
| delete [] chbuff; | |
| return os; | |
| } | |
| ostream& operator <<(ostream& os, const string& out) | |
| { | |
| char *chbuff = new char[out.length() + 1]; | |
| CharToOem(out.c_str(), chbuff); | |
| cout.write(chbuff, out.length()); | |
| delete [] chbuff; | |
| return os; | |
| } | |
| double Fx(double x) | |
| { | |
| return x - cos(x); | |
| } | |
| double pFx(double x) | |
| { | |
| return 1 + sin(x); | |
| } | |
| double Toch() | |
| { | |
| double eps; | |
| cout << "С Какой Точностью Выполнить Вычисления?\n 1- eps=0,000001\n 2- eps=0,00000001\n"; | |
| do | |
| { | |
| cin >> eps; | |
| } while(eps != 1 && eps!= 2); | |
| return eps == 1 ? 0.00000001 : 0.000001; | |
| } | |
| void Inter() | |
| { | |
| double x=0, eps=0; | |
| short chi=0; | |
| eps=Toch(); | |
| while(abs(Fx(x))>=eps) | |
| { | |
| x=x-Fx(x); | |
| chi=chi+1; | |
| } | |
| cout << "X=" << x << endl; | |
| cout << "Количество Шагов " << chi << endl; | |
| } | |
| void Newton() | |
| { | |
| double x=0, eps=0; | |
| short chi=0; | |
| eps=Toch(); | |
| while(abs(Fx(x))>=eps) | |
| { | |
| x=x-Fx(x)/pFx(x); | |
| chi=chi+1; | |
| } | |
| cout << "X=" << x << endl; | |
| cout << "Количество Шагов " << chi << endl; | |
| } | |
| void Polovin() | |
| { | |
| double x1=0, x2=2, x3=0, eps=0; | |
| short chi=0; | |
| eps=Toch(); | |
| while(abs(Fx(x3))>=eps) | |
| { | |
| x3=x1+(x2-x1)/2; | |
| if(abs(Fx(x3))>=eps) | |
| { | |
| if(Fx(x1)*Fx(x3)<0) | |
| { | |
| x2=x3; | |
| } | |
| if(Fx(x3)*Fx(x2)<0) | |
| { | |
| x1=x3; | |
| } | |
| } | |
| chi=chi+1; | |
| } | |
| cout << "X=" << x3 << endl; | |
| cout << "Количество Шагов " << chi << endl; | |
| } | |
| int main() | |
| { | |
| short ch; | |
| char pov; | |
| do | |
| { | |
| cout << "Каим Способом Выполнить Вычисления?(1-3)\n 1- Метод Простой Интерации\n 2- Метод Ньютона\n 3- Метод Половинного Деления\n"; | |
| cin >> ch; | |
| if(ch<1 || ch>3) | |
| { | |
| cout << "Ошибка Ввода!"; | |
| }else{ | |
| if(ch == 1) | |
| { | |
| Inter(); | |
| } | |
| if(ch == 2) | |
| { | |
| Newton(); | |
| } | |
| if(ch == 3) | |
| { | |
| Polovin(); | |
| } | |
| } | |
| cout << "\n Повторить? (Y/N) "; | |
| cin >> pov; | |
| if(pov == 'Y' || pov == 'y' || pov == 'N' || pov =='n') | |
| { | |
| if(pov == 'Y' || pov == 'y') | |
| { | |
| cout << "\n Перезапускаем Программу\n\n"; | |
| } | |
| if(pov == 'N' || pov =='n') | |
| { | |
| cout << "\n Закрываем Программу\n\n"; | |
| } | |
| }else{ | |
| cout << "\n Ошибка Ввода!\n\n"; | |
| } | |
| } while (pov=='y' || pov =='Y'); | |
| system("pause"); | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment