| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 768 人关注过本帖
标题:c++ primer plus初学,编程题练习
只看楼主 加入收藏
天生丽质
Rank: 2
等 级:论坛游民
威 望:1
帖 子:12
专家分:50
注 册:2015-7-26
结帖率:100%
收藏
 问题点数:0 回复次数:7 
c++ primer plus初学,编程题练习
本菜鸟开始学“C++ Primer Plus 第6版 中文版”了,按照章节学习并把章节后面所有的编程题全部做出来,希望大神多多指点!!
搜索更多相关主题的帖子: 中文版 
2015-07-28 21:46
天生丽质
Rank: 2
等 级:论坛游民
威 望:1
帖 子:12
专家分:50
注 册:2015-7-26
收藏
得分:0 
3-4:编写一个程序,要求用户以正数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下面的类似:
Enter the number of seconds:31600000
31600000 seconds=365 days,17 hours,46 minutes,40 seconds
程序代码:
#include<iostream>
using namespace std;

int main()
{
    const int da_to_ho=24;
    const int ho_to_mi=60;
    const int mi_to_se=60;
    long s;
    long days,hours,minutes,seconds;
    cout<<"enter the number of seconds:";
    cin>>s;
    days=s/da_to_ho/ho_to_mi/mi_to_se;
    hours=s%(da_to_ho*ho_to_mi*mi_to_se)/(ho_to_mi*mi_to_se);
    //醉醉的   当初写上一个语句是全部用的是除法  结果小于0出错 杀毒软件说我的这个程序有可能是木马哈哈哈
    minutes=s%(ho_to_mi*mi_to_se)/(mi_to_se);
    seconds=s%(mi_to_se);
    cout<<s<<" seconds ="<<days<<"days,"<<hours<<"hours,"<<minutes<<"minutes,"<<seconds<<"seconds\n";
    return 0;
}
图片附件: 游客没有浏览图片的权限,请 登录注册

2015-07-28 21:51
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:0 
掌柜的也在看这本书,有时间一起交流学习哈
2015-07-28 21:52
天生丽质
Rank: 2
等 级:论坛游民
威 望:1
帖 子:12
专家分:50
注 册:2015-7-26
收藏
得分:0 
回复 3楼 hjx1120
掌柜牛牛的   我向掌柜学习
2015-07-28 21:53
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:0 
回复 4楼 天生丽质
掌柜的是一个小白,人没关系又没后台,人长的又丑,又不能靠脸吃饭,文凭也没有的社会败类,人渣中的战斗机
在不努力自学一门手艺,掌柜的好当心现在的自己,前途全是黑暗
2015-07-28 21:59
天生丽质
Rank: 2
等 级:论坛游民
威 望:1
帖 子:12
专家分:50
注 册:2015-7-26
收藏
得分:0 
编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)将这些信息存储在long long变量中,并让程序显示美国(或其他国家)的人口占全球人的百分比,该程序的输出应与下面类型相似:
enter the world's population:6898758899
enter the population of the us:310783781
the population of the us is 4.50492% of the world population.
程序代码:
#include<iostream>
using namespace std;
int main()
{
    cout.setf(ios_base::fixed,ios_base::floatfield);
    double us,world;//题目要求是longlong类型,可是系统不支持,只能用double来代替。
    cout<<"enter the world's population: ";
    cin>>world;
    cout<<"enter the population of the us: ";
    cin>>us;
    const double n=100;
    double percent=us/world*n;
    cout<<"the population of the us is "<<percent<<"% of the world population.";
    return 0;
}

2015-07-28 22:30
天生丽质
Rank: 2
等 级:论坛游民
威 望:1
帖 子:12
专家分:50
注 册:2015-7-26
收藏
得分:0 
以下是引用天生丽质在2015-7-28 22:30:56的发言:

编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)将这些信息存储在long long变量中,并让程序显示美国(或其他国家)的人口占全球人的百分比,该程序的输出应与下面类型相似:
enter the world's population:6898758899
enter the population of the us:310783781
the population of the us is 4.50492% of the world population.
#include
using namespace std;
int main()
{
    cout.setf(ios_base::fixed,ios_base::floatfield);
    double us,world;//题目要求是longlong类型,可是系统不支持,只能用double来代替。
    cout<<"enter the world's population: ";
    cin>>world;
    cout<<"enter the population of the us: ";
    cin>>us;
    const double n=100;
    double percent=us/world*n;
    cout<<"the population of the us is "<

图片附件: 游客没有浏览图片的权限,请 登录注册

明天换一个软件试试用long long。晚安各位。
2015-07-28 22:32
醒山
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:9
帖 子:463
专家分:2071
注 册:2015-5-25
收藏
得分:0 
楼主写代码好细心,若是我写肯定不会加 cout.setf(ios_base::fixed,ios_base::floatfield);若是那样就太不精确了,要向你学习,掌柜的也牛

[ 本帖最后由 醒山 于 2015-7-29 17:36 编辑 ]
2015-07-29 17:18
快速回复:c++ primer plus初学,编程题练习
数据加载中...
 
   



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

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