如何用C语言解四元一次方程组?

如何用C语言解四元一次方程组?,第1张

首先毕滚要找到二元一次方程组的通解,扒基例如:

ax+by=m

cx+dy=n

不难算出

x=(md-bn)/(ad-bc)

y=(mc-an)/(bc-ad)

这相当于知道了算法,接下来就可以编程序了:

int main(){

int a,b,c,d,m,n

double x=0,y=0

scanf("%d,%d,%d,%d,%d,%d",&a,&b,&c,&d,&m,&n)

if(a*d==b*c||b*c==a*d)puts("无解")

else{

x=(m*d-b*n)/(a*d-b*c)

y=(m*c-a*n)/(b*c-a*d)

printf("x=%f,y=%f",x,y)

}

return 0

} 输入手此余系数,就可以计算了

public class Test1 {

public static void main(String[] args) {

FourElements e1 = new FourElements(1, 2, 3, 4D)

FourElements e2 = new FourElements(11, 22, 33, 44d)

FourElements e3 = e1.calculate(e2)

System.out.println("After calcualting, the result is: " + e3.toString())

}

}

class FourElements{

private double a, b, c, d

public FourElements(double a, double b, double c, double d){

this.a = a

this.b = b

this.c = c

this.d = d

}

public FourElements calculate(FourElements element2){

double newA = this.a + element2.getA()

double newB = this.b - element2.getB()

double newC = this.c * element2.getC()

double newD = this.d / element2.getD()

return new FourElements(newA, newB, newC, newD)

}

public double getA() {

return a

}

public double getB() {

return b

}

public double getC() {

return c

}

public double getD() {

return d

}

public String toString(){

return "a = " + a + ", b = " + b + ", c = " + c + ", d = " + d

}

}

VERSION 6.00

Begin VB.Form Form1

BorderStyle = 3 'Fixed Dialog

Caption = "猜数大师"

ClientHeight = 2055

ClientLeft = 45

ClientTop = 435

ClientWidth = 700

LinkTopic = "Form1"

MaxButton = 0 'False

MinButton = 0 'False

ScaleHeight = 2055

ScaleWidth = 3465

StartUpPosition = 2 '屏幕中心

Begin VB.CommandButton Command1

Caption = "开始"

Height = 495

Left = 1080

TabIndex = 3

Top = 960

Width = 1215

End

Begin VB.TextBox Text1

Height = 375

Left = 480

TabIndex = 0

Top = 360

Width = 855

End

Begin VB.Label Label2

Caption = "最多输入5次1-31之间的数据,就能猜中数字"

Height = 375

Left = 120

TabIndex = 2

Top = 1560

Width = 3255

End

Begin VB.Label Label1

Caption = "Label1"

Height = 375

Left = 1680

TabIndex = 1

Top = 360

Width = 975

End

End

Attribute VB_Name = "Form1"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = True

Attribute VB_Exposed = False

Option Explicit

Dim p1 As Integer, p2 As Integer

Dim Begin As Boolean

Private Sub Command1_Click()

p1 = 1: p2 = 31

Randomize

Label1.Tag = Int(Rnd * (31 - 1) + 1)

Label1.BackColor = &H8000000F

Begin = True

'Debug.Print Label1.Tag

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

If Not (KeyAscii = 13 And Begin = True) Then Exit Sub

Dim i As Integer

i = Val(Text1.Text)

If i <1 Or i >31 Then

MsgBox "请输入1-31之间的数字!", vbCritical, "提示"

Exit Sub

End If

With Label1

Select Case i - Val(Label1.Tag)

Case 0

.Caption = "你答对了!"

.BackColor = vbGreen

Begin = False

Case Is >0

.Caption = "大了!"

p2 = Val(Text1.Text)

.BackColor = vbRed

Case Is <0

.Caption = "小了!"

p1 = Val(Text1.Text)

.BackColor = vbBlack

End Select

End With

Text1.SetFocus

End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyV And Shift = 2 And Begin = True Then

Text1.Text = IIf(((p1 + p2) Mod 2) = 0, (p1 + p2) / 2, Fix((p1 + p2) / 2) + 1)

End If

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12319194.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存