阿元 發問時間: 電腦與網際網路程式設計 · 1 0 年前

c++ 程式哪裡出錯了?

題目:

if a1x+b1y=c1 and a2x+b2y=c2

請用克拉瑪公式與代入消去法求解,abc為自行輸入

解:

#include <iostream>

#include <windows.h>

using namespace std;

int main(){

int Deter, DeterX, DeterY, A1, A2, B1, B2, C1, C2;

int B1Sign, B2Sign, DeterX_Dem, DeterY_Dem;

bool Det_Ind=false;

SetConsoleTitle("Cramer's Rule");

cout<<"Program Written By Shawncplus To Solve a Linear Equation with Cramer's Rule"<< endl;

cout<<"Please Enter a1: ";

cin>>A1;

cout<<"\nPlease Enter b1: ";

cin>>B1;

cout<<"\nPlease Enter c1: ";

cin>>C1;

cout<<"\nPlease enter a2: ";

cin>>A2;

cout<<"\nPlease enter b2: ";

cin>>B2;

cout<<"\nPlease Enter c2: ";

cin>>C2;

cout<<"The Equation is {\n";

B1Sign=B1*(-1);

B2Sign=B2*(-1);

if (B1>0){

cout<<"\t "<< A1 <<"x+"<<B1<<"y = "<< C1<< endl;}

else if (B1<0){

cout<<"\t "<< A1 <<"x-"<<B1Sign<<"y = "<< C1<< endl;}

if (B2>0){

cout<<"\t "<< A2 <<"x+"<<B2<<"y = "<< C2<< "\n\t\t} " <<endl;}

else if (B2<0){

cout<<"\t "<< A2 <<"x-"<<B2Sign<<"y = "<< C2<< "\n\t\t} " << endl;}

Deter=(A1*B2)-(A2*B1);

DeterX=(C1*B2)-(C2*B1);

DeterY=(A1*C2)-(A2*C1);

if (Deter==0 && DeterX==0 && DeterY==0) {

cout<<"System is Dependant (Infinite Solutions)"<< endl;

Det_Ind = true;

}

else if (Deter==0 && (DeterX!=0 || DeterY!=0)){

cout<<"System is Inconsistent (System Set is \xE9 )"<< endl;

Det_Ind = true;

}

cout<<"D = "<< Deter << endl;

cout<<"D(x) = "<< DeterX << endl;

已更新項目:

cout<<"D(y) = "<< DeterY << endl;

if (Det_Ind==true){

cout<<"Please press Enter to continue. . .";

cin.get();

cin.get();

return 0;

}

2 個已更新項目:

else if (Det_Ind==false){

DeterX_Dem=(DeterX/Deter);

DeterY_Dem=(DeterY/Deter);

cout<<"\nX = "<< DeterX<<"/"<< Deter <<" = "<< DeterX_Dem << endl;

3 個已更新項目:

cout<<"Y = "<< DeterY<<"/"<< Deter <<" = "<< DeterY_Dem << endl;

cout<<"Solution Set is {("<< DeterX_Dem <<","<< DeterY_Dem

4 個已更新項目:

<<")}"<< endl;

}

cout<<"\n\nPlease press Enter to continue. . .";

5 個已更新項目:

cin.get();

cin.get();

return 0;

}

6 個已更新項目:

a1x+b1y=c1

a2x+b2y=c2

請用克拉瑪公式與代入消去法求解,abc為自行輸入

如果要改成C語言的話 要如何修改呢?

2 個解答

評分
  • 1 0 年前
    最佳解答

    標題與內文不一樣@@!

    #include <stdio.h>

    int main(void)

    {

    int Deter, DeterX, DeterY, A1, A2, B1, B2, C1, C2;

    int B1Sign, B2Sign, DeterX_Dem, DeterY_Dem;

    bool Det_Ind=false;

    printf("Program Written By Shawncplus To Solve a Linear Equation with Cramer's Rule\n");

    printf("Please Enter a1: ");

    scanf("%d", &A1);

    printf("\nPlease Enter b1: ");

    scanf("%d", &B1);

    printf("\nPlease Enter c1: ");

    scanf("%d", &C1);

    printf("\nPlease enter a2: ");

    scanf("%d", &A2);

    printf("\nPlease enter b2: ");

    scanf("%d", &B2);

    printf("\nPlease Enter c2: ");

    scanf("%d", &C2);

    printf("The Equation is {\n");

    B1Sign = B1*(-1);

    B2Sign = B2*(-1);

    if (B1>0){

    printf("\t %dx+%dy = %d\n", A1, B1, C1);

    }

    else if (B1 < 0){

    printf("\t %dx-%dy = %d\n", A1, B1Sign, C1);

    }

    if (B2>0){

    printf("\t %dx+%dy = %d\n\t\t} \n", A2, B2, C2);

    }

    else if (B2<0){

    printf("\t %dx-%dy = %d\n\t\t} \n", A2, B2Sign, C2);

    }

    Deter=(A1*B2)-(A2*B1);

    DeterX=(C1*B2)-(C2*B1);

    DeterY=(A1*C2)-(A2*C1);

    if (Deter==0 && DeterX==0 && DeterY==0) {

    printf("System is Dependant (Infinite Solutions)\n");

    Det_Ind = true;

    }

    else if (Deter==0 && (DeterX!=0 || DeterY!=0)){

    printf("System is Inconsistent (System Set is \xE9 )\n");

    Det_Ind = true;

    }

    printf("D = %d\n", Deter);

    printf("D(x) = %d\n", DeterX);

    printf("D(y) = %d\n", DeterY);

    if (Det_Ind==true){

    printf("Please press Enter to continue. . .");

    getchar();

    getchar();

    return 0;

    }

    else if (Det_Ind==false){

    DeterX_Dem=(DeterX/Deter);

    DeterY_Dem=(DeterY/Deter);

    printf("\nX = %d/%d = %d\n", DeterX, Deter, DeterX_Dem);

    printf("\nY = %d/%d = %d\n", DeterY, Deter, DeterY_Dem);

    printf("Solution Set is {(%d,%d)}\n", DeterX_Dem, DeterY_Dem);

    }

    printf("\n\nPlease press Enter to continue. . .");

    getchar();

    getchar();

    return 0;

    }

    參考資料: 自己
  • 匿名使用者
    7 年前

    如果你很確定自己很想結婚

    婚友社的確是一個有效率的管道

    年紀越大壓力越大

    以前我去婚友社時

    年輕的女生機會總是比較多

    時間總是不站在女生這邊的

    所以如果你想婚的念頭已經確定

    就去找一家評價好一點的婚友社!

    行動吧!

    這是我之前參加的婚友社我覺得不錯

    或搜尋"紅娘李姐"

    不過還是建議你自己去諮詢過看是否適合你!

還有問題?馬上發問,尋求解答。