Krsy
2021-09-21 16:15
采纳率: 25%
浏览 145

大一新生c语言编程题不会写啊

img


怎么把他这个不同的结果写出来啊,而且这个虚数用什么数据类型啊

  • 好问题 提建议
  • 收藏

1条回答 默认 最新

  • 广大菜鸟 2021-09-21 20:17
    已采纳
    #include "stdio.h"
    #include<math.h>
    #include<stdlib.h>
    typedef struct complex
    {
        double _Val[2]; //real、img
    }complex;
    complex* getResult(double a,double b,double c,complex*res){
        double delta=b*b-4*a*c;
        if(delta>=0){
            res[0]._Val[0] = (-b+sqrt(delta))/(2*a);
            res[0]._Val[1] = 0;
            res[1]._Val[0] = (-b-sqrt(delta))/(2*a);
            res[1]._Val[1] = 0;
        }else{
            double absDelta = -delta;
            double tmp = sqrt(absDelta)/(2*a);
            res[0]._Val[0] = -b/(2*a);
            res[0]._Val[1] = tmp;
            res[1]._Val[1] = -tmp;
            res[1]._Val[0] = -b/(2*a);
        }
        return res;
    }
    int main(){
        double a,b,c;
        complex res[2];
        while(scanf("%lf,%lf,%lf",&a,&b,&c)!=EOF){
            if(a==0){
                printf("The equation is not a quadratic\n");
                continue;
            }
            getResult(a,b,c,res);
            if(res[0]._Val[1]!=0){//复数
            printf("The quation has complex real roots:\n");
            printf("%.4lf+%.4lfi\n",res[0]._Val[0],res[0]._Val[1]);
            printf("%.4lf%.4lfi\n",res[1]._Val[0],res[1]._Val[1]);
            }else if(res[0]._Val[0]!=res[1]._Val[0]){        
                printf("The quation has distinct real roots:%.4lf and %.4lf\n",res[0]._Val[0],res[1]._Val[0]);
            }else{
                printf("The quation has two equal real roots:%.4lf\n",res[0]._Val[0]);
            }     
        }
        system("pause");
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题