c - 如何显示已被赋予多个值的变量的最大值? (C 编程)

标签 c

我正在为我的 C 编程类(class)布置家庭作业,所以我不是在寻找直接的答案,而是在寻找说明或指导。我的代码非常初级,我们现在只用了 3 周的 C,所以我提前道歉。到目前为止,我们只介绍了条件、循环、计数器、多选的切换/中断/继续、逻辑运算符和读取字符,所以如果你提到除此之外的其他内容,我可能会一无所知。

 7 #include <stdio.h>
 8 #include <math.h>
 9
10 int main(void)
11 {
12
13     /*Define variables*/
14     int response, btaxrate, taxrate;
15     float income,taxamount, netincome;
16
17
18     /* Start the dowhile loop for running the program more than once if the user wants to */
19     do{
20
21
22     /*Grab the income amount from the user */
23     printf("Enter the annual income: ");
24     scanf("%f", &income);
25     /* Check to make sure it is above 0 with a while loop */
26     while(income <= 0)
27         {
28             printf("Invalid income, enter the income again: ");
29             scanf("%f", &income);
30         }
31
32     /* Grab the base taxrate from the user */
33     printf("Enter the base tax rate: ");
34     scanf("%d", &btaxrate);
35     /* Check to make sure it is <10 or >30 with a while loop */
36     while(btaxrate < 10 || btaxrate > 30)
37         {
38             printf("Invalid base tax rate, enter the tax rate again: ");
39             scanf("%d", &btaxrate);
40         }
41
42     /* Determine what the taxrate is based on the income amount */
43     if(income >= 0 && income < 50000)
        {
45             taxrate=btaxrate;
46         }
47     if(income >= 50000 && income < 100000)
48         {
49             taxrate=btaxrate+10;
50         }
51     if(income >= 100000 && income < 250000)
52         {
53             taxrate=btaxrate+20;
54         }
55     if(income >= 250000 && income < 500000)
56         {
57             taxrate=btaxrate+25;
58         }
59     if (income >= 500000)
60         {
61             taxrate=btaxrate+30;
62         }
63
64
65     /* Equations for taxamount and netincome */
66     taxamount=(float)(income*taxrate)/(float)100;
67     netincome=(float)income-(float)taxamount;
68
69
70     /* Tell them their tax rate, how much they pay in taxes, and their net income after taxes*/
71     printf("Your tax rate is: %d%%", taxrate);
72     printf("\nYou pay $%.2f in taxes.", taxamount);
73     printf("\nAfter taxes your net income is: $%.2f", netincome);
74
75
76     /* Ask the user if they want to run the program again, get a value for yes if so */
77     printf("\nDo you want to continue? (0:Exit  1:Continue:)");
78     scanf("%d", &response);
79
80
81     } /* End do */
82
83     /* While part of dowhile to see if the program runs again */
84     while(response == 1);

我应该显示根据用户重新运行程序的次数确定的最高值、最低值和平均税额,然后打印出来。因此,例如,如果他们运行了 5 次(按 1 继续)并且 taxamount 的值为 15000、8000、20000、35000 和 100000,我将如何打印“100000 是最高的,8000 是最低的,平均值是 35600"?

最佳答案

您可以创建一个临时变量,在第一次迭代中将 taxamount 的值保存在那里,并在后续迭代中检查新值是否更大并相应地更新它。

从那里我想你可以猜出如何进行剩余的计算。

关于c - 如何显示已被赋予多个值的变量的最大值? (C 编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28628106/

相关文章:

c - 在 Unix 中使用文件重定向后,如何重新定义 stdin 以指向控制台?

c - 十进制到 BASE-26 表示法

c - 开关盒 : case label does not reduce to an integer constant

c - 是否有更优化的方法来处理其中一些功能?

c - 为什么我的编译器不接受 fork(),尽管我包含了 <unistd.h>?

android - 如何在Android中获取#define的值

c - 在c中打印出字符串数组的元素

c - 为什么输出是这样的?

c - 如何使用指针访问使用 malloc 创建的二维数组的元素?

C 代码 - 一种在没有 "if"的情况下将 5 转换为 7 以及将 7 转换为 5 的方法