C 练习实例2

C 练习实例2

#include "stdafx.h"

/*

Section 与 Percent 对应关系:

利润<=Section[i] 时 , 提成为Percent[i]

*/

int Section[] = { 100000, 200000, 400000, 600000, 1000000 };

double Percent[] = { (double)10 / 100, 7.5 / 100, (double)5 / 100, (double)3 / 100, 1.5 / 100 };

//超过最大限定值时的提成比

double OtherPercent = (double)1 / 100;

int Len = sizeof(Section) / sizeof(int);

/*

说明:

index 在[0,Len) 范围内时,说明 Profit<= Section[Len-1] 或 Profit<=Section[0]

index = Len 时, 说明 Profit > Section[Len-1]

*/

int Split(const double& profit)

{

int index = -1, i=0;

if (profit <= 0.0)

return index;

for (; i < Len; i++)

{

if ((profit/Section[i]) >1.0)

continue;

index = i;

break;

}

//profit>Section[Len-1]时,index赋值为Len

if (i == Len)

index = i;

return index;

}

int _tmain(int argc, _TCHAR* argv[])

{

double Profit=0.0, GiveOut=0.0;

//输入利润

printf_s("利润:");

scanf_s("%lf", &Profit);

//计算应发放奖金

int index = Split(Profit);

double tempprofit = Profit;

while (index>=0)

{

//超过最大限定值

if (index == Len)

{

GiveOut += (tempprofit - Section[Len-1]) * OtherPercent;

tempprofit = Section[Len-1];

}

else

{

double temp;

//tempprofit 是否在 Section[0] 范围内

temp = (index-1)>=0? tempprofit-Section[index-1] : tempprofit-0;

GiveOut += temp * Percent[index];

tempprofit = (index-1)>=0 ? Section[index-1]:0;

}

index = Split(tempprofit);

}

printf_s("应发放奖金:%g\n", GiveOut);

return 0;

}阿雪 阿雪

wxd***108@126.com

7年前 (2018-05-23)