技術(shù)交流QQ群:1027579432,歡迎你的加入敌蚜!
#include "string"
#include "vector"
#include "fstream"
#include "iostream"
using namespace std;
int cost[10][10];
// 如果源文件中的每行數(shù)據(jù)數(shù)量不一樣,demo2中的方法就不行了
int main(){
int Num_3, Num_2;
int v, w, weight;
ifstream infile; // 讀操作,輸入流
ofstream outfile; // 寫操作还棱,輸出流
infile.open("E:\\C++\\cpp_Code\\data1.txt", ios::in);
if (!infile.is_open())
cout << "打開文件操作失敗...\n";
infile >> Num_3 >> Num_2; // 先讀取第一行
while(0!=Num_3){ // 讀取第3個數(shù)據(jù)
infile >> v >> w >> weight;
cost[v][w] = weight;
cost[w][v] = weight;
Num_3--;
}
while(0!=Num_2){ // 讀取第2個數(shù)據(jù)
infile >> v >> w;
cost[v][w] = 100;
cost[w][v] = 100;
Num_2--;
}
infile.close();
outfile.open("E:\\C++\\cpp_Code\\result1.txt", ios::out);
if(!outfile.is_open())
cout << "打開文件失敗\n";
for(int i=0;i<=9;i++){
for(int j=0;j<=10;j++){
outfile << i << "\t" << j << "\t" << cost[i][j] << endl;
}
}
outfile.close();
return 0;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者