2

I made this program that calculates tax for a range of salaries based on user input. Everything is working fine except for the fact that i cant seem to find a way to make the application ignore the last 3 printf's and just print an error message if the user enters a negative value. I dont want to display the last 3 printf's if the user enters a negative number, instead i want the application to just display "Error: you have entered a negative number" or something of the sort. I'm using visual studio as my compiler, and this is a C application. Help plz. And also please go easy on me its for my programming class in which we are still doing very basic stuff.

Here is the code:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{

    float salary, tax = 0;
    printf("\n\n\tEnter the salary ammount per year: \t\t$");
    scanf("%f", &salary);

    if (salary <= 10000){
        tax=0;
    } 
    else if (salary > 10000 && salary <= 40000){
        tax = (salary - 10000)*0.2;
    } else if (salary > 40000 && salary <= 50000){
        tax = 6000 + (salary - 40000)*0.3 ;
    } else if (salary > 50000 && salary <= 75000){
        tax = 9000 + (salary - 50000)*0.4;
    } else if (salary > 75000.01 && salary <= 100000){
        tax = 19000  + (salary - 75000)*0.5;
    } else if (salary > 100000 && salary >=100000.01){
        tax = 31500 + (salary - 100000)*0.6;
    }



    printf("\nSalary per year:\t\t $ %.2f\n\n", salary);
    printf("Tax ammount per year:\t\t $ %.2f\n\n", tax);
    printf("Salary after tax:\t\t $ %.2f\n", salary-tax);


    getchar();getchar();
} 
4

3 回答 3

5

您已经证明您知道如何使用if语句来检查一个是否int落在给定范围内。只需将相同的逻辑应用于输出即可。负数是< 0

例如:

if(salary < 0) {
    //print error
} else {

    if (salary <= 10000) {
        tax=0;
    } else if (salary > 10000 && salary <= 40000) {
        tax = (salary - 10000)*0.2;
    } else if (salary > 40000 && salary <= 50000) {
        tax = 6000 + (salary - 40000)*0.3 ;
    } else if (salary > 50000 && salary <= 75000) {
        tax = 9000 + (salary - 50000)*0.4;
    } else if (salary > 75000/*.01*/ && salary <= 100000) {
        tax = 19000  + (salary - 75000)*0.5;
    } else /*if (salary > 100000 && salary >=100000.01)*/ {
        tax = 31500 + (salary - 100000)*0.6;
    }

    printf("\nSalary per year:\t\t $ %.2f\n\n", salary);
    printf("Tax ammount per year:\t\t $ %.2f\n\n", tax);
    printf("Salary after tax:\t\t $ %.2f\n", salary-tax);
}

最后else if,你真的不需要if声明,只需要else. 尽管可以肯定(并且为了清楚起见)包含它是可以的。但是,您绝对不需要** salary >=100000.01) The>=将仅truesalary > 100000已评估为时评估为true。只有双方都评估&&为 ,才会评估为。唯一一种可能是真实的,另一种是不真实的方法是,如果有人输入了一个奇数薪水(100000.005),但即使输入了这个薪水,他们仍然需要在这个税级中。truetrue

无论如何,您的printf语句现在包含在else块的if(salary<0) {}else{}块中,因此它们只会在薪水 >= 0 时打印。

于 2013-10-11T03:55:39.077 回答
1

这不是您问题的答案,但最好让计算机完成所有计算工作。您的代码有一些您必须手动计算的中间结果。为什么不让电脑来做呢?它还允许您通过编辑关键特征、括号和费率轻松更改费率结构,而不必担心中间结果。

float tax = 0;
struct {
   float basis;
   float rate;
} *ptr, rateStructure[] = 
// list these from high to low brackets
{
 {100000,0.6},
 { 75000,0.5},
 { 50000,0.4},
 { 40000,0.3},
 { 10000,0.2},
 {     0,0.1}
};

ptr = rateStructure;
do {
  float bracket = salary - ptr->basis; 
  if (bracket > 0) {
      tax += bracket * ptr->rate; 
      salary -= bracket; 
  }
  ptr++;
} while (salary);
于 2013-10-11T07:05:30.447 回答
0

我认为您的代码容易出错,并且可以简化很多:

if (salary <= 10000){
    tax=0;
} else if (salary <= 40000){
    tax = (salary - 10000)*0.2;
} else if (salary <= 50000){
    tax = 6000 + (salary - 40000)*0.3 ;
} else if (salary <= 75000){
    tax = 9000 + (salary - 50000)*0.4;
} else if (salary <= 100000){
    tax = 19000  + (salary - 75000)*0.5;
} else {
    tax = 31500 + (salary - 100000)*0.6;
}
于 2013-10-11T05:52:05.417 回答