如何使trunc工作? [英] How do I make trunc work?

查看:75
本文介绍了如何使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.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/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[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/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工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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