精華區beta NTUCH-HW 關於我們 聯絡資訊
功能 用到的觀念 CODE #include <iostream> #include <iomanip> using namespace std; #include <string> #include <fstream> #include <cstdlib> const int n=35; class sortfile { public://declare the function void input(string,int&,int&,double[n][10],string[n],string[6]); void sort02(double[10][10],string[10],double[10][10],int,int,string[6]); void sort05(double[10][10],string[10],double[10][10],int,int,string[6]); void outfile(string,string[10],double[10][10],int,int,string[6]); }; void sortfile::input(string filename,int& deter_row,int& deter_column,double numA[n][10],string name[n],string other[6]) {//this object helps read the file string seten[5];//this store other word exclude the table string* a[100];//store the address char check; int row=0,column=0,k=0,i=0,j; ifstream read; read.open(filename.c_str()); if (read.fail()) // check for a successful open { cout << "\nThe file was not successfully opened" << "\n Please check that the file currently exists." <<endl; system("pause"); exit(0); } cout << "\nThe file has been successfully opened for reading"<< endl; do//run the loop to read the content { getline(read,seten[0],'\n');//get the content in first line getline(read,seten[1],'\n');//get the content in second line getline(read,seten[2],'\n');//get the content in third line do//read the number to each element { read>>name[i];//read the sentence a[i]=&name[i];//store the address of the sentence i++;//get the next icon if(row<32)//if row equal 32, it's end { for(column=0;column<7;column++)//store the content in order read>>numA[row][column]; { row=row+1; } read>>ws;//read the white space } else break; }while(read.good());//if there are nothing else, jump out of the loop read>>ws; }while(read.good());//if there are nothing else, jump out of the loop for(i=0;i<32;i++)//store the country name in order { name[i]=*a[i]; } read.close();//close the file deter_row=row;//send the final row deter_column=column;//send the final column for(i=0;i<3;i++)//store other words { other[i]=seten[i]; } other[3]=*a[row]; } void sortfile::sort02(double numA[n][10],string a[n], double result[n][10],int row1,int column1,string other[6])//arrange the data { sortfile save;//declare an object int i,j,k,ch1; string temps,tempstr[n],filename; double temp; for(int ii=0;ii<row1;ii++)//bubble sort for the name and the data { tempstr[ii]=a[ii]; for(int jj=0;jj<column1;jj++) { result[ii][jj]=numA[ii][jj]; } } for(i=0;i<33;i++) { for(k=0;k<31;k++) { if((result[k][2]-result[k+1][2])>=0) { temps=tempstr[k]; tempstr[k]=tempstr[k+1]; tempstr[k+1]=temps; for(j=0;j<7;j++) { temp=result[k][j]; result[k][j]=result[k+1][j]; result[k+1][j]=temp; } } } } for(i=0;i<32;i++)//print the data and country name { cout<<setw(10)<<left<<tempstr[i]; for(j=0;j<7;j++) { cout<<"\t"<<result[i][j]; } cout<<endl; } 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: ";//ask you to enter the name cin>>filename; save.outfile(filename,tempstr,result,row1,column1,other);//an object to do saving } else exit(0);//enter any key exit } void sortfile::sort05(double numA[n][10],string a[n], double result[n][10],int row1,int column1,string other[6])//the action is the same { sortfile save; int i,j,k,ch1; string temps,tempstr[n],filename; double temp; for(int ii=0;ii<row1;ii++) { tempstr[ii]=a[ii]; for(int jj=0;jj<column1;jj++) { result[ii][jj]=numA[ii][jj]; } } for(i=0;i<100;i++) { for(k=0;k<31;k++) { if((result[k][5]-result[k+1][5])>=0) { temps=tempstr[k]; tempstr[k]=tempstr[k+1]; tempstr[k+1]=temps; for(j=0;j<7;j++) { temp=result[k][j]; result[k][j]=result[k+1][j]; result[k+1][j]=temp; } } } } for(i=0;i<32;i++) { cout<<setw(10)<<left<<tempstr[i]; for(j=0;j<7;j++) { cout<<"\t"<<result[i][j]; } cout<<endl; } 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; save.outfile(filename,tempstr,result,row1,column1,other); } else exit(0); } void sortfile::outfile(string filename,string arrangestr[10],double arrange[10][10],int row,int column,string outputword[6]) { ofstream out;//it'll ouput the word out.open(filename.c_str());//check whether the file is exist if (out.fail()) // check for a successful open { cout << "\nThe file was not successfully opened" << "\n Please check that the file currently exists." <<endl; exit(0); } cout<< "\nThe file has been successfully saved"<< endl; out<<outputword[0]<<endl;//output some sentence out<<outputword[1]<<endl; out<<outputword[2]<<endl; for(int i=0;i<row;i++)//run the loop to write the data to a file { out<<setw(16)<<left<<arrangestr[i]; for(int j=0;j<column;j++) { out<<arrange[i][j]<<"\t";//give tab between elements } out<<'\n'; } out<<outputword[3]<<endl; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.7.59