#include <stdio.h>
typedef struct
{
long base;
long limit;
double taxRate;
}tRateTable;
tRateTable RateTable[] =
{
{0, 10000, 0.1},
{10000, 20000, 0.12},
{20000, 30000, 0.15},
{30000, 40000, 0.18},
{40000, 1e8, 0.2}
};
/* calculate the tax that you should pay */
double CalTax(double income)
{
int i;
double tax;
tax = 0;
for(i=0; i<(sizeof(RateTable)/sizeof(tRateTable)); i++)
{
if(income >= RateTable[i].base)
{
if(income > RateTable[i].limit)
{
tax += (RateTable[i].limit - RateTable[i].base) * RateTable[i].taxRate;
continue;
}
else
{
tax += (income - RateTable[i].base) * RateTable[i].taxRate;
return tax;
}
}
}
return tax;
}
/* a group of test samples */
int main()
{
int i;
int flag;
flag = 0; //means success
double income[7] = {0, 10000, 20000, 30000, 40000, 50000, 60000};
double sample[7] = {0, 1000, 2200, 3700, 5500, 7500, 9500};
for(i=0; i<7; i++)
{
if(CalTax(income[i]) == sample[i])
{
printf("Test %d succeed.\n", (i+1));
}
else
{
printf("Test %d failed.\n", (i+1));
flag = 1; //means error
}
}
if(flag == 0)
printf("All succeed.\n");
return 0;
}