typedef class { public: int type; int d0; int d1; int d2; int sum; float ave; } Str0; typedef class { public: int type; int sum; int d0; int d1; int d2; float ave; } Str1; typedef class { public: int sum; float ave; int type; int d0; int d1; int d2; } Str2; Str0 str0 = { 0, 10, 20, 30 }; //sum, ave は省略 Str1 str1 = { 1, 0, 78, 83, 86 }; //ave は省略 Str2 str2 = { 0, 0, 2, 86, 84, 90 }; //type は 2 |
template<class Type> void Sum(Type* str) { switch(str->type) { case 0: str->sum= str->d0+str->d1+str->d2; break; case 1: str->ave= (str->d0+str->d1+str->d2)/3.0f; break; case 2: str->sum= str->d0+str->d1+str->d2; str->ave= str->sum/3.0f; break; } } |
Sum(&str0); Sum(&str1); Sum(&str2); printf("str0 d1=%d d2=%d d3=%d Sum=%d\n", str0.d0,str0.d1,str0.d2,str0.sum); printf("str1 d1=%d d2=%d d3=%d Ave=%f\n", str1.d0,str1.d1,str1.d2,str1.ave); printf("str2 d1=%d d2=%d d3=%d Sum=%d Ave=%f\n", str2.d0,str2.d1,str2.d2,str2.sum,str2.ave); |