问题描述
编写一个个人所得税计算程序
计算方式为:1、5000不交税;2、5000-15000:交10%;3、15000-35000:交15%;35000以上交20%;
例如用户工资是38000,则计算方式为50000.0+100000.10+200000.15+30000.20;
#include <iostream>using namespace std;double shuishou(double); //函数原型double basePay; //税前工资const double fir_income_tax = 0.10; //第一档所得税率const double sec_income_tax = 0.15;//第二档所得税率const double tr_income_tax = 0.20;//第三档所得税率const int base = 5000; //开始计算所得税下限const int sec = 15000; //所得税第二档计算上限const int tre = 35000; //所得税最高档计算下限double income_tax; //实际所得税int main(){ cout << '请输入您的工资n'; while (cin >> basePay) //判断用户输入是否有效 {if (basePay == 0 || basePay < 0) //如果用户输入0或小于零程序退出 break;else{ double show; show = shuishou(basePay); cout << '您应交个人所得税为:'<<show << '元n'; cout << '请输入您的工资!n'; cin >> basePay;} } return 0;}double shuishou(double basePay){ if (basePay <= base) //如果用户工资小于等于5000,则不交所得税return 0; else if (basePay > base&&basePay < sec) {//用户工资进入第一档税收计算范围income_tax = (basePay - base)*fir_income_tax; return income_tax; } else if (basePay > sec&&basePay < tre) {//用户工资进入第二档税收计算范围income_tax = (basePay - sec)*sec_income_tax + (sec - base)*0.10;return income_tax; } else if (basePay > tre) {//用户工资进第三档税收计算范围income_tax = (basePay - tre)*tr_income_tax + (tre - sec)*0.15 + (sec - base)*0.10;return income_tax; } return 0;}
以上是我写的程序,这个程序的问题是显示应交所得税后下次输入须先敲下回车,如何使用cin才能输入内容敲回车即可回显,而不是先敲一下再输入才回显?
问题解答
回答1:去掉cin >> basePay; 即可。
#include <iostream>using namespace std;double shuishou(double); //函数原型double basePay; //税前工资const double fir_income_tax = 0.10; //第一档所得税率const double sec_income_tax = 0.15;//第二档所得税率const double tr_income_tax = 0.20;//第三档所得税率const int base = 5000; //开始计算所得税下限const int sec = 15000; //所得税第二档计算上限const int tre = 35000; //所得税最高档计算下限double income_tax; //实际所得税int main(){ cout << '请输入您的工资n'; while (cin >> basePay) //判断用户输入是否有效 {if (basePay == 0 || basePay < 0) //如果用户输入0或小于零程序退出 break;else{ double show; show = shuishou(basePay); cout << '您应交个人所得税为:'<<show << '元n'; cout << '请输入您的工资!n'; cin >> basePay;} } return 0;}double shuishou(double basePay){ if (basePay <= base) //如果用户工资小于等于5000,则不交所得税return 0; else if (basePay > base&&basePay < sec) {//用户工资进入第一档税收计算范围income_tax = (basePay - base)*fir_income_tax; return income_tax; } else if (basePay > sec&&basePay < tre) {//用户工资进入第二档税收计算范围income_tax = (basePay - sec)*sec_income_tax + (sec - base)*0.10;return income_tax; } else if (basePay > tre) {//用户工资进第三档税收计算范围income_tax = (basePay - tre)*tr_income_tax + (tre - sec)*0.15 + (sec - base)*0.10;return income_tax; } return 0;}回答2:
非常感谢!我试试