当前位置:网站首页>求方程ax^2+bx+c=0的根(C语言)
求方程ax^2+bx+c=0的根(C语言)
2022-07-07 08:12:00 【璞玉牧之】
一、题目
求方程ax2+bx+c=0的根,用3个函数分别求当:bx2-4ac大于0、等于0和小于0时的根并输出结果。从主函数输入a,b,c的值。
二、代码及结果
1.代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float x1,x2,t,p,q;
int main()
{
void Greater_than_Zero(float a,float b);
void Equal_to_Zero(float a,float b);
void Smaller_than_zero(float a,float b);
float a,b,c;
printf("请输入a,b,c的值:\n");
scanf("%f,%f,%f",&a,&b,&c);
printf("解方程:%4.2f*x*x+%4.2f*x+%4.2f=0\n",a,b,c);
t=b*b-4*a*c;
if(t>0)
{
Greater_than_Zero(a,b);
printf("x1=%f\nx2=%f\n",x1,x2);
}
else if(t==0)
{
Equal_to_Zero(a,b);
printf("x1=x2=%f\n",x1);
}
else
{
Smaller_than_zero(a,b);
printf("x1=%f+%fi\nx2=%f-%fi\n",p,q,p,q);
}
return 0;
}
void Greater_than_Zero(float a,float b)
{
x1=(-b+sqrt(t))/(2*a);
x2=(-b+sqrt(t))/(2*a);
}
void Equal_to_Zero(float a,float b)
{
x1=x2=(-b)/(2*a);
}
void Smaller_than_zero(float a,float b)
{
p=-b/(2*a);
q=sqrt(-t)/(2*a);
}
2.运行结果
(1)两个不等的实根

(2)两个相等的实根

(3)两个共轭的复根

边栏推荐
- Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
- Easyexcel read write simple to use
- Guide de signature du Code Appx
- Inno Setup 打包及签名指南
- ArcGIS operation: batch modify attribute table
- Postman tutorial - scripting
- Leetcode exercise - 113 Path sum II
- C logging method
- Memory ==c language 1
- When there are pointer variable members in the custom type, the return value and parameters of the assignment operator overload must be reference types
猜你喜欢

反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释

Appx code signing Guide

SQLyog数据库怎么取消自动保存更改

Postman interface test II

The request object parses the request body and request header parameters

LeetCode 练习——113. 路径总和 II

Methods of adding centerlines and centerlines in SolidWorks drawings

ArcGIS operation: converting DWG data to SHP data

Bean operation domain and life cycle

PDF文档签名指南
随机推荐
ArcGIS operation: batch modify attribute table
Appx代碼簽名指南
EasyExcel读取写入简单使用
大整数类实现阶乘
Why does the starting service report an error when installing MySQL? (operating system Windows)
【华为机试真题详解】高矮个子排队
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
VS Code指定扩展安装位置
嵌入式工程师如何提高工作效率
Embedded background - chip
一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
Google Colab装载Google Drive(Google Colab中使用Google Drive)
Postman interface test III
STM32基础知识—内存映射
Postman tutorial - scripting
Postman interface test I
LeetCode 练习——113. 路径总和 II
SQLyog数据库怎么取消自动保存更改
ES6中的原型对象
Some thoughts on the testing work in the process of R & D