第五周作业

                                                        第五周作业

#include <iostream>  

#include "stdafx.h"  

#include <fstream>  

#include <string.h>  

  

using namespace std;  

  

#define  MATRIX_SIZE 44     //存放txt文档数据的数组长度为22*2 = 44  

#define  VERTEX_NUM 13      //图的顶点数为13  

10 #define  EGDE_NUM 22            //边的数目为13  

11   

12   

13 void getDataFromFile(const char *filename,int *data)            //txt文档的数字存入data数组  

14 {  

15     ifstream fin;  

16     int index = 0;  

17     fin.open(filename, ios::in);  

18     if (!fin)  

19     {  

20         cerr << "the fucking file openning failed!" << endl;  

21         exit(1);  

22     }  

23   

24     //忽略读入顶点数和边数  

25     fin.ignore(5,'/n');                                               

26   

27     while(!fin.eof())                         

28     {  

29         fin >> data[index++];  

30     }  

31   

32     fin.close();  

33     //return index;  

34 }  

35   

36 void writeToFile(const char * fileneme, int tem_array[VERTEX_NUM][VERTEX_NUM], int index)           //将排序好的数组数据写入新的txt文档  

37 {  

38     fstream datafile;  

39     datafile.open(fileneme, ios::out|ios::trunc);  

40     if (!datafile)  

41     {  

42         cerr << "file open failed!" << endl;  

43         exit(1);  

44     }  

45     for (int i = 0; i < index; i ++)  

46     {  

47         for (int j = 0; j < index; j ++)  

48         {  

49             datafile << tem_array[i][j];  

50         }  

51     }  

52     datafile.close();  

53 }  

54   

55 //template <class DataType>  

56 class GraphReverse  

57 {  

58 public:  

59     GraphReverse(int array[], int v, int e);  

60     ~GraphReverse(){};  

61   

62     void pullMatrix(int array[]);               //对邻接矩阵数组填充1  

63     void printMatrix(int array[]);                          //打印邻接矩阵数组  

64     void getMatrix(int  tem_array[VERTEX_NUM][VERTEX_NUM]);  

65     void createReverseGraph(int array[]);               //构建反向图  

66 private:  

67     int vertexNum, egdeNum;  

68     int adj_matrix[VERTEX_NUM][VERTEX_NUM];  

69     int vertex[VERTEX_NUM];  

70     //int egde[EGDE_NUM][EGDE_NUM];  

71 };  

72   

73 GraphReverse::GraphReverse(int array[], int v, int e)  

74 {  

75     vertexNum = v;  

76     egdeNum = e;  

77   

78     for (int i = 0; i < VERTEX_NUM; i ++)                        //初始化邻接矩阵数组  

79     {   for (int j = 0; j < VERTEX_NUM; j ++)  

80     {  

81         adj_matrix[i][j] = 0;  

82     }  

83     }  

84   

85     for (int i = 0; i < VERTEX_NUM; i ++)  

86     {  

87         vertex[i] = i;  

88     }  

89       

90   

91 }  

92   

93 void GraphReverse::pullMatrix(int array[])  

94 {  

95     int a = 0,b = 0;  

96     for (int i = 0; i < MATRIX_SIZE; i = i +2)  

97     {  

98         a = array[i];  

99         b = array[i + 1];  

100         adj_matrix[a][b] = 1;  

101     }  

102 }  

103   

104 void GraphReverse::printMatrix(int array[])  

105 {  

106     for (int i = 0; i < VERTEX_NUM; i ++)  

107     {  

108         cout << i << ": " ;  

109         for (int j = 0; j < MATRIX_SIZE; j = j + 2)  

110         {  

111             if (array[j] == i)  

112             {  

113                 cout << array[j + 1] << "  ";  

114             }  

115         }  

116         cout << endl;  

117     }  

118   

119 }  

120   

121 void GraphReverse::getMatrix(int  tem_array[VERTEX_NUM][VERTEX_NUM])  

122 {  

123     for (int i = 0; i < VERTEX_NUM; i ++)  

124     {  

125         for (int j = 0; j < VERTEX_NUM; j ++)  

126         {  

127             tem_array[i][j] = adj_matrix[i][j];  

128         }  

129     }  

130   

131 }  

132   

133 void GraphReverse::createReverseGraph(int array[])  

134 {  

135     int tem = 0;  

136     for (int i = 0; i < MATRIX_SIZE; i = i + 2)  

137     {  

138         tem = array[i];  

139         array[i] = array[i + 1];  

140         array[i + 1] = tem;  

141     }  

142       

143 }  

144   

145 int main()  

146 {  

147     int * data = new int[MATRIX_SIZE];  

148     int adj_tem_matrix[VERTEX_NUM][VERTEX_NUM];  

149     getDataFromFile("tinyDG.txt", data);  

150     GraphReverse mGraph(data, VERTEX_NUM, EGDE_NUM);  

151   

152     mGraph.pullMatrix(data);  

153     cout << "the adjacency list is:" << endl;  

154     mGraph.printMatrix(data);  

155   

156     cout << "the adjacency list of resverse graph is:" << endl;  

157     mGraph.createReverseGraph(data);  

158     mGraph.printMatrix(data);  

159     //mGraph.getMatrix(adj_tem_matrix);  

160     //writeToFile("tinyG_matrix.txt", adj_tem_matrix,  matrix_num);       

161     delete []data;    

162     return 0;  

163 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值