該例子說明幾個問題
1. 解一元一次方程組,一般表達式
a1x + b1y = c1
a2x + b2jy = c2
2. 例如:
2x + y = 3
x - y = 6
********************************************************************/
#include "stdio.h"
typedef struct
{
double a;
double b;
double c;
} KLM_Type; //克拉默法則
typedef struct
{
double x;
double y;
} Point; //方程組的解坐標(x, y)
Point fun(KLM_Type klm1, KLM_Type klm2);
int main(void)
{
Point pt;
KLM_Type klm1, klm2;
printf("請輸入第一個一元一次方程的系數:\n");
printf("a1 = ");
scanf("%lf", &klm1.a);
printf("b1 = ");
scanf("%lf", &klm1.b);
printf("c1 = ");
scanf("%lf", &klm1.c);
printf("\n請輸入第二個一元一次方程的系數:\n");
printf("a2 = ");
scanf("%lf", &klm2.a);
printf("b2 = ");
scanf("%lf", &klm2.b);
printf("c2 = ");
scanf("%lf", &klm2.c);
pt = fun(klm1, klm2);
printf("\nx = %lf\n", pt.x);
printf("y = %lf\n", pt.y);
return 0;
}
Point fun(KLM_Type klm1, KLM_Type klm2)
{
Point pt;
double a1 = klm1.a;
double b1 = klm1.b;
double c1 = klm1.c;
double a2 = klm2.a;
double b2 = klm2.b;
double c2 = klm2.c;
double D = a1 * b2 - b1 * a2;
double D1 = c1 * b2 - b1 * c2;
double D2 = a1 * c2 - c1 * a2;
pt.x = D1 / D;
pt.y = D2 / D;
return pt;
}
/********************************************************************
請輸入第一個一元一次方程的系數:
a1 = 2
b1 = 1
c1 = 3
請輸入第二個一元一次方程的系數:
a2 = 1
b2 = -1
c2 = 6
x = 3.000000
y = -3.000000
Press any key to continue
********************************************************************/