Source.cpp
#include "Header.h"
void Kaiten::storagePictureSize(const char& input)
{
m_pictureSize = (int)(input - '0');
}
std::string Kaiten::changeHexToBit(const char& hexCharData)
{
std::string result = "";
int hexData;
iswdigit(hexCharData) ? hexData = (int)(hexCharData - '0') : hexData = (int)(hexCharData - 'a' + 10);
for(int i=4; 0<i; i--){
if(pow(2,i-1) <= hexData){
result += "*";
hexData -= pow(2,i-1);
}else{
result += "-";
}
}
return result;
}
std::string Kaiten::changeBitToHex(std::string turnHexData)
{
std::string result = "";
for(int i=0; i<turnHexData.size(); i +=4){
int hexData = 0;
for(int j=0; j<4 && i+j<turnHexData.size(); j++){
if(turnHexData.at(i+j) == '*')
hexData += pow(2,3-j);
}
hexData < 10 ? result += std::to_string(hexData) : result += hexData -10 + 'a';
hexData = 0;
}
return result;
}
std::string Kaiten::getPictureBitDatas(const std::string& input)
{
std::string hexDataString = input.substr(2);
std::string result = "";
for(int i=0; i<hexDataString.size(); i++){
result += changeHexToBit(hexDataString.at(i));
}
return result;
}
std::string Kaiten::makeTurnPicture(const std::string& hexData, char** picture, char** turnPicture)
{
std::string hexStr = "";
for(int i=0; i<m_pictureSize; i++){
for(int j=0; j<m_pictureSize; j++){
picture[i][j] = hexData.at(i*m_pictureSize+j);
}
}
for(int i=0; i<m_pictureSize; i++){
for(int j=0; j<m_pictureSize; j++){
turnPicture[i][j] = picture[m_pictureSize-1-j][i];
}
}
for(int i=0; i<m_pictureSize; i++){
for(int j=0; j<m_pictureSize; j++){
hexStr += turnPicture[i][j];
}
}
return hexStr;
}
const std::string Kaiten::solve(const std::string& input)
{
storagePictureSize(input.at(0));
char** picture = new char*[m_pictureSize];
char** turnPicture = new char*[m_pictureSize];
for( int i=0; i<m_pictureSize; i++ ) {
picture[i] = new char[m_pictureSize];
turnPicture[i] = new char[m_pictureSize];
}
std::string hexData = getPictureBitDatas(input);
std::string turnHexData = makeTurnPicture(hexData, picture, turnPicture);
std::string result = changeBitToHex(turnHexData);
return input.substr(0,2) + result;
}
void Kaiten::test(const std::string& input, const std::string& output)
{
std::string result = solve(input);
if(result == output){
std::cout << "test is OK!" << std::endl;
}else{
std::cout << "test is NG!" << std::endl;
}
}
int main()
{
Kaiten kaiten;
kaiten.test("3:5b8", "3:de0");
kaiten.test("1:8", "1:8");
kaiten.test("2:8", "2:4");
kaiten.test("2:4", "2:1");
kaiten.test("2:1", "2:2");
kaiten.test("3:5d0", "3:5d0");
kaiten.test("4:1234", "4:0865");
kaiten.test("5:22a2a20", "5:22a2a20");
kaiten.test("5:1234567", "5:25b0540");
kaiten.test("6:123456789", "6:09cc196a6");
kaiten.test("7:123456789abcd", "7:f1a206734b258");
kaiten.test("7:fffffffffffff", "7:ffffffffffff8");
kaiten.test("7:fdfbf7efdfbf0", "7:ffffffffffc00");
kaiten.test("8:123456789abcdef1", "8:f0ccaaff78665580");
kaiten.test("9:112233445566778899aab", "9:b23da9011d22daf005d40");
return 0;
};
Header.h
#ifndef _HEADER_H
#define _HEADER_H
#include <string>
#include <iostream>
#include <math.h>
#include <vector>
class Kaiten
{
public:
void test(const std::string& input, const std::string& output);
private:
const std::string solve(const std::string& input);
void storagePictureSize(const char& input);
std::string getPictureBitDatas(const std::string& input);
std::string changeHexToBit(const char& hexCharData);
std::string changeBitToHex(std::string turnHexData);
std::string makeTurnPicture(const std::string& hexData, char** picture, char** turnPicture);
unsigned int m_pictureSize;
};
#endif