编程求一元二次方程 上机作业

  1. // Copyright (c) 2014软件技术2班      
  2. // All rights reserved.       
  3. // 作    者:B38        
  4. // 完成日期:2014年 10 月 26 日       
  5. // 版 本 号:v1.0          
  6. // 问题描述:创建一个程序求一元二次方程  
  7. //输入描述:任意输入 a b c 值,根据公式计算x1,x2并输出  
  8.  
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  

  9. namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
  10.             double x1 = 0, x2 = 0;  //定义double类型,名称x1.x2初始化为0  
                double a, b, c, dt;//定义变量
                Console.Title = ("求一元二次方程");
                Console.WriteLine("ax^2+bx=0");
                Console.WriteLine("请输入a的值:");
                a = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("请输入b的值:");
                b = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("请输入c的值:");
                c = Convert.ToDouble(Console.ReadLine());//输入数据
                dt = b * b - 4 * a * c;                       //Δ的公式  
                if (dt > 0)                   //判断Δ  
                {
                    x1 = (-b + Math.Sqrt(dt)) / 2.0 * a;   //一元二次方程公式输出
      
                    x2 = (-b - Math.Sqrt(dt)) / 2.0 * a;
                    Console.WriteLine("有两个实数根");
                    Console.WriteLine("x1={0}\nx2={1}", x1, x2);
                }
                else if (dt == 0)
                {
                    x1 = x2 = -b / (2 * a);      //一元二次方程公式输出  
                    Console.WriteLine("x1=x2={0}", x1);
                    Console.WriteLine("仅一个解");
                }


                else
                {
                    Console.WriteLine("无解");


                }




                Console.Read();
            }
        }
    } 

输出:



总结:

1.通过本作业我学会了C#的编程知识

2.我学会了用编程知识求一元二次方程的问题,增长了经验,收获了自信,感到了快乐

3.我发现C#编程还蛮好玩的,以后还需加强编程的各种能力

4.编程不仅可以用来控制显示的数据还可以用来求解



你可能感兴趣的:(上机作业)