?? st-l5.cpp
字號:
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip.h>
#include <ctype.h>
#include <string.h>
typedef float(*pFunc)(float);
////Root of equation////
float root(pFunc F,float a,float b,float eps)
{
float Fa,Fc,c;
Fa = (*F)(a);
c= (a+b)/2;
Fc=(*F)(c);
if (fabs(Fc)<eps) return c;
else if(Fa*Fc>0) return root(F,c,b,eps);
else return root(F,a,c,eps);
}
////Function f1////
float f1(float x)
{
return pow(x,2)-16;
}
////Function f2////
float f2(float x)
{
return pow(x,3)+ 2*pow(x,2)- 8;
}
////Function f3////
float f3(float x)
{
return pow(x,4) - 2*pow(x,3) - 2;
}
////Check real number for string str////
int check_num(char str[20])
{
int i,k=0,OK=1;
if ((str[0]=='+')|(str[0]=='-')|(isdigit(str[0])))
{
for(i=1;str[i];i++)
{
if(!((isdigit(str[i]))|(str[i]==',')|(str[i]=='.')))
{
OK=0;break;
}
else
{
if((str[i]==',')|(str[i]=='.')) k++;
if ((k > 1)|(str[strlen(str)-1]==',')|(str[strlen(str)-1]=='.'))
{
OK=0;break;
}
}
} //end of for
if ((k==1)&&((str[0]=='+')|(str[0]=='-'))&&(!isdigit(str[1])))
OK = 0;
}
else OK=0;
return OK;
}
////Input data////
void input(pFunc F,float &a,float &b,float &eps)
{
char str[20];
int OK;
cout <<" \n------------------------INPUT---------------------\n";
//// Input a ////
do
{
cout <<" \n-Input value of a : a = ";gets(str);
OK=check_num(str);
if (!OK) cout <<" Error input value of a. Please try again!\n";
}
while(!OK);
a = atof(str);
//// Input b ////
do
{
cout <<" \n-Input value of b : b = ";gets(str);
OK=check_num(str);
if(OK)
{
b = atof(str);
if ((b <= a)|((*F)(b)*(*F)(a)>0)) OK=0;
}
if (!OK) cout <<" Error input value b > a(f(b)*f(a)<0). Please try again!\n";
}
while(!OK);
b = atof(str);
//// Input precision ////
do
{
cout <<" \n-Input value of precision(Eps > 0) : Eps = ";gets(str);
OK=check_num(str);
if(OK)
{
eps = atof(str);
if (eps <= 0) OK=0;
}
if (!OK) cout <<" Error input value precision(Eps > 0). Please try again!\n";
}
while(!OK);
eps = atof(str);
}
////MAIN////
void main()
{
float a,b,eps,rootF;
int choice,k;
do
{
clrscr();
cout <<"Program to find roots of equation with dividual in two method on interval (a,b).\n" ;
cout <<" Select an equation : \n\n";
cout <<" 1.x^2 - 16 = 0\n\n";
cout <<" 2.x^3 + 2x^2 - 8 = 0\n\n";
cout <<" 3.x^4 - 2x^3 - 2 = 0\n\n";
cout <<" 4.Press any key to exit...\n\n";
cout <<" You select : ";choice = getch();
switch (choice) {
case '1' : cout <<" 1.x^2 - 16 = 0\n";
input(f1,a,b,eps);
rootF = root(f1,a,b,eps);
break;
case '2' : cout <<" 2.x^3 + 2x^2 - 8 = 0\n";
input(f2,a,b,eps);
rootF = root(f2,a,b,eps);
break;
case '3' : cout <<" 3.x^4 - 2x^3 - 2 = 0\n";
input(f3,a,b,eps);
rootF = root(f3,a,b,eps);
break;
default :
cout <<" exit!";
getch();
exit(1);
}
cout <<"\n---ROOT OF EQUATION WITH DIVIDUAL IN TWO METHOD---\n";
cout <<setiosflags(ios::showpoint) <<setprecision(5);
cout <<"\n Root of equation : x = " <<rootF;
cout <<"\n\n Do you want to continue ?(Y/N)";
k=getch();
}
while((k=='y')|(k=='Y'));
} ////END OF MAIN
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -