Вычисление значения арифметического выражения - C#
Формулировка задачи:
1 По нажатию кнопки реализовать вычисление значения арифметического выражения C=2*x4-3*x3-5*x+6, и вывести его в TextBox, подписав его Laible’ом с текстом этого выражения.
2.В RadioButton вывести: каким методом решалось уравнение, после нажатия кнопки и ввода координаты х.
Листинг программы
- double x, y;
- static void Main()
- {
- Double a, b, c, D, x1, x2;
- Console.WriteLine("..............................................................");
- Console.WriteLine("Программа вычисления квадратного корня с помощью дискриминанта");
- Console.ReadLine();
- Console.WriteLine("Введите а, b, c");
- Console.ForegroundColor = ConsoleColor.Yellow;
- a = Convert.ToDouble(Console.ReadLine());
- b = Convert.ToDouble(Console.ReadLine());
- c = Convert.ToDouble(Console.ReadLine());
- D = b * b - 4 * a * c;
- if (D >= 0)
- {
- x1 = (-b + Math.Sqrt(D)) / (2 * a);
- x2 = (-b - Math.Sqrt(D)) / (2 * a);
- Console.WriteLine("X1 ={0} X2 = {1}", x1, x2);
- }
- else
- {
- Console.WriteLine("Корней нет");
- }
- Console.ReadLine();
- Console.Read();
- }
- }
Решение задачи: «Вычисление значения арифметического выражения»
textual
Листинг программы
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Globalization;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- namespace Program
- {
- public class MainForm : Form
- {
- public MainForm()
- {
- InitializeComponents();
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- components.Dispose();
- base.Dispose(disposing);
- }
- private void InitializeComponents()
- {
- components = new Container();
- //labelX
- labelX = new Label();
- labelX.Name = "labelX";
- labelX.Location = new Point(60, 12);
- labelX.Size = new Size(65, 30);
- labelX.Text = "Введите X:";
- //labelResult
- labelResult = new Label();
- labelResult.Name = "labelResult";
- labelResult.Location = new Point(10, 42);
- labelResult.Size = new Size(120, 30);
- labelResult.Text = "2x^4 - 3x^3 - 5x + 6 = ";
- //textboxX
- textboxX = new TextBox();
- textboxX.Name = "textboxX";
- textboxX.Location = new Point(130, 10);
- textboxX.Size = new Size(150, 30);
- //textboxResult
- textboxResult = new TextBox();
- textboxResult.Name = "textboxResult";
- textboxResult.Location = new Point(130, 40);
- textboxResult.Size = new Size(150, 30);
- textboxResult.Enabled = false;
- textboxResult.BackColor = Color.FromName("White");
- //labelError
- labelError = new Label();
- labelError.Name = "labelError";
- labelError.Location = new Point(85, 75);
- labelError.Size = new Size(150, 30);
- labelError.ForeColor = Color.FromName("Red");
- labelError.Text = "";
- //buttonSubmit
- buttonSubmit = new Button();
- buttonSubmit.Name = "buttonSubmit";
- buttonSubmit.Location = new Point(110, 110);
- buttonSubmit.Size = new Size(100, 30);
- buttonSubmit.Click += new EventHandler(buttonSubmit_Click);
- buttonSubmit.Text = "Рассчитать";
- //this
- this.Text = "Решение уравнения";
- this.Size = new Size(320, 200);
- this.Controls.Add(labelX);
- this.Controls.Add(labelResult);
- this.Controls.Add(textboxX);
- this.Controls.Add(textboxResult);
- this.Controls.Add(labelError);
- this.Controls.Add(buttonSubmit);
- }
- private void buttonSubmit_Click(object sender, EventArgs e)
- {
- labelError.Text = "";
- string pattern =@"^[0-9]+[.,]?[0-9]+$";
- Regex rgx = new Regex(pattern);
- Match match = rgx.Match(textboxX.Text);
- if (String.Empty == match.Value)
- {
- labelError.Text = "Неверный формат данных!";
- return;
- }
- textboxX.Text = textboxX.Text.Replace(".", ",");
- double x = Convert.ToDouble(textboxX.Text, CultureInfo.CurrentCulture);
- double res = 2 * Math.Pow(x, 4) - 3 * Math.Pow(x, 3) - 5 * x + 6;
- textboxResult.Text = res.ToString();
- }
- private IContainer components = null;
- private Label labelX;
- private TextBox textboxX;
- private Label labelResult;
- private TextBox textboxResult;
- private Label labelError;
- private Button buttonSubmit;
- }
- public class Program
- {
- [STAThread]
- public static void Main(string[] args)
- {
- Application.EnableVisualStyles();
- Application.Run(new MainForm());
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д