那位大大 帮忙啊 急 用c语言做 给定程序功能是计算S=f(-n)+f(-n+1)+…+f(0)+f(1)+f(2)+…+f(n)的值

来源:百度知道 编辑:UC知道 时间:2024/05/22 00:32:51
给定程序功能是计算S=f(-n)+f(-n+1)+…+f(0)+f(1)+f(2)+…+f(n)的值。
例如, 当n为5时, 函数值应为: 10.407143。
┌(x+1)/(x-2) x>0
f(x) = ┤0 x=0 或 x=2
└(x-1)/(x-2) x<0

这就是题目的全部了,请帮帮忙~~

计算过程分成4段,
f(-n)到f(-1);
f(0);
f(1);
f(2);
f(3)到f(n);
f(-n)到f(-1)你试下这个:
using System;

public class program
{
public static void Main()
{
float s1, s2=0, s3, s4=0, s=0;
int i, j;
int n;
string str;
Console.WriteLine("请输入要计算的整数:");
str = Console.ReadLine();
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
byte[] bytestr = ascii.GetBytes(str);
n = Convert.ToInt32(str);
if (n >= 1)
{
for (i = 1; i <= n; i++)
{
s1 = (((float)-i - 1) / ((float)-i - 2));
s2 = s2 + s1;
}
s3 = (1 + 1) / (1 - 2);
for (j = 3; j <= n;j++ )
{
s4 += ((float)j + 1) / ((float)j - 2);
}