一元二次方程式求解

C++基礎語法

這是一段C++基礎語法應用
包括:輸出,輸入,使用math.h函示庫,IF的判斷。 

// EquationSolution.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <math.h>//使用math.h函示庫 為了計算開根號

int main()
{
	printf("We want to calutate aX^2 +bX+c=0. X value.Please enter a,b,c value\n");
    //宣告整數變數 a,b,c賦予預設值0
	int a = 0;
	int b = 0;
	int c = 0;
    //scanf_s針對使用者input的資訊做處理。其%d代表scanf_s會去尋找所輸入一個整數型資料,並把所輸入的值放入一個記憶體空間。
    //\n則代表換行。這裡一共會抓取所輸入3個整數a,b,c
    //&a代表變數a的記憶體位置
	scanf_s("%d\n%d\n%d", &a, &b, &c);//User input a,b,c value

	double x1 = 0; double x2 = 0;//assign defalut value to X1,X2.
	double delta = b*b - 4 * a*c;//calculate delta value

	printf("%dX^2+%dX+%d=0\n", a, b, c);

	//Judge delta range to decide how many  root.
	if (delta > 0) {
		x1 = (-b + sqrt(delta)) / (2 * a);
		x2 = (-b - sqrt(delta)) / (2 * a);
		printf("There are two roots,%f,%f", x1, x2);
	}
	else if (delta == 0)//判斷相等必要用==,這裡容易誤寫成=,=在C++代表賦值
	{
		x1 = -b / (2 * a);
		printf("There is  only one root,%f", x1);
	}
	else
	{
		printf("There is no root");
	}
	
	return 0;
}