博文

目前显示的是 五月, 2017的博文

在实际编程中遇到的一些问题

在实际编程中遇到的一些问题   ➔ 使用floor函数。floor(x)返回的是小于或等于x的最大整数。     如:floor(10.5) == 10    floor(-10.5) == -11  ➔使用ceil函数。ceil(x)返回的是大于x的最小整数。     如:ceil(10.5) == 11    ceil(-10.5) ==-10      ➔floor()是向负无穷大舍入,floor(-10.5) == -11;    ceil()是向正无穷大舍入,ceil(-10.5) == -10   ➔fix     朝零方向取整,如fix(-1.3)=-1; fix(1.3)=1;     floor     朝负无穷方向取整,如floor(-1.3)=-2; floor(1.3)=1;     ceil     朝正无穷方向取整,如ceil(-1.3)=-1; ceil(1.3)=2;     round     四舍五入到最近的整数,如 round(-1.3)=-1;round(-1.52)=-2;round(1.3)=1;round(1.52)=2  ➔有关头文件的使用       对于floor函数等,要引入<cmath>,不用写成std::floor来使用。 ➔atof函数的使用( ascii to floating point numbers)    头文件:  #include <stdlib.h>    功 能: 把字符串转换成浮点数    

Warning: Control reaches end of non-void function

图片
Warning: Control reaches end of non-void function--Reason and Solution   The reason for this warning is that the compiler thinks your function may never return something at the end of the function, though you are 100% sure that it can return what you expect.  The solution is simple, just add a statement returning something at the end of you function body. And the warning or error will disappear. UPDATE!  Last time I said that the solution the the problem is to add a expression "return 0;" to the end of the function. However, when I was having class, our teacher gave us a better solution! That is: REMOVE THE ELSE SENTENCE !  If the condition is satisfied, the following expression will be executed and an  integer will be returned. If the condition is not satisfied, directly return a -1 and the function is ended. Here is the demonstration: This way is succinct and more logical. But it seems it can't be applied in the first example. ...