精華區beta NTUCH-HW 關於我們 聯絡資訊
功能 用到的觀念 CODE #include <iostream> #include <iomanip> #include"matrix.h" using namespace std; #include <string> #include <fstream> #include <cstdlib> int main() { int calmatrix1[10][10],i,j,k,row,column,calmatrix2[10][10],ch,m,n,l,choice;//calmatrix1 and calmatrix2 stores the keyin number int row1,row2,column1,column2,calmatrix3[10][10],calmatrix4[10][10],ch1 ,result[10][10]={0},ch2; //calmatrix3 and calmatrix4 stores the keyin number. result stores the result after calculating matrix keyin,calmatrix,call,saveit;//keyin is used to call the function to let the user to key in. //calmatrix is used to calculate. call is to read a file from file. saveit is to save the result string filename,filename1;//set a string variable cout<<"instuction for the program"<<endl; cout<<"1 2 3"<<endl; cout<<"11 22 33"<<endl; cout<<"12 23 34"<<endl; cout<<"this matrix 'a' means:"<<endl; cout<<"a[0][0]=1"<<setw(15)<<"a[0][1]=2"<<setw(15)<<"a[0][2]=3"<<endl; cout<<"a[1][0]=11"<<setw(15)<<"a[1][1]=22"<<setw(15)<<"a[1][2]=33"<<endl; cout<<"a[2][0]=12"<<setw(15)<<"a[2][1]=23"<<setw(15)<<"a[2][2]=34"<<endl; cout<<"if you finish, enter any key"<<endl; system("pause"); system("cls");//clean the screen cout<<"first let's test the program"<<endl; int testmul1[10][10]={{1,2,3},{2,3,4},{3,4,1},{4,1,2}}; int testmul2[10][10]={{1,2,3,0},{2,3,0,1},{3,0,1,2}},testresult[10][10]={0}, testm=4,testn=3,testl=4; int testmul3[10][10]={{1,2,3,4},{2,3,4,1},{3,4,1,2},{4,1,2,3}}; int testmul4[10][10]={{1,2,3,0},{2,3,0,1},{3,0,1,2},{0,1,2,3}},testr=4,testc=4; cout<<"test the multiple function"<<endl; for(i=0;i<4;i++) { for(j=0;j<3;j++) { cout<<setw(3)<<testmul1[i][j]; } cout<<endl; } cout<<"X"<<endl; for(i=0;i<3;i++) { for(j=0;j<4;j++) { cout<<setw(3)<<testmul2[i][j]; } cout<<endl; } cout<<"="<<endl; calmatrix.calmultiple(testmul1,testmul2,testm,testn,testl,testresult); cout<<"test the plus function"<<endl; for(i=0;i<4;i++) { for(j=0;j<4;j++) { cout<<setw(3)<<testmul1[i][j]; } cout<<endl; } cout<<"X"<<endl; for(i=0;i<4;i++) { for(j=0;j<4;j++) { cout<<setw(3)<<testmul2[i][j]; } cout<<endl; } cout<<"="<<endl; calmatrix.calplus(testmul3,testmul4,testr,testc,testresult); system("pause"); system("cls");//clean the screen cout<<"if you want to enter the matrix by keyboard, press 1."<<endl;//show the content cout<<"if you want to read from file, press 2."<<endl;//show the content and give choices cin>>ch2;//enter the choice to ch2 if(ch2==1)//if ch2 equal to 1, the matrix is entered by keyboard { cout<<"if you want to plus or minus press 1. if you want to multiply press 2."<<endl;//show the content and give choices cin>>choice;//enter the choice to ch2 system("cls");//clean the screen if(choice==1)//if ch2 equal to 1, do plus or minus { keyin.enterbykey(calmatrix1,calmatrix2,row,column); cout<<"you want to plus, press 1. you want to minus, press 2."<<endl; cin>>ch; if(ch==1) { cout<<"matrixA+matrixB="<<endl; calmatrix.calplus(calmatrix1,calmatrix2,row,column,result); cout<<"do you want to save? press 1 or press any key to exit"<<endl; cin>>ch1; if(ch1==1) { cout << "Please enter the name of the file: "; cin>>filename; saveit.outfile(filename,row,column,result); } else exit(0); } else if(ch==2) { cout<<"matrixA-matrixB="<<endl; calmatrix.calminus(calmatrix1,calmatrix2,row,column,result); cout<<"do you want to save? press 1 or press any key to exit"<<endl; cin>>ch1; if(ch1==1) { cout << "Please enter the name of the file: "; cin>>filename; saveit.outfile(filename,row,column,result); } else exit(0); } else cout<<"you type wrong"<<endl; } else if(choice==2) { keyin.enterbykey1(calmatrix1,calmatrix2,m,n,l); cout<<"the result A*B"<<endl; calmatrix.calmultiple(calmatrix1,calmatrix2,m,n,l,result); cout<<"do you want to save? press 1 or press any key to exit"<<endl; cin>>ch1; if(ch1==1) { cout << "Please enter the name of the file: "; cin>>filename; saveit.outfile(filename,m,l,result); } else exit(0); } } else if(ch2==2) { cout << "Please enter the name of the file you wish to open: "; cin>>filename; call.inputfromfile(filename,row1,column1,calmatrix3); cout << "Please enter another name of the file you wish to open: "; cin>>filename1; call.inputfromfile(filename1,row2,column2,calmatrix4); cout<<"you want to plus, press 1. you want to minus, press 2."<< endl; cout<<"you want to multiply, press 3."<<endl; cin>>ch; if(ch==1) { if(row1==row2&&column1==column2) { cout<<"matrixA+matrixB="<<endl; calmatrix.calplus(calmatrix3,calmatrix4,row1,column1,result); cout<<"do you want to save? press 1 or press any key to exit"<<endl; cin>>ch1; if(ch1==1) { cout << "Please enter the name of the file: "; cin>>filename; saveit.outfile(filename,row1,column1,result); } else exit(0); } else cout<<"the calculation is invalid"<<endl; } else if(ch==2) { if(row1==row2&&column1==column2) { cout<<"matrixA-matrixB="<<endl; calmatrix.calminus(calmatrix3,calmatrix4,row2,column2,result); cout<<"do you want to save? press 1 or press any key to exit"<<endl; cin>>ch1; if(ch1==1) { cout << "Please enter the name of the file: "; cin>>filename; saveit.outfile(filename,row2,column2,result); } else exit(0); } else cout<<"the calculation is invalid"<<endl; } else if(ch==3) { if(column1==row2) { cout<<"matrixA X matrixB="<<endl; calmatrix.calmultiple(calmatrix3,calmatrix4,row1,column1,column2,result); cout<<"do you want to save? press 1 or press any key to exit"<<endl; cin>>ch1; if(ch1==1) { if(row1==row2&&column1==column2) cout << "Please enter the name of the file: "; cin>>filename; saveit.outfile(filename,row1,column2,result); } else exit(0); } else cout<<"the calculation is invalid"<<endl; } } else cout<<"you type wrong"<<endl; system("pause"); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.7.59