c++ 求函数和轴围成面积
要求在五个函数中选择f1(x)=2x^5+x^3–10x+2f2(x)=6x^2–x+10f3(x)=5x+3f4(x)=2x^3+120f5(x)=2x^2然后选择具体...
要求在五个函数中选择
f1(x) = 2x^5 + x^3 – 10x + 2
f2(x) = 6x^2 – x + 10
f3(x) = 5x + 3
f4(x) = 2x^3 + 120
f5(x) = 2x^2
然后选择具体是矩形还是梯形方式围成面积计算(分n个点围成矩形面积求和)
写了矩形的发现运算发现答案会得出负数OTL 梯形的不会写 求大神 展开
f1(x) = 2x^5 + x^3 – 10x + 2
f2(x) = 6x^2 – x + 10
f3(x) = 5x + 3
f4(x) = 2x^3 + 120
f5(x) = 2x^2
然后选择具体是矩形还是梯形方式围成面积计算(分n个点围成矩形面积求和)
写了矩形的发现运算发现答案会得出负数OTL 梯形的不会写 求大神 展开
1个回答
展开全部
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "iostream"
#include "math.h"
using namespace std;
int n = 0;
double a = 0;
double b = 0;
double (*f)(double) = NULL;
int function = 0;
char functionStr[100] = {0};
int calculate = 0;
double areaRectangle = 0;
double areaTrapezoid = 0;
double f1(double x)
{
return pow(x,5)*2+pow(x,3)-x*10+2;
}
double f2(double x)
{
return pow(x,2)*6-x+10;
}
double f3(double x)
{
return x*5+3;
}
double f4(double x)
{
return pow(x,3)*2+120;
}
double f5(double x)
{
return pow(x,2)*2;
}
double rectangleArea(double length, double height)
{
return length*height;
}
double trapezoidsArea(double length_1, double lenght_2, double height)
{
return (length_1+lenght_2)*height/2;
}
int chooseFunc(int function)
{
switch (function)
{
case 1:
f=f1;
sprintf(functionStr, "2x^5 + x^3 - 10x + 2");
return 1;
case 2:
f=f2;
sprintf(functionStr, "6x^2 - x + 10");
return 1;
case 3:
f=f3;
sprintf(functionStr, "5x + 3");
return 1;
case 4:
f=f4;
sprintf(functionStr, "2x^3 + 120");
return 1;
case 5:
f=f5;
sprintf(functionStr, "2x^2");
return 1;
default:
f=NULL;
return 0;
}
}
int getInput()
{
int ret = 0;
//input function
cout << "Choose a function (1,2,3,4,5,other(quit)):";
cin >> function;
ret = chooseFunc(function);
if(ret == 0)
return 0;
//input calculate
cout << "Would you like to calculate the area using the rectangle, trapezoid, or both (1,2,3):";
cin >> calculate;
//input n
if(calculate == 1)
{
cout << "How many rectangle do you want?" << endl;
}
if(calculate == 2)
{
cout << "How many trapezoid do you want?" << endl;
}
if(calculate == 3)
{
cout << "How many rectangle and trapezoid do you want?" << endl;
}
cin >> n;
if(calculate != 1 && calculate != 2 && calculate != 3)
return 0;
//input a,b
cout << "Please select a starting point, a = ";
cin >> a;
cout << "Please select a ending point, b = ";
cin >> b;
return 1;
}
int calculateArea()
{
double areaRectangle = 0;
double areaTrapezoid = 0;
double x_length = (b-a)/n;
int i = 0;
if(calculate == 1 || calculate == 3)
{
for(i = 0; i < n; i++)
{
areaRectangle += x_length*(*f)(a+i*x_length);
}
cout << "The area under [" << functionStr << "] between " << a << " and " << b <<" is " << areaRectangle << " -- rectangle" << endl;
}
if(calculate == 2 || calculate == 3)
{
for(i = 0; i < n; i++)
{
areaTrapezoid += x_length*((*f)(a+i*x_length)+(*f)(a+(i+1)*x_length))/2;
}
cout << "The area under [" << functionStr << "] between " << a << " and " << b <<" is " << areaTrapezoid << " -- trapezoids" << endl;
}
return 1;
}
int main()
{
int ret = 0;
ret = getInput();
if(ret == 0)
return 0;
ret = calculateArea();
return ret;
}
北京埃德思远电气技术咨询有限公司
2023-08-25 广告
2023-08-25 广告
"整定计算的工作步骤,大致如下:1.确定整定方案所适应的系统情况。2.与调度部门共同确定系统的各种运行方式。3.取得必要的参数与资料(保护图纸,设备参数等)。4.结合系统情况,确定整定计算的具体原则。5.进行短路计算。6.进行保护的整定计算...
点击进入详情页
本回答由北京埃德思远电气技术咨询有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询