zoukankan      html  css  js  c++  java
  • 1028: C语言程序设计教程(第三版)课后习题8.2

    题目描述

    求方程 的根,用三个函数分别求当b^2-4ac大于0、等于0、和小于0时的根,并输出结果。从主函数输入a、b、c的值。

    输入

    a b c

    输出

    x1=? x2=?

    样例输入

    4 1 1

    样例输出

    x1=-0.125+0.484i x2=-0.125-0.484i


     1 #include <stdio.h>
     2 #include <math.h>
     3 
     4 // delta = 0
     5 void delta_eq_0(int a, int b, int c)
     6 {
     7     float res = (float)(0 - b) / (2 * a);
     8     printf("x1=%.3f x2=%.3f
    ", res, res);
     9 }
    10 
    11 // delta > 0
    12 void delta_gt_0(int a, int b, int c)
    13 {
    14     int delta = b * b - 4 * a * c;
    15     float res1, res2;
    16     res1 = (-b+sqrt(delta)) / (2 * a);
    17     res2 = (-b-sqrt(delta)) / (2 * a);
    18     printf("x1=%.3f x2=%.3f
    ", res1, res2);
    19 }
    20 
    21 // delta < 0
    22 void delta_lt_0(int a, int b, int c)
    23 {
    24     int delta = b * b - 4 * a * c;
    25     float res1, res2;
    26     res1 = (float)(0 - b) / (2 * a);
    27     res2 = sqrt(abs(delta)) / (2 * a);
    28     printf("x1=%.3f+%.3fi x2=%.3f-%.3fi
    ", res1, res2, res1, res2);
    29 }
    30 
    31 // 计算结果
    32 void caculate(int a, int b, int c)
    33 {
    34     int delta = b * b - 4 * a * c;
    35     
    36     if(delta == 0)    
    37         delta_eq_0(a, b, c);
    38     else if(delta > 0)
    39         delta_gt_0(a, b, c);
    40     else        
    41         delta_lt_0(a, b, c);
    42 }
    43 
    44 
    45 int main(int argc, char const *argv[])
    46 {
    47 
    48     int a, b, c;
    49     scanf("%d%d%d", &a, &b, &c);
    50     caculate(a, b, c);
    51     return 0;
    52 }
  • 相关阅读:
    LeetCode "Super Ugly Number" !
    LeetCode "Count of Smaller Number After Self"
    LeetCode "Binary Tree Vertical Order"
    LeetCode "Sparse Matrix Multiplication"
    LeetCode "Minimum Height Tree" !!
    HackerRank "The Indian Job"
    HackerRank "Poisonous Plants"
    HackerRank "Kundu and Tree" !!
    LeetCode "Best Time to Buy and Sell Stock with Cooldown" !
    HackerRank "AND xor OR"
  • 原文地址:https://www.cnblogs.com/hello-lijj/p/7851299.html
Copyright © 2011-2022 走看看