質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Q&A

解決済

1回答

565閲覧

隣接行列のデータをファイルから読み込む

pypy7

総合スコア15

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

0グッド

0クリップ

投稿2019/09/07 09:17

グラフのデータが書き込まれているファイルからデータを読み込むプログラムがあるのですが,以下のようなコードの場合グラフのデータファイルをどのように書けばいいのかわかりません.

struct Graph { int nodes; int edges; int* nindex; int* nlist; int* eweight; }; Graph readGraph(const char* const fname) { Graph g; int cnt; FILE* f = fopen(fname, "rb"); if (f == NULL) { fprintf(stderr, "ERROR: could not open file %s\n\n", fname); exit(-1); } cnt = fread(&g.nodes, sizeof(g.nodes), 1, f); if (cnt != 1) { fprintf(stderr, "ERROR: failed to read nodes\n\n"); exit(-1); } cnt = fread(&g.edges, sizeof(g.edges), 1, f); if (cnt != 1) { fprintf(stderr, "ERROR: failed to read edges\n\n"); exit(-1); } if ((g.nodes < 1) || (g.edges < 0)) { fprintf(stderr, "ERROR: node or edge count too low\n\n"); exit(-1); } g.nindex = (int*)malloc((g.nodes + 1) * sizeof(g.nindex[0])); g.nlist = (int*)malloc(g.edges * sizeof(g.nlist[0])); g.eweight = (int*)malloc(g.edges * sizeof(g.eweight[0])); if ((g.nindex == NULL) || (g.nlist == NULL) || (g.eweight == NULL)) { fprintf(stderr, "ERROR: memory allocation failed\n\n"); exit(-1); } cnt = fread(g.nindex, sizeof(g.nindex[0]), g.nodes + 1, f); if (cnt != g.nodes + 1) { fprintf(stderr, "ERROR: failed to read neighbor index list\n\n"); exit(-1); } cnt = fread(g.nlist, sizeof(g.nlist[0]), g.edges, f); if (cnt != g.edges) { fprintf(stderr, "ERROR: failed to read neighbor list\n\n"); exit(-1); } cnt = fread(g.eweight, sizeof(g.eweight[0]), g.edges, f); if (cnt == 0) { free(g.eweight); g.eweight = NULL; } else { if (cnt != g.edges) { fprintf(stderr, "ERROR: failed to read edge weights\n\n"); exit(-1); } } fclose(f); return g; }

形式は圧縮隣接行列?だと思うのですがどのように書けばいいのでしょうか

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

C++

1#include <cstdio> // fopen, fclose, fread, fwrite, printf, putchar 2#include <cstdlib> // exit 3using namespace std; 4 5struct Graph { 6 int nodes; 7 int edges; 8 int *nindex; 9 int *nlist; 10 int *eweight; 11}; 12 13Graph readGraph(const char *const fname) 14{ 15 Graph g; 16 int cnt; 17 18 FILE *f = fopen(fname, "rb"); 19 if (f == NULL) { 20 fprintf(stderr, "ERROR: could not open file %s\n\n", fname); 21 exit(-1); 22 } 23 cnt = fread(&g.nodes, sizeof(g.nodes), 1, f); 24 if (cnt != 1) { 25 fprintf(stderr, "ERROR: failed to read nodes\n\n"); 26 exit(-1); 27 } 28 cnt = fread(&g.edges, sizeof(g.edges), 1, f); 29 if (cnt != 1) { 30 fprintf(stderr, "ERROR: failed to read edges\n\n"); 31 exit(-1); 32 } 33 if ((g.nodes < 1) || (g.edges < 0)) { 34 fprintf(stderr, "ERROR: node or edge count too low\n\n"); 35 exit(-1); 36 } 37 38 g.nindex = (int *) malloc((g.nodes + 1) * sizeof(g.nindex[0])); 39 g.nlist = (int *) malloc(g.edges * sizeof(g.nlist[0])); 40 g.eweight = (int *) malloc(g.edges * sizeof(g.eweight[0])); 41 if ((g.nindex == NULL) || (g.nlist == NULL) || (g.eweight == NULL)) { 42 fprintf(stderr, "ERROR: memory allocation failed\n\n"); 43 exit(-1); 44 } 45 46 cnt = fread(g.nindex, sizeof(g.nindex[0]), g.nodes + 1, f); 47 if (cnt != g.nodes + 1) { 48 fprintf(stderr, "ERROR: failed to read neighbor index list\n\n"); 49 exit(-1); 50 } 51 cnt = fread(g.nlist, sizeof(g.nlist[0]), g.edges, f); 52 if (cnt != g.edges) { 53 fprintf(stderr, "ERROR: failed to read neighbor list\n\n"); 54 exit(-1); 55 } 56 cnt = fread(g.eweight, sizeof(g.eweight[0]), g.edges, f); 57 if (cnt == 0) { 58 free(g.eweight); 59 g.eweight = NULL; 60 } else { 61 if (cnt != g.edges) { 62 fprintf(stderr, "ERROR: failed to read edge weights\n\n"); 63 exit(-1); 64 } 65 } 66 fclose(f); 67 68 return g; 69} 70 71void writeGraph(const Graph *g, const char *fname) 72{ 73 if ((g->nodes < 1) || (g->edges < 0)) { 74 fprintf(stderr, "ERROR: node or edge count too low\n\n"); 75 exit(-1); 76 } 77 FILE *f = fopen(fname, "wb"); 78 if (f == NULL) { 79 fprintf(stderr, "ERROR: could not create file %s\n\n", fname); 80 exit(-1); 81 } 82 int cnt = fwrite(&g->nodes, sizeof(g->nodes), 1, f); 83 if (cnt != 1) { 84 fprintf(stderr, "ERROR: failed to write nodes\n\n"); 85 exit(-1); 86 } 87 cnt = fwrite(&g->edges, sizeof(g->edges), 1, f); 88 if (cnt != 1) { 89 fprintf(stderr, "ERROR: failed to write edges\n\n"); 90 exit(-1); 91 } 92 93 cnt = fwrite(g->nindex, sizeof(g->nindex[0]), g->nodes + 1, f); 94 if (cnt != g->nodes + 1) { 95 fprintf(stderr, "ERROR: failed to write neighbor index list\n\n"); 96 exit(-1); 97 } 98 cnt = fwrite(g->nlist, sizeof(g->nlist[0]), g->edges, f); 99 if (cnt != g->edges) { 100 fprintf(stderr, "ERROR: failed to write neighbor list\n\n"); 101 exit(-1); 102 } 103 cnt = fwrite(g->eweight, sizeof(g->eweight[0]), g->edges, f); 104 if (cnt != g->edges) { 105 fprintf(stderr, "ERROR: failed to write edge weights\n\n"); 106 exit(-1); 107 } 108 fclose(f); 109} 110 111int main() 112{ 113 int index[4] = { 1, 2, 3, 4 }; 114 int list[5] = { 5, 6, 7, 8, 9 }; 115 int weight[5] = { 10, 11, 12, 13, 14 }; 116 117 Graph g, g2; 118 g.nodes = 4 - 1; g.edges = 5; 119 g.nindex = index; g.nlist = list; g.eweight = weight; 120 121 writeGraph(&g, "graph.data"); 122 g2 = readGraph("graph.data"); 123 124 for (int i = 0; i <= g2.nodes; i++) printf(" %d", g2.nindex[i]); 125 putchar('\n'); 126 for (int i = 0; i < g2.edges; i++) printf(" %d", g2.nlist[i]); 127 putchar('\n'); 128 for (int i = 0; i < g2.edges; i++) printf(" %d", g2.eweight[i]); 129 putchar('\n'); 130}

投稿2019/09/07 09:55

kazuma-s

総合スコア8224

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

pypy7

2019/09/09 05:44

ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問