| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 727 人关注过本帖
标题:求解答,非零整数的倒数
只看楼主 加入收藏
大笑的蚂蚁
Rank: 1
等 级:新手上路
帖 子:12
专家分:5
注 册:2012-6-9
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:6 
求解答,非零整数的倒数
编写一个程序,计算用户输入非0整数的倒数,该程序应把计算的结果存储在double类型的变量中,再输出它

#include<iostream>
using std::cout;
using std::endl;
using std::cin;

int main() {
   
    int input=0;
   
    cout<<"enter interger number:"<<endl;
    cin >>input;
   
    int output=1/input;
   
    double reciprocal =static_cast<int>(output);
   
    cout<<"the number you get reciprocal is :"<<endl;
    cout<<reciprocal<<endl;
   
    return 0;
   
}

以上这样是正确的吗???????????????
搜索更多相关主题的帖子: double include return number 
2012-06-21 17:35
liudw2
Rank: 4
等 级:业余侠客
帖 子:85
专家分:248
注 册:2011-7-31
收藏
得分:4 
错了,input不要初始化, 还有output的数据类型要注意, int的倒数是0,你先执行 int output = 1/input之后的话,output已经为0了
2012-06-21 23:07
kangtachan
Rank: 2
等 级:论坛游民
帖 子:8
专家分:16
注 册:2012-6-19
收藏
得分:5 
2楼的哥哥,input不初始化vc里面会变成int类型的最大值;input初不初始化和结果没关系,因为c无论怎样都是破坏性写入的;

安全类型转换static_cast<int>(output);这里好像不对哦~你这样写的意思是output类型安全转换成为int型,然后double XX=int XXX;

我猜楼主的意思应该是想将int安全转换成double然后再赋值给double变量
2012-06-22 01:49
大笑的蚂蚁
Rank: 1
等 级:新手上路
帖 子:12
专家分:5
注 册:2012-6-9
收藏
得分:0 
回复 3楼 kangtachan
我猜楼主的意思应该是想将int安全转换成double然后再赋值给double变量

正是这个意思,我应当这样写   : double reciprocal=static_cast<double>(input) 这样对吗??

另外想问一下,我写的这个程序,正确吗???

还想问一句,你用什么样的c++编译软件
2012-06-22 02:39
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:5 
#include<iostream>
using std::cout;
using std::endl;
using std::cin;

int main() {

    int input=0;

    cout<<"enter interger number:"<<endl;
    cin >>input;

    if(input > 0)
    {
        double reciprocal = 1 / static_cast<double>(input);

        cout<<"the number you get reciprocal is :"<<endl;
        cout<<reciprocal<<endl;
    }
    else cout << "error: please Non negative number!" << endl;

    return 0;

}

我们都在路上。。。。。
2012-06-22 10:41
大笑的蚂蚁
Rank: 1
等 级:新手上路
帖 子:12
专家分:5
注 册:2012-6-9
收藏
得分:0 
回复 5楼 hellovfp
小问一下,你用什么样的编译软件呢
2012-06-22 15:37
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:6 
回复 6楼 大笑的蚂蚁
MinGW(gcc)编译器

我们都在路上。。。。。
2012-06-22 15:42
快速回复:求解答,非零整数的倒数
数据加载中...
 
   



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

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