Skip to content

Instantly share code, notes, and snippets.

@LifeMoroz
Created September 22, 2012 13:49
Show Gist options
  • Save LifeMoroz/3766218 to your computer and use it in GitHub Desktop.
Save LifeMoroz/3766218 to your computer and use it in GitHub Desktop.
Lab4
#include <math.h>
#include <windows.h>
#include <iostream>
using namespace std;
int n;
double f(double x)
{
return x - n*cos(x);
}
void first()
{
double x=0, eps;
int i=0;
cout << "Укажите точность" << endl;
cin >> eps;
while(abs(f(x))>=eps)
{
x=f(x);
i=i+1;
}
cout << "X=" << x << endl;
cout << "Количество Шагов " << i << endl;
}
void second()
{
int n=0;
double a,b,c,eps;
cout<<"a="; cin>>a;
cout<<"b="; cin>>b;
cout<<"eps="; cin>>eps;
do {
c=(a+b)/2;
if (f(c)*f(a)<=0) b=c;
else a=c;
n++;
}
while (fabs(a-b)>=eps);
cout<<"c="<<c<<"\n";
cout<<"n="<<n<<"\n";
system("pause");
}
void third()
{
int n=0;
double a,b,c,eps;
cout<<"a=" << end; cin>>a;
cout<<"b=" << end; cin>>b;
cout<<"eps="; cin>>eps;
do {
c=(a+b)/2;
if (f(c)*f(a)<=0) b=c;
else a=c;
n++;
}
while (fabs(a-b)>=eps);
cout<<"c="<<c<<endl;
cout<<"n="<<n<<endl;
}
int main()
{
setlocale(0,"rus");
char k;
do
{
cout << "Какое задание выполнить?" << endl
<< "Точность указыватся в ходе выполнения задания." <<endl
<< "Для x - cos(x) введите 1, для x - 10*cos(x) введите 10." << endl;
cin >> n;
cout << "Укажите способ, которым будем считать"<< endl;
cout << "Метод просты итераций введите 1"<< endl;
cout << "Метод Ньютона введите 2"<< endl;
cout << "Метод половинного деления введите 3"<< endl;
cin >> k;
switch (k)
{
case 1:
first();
break;
case 2:
second();
break;
case 3:
third();
break;
}
while (1)
{
cout << "Повторить? [y/n]"<< endl;
cin >>k;
if(k!= 'y' || k!= 'Y' || k!= 'n' || k!='N')
{
cout << "Ошибка Ввода!" << endl;
} else {break;}
}
}
while(k == 'y' || k == 'Y');
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment