본문 바로가기

프로그래밍/숙제도둑질

[CPP|초급] mod 클래스

반응형
아마 숙제 출제자의 의도와는 맞지 않는 답일 듯. 나머지연산 클래스를 만들어서 해 봤다. 클래스 연습이겠다.


이게 메인

// ModClass.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Mod.h"


int main(int argc, char* argv[])
{
CMod mod47(47);
int a, b;

printf("7^13 mod 47 = %d\n", mod47.Pow(7, 13));
printf("7^13 x 21 mod 47 = %d = a\n", mod47.Mul(mod47.Pow(7, 13), 21));
printf("14^13 mod 47 = %d\n", mod47.Pow(14, 13));
printf("14^13 x 17 mod 47 = %d = b\n", mod47.Mul(mod47.Pow(14, 13), 17));
a = mod47.Mul(mod47.Pow(7, 13), 21);
b = mod47.Mul(mod47.Pow(14, 13), 17);
printf("a x b mod 47 = %d\n", mod47.Mul(a, b));
printf("a x b mod 47 mod 23 = %d\n", mod47.Mul(a, b) %23);

return 0;
}

여기가 클래스 정의

// Mod.cpp: implementation of the CMod class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Mod.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMod::CMod()
{

}

CMod::CMod(int n)
{
m_n = n;
}

CMod::~CMod()
{

}

int CMod::Add(int a, int b)
{
return (a + b) % m_n;
}

int CMod::Sub(int a, int b)
{
return (a - b) % m_n;
}

int CMod::Mul(int a, int b)
{
return ((a % m_n) * ( b % m_n))% m_n;
}

int CMod::Pow(int a, int b)
{
int ret = 1;

for(int i = 0; i < b; i++)
{
ret = Mul(ret, a);
}
return ret;
}

여기가 소스 프로젝트. VC6.0 프로젝트인데, 별로 대단한 것 없는 콘솔 프로젝트임. 아차, 파이썬으로 검산해 봤는데, 파이썬이 버벅버벅 안하고 기냥 바로 답을 내 줘서 고마웠음.
728x90