Skip to content

Instantly share code, notes, and snippets.

@joennlae
Created November 19, 2016 14:53
Show Gist options
  • Save joennlae/e6643df5f29d937465d22ae339156ff0 to your computer and use it in GitHub Desktop.
Save joennlae/e6643df5f29d937465d22ae339156ff0 to your computer and use it in GitHub Desktop.
// UNCOMMENT THE NEXT LINE TO ENABLE THE TESTS AND BEFORE SUBMITTING
//#include "tests.h"
#include <iostream>
#include <fstream>
#include <string>
char convertToChar(std::string a);
void runThroughString(std::string &a);
int main(){
std::cin >> std::noskipws;
std::string input;
std::cin >> input;
std::ifstream in(input.c_str());
std::string value;
std::string a;
while(in >> value){
a = value;
std::cout << convertToChar(a);
}
}
char convertToChar(std::string a){
int res = 0;
int num = 0;
for(int i = 0; i < 8; i++){ // convert binary to int
if((int)a[i] == 49) num = 1; // convert ascii to int
else num = 0;
res = res * 2 + num;
}
return (char)res;
}
// UNCOMMENT THE NEXT LINE TO ENABLE THE TESTS AND BEFORE SUBMITTING
#include "tests.h"
#include <iostream>
#include <vector>
void read_in(int letter, std::vector<std::vector<int> >& v);
void print(int letter, std::vector<std::vector<int> >& v);
void cross_product(std::vector<std::vector<int> >& a,std::vector<std::vector<int> >& b,std::vector<std::vector<int> >& res);
void matrix_vector(std::vector<std::vector<int> >& a,std::vector<std::vector<int> >& b,std::vector<std::vector<int> >& res);
void matrix_product(std::vector<std::vector<int> >& a,std::vector<std::vector<int> >& b,std::vector<std::vector<int> >& res);
int main(){
char input[2];
std::vector<std::vector<int> >a (3, std::vector<int>(3,0));
std::vector<std::vector<int> >b (3, std::vector<int>(3,0));
std::vector<std::vector<int> >res (3, std::vector<int>(3,0));
//read input
std::cin >> input[0];
std::cin >> input[1];
read_in((int)input[0],a);
read_in((int)input[1],b);
if((int)input[0] == (int)input[1]){
if((int)input[0] == 118){
cross_product(a,b,res);
}
else{
matrix_product(a,b,res);
}
}
else{
matrix_vector(a,b,res);
}
print((int)input[1],res);
}
void read_in(int letter, std::vector<std::vector<int> >& v){
if(letter==118){//v
for(int i = 0; i <3; i++){
std::cin >> v[0][i];
}
}
else{//m //109
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
std::cin >> v[i][j];
}
}
}
}
void print(int letter, std::vector<std::vector<int> >& v){
if(letter==118){//v
for(int i = 0; i <3; i++){
std::cout << v[0][i] << "\n";
}
}
else{//m //109
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
std::cout << v[i][j] << " ";
}
std::cout << "\n";
}
}
}
void cross_product(std::vector<std::vector<int> >& a,std::vector<std::vector<int> >& b,std::vector<std::vector<int> >& res){
int pos = 1;
for(int i = 0; i < 3; i++){
res[0][i] = a[0][pos]*b[0][((pos+1)%3)] - a[0][((pos+1) % 3)]*b[0][pos];
pos = (pos+1)%3;
}
}
void matrix_vector(std::vector<std::vector<int> >& a,std::vector<std::vector<int> >& b,std::vector<std::vector<int> >& res){
for(int i = 0; i < 3 ; i++){
for(int j = 0; j <3; j++){
res[0][i] += b[0][j] * a[i][j];
}
}
}
void matrix_product(std::vector<std::vector<int> >& a,std::vector<std::vector<int> >& b,std::vector<std::vector<int> >& res){
for(int i = 0; i<3; i++){
for(int j=0; j<3; j++){
for(int k=0; k<3; k++){
res[i][j] += a[i][k] * b[k][j];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment