我的代码是:
if(nemrh<=305){ nemf=75; }
if(305<nemrh<=325){ nemcarpan=0.05; nemkalan=nemrh-305; nemaratop=nemkalan*nemcarpan; nemf=76+nemaratop; }
if(325<nemrh<=350){ nemcarpan=0.04; nemkalan=nemrh-325; nemaratop=nemkalan*nemcarpan; nemf=77+nemaratop; }
if(350<nemrh<=370){ nemcarpan=0.05; nemkalan=nemrh-350; nemaratop=nemkalan*nemcarpan; nemf=78+nemaratop; }
if(370<nemrh<=393){ nemcarpan=0.0435; nemkalan=nemrh-370; nemaratop=nemkalan*nemcarpan; nemf=79+nemaratop; }
if(393<nemrh<=415){ nemcarpan=0.0454; nemkalan=nemrh-393; nemaratop=nemkalan*nemcarpan; nemf=80+nemaratop; }
if(415<nemrh<=440){ nemcarpan=0.04; nemkalan=nemrh-415; nemaratop=nemkalan*nemcarpan; nemf=81+nemaratop; }
if(440<nemrh<=465){ nemcarpan=0.04; nemkalan=nemrh-440; nemaratop=nemkalan*nemcarpan; nemf=82+nemaratop; }
if(465<nemrh<=490){ nemcarpan=0.04; nemkalan=nemrh-465; nemaratop=nemkalan*nemcarpan; nemf=83+nemaratop; }
if(490<nemrh<=515){ nemcarpan=0.04; nemkalan=nemrh-490; nemaratop=nemkalan*nemcarpan; nemf=84+nemaratop; }
if(515<nemrh<=540){ nemcarpan=0.04; nemkalan=nemrh-515; nemaratop=nemkalan*nemcarpan; nemf=85+nemaratop; }
if(540<nemrh<=563){ nemcarpan=0.0435; nemkalan=nemrh-540; nemaratop=nemkalan*nemcarpan; nemf=86+nemaratop; }
if(563<nemrh<=592){ nemcarpan=0.0344; nemkalan=nemrh-563; nemaratop=nemkalan*nemcarpan; nemf=87+nemaratop; }
if(592<nemrh<=620){ nemcarpan=0.0357; nemkalan=nemrh-592; nemaratop=nemkalan*nemcarpan; nemf=88+nemaratop; }
if(620<nemrh<=648){ nemcarpan=0.0357; nemkalan=nemrh-620; nemaratop=nemkalan*nemcarpan; nemf=89+nemaratop; }
if(648<nemrh<=678){ nemcarpan=0.0333; nemkalan=nemrh-648; nemaratop=nemkalan*nemcarpan; nemf=90+nemaratop; }
if(678<nemrh<=705){ nemcarpan=0.037; nemkalan=nemrh-678; nemaratop=nemkalan*nemcarpan; nemf=91+nemaratop; }
if(705<nemrh<=735){ nemcarpan=0.03333; nemkalan=nemrh-705; nemaratop=nemkalan*nemcarpan; nemf=92+nemaratop; }
if(735<nemrh<=766){ nemcarpan=0.0322; nemkalan=nemrh-735; nemaratop=nemkalan*nemcarpan; nemf=93+nemaratop; }
if(766<nemrh<=797){ nemcarpan=0.0322; nemkalan=nemrh-766; nemaratop=nemkalan*nemcarpan; nemf=94+nemaratop; }
if(797<nemrh<=830){ nemcarpan=0.0303; nemkalan=nemrh-797; nemaratop=nemkalan*nemcarpan; nemf=95+nemaratop; }
if(830<nemrh<=860){ nemcarpan=0.0333; nemkalan=nemrh-830; nemaratop=nemkalan*nemcarpan; nemf=96+nemaratop; }
if(860<nemrh<=894){ nemcarpan=0.0294; nemkalan=nemrh-860; nemaratop=nemkalan*nemcarpan; nemf=97+nemaratop; }
如何以简短的方式编写此代码?我想纠正它的简短版本。有人能告诉我吗?
答案 0 :(得分:2)
int arr1[] = {305, 325, 350, ..., 860, 894};
double arr2[] = {0.0, 0.05, 0.04, ..., 0.0333, 0.0294};
int arr3[] = {0, 305, 325, ..., 830, 860};
size_t i = 0;
if(nemrh <= 305} { nemf=75; }
for(i = 1; i < sizeof arr1 / sizeof arr1[0]; ++i) {
if(arr1[i-1] < nemrh && nemrh <= arr1[i]) { /* Hoping this is what you want */
nemcarpan = arr2[i];
nemkalan = nemrh - arr3[i];
nemaratop = nemkalan * nemcarpan;
nemf = 75 + i + nemaratop;
}
}
您应该将arr1
和arr2
重命名为更清晰的内容。从prajmus的评论中,如果nemrh保证即使以后也不能修改,你可以更加依赖代码:
int arr1[] = {305, 325, 350, ..., 860, 894};
double arr2[] = {0.0, 0.05, 0.04, ..., 0.0333, 0.0294};
int arr3[] = {0, 305, 325, ..., 830, 860};
size_t i = 0;
for(i = 0; i < sizeof arr1 / sizeof arr1[0]; ++i) {
if(nemrh <= arr1[i]) {
nemcarpan = arr2[i];
nemkalan = nemrh - arr3[i];
nemaratop = nemkalan * nemcarpan;
nemf = (i == 0) ? 75 : 75 + i + nemaratop;
break;
}
}
答案 1 :(得分:1)
假设305<nemrh<=325
表示305 < nemrh && nemrh <= 325
#define BASE 75
const struct
{
int a;
double b;
} NEMRH_ARRGH[] =
{
{ 305, 0.05 }, { 325, 0.04 }, { 350, 0.05 }, { 370, 0.0435 }, { 393, 0.0454 },
{ 415, 0.04 }, { 440, 0.04 }, { 465, 0.04 }, { 490, 0.04 }, { 515, 0.04 },
{ 540, 0.0435 }, { 563, 0.0344 }, { 592, 0.0357 }, { 620, 0.0357 }, { 648, 0.0333 },
{ 678, 0.037 }, { 705, 0.03333 }, { 735, 0.0322 }, { 766, 0.0322 }, { 797, 0.0303 },
{ 830, 0.0333 }, { 860, 0.0294 }
};
if (nemrh <= 305)
nemf = BASE;
// But what about 'nemkalan' and co?
else if (nemrh > 894)
; // Do nothing? Really?
else
for (unsigned i = 0; i < sizeof NEMRH_ARRGH / sizeof *NEMRH_ARRGH; ++i)
if (nemrh > NEMRH_ARRGH[i].a)
{
nemcarpan = NEMRH_ARRGH[i].b;
nemkalan = nemrh - NEMRH_ARRGH[i].a;
nemaratop = nemkalan * nemcarpan;
nemf = BASE + 1 + i + nemaratop;
break;
}