两个例题代码如下,第二个例题if else he ?:两种求法:
#include<stdio.h>
#include<windows.h>
void test01()
{
double sales;
float tc;
printf("please input sales:\n");
scanf("%lf", &sales);
//大于2万销售额提出10%,大于1万提出8%,小于1万提成5%
tc = (sales > 20000 ? sales * 0.1 : (sales > 10000 ? sales * 0.08 : sales * 0.05));
printf("tc=%f\n", tc);
}
void test02()
{
double sales;
//利润为percentage ,当年利润profit
double profit, percentage;
printf("please input profit:\n");
scanf_s("%lf", &profit);
//企业利润低于10万提出10%,大于10万<20万 提出7.5%,>20W<40W提成5%,>40W<60W 提出3%,>60W<100W提出1.5%,>100W提出1%
percentage = (profit > 1000000 ? (profit - 1000000) * 1/100 + 100000 * 10/100 + 100000 * 7.5/100 + 200000 * 5/100 + 200000 * 3/100 + 400000 * 1.5/100 : (profit > 600000 ? (profit - 600000) *1.5/100 + 100000 * 10/100 + 100000 * 7.5/100 + 200000 * 5/100 + 200000 * 3/100 : (profit > 400000 ? (profit - 400000) * 3/100 + 100000 * 10/100 + 100000 * 7.5/100 + 200000 * 5/100 : (profit > 200000 ? (profit - 200000) * 5/100 + 100000 * 10/100 + 100000 * 7.5/100 : (profit > 100000 ? (profit - 100000) * 7.5/100 + 100000 * 10/100 : profit * 10/100)))));
printf("profit is %f can get percentage ", profit);
printf("%f\n", percentage);
}
void test03()
{
double sales;
//利润为percentage ,当年利润profit
double profit, percentage;
printf("please input profit:\n");
scanf_s("%lf", &profit);
//企业利润低于10万提出10%,大于10万<20万 提出7.5%,>20W<40W提成5%,>40W<60W 提出3%,>60W<100W提出1.5%,>100W提出1%
if (profit > 1000000)
percentage = (profit - 1000000) * 1/100 + 100000 * 10/100 + 100000 * 7.5/100 + 200000 * 5/100 + 200000 * 3/100 + 400000 * 1.5/100;
else if (profit > 600000)
percentage = (profit - 600000) * 1.5/100 + 100000 * 10/100 + 100000 * 7.5/100 + 200000 * 5/100 + 200000 * 3/100;
else if (profit > 400000)
percentage = (profit - 400000) * 3/100 + 100000 * 10/100 + 100000 * 7.5/100 + 200000 * 5/100;
else if (profit > 200000)
percentage = (profit - 200000) * 5/100 + 100000 * 10/100 + 100000 * 7.5/100;
else if (profit > 100000)
percentage = (profit - 100000) * 7.5/100 + 100000 * 10/100;
else
percentage = profit * 10/100;
/*percentage = (profit > 1000000 ? (profit - 1000000) * 1/100 + 100000 * 10/100 + 100000 * 7.5/100 + 200000 * 5/100 + 200000 * 3/100 + 400000 * 1.5/100 : (profit > 600000 ? (profit - 600000) *1.5/100 + 100000 * 10/100 + 100000 * 7.5/100 + 200000 * 5/100 + 200000 * 3/100 : (profit > 400000 ? (profit - 400000) * 3/100 + 100000 * 10/100 + 100000 * 7.5/100 + 200000 * 5/100 : (profit > 200000 ? (profit - 200000) * 5/100 + 100000 * 10/100 + 100000 * 7.5/100 : (profit > 100000 ? (profit - 100000) * 7.5/100 + 100000 * 10/100 : profit * 10/100)))));*/
printf("profit is %f can get percentage ", profit);
printf("%f\n", percentage);
}
main()
{
test01();
test02();
test03();
system("pause");
}