• c#部分---用函数的四种格式做一元二次方程


    格式一:(无参无返)

     public void fangcheng() 
            {
                Console.WriteLine("请输入a的值");
                double a = int.Parse(Console.ReadLine());
                Console.WriteLine("请输入b的值");
                double b = int.Parse(Console.ReadLine());
                Console.WriteLine("请输入c的值");
                double c = int.Parse(Console.ReadLine());
                double de = b * b - 4 * a * c;
                if (a != 0)
                {
                    if (de > 0)
                    {
                        double x1 = ((-b) + Math.Sqrt(de)) / 2 * a;
                        double x2 = ((-b) - Math.Sqrt(de)) / 2 * a;
                        Console.WriteLine("方程有两个解,分别是{0},{1}", x1, x2);
                    }
                    else if (de == 0)
                    {
                        double x = (-b) / 2 * a;
                        Console.WriteLine("此方程有一个实数解,是{0}", x);
                    }
                    else
                    {
                        Console.WriteLine("此方程无解!");
                    }
                }
                else
                {
                    Console.Write("此方程不是一元二次方程");
    
                }
            
            
            
            
            }

    //格式1
    //Program hanshu = new Program();
    //hanshu.fangcheng();

     

    格式二:有参无返

     public void fangcheng1(double a,double b,double c) 
            {
                double de = b * b - 4 * a * c;
                if (a != 0)
                {
                    if (de > 0)
                    {
                        double x1 = ((-b) + Math.Sqrt(de)) / 2 * a;
                        double x2 = ((-b) - Math.Sqrt(de)) / 2 * a;
                        Console.WriteLine("方程有两个解,分别是{0},{1}", x1, x2);
                    }
                    else if (de == 0)
                    {
                        double x = (-b) / 2 * a;
                        Console.WriteLine("此方程有一个实数解,是{0}", x);
                    }
                    else
                    {
                        Console.WriteLine("此方程无解!");
                    }
                }
                else
                {
                    Console.Write("此方程不是一元二次方程");
    
                }
            
            
            
            }
      //格式2
              // Program hanshu = new Program();
              // hanshu.fangcheng1(a,b,c);

    格式三:(有参有返)

     public string fangcheng2(double a, double b, double c,out string jie) 
            {
               
                double de = b * b - 4 * a * c;
                
                if (a != 0)
                {
                    if (de > 0)
                    {
                        double x1 = ((-b) + Math.Sqrt(de)) / 2 * a;
                        double x2 = ((-b) - Math.Sqrt(de)) / 2 * a;
                      jie="方程有两个解,分别是"+ x1+ x2;
                    }
                    else if (de == 0)
                    {
                        double x = (-b) / 2 * a;
                        jie = "此方程有一个实数解,是{0}" + x;
                    }
                    else
                    {
                       jie="此方程无解!";
                    }
                }
                else
                {
                    jie="此方程不是一元二次方程";
    
                }
    
                return jie;
            }
    
     //格式3
               
                //Console.WriteLine("请输入a的值");
                //double a = int.Parse(Console.ReadLine());
                //Console.WriteLine("请输入b的值");
                //double b = int.Parse(Console.ReadLine());
                //Console.WriteLine("请输入c的值");
                //double c = int.Parse(Console.ReadLine());
                //string jie;
                //Program hanshu = new Program();
                //hanshu.fangcheng2(a,b,c,out jie);
                //Console.WriteLine(jie);
            

    格式四:(无参有返)

     public string fangcheng3()
            {
                Console.WriteLine("请输入a的值");
                double a = int.Parse(Console.ReadLine());
                Console.WriteLine("请输入b的值");
                double b = int.Parse(Console.ReadLine());
                Console.WriteLine("请输入c的值");
                double c = int.Parse(Console.ReadLine());
                double de = b * b - 4 * a * c;
                string jie;
    
                if (a != 0)
                {
                    if (de > 0)
                    {
                        double x1 = ((-b) + Math.Sqrt(de)) / 2 * a;
                        double x2 = ((-b) - Math.Sqrt(de)) / 2 * a;
                        jie = "方程有两个解,分别是" + x1 + x2;
                    }
                    else if (de == 0)
                    {
                        double x = (-b) / 2 * a;
                        jie = "此方程有一个实数解,是{0}" + x;
                    }
                    else
                    {
                        jie = "此方程无解!";
                    }
                }
                else
                {
                    jie = "此方程不是一元二次方程";
    
                }
    
                return jie;
            }
    
     //格式4
                //Program hanshu = new Program();
                //string jie=hanshu.fangcheng3();
    
                //Console.WriteLine(jie);
  • 相关阅读:
    js optional chaining operator
    Linux Bash Script conditions
    Linux Bash Script loop
    js Nullish Coalescing Operator
    js sort tricks All In One
    React Portal All In One
    最小生成树&&次小生成树
    链式前向星实现以及它的遍历
    [2015.11.8|9图论]解题代码集合
    最短路算法及其延伸
  • 原文地址:https://www.cnblogs.com/xingyue1988/p/5977463.html
Copyright © 2020-2023  润新知