求一元二次方程

  1. // Copyright (c) 2014软件技术2班      
  2. // All rights reserved.       
  3. // 作    者:B39        
  4. // 完成日期:2014年 10 月 24 日       
  5. // 版 本 号:v1.0          
  6. // 问题描述:创建一个程序求一元二次方程  
  7. //输入描述:任意输入 a b c 值,根据公式计算x1,x2并输出  
  8.   
  9. using System;  
  10. using System.Collections.Generic;  
  11. using System.Linq;  
  12. using System.Text;  
  13. using System.Threading.Tasks;  
  14.   
  15. namespace yiyuanerci 
  16. {  
  17.     class Program  
  18.     {  
  19.         static void Main(string[] args)  
  20.         {  
  21.             double x1 =0 , x2 = 0;  //定义double类型,名称x1.x2初始化为0  
  22.             double a, b, c, dt;  
  23.             Console.Title = ("求一元二次方程");  
  24.             Console.WriteLine("ax^2+bx=0");  
  25.             Console.WriteLine("请输入a的值:");  
  26.             a = Convert.ToInt32(Console.ReadLine());  
  27.             Console.WriteLine("请输入b的值:");  
  28.             b = Convert.ToInt32(Console.ReadLine());  
  29.             Console.WriteLine("请输入c的值:");  
  30.             c = Convert.ToInt32(Console.ReadLine());  
  31.             dt = b * b - 4 * a * c;                       //Δ的公式  
  32.             if (dt > 0)                   //判断Δ  
  33.             {  
  34.                 x1 = (-b + Math.Sqrt(dt)) / 2.0 * a;   //一元二次方程公式  
  35.                 x2 = (-b - Math.Sqrt(dt)) / 2.0 * a;  
  36.                 Console.WriteLine("有两个实数根");  
  37.                 Console.WriteLine("x1={0}\nx2={1}", x1, x2);  
  38.             }  
  39.             else if (dt == 0)  
  40.             {  
  41.                 x1 = x2 = -b / (2 * a);      //一元二次方程公式  
  42.                 Console.WriteLine("x1=x2={0}", x1);  
  43.                 Console.WriteLine("仅一个解");  
  44.             }  
  45.   
  46.             else  
  47.             {  
  48.                 Console.WriteLine("无解");  
  49.   
  50.             }  
  51.             
  52.   
  53.             Console.Read();  
  54.   
  55.   
  56.   
  57.         }  
  58.     }  

    }

           输出:
                      


                                         
                 

                  总结:

                   在这里我学会求一元二次方程的解


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值