Trending News
C++ 讀寫檔
求詳細寫法! 以下為說明:
檔案第一行兩數字表示大小為m*n的陣列,然後下面每排的數字各代表 "x" "y" "值"
拿範例中 1 2 3 這排來說就是,即表示 ary[1][2]=3 ,沒列出來的陣列元素皆為0
(也就是該陣列為sparse matrix)
而我現在需要的output就是把陣列全印出來
PS:下面的排數箭頭只是示意,不是範例中會出現的
input範例
5 6 →第一排
1 2 3
4 5 6
2 4 1
3 5 1 →最後一排
output範例
0 0 0 0 0 0 →第一排
0 0 3 0 0 0
0 0 0 0 1 0
0 0 0 0 0 1
0 0 0 0 0 6 →最後一排
不好意思,標題已說是讀寫檔.....也就是要開檔
1 Answer
- 8 years agoFavorite Answer
#include "stdafx.h"//若使用devC++請將此行刪除
#include "stdlib.h"
#include "stdio.h"
int main()
{
int ary[5][6] = {0}, i = 0, j = 0;//設訂陣列與變數初值
ary[1][2] = 3;//設定input初值
ary[4][5] = 6;
ary[2][4] = 1;
ary[3][5] = 1;
for(i;i<5;i++)
{
for(j;j<6;j++)
{
printf("%d",ary[i][j]);
}
j = 0;//印完第一排後歸零
printf("\n");//換行
}
system("Pause");
return 0;
}
2012-11-09 16:50:26 補充:
#include "stdafx.h"//若使用devC++請將此行刪除
#include "stdlib.h"
#include "stdio.h"
#include
#include
#include
using namespace std;
int main()
{
fstream fin, fout;
fin.open("input.txt",ios::in);
fout.open("output.txt",ios::out);
char ch={0};
int ary[10][10]={0};
2012-11-09 16:50:44 補充:
int ary_tmp[50]={0};
int i=0,j=0;
fin.seekg(0,ios::beg);
for(i=1;i<50;i++)
{
fin.get(ch);
if(i%2 != 0)
ary_tmp[i] = atoi(&ch);
}
fin.close();
ary[ary_tmp[5]][ary_tmp[7]] = ary_tmp[9];//設定input初值
ary[ary_tmp[11]][ary_tmp[13]] = ary_tmp[15];
ary[ary_tmp[17]][ary_tmp[19]] = ary_tmp[2
2012-11-09 16:51:05 補充:
ary[ary_tmp[17]][ary_tmp[19]] = ary_tmp[21];
ary[ary_tmp[23]][ary_tmp[25]] = ary_tmp[27];
cout<<"output.txt"<
2012-11-09 16:51:19 補充:
cout<<"output.txt"<
2012-11-09 16:51:44 補充:
for(i=0;i
2012-11-09 16:52:53 補充:
cout<<"output.txt"<
2012-11-09 16:53:20 補充:
不給寫了XD
2012-11-09 16:53:44 補充:
cout<<"output.txt"<
2012-11-09 16:54:40 補充:
cout<<"output.txt"<
2012-11-09 17:00:41 補充:
cout << "output.txt" < < endl;
for(i=0; i
2012-11-09 17:01:15 補充:
cout << "output.txt" < < endl;
for(i=0; i < a ry_tmp[1]; i++)
{
for(j=0; j < a ry_tmp[3]; j++)
{
cout << ary[i][j];
fout << ary[i][j];
}
cout << endl;//換行
fout << endl;
}
fout.close();
system("Pause");
return 0;
}
2012-11-09 17:01:33 補充:
cout << "output.txt" < < endl;
for(i=0; i < a ry_tmp[1]; i++)
{
for(j=0; j < a ry_tmp[3]; j++)
{
cout << ary[i][j];
fout << ary[i][j];
}
cout << endl;//換行
fout << endl;
}
fout.close();
system("Pause");
return 0;
}
2012-11-09 17:03:38 補充:
arytmp 變數中間有空格事我故意的~這樣yahoo才讓我顯示文章
請把空格消去
Source(s): me