如何使trunc工作?

编程入门 行业动态 更新时间:2024-10-19 18:19:10
本文介绍了如何使trunc工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,我刚开始学习编码,c ++即使用代码块。当我想使用trunc时,我遇到了这个问题。我的目标是计算男孩吃了多少苹果,如果他吃了20%剩下的东西,他每天都会离开,直到他剩下3个苹果。他不能吃苹果的一半或三分之一或类似的东西,只吃完整的苹果,所以他只吃健康的数字(即截断)。

Hello, I have just started learning coding, c++ that is, using codeblocks. I came across this problem when I wanted to use trunc. My goal is to count how many apples boy eats and has left every day if he eats 20% of whats left, until he has 3 apples left. He can't eat half or third of apple or anything like that, only full apples, so he eats only healthy number (that is trunc).

while (n>=3) { day++; // day number ate = (n / 100) * 20; // Eats 20% apples left = (n / 100) * 80; // Left 80% apples trunc(n); // Problem around here probably n = left; cout<< day << " day - "; cout << setprecision(0) << fixed << ate << " ate "; cout << setprecision(0) << fixed << n << " left " <<endl; }

我认为问题是这个数字有点(。),因为如果我运行这个程序:

I think problem is the dot ( . ) that number has, because if I run this program:

double numb; cout << "Enter number: "; cin >> numb; trunc (numb); cout << "Number: " << numb<<endl;

如果我用(,)键入1,9或其他东西它有效,但如果我这样做( 。)例如1.9它不再起作用了。 不确定我是否已经足够理解它(英语不是我的主要语言),但我希望你可以帮帮我 [UpDate]

If I type 1,9 or something with (,) it works, but if I do the same with (.) for example 1.9 it doesn't work anymore. Not sure if I described it good enough to understand (Eng. not my main language), but I hope u can help me out [UpDate]

#include iostream #include cmath #include iomanip using namespace std; int main() { double n,day, ate,left; cout << "Apple counter" << endl<<endl; cout << "Type in apple number: "; cin >> n; day = 0; while (n>=3) { day++; // Add's 1 day every loop ate = (n / 100) * 20; // He eats 20% of whats left that day left = (n / 100) * 80; // Left 80% of what was left before n = left; // n is what's left for the next loop calculation trunc(n); // I want n to be fixed to healthy number cout<< day << " day - "; cout << setprecision(0) << fixed << ate << " ate "; cout << setprecision(0) << fixed << n << " left " <<endl; } day++; cout << day << " day - " << "He ate last apples"<<endl; // When boy has 3 or less apples left, he eats them all cout << "No apples left" <<endl; return 0; }

我不知道你是否能理解,但我将我的项目翻译成英文,所以对你来说更容易。将setprecision(0)更改为(2)或任何其他数字,你会看到我的问题。 我尝试过: 将double改为int会完全破坏我的程序。

I don't know if u can understand, but I translated my project to English so its easier for u. Change setprecision (0) to (2) or any other number and u will see my problem. What I have tried: Changing double to int completely breaks my program.

推荐答案

你应该学会尽快使用调试器可能。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。 调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。 调试器 - 维基百科,免费的百科全书 [ ^ ] 掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ] http: //docs.oracle/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ] www.jetbrains/idea/help/debugging-your -first-java-application.html [ ^ ] 逐行在调试器上运行程序,检查变量并查看程序停止匹配的时间和原因你的期望。通过这种方式,您将学到很多关于语言适用性的知识。 对于 trunc ,请在文档中获取战利品,看看代码示例,你的错误应该是显而易见的。 You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect. The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect. Debugger - Wikipedia, the free encyclopedia[^] Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^] docs.oracle/javase/7/docs/technotes/tools/windows/jdb.html[^] www.jetbrains/idea/help/debugging-your-first-java-application.html[^] Run your program on debugger line by line, inspect the variables and see when and why your program stop to match your expectations. This way you will learn a lot about the language suitabilities. For trunc, have a loot at documentation, see code samples, your mistake should be obvious.

问题是,你不明白你的计算机程序只能以正确的格式理解数据。 (计算机和软件可能非常愚蠢)。 ,是系统上浮点数的分隔符,因此它与cin运算符一起使用。 但是。不明白,所以你的程序无法解释它。直接输出你的输入。 我猜1.9会得到与1RTFM9相同的结果; - ) The problem is, that you dont understand that your computer program only understand data in the correct format. (Computers and software can be very stupid). The "," is the delimiter sign on your system for float so it works with the cin operator. BUT the "." isnt understood, so your program cant interpret it. Make an direct output of your input. I guess that "1.9" will give the same result as "1RTFM9" ;-)

更多推荐

如何使trunc工作?

本文发布于:2023-11-25 15:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630304.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   trunc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!