简单循环小程序,求大神帮忙简化,在循环语句前的判断语句怎么才能简化掉
题目要求使用户可以多次输入多笔交易并且程序可以计算每次的佣金。程序在用户输入的交易额为0时终止程序代码:
#include <stdio.h> int main (void) { float commission, value; printf("Enter value of trade: "); scanf ("%f", &value); if (value < 2500.00f) commission = 30.00f + .017f * value; else if (value < 6250.00f) commission = 56.00f + .0066f * value; else if (value < 20000.00f) commission = 76.00f + .0034f * value; else if (value < 50000.00f) commission = 100.00f + .0022f * value; else if (value < 500000.00f) commission = 155.00f + .0011f * value; else commission = 255.00f + .0009f * value; if (commission < 39.00f) commission = 39.00f; if (value == 0) return 0; printf("Commission: $%.2f\n", commission); while (value != 0) { printf("Enter value of trade: "); scanf ("%f", &value); if (value < 2500.00f) commission = 30.00f + .017f * value; else if (value < 6250.00f) commission = 56.00f + .0066f * value; else if (value < 20000.00f) commission = 76.00f + .0034f * value; else if (value < 50000.00f) commission = 100.00f + .0022f * value; else if (value < 500000.00f) commission = 155.00f + .0011f * value; else commission = 255.00f + .0009f * value; if (commission < 39.00f) commission = 39.00f; if (value == 0) break; printf("Commission: $%.2f\n", commission); } return 0; }
参考运行结果:
Enter value of trade: 30000
Commission: $166.00
Enter value of trade: 20000
Commission: $144.00
Enter value of trade: 0
[此贴子已经被作者于2018-12-3 21:10编辑过]