2014-12-14 2 views
-3

2 개의 .txt 파일을 2 5 * 5 배열로 읽는 코드를 작성해야합니다. 여러 섹션에서 코드를 검사하여 작동하는지 확인하고 데이터 형식을 .txt 파일을 내 2 배열에 저장하는 데 문제가없는 경우 문제는 이제 2 행을 행렬로 사용하여 함께 곱하면됩니다. 3 배열/매트릭스를 만듭니다. 나는이 작업을 수행하는 방법에 대해 몇 시간 동안 인터넷과 포럼을 검색해 왔으며, 내가 발견 한 것을 기반으로 아래 코드를 만들었습니다. 나에게 코드는 (내가 초보자 오전 비록 내가 C++에 올 때) 소리를 보이지만 내 배열 3.C++에서 배열을 사용하는 행렬 곱셈

using namespace std; 
    #include<iostream> 
    #include<fstream> 
    #include<string> 

int main() 
{ int count1,count2; 
int i,j,m,n; 
int myarray1[5][5]; 
int myarray2[5][5]; 
int myarray3[5][5]; 

string file1,file2,mystring1,mystring2; 


cout<<"please enter the name of the first file"<<endl; 
cin>>file1; 
cout<<"please enter the name of the second file"<<endl; 
cin>>file2; 

ifstream inFile; 
inFile.open(file1.c_str()); 
for (count1=0;count1<26;++count1) 
{ 
    for(i=0;i<5;i++) 
    { 
     for(j=0;j<5;j++) 


     { 
      while (!inFile.eof()) 
       { 
        getline(inFile,mystring1,','); 

        int value1 = atoi(mystring1.c_str()); 
        myarray1[i][j]=value1; 
        cout<<myarray1[i][j]; 


       } 
     } 
    } 

} 

system("pause"); 


inFile.close(); 
system("pause"); 
inFile.open(file2.c_str()); 
for (count2=0;count2<26;++count2) 
{ 
    for(i=0;i<5;i++) 
    { 
     for(j=0;j<5;j++) 
     { 
      while (!inFile.eof()) 
       { 
        getline(inFile,mystring2,','); 

        int value2 = atoi(mystring2.c_str()); 
        myarray2[i][j]=value2; 
        cout<<myarray2[i][j]; 


       } 
     } 
    } 

} 
inFile.close(); 
system("pause"); 

for(m=0;m<5;m++) 
{ 

    for(n=0;n<5;n++) 
     { 
      myarray3[m][n]=(myarray1[0][m]*myarray2[n][0]) 
          +(myarray1[1][m]*myarray2[n][1]) 
          +(myarray1[2][m]*myarray2[n][2]) 
          +(myarray1[3][m]*myarray2[n][3]) 
          +(myarray1[4][m]*myarray2[n][4]); 

      cout<<myarray3[m][n]<<endl; 
     } 

system("pause"); 

} 

를 표시 할 때 나는 이상한 출력을 얻고이 출력 내가 얻을 :

please enter the name of the first file 
C:\matrix1.txt 
please enter the name of the second file 
C:\matrix2.txt 
1234554321234543212345432Press any key to continue . . . 
Press any key to continue . . . 
5678923412457892562112345Press any key to continue . . . 
-1546188214 
1030792152 
1030792152 
1030792152 
1030792152 
-1546188228 
-858993456 
-858993456 
-858993456 
-858993456 
-1546188228 
-858993456 
-858993456 
-858993456 
-858993456 
-1546188228 
-858993456 
-858993456 
+0

몇 가지 문제 :'for (count1 = 0; count1 <26; ++ count1)'는 for (count1 = 0; count1 <25; ++ count1)'이 아니어야합니까? 'while (! inFile.eof())'이 Q & A를 참고하십시오 : [iostream :: eof가 루프 상태에있는 것이 잘못된 이유는 무엇입니까?] (http://stackoverflow.com/questions/5605125/why-is-iostreameof- inside-a-loop-condition-considered-wrong) –

+0

데이터 예제를 제공해 주시겠습니까? (출력이 "이상한"방법은 무엇입니까? 계산 후, 즉 나중에 다시 인쇄하려고 할 때 myarray3을 인쇄 할 때 이상한가요?) –

답변

0

이것을 사용하여 파일에서 입력을 가져옵니다. 이 작업까지 희망이

 ifstream inFile; 
     inFile.open(file1.c_str()); 

     i=0; j=0; 
     while (getline(inFile,mystring1)) 
     { 

       int val=0; 

       for(int k=0; k<mystring1.size(); k++) 
       { 

        if(mystring1[k]==',') 
        { 
         myarray1[i][j++]=val; 
         val=0; 
         continue; 
        } 
        val =val*10+mystring1[k]-48; 
       } 
       myarray1[i][j++]=val; 
       i++; 
       j=0; 
      } 

     inFile.close(); 
+0

대단히 감사합니다. D – Jimmyh

+0

입력 파일을 다음과 같이 첨부하십시오. 귀하의 게시물 – aerokite

+0

내 출력을 첨부하고 입력 파일도 dispayed – Jimmyh

0

다른 대답은 이미 매트릭스 곱셈의 논리 오류를 해결합니다. 행렬을 읽는 코드에서 내가 겪고있는 문제를 지적하고 싶다.

당신은이 : 파일에 충분한 데이터가 있다면

for (count1=0;count1<26;++count1) 

, 당신은 독서를 끝낼 것 :

for (count1=0;count1<26;++count1) 
{ 
    for(i=0;i<5;i++) 
    { 
     for(j=0;j<5;j++) 
     { 
     while (!inFile.eof()) 
     { 
      getline(inFile,mystring1,','); 

      int value1 = atoi(mystring1.c_str()); 
      myarray1[i][j]=value1; 
      cout<<myarray1[i][j]; 
     } 
     } 
    } 
} 

내가 어디에 외부 루프와 함께 일을하려고 모르겠어요 파일 번호는 26 x 5 x 5입니다. 파일에 5 x 5 행렬에 대한 데이터 만 있으므로 외부 루프를 완전히 제거 할 수 있습니다.