Надо было написать программу, которая считает сумму ряда
Пытался сам сделать, вроде неверно. При оюбом отрицательном щначении выдает 0. Посмотрите, пожалуйста, и помогите исправить. Есть ли другие ошибки? Артктангенс я позже рассмотрю
На форму добавил кнопку, лэйбл, ЛэйбледЭдит
Код:
#include <vcl.h>
#include <math.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double sum = 0, x, eps = 0.000001, temp;
int i = 1, n = 1;
double z = pow(-1, n), v = pow(x, 2 * n - 1)/(2 *n -1);
String res = "fabs(x) > 1";
x = StrToFloatDef(LabeledEdit1->Text, 0);
if (fabs(x) <= 1)
{
temp = x;
while (temp >= eps)
{
sum += temp;
i++;
if (i == 2)
{
n = 2;
temp = x*x/2;
}
else
{
n += i;
temp = z * v;
}
}
res = sum;
}
Label1->Caption = res;
}
//---------------------------------------------------------------------------