4
1 2 2.5 4
5 6.5 7 8
9 -0.5 11 1
-1.3 15.2 15 1
Үр дүн нь консолд хэвлэгдэхийн зэрэгцээ output.txt текст файлд алхам болгоны үр дүнгийн хамт бичигдэнэ. Зарим нэгэн үйлдлүүдийг нь болоод бүхэлд өөр нэг бодлогод ашиглахад тохиромжтой шүү.
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <mem.h>
ifstream ifs;
ofstream ofs;
int main() {
//Bodlogyn ogogdliig unshikh file
ifs.open("input.txt", ios::in);
//Bodlogyn khariug bichikh file
ofs.open("output.txt", ios::out);
int n, i, j, k, w, p;
float a[100][100], top[100][100], bottom[100][100], reverse[100][100], b[100][100], det, q, temp, A;
bool end = false;
if(ifs.is_open()) {
//File-iin ekhnii element khusnegtiin khemjees bolokh n too baina
ifs>>n;
cout<<"\n Ogogdson ni\n\n";
ofs<<" Ogogdson ni\n\n";
cout<<" n = "<<n<<endl;
ofs<<" n = "<<n<<endl;
//Khusnegtiin elementuudiig file-s unshikh
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
ifs>>a[i][j];
for(i=1; i<=n; i++) {
cout<<endl;
ofs<<endl;
for(j=1; j<=n; j++) {
cout.width(8);
ofs.width(8);
cout<<a[i][j];
ofs<<a[i][j];
}
}
memcpy(bottom, a, sizeof(a));
ofs<<"\n\n Dood gurvaljin matrix gargakh";
for(i=1; i<n; i++) {
for(j=i+1; j<=n; j++) {
p = -1;
for(w=i; w<=n; w++)
if(bottom[i][w] != 0) {
p = w;
break;
}
if(p != -1) {
//0-ees yalgaatai element bukhii bagana bolon tukhain davtaltand khargalzakh bagana 2-yn bairyg solikh
cout<<endl;
ofs<<endl;
for(j=1; j<=n; j++) {
cout.width(8);
ofs.width(8);
cout<<a[i][j];
ofs<<a[i][j];
}
}
memcpy(bottom, a, sizeof(a));
ofs<<"\n\n Dood gurvaljin matrix gargakh";
for(i=1; i<n; i++) {
for(j=i+1; j<=n; j++) {
p = -1;
for(w=i; w<=n; w++)
if(bottom[i][w] != 0) {
p = w;
break;
}
if(p != -1) {
//0-ees yalgaatai element bukhii bagana bolon tukhain davtaltand khargalzakh bagana 2-yn bairyg solikh
for(w=1; w<=n; w++) {
temp = bottom[w][i];
bottom[w][i] = bottom[w][p];
bottom[w][p] = temp;
}
q = bottom[j][i] / bottom[i][i];
for(k=i; k<=n; k++)
bottom[j][k] -= bottom[i][k] * q;
}
else {
cout<<"\n\n Dood gurvaljin gargakh yavtsad "<<(i-1)<<"-r khuvirgaltyn daraa\n";
for(int ii=1; ii<=n; ii++) {
cout<<endl;
for(int jj=1; jj<=n; jj++) {
cout.width(10);
cout<<fixed<<setprecision(4)<<bottom[ii][jj];
}
}
cout<<"\n\n Matrixyn "<<i<<"-r moriin bukh element 0-tei tentsuu uchraas det(a) = 0 baina. Tsaash ni khuvirgakh shaardlagagui.\n";
ofs<<"\n\n Matrixyn "<<i<<"-r moriin bukh element 0-tei tentsuu uchraas det(a) = 0 baina. Tsaash ni khuvirgakh shaardlagagui.\n";
det = 0;
end = true;
break;
}
}
if(end)
break;
else {
//Alkham bolgony matrixyn khuvirgaltyg "output.txt" file-d bichikh
ofs<<"\n\n "<<i<<"-r khuvirgalt\n";
for(int ii=1; ii<=n; ii++) {
ofs<<endl;
for(int jj=1; jj<=n; jj++) {
ofs.width(10);
ofs<<fixed<<setprecision(4)<<bottom[ii][jj];
}
}
}
}
if(!end) {
//Dood gurvaljin matrixyg khevlekh
cout<<"\n\n Dood gurvaljin matrix\n";
//det-r a matrixyn todorhoilogchiig temdeglev
det = 1;
for(i=1; i<=n; i++) {
cout<<endl;
for(j=1; j<=n; j++) {
cout.width(10);
cout<<fixed<<setprecision(4)<<bottom[i][j];
}
det *= bottom[i][i];
}
cout<<"\n\n det(a) = "<<fixed<<setprecision(2)<<det<<endl;
ofs<<"\n\n det(a) = "<<fixed<<setprecision(2)<<det<<endl;
}
end = false;
ofs<<"\n Deed gurvaljin matrix gargakh";
memcpy(top, a, sizeof(a));
for(i=n; i>0; i--) {
for(j=i-1; j>=0; j--) {
p = -1;
for(w=i; w>=0; w--)
if(top[i][w] != 0) {
p = w;
break;
}
if(p != -1) {
//0-ees yalgaatai element bukhii bagana bolon tukhain davtaltand khargalzakh bagana 2-yn bairyg solikh
for(w=1; w<=n; w++) {
temp = top[w][i];
top[w][i] = top[w][p];
top[w][p] = temp;
}
q = top[j][i] / top[i][i];
for(k=i; k>=0; k--)
top[j][k] -= top[i][k] * q;
}
else {
cout<<"\n\n Dood gurvaljin gargakh yavtsad "<<(n - i)<<"-r khuvirgaltyn daraa\n";
for(int ii=1; ii<=n; ii++) {
cout<<endl;
for(int jj=1; jj<=n; jj++) {
cout.width(10);
cout<<fixed<<setprecision(4)<<top[ii][jj];
}
}
cout<<"\n\n Matrixyn "<<i<<"-r moriin bukh element 0-tei tentsuu uchraas det(a) = 0 baina. Tsaash ni khuvirgakh shaardlagagui.\n";
ofs<<"\n\n Matrixyn "<<i<<"-r moriin bukh element 0-tei tentsuu uchraas det(a) = 0 baina. Tsaash ni khuvirgakh shaardlagagui.\n";
det = 0;
end = true;
break;
}
}
if(end)
break;
else {
//Alkham bolgony matrixyn khuvirgaltyg "outpu.txt" file-d bichikh
ofs<<"\n\n "<<(n-i+1)<<"-r khuvirgalt\n";
for(int ii=1; ii<=n; ii++) {
ofs<<endl;
for(int jj=1; jj<=n; jj++) {
ofs.width(10);
ofs<<fixed<<setprecision(4)<<top[ii][jj];
}
}
}
}
if(!end) {
//Deed gurvaljin matrixyg khevlekh
cout<<"\n\n Deed gurvaljin matrix\n";
//det-r a matrixyn todorhoilogchiig temdeglev
det = 1;
for(i=1; i<=n; i++) {
cout<<endl;
for(j=1; j<=n; j++) {
cout.width(10);
cout<<fixed<<setprecision(4)<<top[i][j];
}
det *= top[i][i];
}
cout<<"\n\n det(a) = "<<fixed<<setprecision(2)<<det<<endl;
ofs<<"\n\n det(a) = "<<fixed<<setprecision(2)<<det<<endl;
cout<<"\n //Deed, dood gurvaljind shiljuulj olson todorkhoilogchid tentsuu baigaa biz?!\n";
ofs<<"\n //Deed, dood gurvaljind shiljuulj olson todorkhoilogchid tentsuu baigaa biz?!\n";
}
cout<<"\n Urvuu matrix\n";
ofs<<"\n Urvuu matrix\n";
long detL = (long)(det * 10000);
if(detL == 0) {
cout<<"\n Todorkhoilogch ni 0-tei tentsuu uchraas urvuu matrixgui baina. Ooroor khelbel, ogogdson matrix ni bokhson matrix baina.";
ofs<<"\n Todorkhoilogch ni 0-tei tentsuu uchraas urvuu matrixgui baina. Ooroor khelbel, ogogdson matrix ni bokhson matrix baina.";
}
else {
for(int i1=1; i1<=n; i1++) {
for(int j1=1; j1<=n; j1++) {
for(int i2=1; i2<i1; i2++) {
for(int j2=1; j2<j1; j2++) {
b[i2][j2] = a[i2][j2];
}
for(int j2=j1+1; j2<=n; j2++) {
b[i2][j2-1] = a[i2][j2];
}
}
for(int i2=i1+1; i2<=n; i2++) {
for(int j2=1; j2<j1; j2++) {
b[i2-1][j2] = a[i2][j2];
}
for(int j2=j1+1; j2<=n; j2++) {
b[i2-1][j2-1] = a[i2][j2];
}
}
for(i=1; i<n-1; i++) {
for(j=i+1; j<=n-1; j++) {
p = -1;
for(w=i; w<=n; w++)
if(b[i][w] != 0) {
p = w;
break;
}
if(p != -1) {
//0-ees yalgaatai element bukhii bagana bolon tukhain davtaltand khargalzakh bagana 2-yn bairyg solikh
for(w=1; w<=n-1; w++) {
temp = b[w][i];
b[w][i] = b[w][p];
b[w][p] = temp;
}
q = b[j][i] / b[i][i];
for(k=i; k<=n-1; k++)
b[j][k] -= b[i][k] * q;
}
}
}
A = 1;
for(i=1; i<n; i++)
A *= b[i][i];
if((i1 + j1) % 2 == 1)
A *= -1;
reverse[j1][i1] = A;
}
}
for(i=1; i<=n; i++) {
cout<<endl;
ofs<<endl;
for(j=1; j<=n; j++) {
reverse[i][j] /= det;
cout.width(10);
cout<<fixed<<setprecision(4)<<reverse[i][j];
ofs.width(10);
ofs<<fixed<<setprecision(4)<<reverse[i][j];
}
}
}
}
else
cout<<" \"input.txt\" file oldsongui";
cout<<endl<<endl;
ifs.close();
ofs.close();
system("pause");
}
yaay bas file ntr sain sudlaagui changa l yum bna
ReplyDeletegehde goy blog bna... bayarlalaa
ReplyDeleteamjilt husey