计算正确的小数位数时出错 [英] Trouble calculating correct decimal digits

查看:125
本文介绍了计算正确的小数位数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个程序,将做一些简单的计算,但我有麻烦的程序不做正确的数学,或正确放置十进制,或某事。

I am trying to create a program that will do some simple calculations, but am having trouble with the program not doing the correct math, or placing the decimal correctly, or something. Some other people I asked cannot figure it out either.

这里是代码: http://pastie.org/887352

输入以下数据时:


  • 每周工资: 500

  • 提高: 3 li>使用年数: 8

  • Weekly Wage: 500
  • Raise: 3
  • Years Employed: 8

输出以下数据:

Year   Annual Salary
1      $26000.00
2      $26780.00
3      $27560.00
4      $28340.00
5      $29120.00
6      $29900.00
7      $30680.00
8      $31460.00

输出:

Year   Annual Salary
1      $26000.00
2      $26780.00
3      $27583.40
4      $28410.90
5      $29263.23
6      $30141.13
7      $31045.36
8      $31976.72


b $ b

以下是任务的完整描述:

Here is the full description of the task:

8.17(付款计算器应用程序)开发一个应用程序,员工每年超过用户指定年数的金额。假设员工每年收到一次加薪。用户在申请中指定初始每周工资,提高金额(以每年百分比计)以及计算所赚取金额的年数。应用程序应如图所示运行。 8.22。在您的文本。 (图8.22是我的程序应该发布的输出)

8.17 ( Pay Raise Calculator Application) Develop an application that computes the amount of money an employee makes each year over a user- specified number of years. Assume the employee receives a pay raise once every year. The user specifies in the application the initial weekly salary, the amount of the raise (in percent per year) and the number of years for which the amounts earned will be calculated. The application should run as shown in Fig. 8.22. in your text. (fig 8.22 is the output i posted above as what my program should be posting)


  1. 打开模板源代码文件。在文本编辑器或IDE中打开PayRaise.cpp文件。

  1. Opening the template source code file. Open the PayRaise.cpp file in your text editor or IDE.

定义变量并提示用户输入。为了存储用户输入的增加百分比和就业年份,在第12行之后定义int变量rate和年份。同时定义双变量工资以存储用户的年度工资。然后,插入语句,提示用户提高百分比,工作年限和开始每周工资。将键盘输入的值分别存储在rate,years和wage变量中。要找到年度工资,请将新工资乘以52(每年的周数),然后将结果存储在工资中。

Defining variables and prompting the user for input. To store the raise percentage and years of employment that the user inputs, define int variables rate and years, in main after line 12. Also define double variable wage to store the user’s annual wage. Then, insert statements that prompt the user for the raise percentage, years of employment and starting weekly wage. Store the values typed at the keyboard in the rate, years and wage variables, respectively. To find the annual wage, multiply the new wage by 52 (the number of weeks per year) and store the result in wage.

显示表格标题和格式化输出。使用左和setw流操纵器来显示表头,如图所示。 8.22在你的文本。第一列应为六个字符宽。然后使用固定和设置精确流操纵器格式化浮点值,小数点左侧有两个位置。

Displaying a table header and formatting output. Use the left and setw stream manipulators to display a table header as shown in Fig. 8.22 in your text. The first column should be six characters wide. Then use the fixed and setprecision stream manipulators to format floating- point values with two positions to the left of the decimal point.

编写for语句标题。插入for语句。在for语句标头中的第一个分号之前,定义和初始化变量计数器为1.在第二个分号之前,输入循环继续条件,这将导致for语句循环,直到计数器达到输入的年数。在第二个分号后,输入计数器的增量,以使for语句对每个年份执行一次。

Writing a for statement header. Insert a for statement. Before the first semicolon in the for statement header, define and initialize the variable counter to 1. Before the second semicolon, enter a loop- continuation condition that will cause the for statement to loop until counter has reached the number of years entered. After the second semicolon, enter the increment of counter so that the for statement executes once for each number of years.

计算加薪。在for语句的正文中,在第一列中显示计数器的值,在第二列中显示工资值。然后计算下一年的新的周工资,并将结果值存储在工资变量中。为此,请将百分比增加1(请务必将百分比除以100.0),并将结果乘以工资中的当前值。

Calculating the pay raise. In the body of the for statement, display the value of counter in the first column and the value of wage in the second column. Then calculate the new weekly wage for the following year, and store the resulting value in the wage variable. To do this, add 1 to the percentage increase (be sure to divide the percentage by 100.0 ) and multiply the result by the current value in wage.

保存,编译并运行应用程序。输入工资增长的增长百分比和年数。查看结果以确保显示正确的年份,并确保未来工资结果正确。

Save, compile and run the application. Input a raise percentage and a number of years for the wage increase. View the results to ensure that the correct years are displayed and that the future wage results are correct.

关闭命令提示符窗口

我们无法想象!

推荐答案

我将你的for循环改为:

I changed your for loop to this:

cout << (i+1) << "      $" << wage*52 << "\n";
wage = wage * (1+(raise/100.0));

它确实起作用了!我看到你不明白问题的语言。

And it did worked!. I see you didn't understand the language of the problem.

这篇关于计算正确的小数位数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆