C 练习实例2
题目:企业发放的奖金根据利润提成。
- 利润(I)低于或等于10万元时,奖金可提10%;
- 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;
- 20万到40万之间时,高于20万元的部分,可提成5%;
- 40万到60万之间时高于40万元的部分,可提成3%;
- 60万到100万之间时,高于60万元的部分,可提成1.5%;
- 高于100万元时,超过100万元的部分按1%提成。
从键盘输入当月利润I,求应发放奖金总数?
程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
实例
#include<stdio.h>
int main()
{
double i;
double bonus1,bonus2,bonus4,bonus6,bonus10,bonus;
printf("你的净利润是:\n");
scanf("%lf",&i);
bonus1=100000*0.1;
bonus2=bonus1+100000*0.075;
bonus4=bonus2+200000*0.05;
bonus6=bonus4+200000*0.03;
bonus10=bonus6+400000*0.015;
if(i<=100000) {
bonus=i*0.1;
} else if(i<=200000) {
bonus=bonus1+(i-100000)*0.075;
} else if(i<=400000) {
bonus=bonus2+(i-200000)*0.05;
} else if(i<=600000) {
bonus=bonus4+(i-400000)*0.03;
} else if(i<=1000000) {
bonus=bonus6+(i-600000)*0.015;
} else if(i>1000000) {
bonus=bonus10+(i-1000000)*0.01;
}
printf("提成为:bonus=%lf",bonus);
printf("\n");
}
以上实例输出结果为:
你的净利润是: 120000 提成为:bonus=11500.000000

文人墨客
参考方法:
#include <stdio.h> #define MAX_ARRAY_PROFIT 6 const double profit1[MAX_ARRAY_PROFIT]={0,100000,200000,400000,600000,1000000}; const double percent[MAX_ARRAY_PROFIT]={0.1,0.075,0.05,0.03,0.015,0.01}; int main() { unsigned char i; double p; double bonus; printf("你的净利润是:\n"); scanf("%lf",&p); for(i=0,bonus=0;i<MAX_ARRAY_PROFIT-1;i++) { if(p>profit1[i] && p<profit1[i+1]) { bonus +=(p-profit1[i])*percent[i]; } if(p>=profit1[i+1]){ bonus +=(profit1[i+1]-profit1[i])*percent[i]; } } if(p>=profit1[MAX_ARRAY_PROFIT-1]){ bonus +=(p-profit1[MAX_ARRAY_PROFIT-1])*percent[MAX_ARRAY_PROFIT-1]; } printf("提成为:bonus=%lf",bonus); printf("\n"); return 0; }文人墨客
对笔记2进行更正:
#include<stdio.h> int main(){ int i; double lirun; double a; double jiangjin = 0; float ticheng[] = {0,0.1, 0.075, 0.05, 0.03, 0.015, 0.01}; printf("您好,请问您的净利润是多少?\n"); scanf("%lf", &lirun); a=lirun; float fanwei[] = {0,100000, 200000, 400000, 600000, 1000000, a}; for (i=1;i<7;i++) { if (a < fanwei[i]){ jiangjin += lirun * ticheng[i]; break; } else { jiangjin += (fanwei[i]-fanwei[i-1]) * ticheng[i]; lirun -= fanwei[i]; } } printf("奖金是%.2lf\n", jiangjin); return 0; }文人墨客
#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; }文人墨客
利用 switch 的击穿现象,分享个计算方法。
#include <stdio.h> int main(){ double d; int money = 100000; float res=0.0; int flag; scanf("%lf",&d); flag = (int)(d/money); flag = flag >10?10:flag; switch(flag){ case 10: res += (d-10*money)*0.01; d = 10*money; case 9: case 8: case 7: case 6: res += (d-6*money)*0.015; d = 6*money; case 5: case 4: res+= (d-4*money)*0.03; d = 4*money; case 3: case 2: res += (d-2*money)*0.05; d = 2*money; case 1: res += (d-money)*0.075; d = money; case 0: res += d *0.1; } printf("%.2f\n",res); return 0; }文人墨客
尝试使用循环优化代码的适用性:
#include<stdio.h> int main() { int i; double lirun; double jiangjin = 0; float fanwei[] = {100000, 200000, 400000, 600000, 1000000}; float ticheng[] = {0.1, 0.075, 0.05, 0.03, 0.015, 0.01}; printf("您好,请问您的净利润是多少?\n"); scanf("%lf", &lirun); for (i=0;i<5;i++) { if (lirun < fanwei[i]) { jiangjin += lirun * ticheng[i]; break; } else { jiangjin += fanwei[i] * ticheng[i]; lirun -= fanwei[i]; } } printf("奖金是%.2lf\n", jiangjin); return 0; }文人墨客
优化如下:
#include <stdio.h> #define WAN 10000 int main() { double I = 0; // 利润 double B = 0; // 奖金 scanf("%lf", &I); I /= WAN; if (I > 100 * WAN) { B += ((I - 100) * 0.01); I = 100; } if (I > 60) { B += ((I - 60) * 0.015); I = 60; } if (I > 40) { B += ((I - 40) * 0.03); I = 40; } if (I > 20) { B += ((I - 20) * 0.05); I = 20; } if (I > 10) { B += ((I - 10) * 0.075); I = 10; } B += (I * 0.1); printf("%lf", B); }