컴퓨터 언어 실습 숙제로 나왔던 가중치 구하기 소스입니다. 참고하세요. 아직 정확하게 구현된건지 검증되지는 않았습니다.
#include
class CWeight{
public:
int w1;
int w2;
int w3;
int w4;
};
class CPattern{
public:
int x1;
int x2;
int x3;
int x4;
};
void Change(CWeight *weight, CPattern pattern, CWeight oldweight,bool flag)
{
if(flag){
weight->w1 = oldweight.w1 - pattern.x1;
weight->w2 = oldweight.w2 - pattern.x2;
weight->w3 = oldweight.w3 - pattern.x3;
weight->w4 = oldweight.w4 - pattern.x4;
}
else{
weight->w1 = oldweight.w1 + pattern.x1;
weight->w2 = oldweight.w2 + pattern.x2;
weight->w3 = oldweight.w3 + pattern.x3;
weight->w4 = oldweight.w4 + pattern.x4;
}
}
int product(CWeight weight,CPattern pattern)
{
int result;
result = (weight.w1 * pattern.x1) + (weight.w2 * pattern.x2)
+ (weight.w3 * pattern.x3) + (weight.w4 * pattern.x4);
return result;
}
void main(void)
{
CWeight weight;
CPattern pat_w[8] = { {0,0,0,1},{1,0,0,1},{1,0,1,1},{1,1,0,1}
,{0,0,1,1},{0,1,1,1},{0,1,0,1},{1,1,1,1}};
int iPatcount, iResult;
bool bChk;
weight.w1 = -1;
weight.w2 = -2;
weight.w3 = -2;
weight.w4 = 0;
bChk = false;
while(!bChk){
bChk = true;
for(iPatcount=0;iPatcount<8;iPatcount++){
iResult = product(weight,pat_w[iPatcount]);
if(iResult < 0 && iPatcount < 4){
Change(&weight,pat_w[iPatcount],weight,false);
bChk = false;
}//if
if(iResult > 0 && iPatcount > 3){
Change(&weight,pat_w[iPatcount],weight,true);
bChk = false;
}//if
}//for
}//while
cout << weight.w1 << "," << weight.w2 << "," << weight.w3 << "," << weight.w4 <<"\n";
}
#include