کد
[B]#include #include #include #include[/B] [B]float a1[4],a2[4],a3[4]; /* Array declaration */ void show(); /* function declaration */ void getdata(float [],float [],float []); /* // // // */ void display(float [],float [],float []); /* // // // */ int diagonally(); void swap(float [],float []); /* // // // */ void jacobi(float [],float [],float []); /* // // // */ void gauss(float [],float [],float []); void answer(); /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^* / /*....................MAIN FUNCTION OF METHOD............*/ void main() /* main function definition */ { int count=4; /* { main function body} */ clrscr();[/B] [B]cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^"< cout<<"..................THIS IS VALID ONLY FOR JACOBI ITTERATIVE METHOD............."<[/B] [B]cout<<"___________________________________________________________________ ___________"< show(); /* function calling */ getdata(a1,a2,a3); /* function calling */ clrscr(); cout< display(a1,a2,a3); /* function calling */ count=diagonally(); /* function calling */ switch (count) { case 0: { answer(); /* function calling */ break; } case 2: {display(a1,a2,a3); answer(); /* function calling */ break; } default: { cout<<"SORRY;YOUR SYSTEM IS NOT DIAGONALLY DOMINENT"; break; } } getch(); } ////////////////////////Function To Check Diagonality//////////////////////// int diagonally() { int f=4,g=4 ,h=4; int count=0; float temp[4]; if(fabs(a1[0])<(fabs(a1[1])+fabs(a1[2]))) {count++; f=1;} if(fabs(a2[1])<(fabs(a2[0])+fabs(a2[2]))) {count++; g=2;} if(fabs(a3[2])<(fabs(a3[0])+fabs(a3[1]))) {count++; h=3;} if(f==1&&g==2&&h==4) swap(a1,a2); /* function calling */ if(f==1&&h==3&&g==4) swap(a1,a3); /* function calling */ if(g==2&&h==3&&f==4) swap(a2,a3); /* function calling */[/B] [B] return(count); } ////////////////////////////Function for jacobi itterative method///////////////////////// void jacobi(float a[],float b[],float c[]) /*function definition */ { float temp[3]; long float j1,j2,j3; cout< cout< cin>>j1; cout<cout<<"------------------------------------------------------------------- ----";[/B] [B]cout<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::";[/B] [B]cout<<"___________________________________________________________________ ____"; cout<X(2)"<<" X(3)"; cout< cout<[/B] [B] for(int s=1;s<=20;s++) { temp[0]=j1;temp[1]=j2;temp[2]=j3; j1=(a[3]-a[1]*temp[1]-a[2]*temp[2])/a[0]; j2=(b[3]-b[0]*temp[0]-b[2]*temp[2])/b[1]; j3=(c[3]-c[0]*temp[0]-c[1]*temp[1])/c[2]; cout<<" "< if(j1==temp[0]&&j2==temp[1]&&j3==temp[2]) break; } } //////////////////////////Function Of Swaping//////////////////////////////////// void swap(float a[],float b[]) /* function definition */ { float temp[4];[/B] [B]cout<<"------------------------------------------------------------------- -------------"< cout<<".....................Your System Is Not Diagonally Dominent....................."< cout<<"___________________Now It Have To Become Diagonally Dominent As__________________"< for(int i=0;i<4;i++) { temp[i]=a[i]; a[i]=b[i]; b[i]=temp[i]; } } //////////////////////Function To Show Equations Form//////////////////////// void show() /* function definition */ { cout<cout<<"__________________Your Equations Will Be Of The Form Like This_________________"< cout< cout<<" a(11)X1 + a(12)X2 + a(13)X3= b(1)"< <<" a(21)X1 + a(22)X2 + a(23)X3= b(2)"< <<" a(31)X1 + a(32)X2 + a(33)X3= b(3)"< } //////////////////////Function To Get Data From User///////////////////////// void getdata(float a[],float b[],float c[]) /* function definition */ { for(int i=0;i<3;i++) { cout<<"a(1"<cout<<"------------------------------------------------------------------- -------"; cout<<" ::::::::::::::::Your Given System Is Like This:::::::::::::::: "< cout<<" "<"< <<" + ?<"< <<" "<"< cout< } ///////////////////////////Function of Gauss Seidal Method//////////////////// void gauss(float a[],float b[],float c[]) /*function definition */ { float temp[3]; long float j1,j2,j3; cout< cout< cin>>j1; cout<cout<<"------------------------------------------------------------------- ----";[/B] [B]cout<<"::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ::::";[/B] [B]cout<<"___________________________________________________________________ ____"; cout<X(2)"<<" X(3)"; cout< cout<[/B] [B] for(int s=1;s<=20;s++) { temp[0]=j1;temp[1]=j2;temp[2]=j3; j1=(a[3]-a[1]*j2-a[2]*j3)/a[0]; j2=(b[3]-b[0]*j1-b[2]*j3)/b[1]; j3=(c[3]-c[0]*j1-c[1]*j2)/c[2]; cout<<" "< if(j1==temp[0]&&j2==temp[1]&&j3==temp[2]) break; } } ///////////////// FUNCTION OF TAKING GAUSS OR JACOBI ////////////////// void answer() { char option; do { cout<<"PRESS [G] FOR GAUSS SEIDAL METHOD: "< <<"AND"< option=getche(); switch (option) { case 'j': { cout<METHOD----------"< jacobi(a1,a2,a3); /* function calling */ break; } case 'g': { cout<METHOD----------"< gauss(a1,a2,a3); /* function calling */ break; } default: { cout<<"-------------YOUR OPTION IS NOT CORRECT TRY AGAIN-------------"< break; } } }while(getche()!=' '); }[/B]
منبع»» انجمن دانشجویان کردستان