正文

钱能的《C++程序设计教程》序列二:chapter2第二部分2005-07-21 12:21:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/xiangyu/3050.html

分享到:

因为字数限制,进行分割。
二、课后练习答案:(不抄题目,没有课本的人可以到网上去下一本)
2.1.
#include<iostream>
using namespace std;
void main()
{
    int a=42486;
    cout<<"42486的8进制输出:"<<oct<<a
        <<",42486的16进制输出:"<<hex<<a<<dec<<endl;//效果是持续的
    unsigned b=42486;
    cout<<signed int(b)<<endl;//result:42486,在我的机器上还没有超出范围
//本题原考虑在16位机器上实验,目前多为32位机器,故不会超过范围的

}

2.2.
#include<iostream>
#include<iomanip>
using namespace std;
void main()
{
    const double PI=3.1415926;//不提倡使用宏定义也就是#define来定义
    double circle,radius;
    cin>>radius;
    circle=2*PI*radius;
    cout<<"圆周率为:"<<setw(13)<<PI<<",The radius is:"<<setw(13)<<radius
        <<",The circle is:"<<setw(13)<<circle<<endl;
}

2.3.(我对输出格式比较厌烦,自己没有做,现在转引别人的答案)
#include <iostream.h>
#include <iomanip.h>

const double e = 2.718281828;

void main()
{
  cout <<setprecision(10) <<e <<endl
       <<setiosflags(ios::fixed) <<setprecision(8) <<e <<endl
       <<setiosflags(ios::scientific) <<e <<endl;
}

2.4.
#include<iostream>
using namespace std;
void main()
{
    const int numofstudent=500;
    cout<<"\"How many students here?\"\n"<<"\""<<numofstudent<<"\""<<endl;
}

2.5.(类似题目在《The C++ programming language》答案序列一已经做得很多,我自己就不作了,答案转载别人的)
#include <iostream.h>
void main()
{
  cout <<"size of char           " <<sizeof(char) <<" byte\n"
       <<"size of unsigned char  " <<sizeof(unsigned char) <<" byte\n"
       <<"size of signed char    " <<sizeof(signed char) <<" byte\n"
       <<"size of int            " <<sizeof(int) <<" byte\n"
       <<"size of unsigned       " <<sizeof(unsigned) <<" byte\n"
       <<"size of signed         " <<sizeof(signed) <<" byte\n"
       <<"size of short          " <<sizeof(short) <<" byte\n"
       <<"size of unsigned short " <<sizeof(unsigned short) <<" byte\n"
       <<"size of long           " <<sizeof(long) <<" byte\n"
       <<"size of signed long    " <<sizeof(signed long) <<" byte\n"
       <<"size of unsigned long  " <<sizeof(unsigned long) <<" byte\n"
       <<"size of float          " <<sizeof(float) <<" byte\n"
       <<"size of double         " <<sizeof(double) <<" byte\n"
       <<"size of long double    " <<sizeof(long double) <<" byte\n";
}

阅读(3576) | 评论(2)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册