简体   繁体   English

用 C 语言求解一对方程 - 给出奇数结果的程序

[英]Solving a pair of equations in C - program giving odd results

First of all, English is not my native language, so I'm not sure if I have the correct mathematical terms, and because of that I was unable to find any previous answers to this question, so apologies in advance if this is a duplicate.首先,英语不是我的母语,所以我不确定我是否有正确的数学术语,因此我无法找到这个问题的任何以前的答案,如果这是重复的,请提前道歉.

Anyway, I need to write a program in C that is meant to solve a pair of equations in the format of a*x + b*y = c , where x and y have the same value across both equations.无论如何,我需要用 C 编写一个程序,用于求解格式为a*x + b*y = c的一对方程,其中 x 和 y 在两个方程中具有相同的值。 The method I'm using consists of dividing all three values (a, b and c) in the first equation by a followed by moving b*y to the other side of the equation so I can get a equation that says x = c - b*y .我使用的方法包括将第一个方程中的所有三个值(a、b 和 c)除以 a,然后将b*y移动到方程的另一侧,这样我就可以得到一个方程,表示x = c - b*y Afterwards, I insert that value of x into the second equation and from there get the value of y, which I then insert back into the first one and get x.之后,我将 x 的值插入到第二个方程中,并从那里得到 y 的值,然后将其插入到第一个方程中并得到 x。

However, my code is giving strange results - for example, the pair of equations x + 3y = 25 and 2x − 5y = −27 (so, the input would be 1 3 25 2 -5 -27) gives the answers -206 and -77 instead of the correct answers of 4 and 7.然而,我的代码给出了奇怪的结果——例如,方程对 x + 3y = 25 和 2x − 5y = −27(因此,输入为 1 3 25 2 -5 -27)给出了答案 -206 和-77 而不是 4 和 7 的正确答案。

I'm posting the entire code since it's fairly short (<40 lines) and I have no idea where the problem is:我发布了整个代码,因为它很短(<40 行),我不知道问题出在哪里:

#include<stdio.h>
void lin_jednacina2(float a1, float b1, float c1, float a2, float b2, float c2)
{
    float x, y;

    a1 = 1;
    b1 = b1 / a1;
    c1 /= a1;

    b1 = -1 * b1;

    b2 = b2 - a2 * b1;
    c2 = c2 - a2 * c1;

    y = c2 / b2;
    x = c1 - b1 * y; 

    printf("\nThe values of x and y are %.2f and %.2f, respectively.", x, y);
    return;
}
int main()
{
    float a1, b1, c1, a2, b2, c2;

    printf("Please enter the values of a, b and c for the first equation, in the format of ax + by = c. \n");
    scanf("%f %f %f", &a1, &b1, &c1);
    printf("Please enter the values of a, b and c for the second equation, in the format of ax + by = c. \n");
    scanf("%f %f %f", &a2, &b2, &c2);

    lin_jednacina2(a1, b1, c1, a2, b2, c2);
    return 0;
}

There is something wrong in your's algorithm - it seems like some things are unnecesary divided by a1...你的算法有问题 - 似乎有些事情是不必要的除以 a1 ......

This works ok:这工作正常:

void lin_jednacina2(float a1, float b1, float c1, float a2, float b2, float c2)
{
    float x, y;

    float b11 = b1 / a1;
    float c11 = c1 / a1;
    float a21 = a2 / a1;

    y = (c2 - a21 * c1) / (b2 - a21 * b1); 
    x = c11 - b11 * y;

    printf("\nThe values of x and y are %.2f and %.2f, respectively.", x, y);
    return;
}

Of course, you should add some "fuses" (zero checks) to protect from dividing by 0.当然,您应该添加一些“保险丝”(零检查)以防止被 0 除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM