计算方程根的程序的奇怪行为

时间:2019-06-01 14:47:03

标签: c# wpf algorithm

我必须为学校编写一个程序,在指定的时间间隔内计算方程ln x-x + 2 = 0的根。

一切正常,我找到了算法,但是程序确实为以下值显示“ nu avem solutii”(这意味着我们没有解决方案): a = 0,b> =7。我似乎不明白为什么,因为0.15something是针对= 0和b <7显示的解决方案。

代码如下:

<Window x:Class="mate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:mate"
        mc:Ignorable="d"
        Title="Calculator radacini" Height="450" Width="800">
    <Grid>
        <TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" Text="Calculator radacini - Octavian Niculescu, 11C" VerticalAlignment="Top" FontFamily="Proxima Nova Alt Rg"/>
        <TextBlock HorizontalAlignment="Center" Margin="0,50,0,0" TextWrapping="Wrap" Text="f:[a, b] -> R, f(x) = ln x - x + 2" VerticalAlignment="Top" FontFamily="Proxima Nova Alt Rg"/>
        <Image x:Name="grafic_functie" HorizontalAlignment="Center" Height="205" VerticalAlignment="Center" Width="343" Source="Untitled.png"/>
        <TextBox x:Name="input" HorizontalAlignment="Center" Height="20" TextWrapping="Wrap" Text="Introdu a, b, intregi" VerticalAlignment="Top" Width="120" Margin="0,71,0,0" FontFamily="Proxima Nova Alt Rg"/>
        <Button HorizontalAlignment="Center" Content="Calculeaza radacinile!" Margin="0,0,0,45" VerticalAlignment="Bottom" Width="155" FontFamily="Proxima Nova Alt Rg" Click="Button_Click"/>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace mate
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string str;
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            str = input.Text;
            string[] cifre = str.Split(' ');
            float a = float.Parse(cifre[0]);
            float b = float.Parse(cifre[1]);
            float c, x=0, y;
            int k = 0, ok=1;
            double n = Math.Floor(Math.Log(Math.Pow(10, 5) * (b - a), 2)) + 1;
            while(k<=n)
            {
                c = (a + b) / 2;
                if ((Math.Log(a) - a + 2) * (Math.Log(c) - c + 2) < 0)
                {
                    b = c;
                }
                else
                {
                    a = c;
                }
                k++;
            }
            if ((Math.Log(a) - a + 2) * (Math.Log(b) - b + 2) > 0)
            {
                ok++;
            }
            x = (a + b) / 2;

            a = float.Parse(cifre[1]);
            b = float.Parse(cifre[0]);
            k = 0;
            n = Math.Floor(Math.Log(Math.Pow(10, 5) * (b - a), 2)) + 1;
            while (k <= n)
            {
                c = (a + b) / 2;
                if ((Math.Log(a) - a + 2) * (Math.Log(c) - c + 2) < 0)
                {
                    b = c;
                }
                else
                {
                    a = c;
                }
                k++;
            }
            y = (a + b) / 2;
            if ((Math.Log(a) - a + 2) * (Math.Log(b) - b + 2) > 0)
            {
                ok++;
            }
            if(b>a || a==b)
            {
                MessageBox.Show("Intervalul este invalid!");
            }
            if(ok==1)
            {
                MessageBox.Show(x.ToString());
            }
            else if (ok == 2)
            {
                MessageBox.Show(x.ToString() + ", " + "3.1461932");
            }
            else
            {
                MessageBox.Show("Nu avem solutii!");
            }
        }
    }
}

那为什么当a = 0且b> = 7时什么也没发生呢? 谢谢。

0 个答案:

没有答案