| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 7596 人关注过本帖
标题:我是新手,请问怎样取整数,去掉小数点后面的数。谢谢!
取消只看楼主 加入收藏
SwanK
Rank: 1
等 级:新手上路
帖 子:68
专家分:3
注 册:2013-1-18
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:5 
我是新手,请问怎样取整数,去掉小数点后面的数。谢谢!
写一个程序,读入一个球体的半径,计算出它的表面积和体积,然后打印
他们向下调整至最接近的整数。
定义一个常数,称为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
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
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
SwanK
Rank: 1
等 级:新手上路
帖 子:68
专家分:3
注 册:2013-1-18
收藏
得分:0 
1。题目是rounded down to the nearest whole number- 如果 用int(volume) 是否合适,符合题意吗?
2。4/3在我程序中算不出正确的数字来 例如我用老师的:假如半径 =5。则 表面积 =314 (这个我可以算对了),体积=532  (这个无论怎么计算都得不出,只得392)
3。究竟怎样表示 4/3 ?

请高手给于帮助。谢谢!
2013-01-20 00:08
SwanK
Rank: 1
等 级:新手上路
帖 子:68
专家分:3
注 册:2013-1-18
收藏
得分:0 
谢谢你们的回答!
我看有3种方式。我用codeblock 试验了一下
4.0/3.0 ---最简单的方法
4/( float)3 -----也可以得出正确结果
或者在定义变量的时候就写上 int a= float 4/3 然后  v= a * PI * r * r *r 也可以的

你们说的C 和 C + + 真的有点不一样哎 , 能否直接给我C++的回答?不过 4.0/3.0 点到了问题解决关键!非常感谢!
如果我以上的想法有错误。请给于指正!
万分感谢!我很喜欢这里。


2013-01-28 02:13
快速回复:我是新手,请问怎样取整数,去掉小数点后面的数。谢谢!
数据加载中...
 
   



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

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