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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#include <iostream>
#include <string>Input 2
#include <fstream>
#include <sstream>
using namespace std;
struct element // i been instructed to use this specifically
{
int r;
int c;
double value;
};
void rowCol(ifstream &file, int &r, int &c)
{
string line = "";
while (getline(file, line))
{
if (line[0] == '#' || line.length() == 0)
continue;
istringstream fn(line);
fn >> r >> c;
break;
}
}
void rowcheck(int row1, int row2, int col1, int col2)
{
if (!(row1 == row2 && col1 == col2))
{
cout << "Cannot compute size of sparse matrix " << endl;
system("pause");
exit(1);
}
if (row1 < 0 || row2 < 0 || col1 < 0 || col2 < 0)
{
cout << "Error size cannot be less than 0 " << endl;
system("pause");
exit(1);
}
}
void inputcheck(ifstream &file1,ifstream &file2)
{
if (!file1 && file2)
{
cout << "Error opening file 1! " << endl;
system("pause");
exit(1);
}
if (file1&&!file2)
{
cout << "Error opening file 2! " << endl;
system("pause");
exit(1);
}
if (!(file1 || file2))
{
cout << "Error opening both files! " << endl;
system("pause");
exit(1);
}
}
int main()
{
string f1, f2;
cout << "Enter name of first file to be read in format name.txt" << endl;
cin >> f1;
cout << "Enter name of second file to be read in format name.txt" << endl;
cin >> f2;
ifstream file1,file2;
file1.open(f1);
file2.open(f2);
inputcheck(file1, file2);
int size1, size2;
string line = "";
int row1 ,col1,row2, col2;
rowCol(file1, row1, col1);
rowCol(file2, row2, col2);
rowcheck(row1, row2, col1, col2);
size1 = row1 * col1;
size2 = row2 * col2;
element *m1 = new element[size1]; // i been instructed to use this
for (int x = 0; x < size1; x++)
{
file1>> m1[x].r >> m1[x].c >> m1[x].value;
}
for (int x = 0; x < size1; x++)
{
if (m1[x].r>0)
cout << m1[x].r << " " << m1[x].c << " " << m1[x].value << endl;
}
cout << "___________" << endl;
element *m2 = new element[size2];
int count = 0; int a, b;
for (int x = 0; x < size2; x++)
{
file2 >> m2[x].r >> m2[x].c >> m2[x].value;
}
for (int x = 0; x < size2; x++)
{
if (m2[x].r> 0)
cout << m2[x].r << " " << m2[x].c << " " << m2[x].value << endl;
}
file1.close(); file2.close();
cout << size1 << endl;
cout << size2 << endl;
delete[] m1;
m1 = NULL;
delete[] m2;
m2 = NULL;
system("pause");
return 0;
}
|