-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
116 lines (102 loc) · 2.81 KB
/
matrix.cpp
File metadata and controls
116 lines (102 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//Take either the input_matrix or input_coordinates file, in txt or csv form,
//and convert the values inside the file into a matrix object (with only the integers value)
#include "matrix.h"
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;
Matrix::Matrix()
{
int** mat;
n = 0;
m = 0;
}
void Matrix::txt_file_to_matrix(unsigned int n, unsigned int m, string file_name){
ifstream file;
file.open(file_name);
//change if name of the gene is an integer
string str;
for(unsigned int i = 0; i < n; i++){
string line;
getline(file,line);
file >> str;
for(unsigned int j = 0; j < m; j++){
file >> mat[i][j];
}
}
file.close();
}
void Matrix::csv_file_to_matrix(unsigned int n, unsigned int m, string file_name){
string row,job[n+1][m+1];
ifstream jobfile(file_name);
std::string fileCommand;
if(jobfile.is_open())
{
// cout << "Successfully open file"<<endl;
int i = 0, j = 0;
while(getline(jobfile,row,','))
{
//cout << "|" << arrival << "|" << endl;
size_t found = row.find("\n");
if (found != std::string::npos) // if newline was found
{
string lastToken = row.substr(0, found);
string nextLineFirstTOken = row.substr(found + 1);
job[i++][j] = lastToken;
j = 0;
if(nextLineFirstTOken != "\n") // when you read the last token of the last line
job[i][j++] = nextLineFirstTOken;
}
else
{
job[i][j++] = row;
}
}//end while
for(int i = 0; i < n+1; ++i)
{
for(int j = 0; j < m+1; ++j)
{
// cout << job[i][j] << " ";
if(i > 0 && j > 0){
mat[i-1][j-1] = stoi(job[i][j]);
}
}
// cout << endl;
}
}//end if for jobfile open
jobfile.close();
}
void Matrix::file_to_matrix(unsigned int n, unsigned int m, string file_name){
mat = new int*[n];
this->n = n;
this->m = m;
for(unsigned int i = 0; i < n; i++){
mat[i] = new int[m];
}
string str_txt ("txt");
string str_csv("csv");
if (file_name.find(str_txt) != string::npos) {
Matrix::txt_file_to_matrix(n,m,file_name);
}
else if(file_name.find(str_csv) != string::npos){
Matrix::csv_file_to_matrix(n,m,file_name);
}
else return;
}
void Matrix::print_matrix(){
cout<<"[";
for (unsigned int i=0;i<n;i++){
cout<<"[";
for (unsigned int j=0;j<m;j++){
cout<<mat[i][j]<<' ';
}
if (i!=n-1){
cout<<"]"<<"\n";
}
else{
cout<<"]"<<"]";
}
}
}