#pragma once
#pragma warning(disable : 4996)
#include <iostream>
#include <fstream>
#include <string>
#include "Vector.h"
#include "Matrix.h"
using namespace std;
int main()
{
string Text;
string oper;
ifstream ReadFile("Input.txt");
ofstream WriteFile("Output.txt");
if (ReadFile.is_open())
{
if (WriteFile.is_open())
{
getline(ReadFile, Text); //vector matrix 구별
if (Text == "vector")
{
double Dresult;
Vector Vresult;
getline(ReadFile, Text); //operation
oper = Text;
Vector vec1;
getline(ReadFile, Text); //dim
if (Text == "3")
{
getline(ReadFile, Text); //1 2 3
char data1[100];
char* ptr1;
char data2[100];
char* ptr2;
for (unsigned int i = 0; i < Text.size(); i++)
{
data1[i] = Text[i];
}
ptr1=strtok(data1, " ");
vec1.x = *ptr1-'0';
ptr1=strtok(NULL, " ");
vec1.y = *ptr1-'0';
ptr1 = strtok(NULL, " ");
vec1.z = *ptr1-'0';
cout << vec1.x << " " << vec1.y << " " << vec1.z;
if (oper == "magn")
{
Dresult= vec1.Magnitude();
WriteFile << Dresult;
}
else if (oper == "add")
{
getline(ReadFile, Text);
if (Text == "3")
{
Vector vec2;
getline(ReadFile, Text); //1 2 3
for (unsigned int i = 0; i < Text.size(); i++)
{
data2[i] = Text[i];
}
ptr2 = strtok(data2, " ");
vec2.x = *ptr2 - '0';
ptr2 = strtok(NULL, " ");
vec2.y = *ptr2 - '0';
ptr2 = strtok(NULL, " ");
vec2.z = *ptr2 - '0';
Vresult=vec1.Add(vec2);
WriteFile << Vresult.x << " " << Vresult.y << " " << Vresult.z << " ";
}
}
else if (oper == "sub")
{
getline(ReadFile, Text);
if (Text == "3")
{
Vector vec2;
getline(ReadFile, Text); //1 2 3
for(unsigned int i = 0; i < Text.size(); i++)
{
data2[i] = Text[i];
}
ptr2 = strtok(data2, " ");
vec2.x = *ptr2 - '0';
ptr2 = strtok(NULL, " ");
vec2.y = *ptr2 - '0';
ptr2 = strtok(NULL, " ");
vec2.z = *ptr2 - '0';
Vresult = vec1.Subtract(vec2);
WriteFile << Vresult.x << " " << Vresult.y << " " << Vresult.z << " ";
}
}
else if (oper == "dot")
{
getline(ReadFile, Text);
if (Text == "3")
{
Vector vec2;
getline(ReadFile, Text); //1 2 3
for (unsigned int i = 0; i < Text.size(); i++)
{
data2[i] = Text[i];
}
ptr2 = strtok(data2, " ");
vec2.x = *ptr2 - '0';
ptr2 = strtok(NULL, " ");
vec2.y = *ptr2 - '0';
ptr2 = strtok(NULL, " ");
vec2.z = *ptr2 - '0';
Dresult = vec1.DotProduct(vec2);
WriteFile << Dresult;
}
}
else if (oper == "cross")
{
getline(ReadFile, Text);
if (Text == "3")
{
Vector vec2;
getline(ReadFile, Text); //1 2 3
for (unsigned int i = 0; i < Text.size(); i++)
{
data2[i] = Text[i];
}
ptr2 = strtok(data2, " ");
vec2.x = *ptr2 - '0';
ptr2 = strtok(NULL, " ");
vec2.y = *ptr2 - '0';
ptr2 = strtok(NULL, " ");
vec2.z = *ptr2 - '0';
Vresult = vec1.CrossProduct(vec2);
WriteFile << Vresult.x << " " << Vresult.y << " " << Vresult.z << " ";
}
}
else
{
printf("잘못된 operation");
}
}
}
else if (Text == "matrix")
{
Matrix Mresult;
getline(ReadFile, Text); //operation
oper = Text;
Matrix mat1;
getline(ReadFile, Text);
char data1[100];
char* ptr1;
char data2[100];
char* ptr2;
char data3[100];
char* ptr3;
for (unsigned int i = 0; i < Text.size(); i++)
{
data1[i] = Text[i];
}
ptr1 = strtok(data1, " ");
mat1.numOfRows = *ptr1 - '0';
ptr1 = strtok(NULL, " ");
mat1.numOfcolumns = *ptr1 - '0';
for (int row = 0; row < mat1.numOfRows; row++)
{
getline(ReadFile, Text);
for (unsigned int i = 0; i < Text.size(); i++)
{
data2[i] = Text[i];
}
ptr2 = strtok(data2, " ");
mat1.ele[row][0] = *ptr2 - '0';
for (int col = 1; col < mat1.numOfcolumns; col++)
{
ptr2 = strtok(NULL, " ");
mat1.ele[row][col] = *ptr2 - '0';
}
}
if (oper == "add")
{
Matrix mat2;
getline(ReadFile, Text);
for (unsigned int i = 0; i < Text.size(); i++)
{
data3[i] = Text[i];
}
ptr3 = strtok(data3, " ");
mat2.numOfRows = *ptr3 - '0';
ptr3 = strtok(NULL, " ");
mat2.numOfcolumns = *ptr3 - '0';
if ((mat1.numOfcolumns == mat2.numOfcolumns) && (mat1.numOfRows == mat2.numOfRows))
{
for (int row = 0; row < mat2.numOfRows; row++)
{
getline(ReadFile, Text);
for (unsigned int i = 0; i < Text.size(); i++)
{
data3[i] = Text[i];
}
ptr3 = strtok(data3, " ");
mat2.ele[row][0] = *ptr3 - '0';
for (int col = 1; col < mat1.numOfcolumns; col++)
{
ptr3 = strtok(NULL, " ");
mat2.ele[row][col] = *ptr3 - '0';
}
}
Mresult = mat1.Add(mat2);
for (int row = 0; row < Mresult.numOfRows; row++)
{
for (int col = 0; col < Mresult.numOfcolumns; col++)
{
WriteFile << Mresult.ele[row][col] << " ";
}
WriteFile << endl;
}
}
else
{
printf("두 행렬의 행, 열 값이 일치하지 않습니다.");
}
}
else if (oper == "sub")
{
Matrix mat2;
getline(ReadFile, Text);
for (unsigned int i = 0; i < Text.size(); i++)
{
data3[i] = Text[i];
}
ptr3 = strtok(data3, " ");
mat2.numOfRows = *ptr3 - '0';
ptr3 = strtok(NULL, " ");
mat2.numOfcolumns = *ptr3 - '0';
if ((mat1.numOfcolumns == mat2.numOfcolumns) && (mat1.numOfRows == mat2.numOfRows))
{
for (int row = 0; row < mat2.numOfRows; row++)
{
getline(ReadFile, Text);
for (unsigned int i = 0; i < Text.size(); i++)
{
data3[i] = Text[i];
}
ptr3 = strtok(data3, " ");
mat2.ele[row][0] = *ptr3 - '0';
for (int col = 1; col < mat1.numOfcolumns; col++)
{
ptr3 = strtok(NULL, " ");
mat2.ele[row][col] = *ptr3 - '0';
}
}
Mresult = mat1.Subtract(mat2);
for (int row = 0; row < Mresult.numOfRows; row++)
{
for (int col = 0; col < Mresult.numOfcolumns; col++)
{
WriteFile << Mresult.ele[row][col] << " ";
}
WriteFile << endl;
}
}
else
{
printf("두 행렬의 행, 열 값이 일치하지 않습니다.");
}
}
else if (oper == "mult")
{
Matrix mat2;
getline(ReadFile, Text);
for (unsigned int i = 0; i < Text.size(); i++)
{
data3[i] = Text[i];
}
ptr3 = strtok(data3, " ");
mat2.numOfRows = *ptr3 - '0';
ptr3 = strtok(NULL, " ");
mat2.numOfcolumns = *ptr3 - '0';
for (int row = 0; row < mat2.numOfRows; row++)
{
getline(ReadFile, Text);
for (unsigned int i = 0; i < Text.size(); i++)
{
data3[i] = Text[i];
}
ptr3 = strtok(data3, " ");
mat2.ele[row][0] = *ptr3 - '0';
for (int col = 1; col < mat2.numOfcolumns; col++)
{
ptr3 = strtok(NULL, " ");
mat2.ele[row][col] = *ptr3 - '0';
}
}
Mresult = mat1.Multiply(mat2);
for (int row = 0; row < Mresult.numOfRows; row++)
{
for (int col = 0; col < Mresult.numOfcolumns; col++)
{
WriteFile << Mresult.ele[row][col] << " ";
}
WriteFile << endl;
}
}
else if (oper == "trans")
{
Mresult=mat1.Transpose();
for (int row = 0; row < Mresult.numOfRows; row++)
{
for (int col = 0; col < Mresult.numOfcolumns; col++)
{
//WriteFile
//cout<< Mresult.ele[row][col] << " ";
}
//WriteFile
//cout<< endl;
}
}
else
{
printf("잘못된 operation");
}
}
else
{
printf("잘못된 target");
}
}
}
ReadFile.close();
WriteFile.close();
return 0;
}