| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 7596 人关注过本帖
标题:我是新手,请问怎样取整数,去掉小数点后面的数。谢谢!
只看楼主 加入收藏
SwanK
Rank: 1
等 级:新手上路
帖 子:68
专家分:3
注 册:2013-1-18
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:16 
我是新手,请问怎样取整数,去掉小数点后面的数。谢谢!
写一个程序,读入一个球体的半径,计算出它的表面积和体积,然后打印
他们向下调整至最接近的整数。
定义一个常数,称为PI,并给它值3.14公式中使用的:

示例输出
半径为5
的表面积是:314
体积为:523
#include <iostream>

using namespace std;

int main()
{
    double radius;
    double surfaceArea;
    int volume;
    double PI;
    double a;
    double b;




    cout << "Program to compute and output the volume and surface area of a sphere" << endl;

     cout <<"Input radius:"<< endl;
    cin >> radius;
    PI  = 3.14;
    a=4;
    b=3;

    surfaceArea=4* PI * radius * radius;
    volume = a / b * PI * radius * radius *radius;


    cout << "Radius is: " << radius << endl;
    cout << "The surface area is:" << surfaceArea << endl;
    cout << "The volume is: " << volume << endl;


    return 0;
}

搜索更多相关主题的帖子: include double volume 
2013-01-18 13:26
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:4 
你想要的是<math.h>中的 double floor( double x ); 吗?
2013-01-18 13:30
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:4 
想保留几位小数就扩大10的多少倍取整

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-01-18 14:11
SwanK
Rank: 1
等 级:新手上路
帖 子:68
专家分:3
注 册:2013-1-18
收藏
得分:0 
谢谢你们那么快的回复!

你看我的程序那里有错误,我刚开始学。
我还没学到“<math.h>中的 double floor( double x ); ”这里。2位
我不要小数位的,只要整数输出
什么叫calculates its surface area and volume then prints them rounded down to the nearest whole number?--这是题目要求。
请帮我看看 ,谢谢!
2013-01-18 14:37
羽VS翼
Rank: 2
等 级:论坛游民
帖 子:26
专家分:43
注 册:2013-1-17
收藏
得分:4 
这还不简单,直接定义一个整数接受结果的值。直接输出就OK了!会有警告,但不会有问题。
例如:double i =3.13;
    int m = i;
    那么m的值就是3了
2013-01-18 14:59
羽VS翼
Rank: 2
等 级:论坛游民
帖 子:26
专家分:43
注 册:2013-1-17
收藏
得分:0 
回复 5楼 羽VS翼
或者直接强转为整形:(int&)double
2013-01-18 15:09
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
以下是引用SwanK在2013-1-18 14:37:23的发言:

谢谢你们那么快的回复!

你看我的程序那里有错误,我刚开始学。
我还没学到“中的 double floor( double x ); ”这里。2位
我不要小数位的,只要整数输出
什么叫calculates its surface area and volume then prints them rounded down to the nearest whole number?--这是题目要求。
请帮我看看 ,谢谢!
我还没学到“<math.h>中的 double floor( double x ); ”这里
------ 你学的是语法,而这是C/C++标准库中的函数。除了这个标准函数外,我想不到你还能用其他什么方法完成题目要求的功能。

什么叫calculates its surface area and volume then prints them rounded down to the nearest whole number?
------ 这句洋文还简单呀:计算它的表面积和体积,然后向下圆整到最近的整数后,打印出它们。
2013-01-18 15:32
张海锋
Rank: 2
等 级:论坛游民
帖 子:52
专家分:37
注 册:2012-12-8
收藏
得分:4 
强制类型转换啊,还有那个PI常量的不是这样定义的吧

2013-01-18 16:11
SwanK
Rank: 1
等 级:新手上路
帖 子:68
专家分:3
注 册:2013-1-18
收藏
得分:0 
感谢你们的回帖。我改了了一下。你们看怎样?请指出错误!谢谢!
#include <iostream>
 
using namespace std;
 //name constants
   const double PI=3.14;
   const int top=4;
   const int base=3;

int main()
 {
  // declare variable
    double radius;
    double surfaceArea;
    double volume;

   //statements:step 1 - step7

    cout << "Program to compute and output the volume and surface area of a sphere" << endl;
 
     cout <<"Enter  radius:"<< endl;                      //step1
     cin >> radius;                                       //step2
     
     surfaceArea=4* PI * radius * radius;                  //step3
     volume = a / b * PI * radius * radius *radius;        //step4
     round(volume)                                         //step5   
   
     cout << "Radius is: " << radius << endl;              //step5
     cout << "The surface area is:" << surfaceArea << endl;//step6
     cout << "The volume is: " << volume << endl;          //step7  
 

    return 0;
 }
 
2013-01-19 11:42
SwanK
Rank: 1
等 级:新手上路
帖 子:68
专家分:3
注 册:2013-1-18
收藏
得分:0 
回复 9楼 SwanK
错了一点:
top=a
base=b
以上要改这2处。忘记了。
谢!
2013-01-19 11:43
快速回复:我是新手,请问怎样取整数,去掉小数点后面的数。谢谢!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019833 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved