CPP_Basic_Code_P3.1-PP3.7.6

CPP_Basic_Code_P3.1-PP3.7.6

//  The Notes Created by Z-Tech on 2017/2/17.
//  All Codes Boot on 《C++ Primer Plus》V6.0
//  OS:MacOS 10.12.4
//  Translater:clang/llvm8.0.0 &g++4.2.1
//  Editer:iTerm 2&Sublime text 3
//  IDE: Xcode8.2.1&Clion2017.1

//P3.1
#include <iostream>
#include <climits>
int main()
{
    using namespace std;
    int n_int=INT_MAX;
    short n_short=SHRT_MAX;//指定最大值
    long n_long=LONG_MAX;
    long long n_llong=LLONG_MAX;
    cout<<"int is "<<sizeof (int)<<" bytes."<<endl;//此处不用n_int而是对类型名int使用,所以必须加()
    cout<<"short is "<<sizeof n_short<<" bytes."<<endl;
    cout<<"long is "<<sizeof n_long<<" bytes."<<endl;
    cout<<"long long is "<<sizeof n_llong<<" bytes."<<endl;
    cout<<endl;

    cout<<"maximum values:"<<endl;
    cout<<"int: "<<n_int<<endl;
    cout<<"short: "<<n_short<<endl;
    cout<<"long: "<<n_long<<endl;
    cout<<"long long: "<<n_llong<<endl<<endl;

    cout<<"Minimum int value = "<<INT_MIN<<endl;//最小值
    cout<<"Bits per byte = "<<CHAR_BIT<<endl;//字节的位数
    return 0;
}

//P3.2
#include <iostream>//有无符号的short溢出后值的变化
#define ZERO 0
#include <climits>
int main()
{
    using namespace std;
    short sam= SHRT_MAX;
    unsigned short sue=sam;

    cout<<"Sam has "<<sam<<" dollars and Sue has "<<sue;
    cout<<" dollars deposited."<<endl//注意此处并未结束
        <<"Add $1 to each account."<<endl<<"Now ";
    sam++;//32767+1>-32768;整形溢出跳转
    sue++;//32767+1=32768
    cout<<"Sam has "<<sam<<" dollars and sue has "<<sue;
    cout<<" dollars deposited.\nPoor Sam!"<<endl;
    sam=sue=ZERO;
    cout<<"Sam has "<<sam<<" dollars and Sue has "<<sue;
    cout<<" dollars deposited."<<endl;
    cout<<"Take $1 from each account."<<endl<<"Now ";
    sam--;//0-1=-1
    sue--;//0-1>65535,整形溢出跳转
    cout<<"Sam has "<<sam<<" dollars and Sue has "<<sue;
    cout<<" dollars deposited."<<endl<<"Luck Sue!"<<endl;
    return 0;
}

//P3.3
#include <iostream>
int main()
{
    using namespace std;
    int chest=42;//十进制
    int waist=0x42;//十六进制
    int inseam=042;//八进制
    cout<<"Monsieur cuts a striking figure!\n";//默认情况下,cout始终以十进制输出
    cout<<"chest= "<<chest<<" (42 in decimal)\n";
    cout<<"waist= "<<waist<<" (0x42 in hex)\n";
    cout<<"inseam= "<<inseam<<" (042 in octal)\n";
    return 0;
}

//P3.4
#include <iostream>
using namespace std;
int main()
{
    int chest=42;
    int waist=42;
    int inseam=42;

    cout<<"Monsieur cuts a striking figure!"<<endl;
    //默认输出即为十进制
    cout<<"chest = "<<chest<<" (decimal for 42)"<<endl;
    cout<<hex;//十六进制,对后方有效,且控制符位于std中
    cout<<"waist = "<<waist<<" (hexadecimal for 42)"<<endl;
    cout<<oct;//八进制
    cout<<"inseam = "<<inseam<<" (octal for 42)"<<endl;

    return 0;
}

//P3.5
#include <iostream>
int main()
{
    using namespace std;
    char ch;
    cout<<"Enter a character: "<<endl;
    cin>>ch;
    cout<<"Hola! ";
    cout<<"Thank you for the "<<ch<<" character."<<endl;
    return 0;
}

//P3.6
#include <iostream>
int main()
{
    using namespace std;
    char ch='M';//如果没有''则将被视为一个未声明的变量
    int i=ch;
    cout<<"The ASCII code for "<<ch<<" is "<<i<<endl;
    cout<<"Add one to the character code:"<<endl;
    i=++ch;
    cout<<"The ASCII code for "<<ch<<" is "<<i<<endl;
    cout<<"Displaying char ch using cout.put(ch): ";
    cout.put(ch);//输出ch所代表的字符
    cout.put('!');//直接输出字符需要''
    cout<<endl<<"Done!Done!Done!"<<endl;
    return 0;
}

//P3.7
#include <iostream>
int main()
{
    using namespace std;
    cout<<"\aOperation \"HypeHype\" is now activated!\n";// \a为振铃符
    cout<<"Enter your agent code:________\b\b\b\b\b\b\b\b";// \b为退格符,可移动光标的位置
    long code;
    cin>>code;
    cout<<"\ayou entered "<< code <<"...\n";
    cout<<"\aCode verified! Proceed with Plan Z3!\n";
    return 0;
}

//P3.7.extra
#include <iostream>
int main()
{
    using namespace std;
    int k\u00F6rper;//变量名为Körper
    cout<<"Let them eat g\u00E2teau.\n";//gâteau
    return 0;
}

//P3.8
#include <iostream>
int main()
{
    using namespace std;
    cout.setf(ios_base::fixed,ios_base::floatfield);//强制显示小数点,不显示e表示法
    float tub=10.0/3.0;//均被初始化为3.333333
    double mint=10.0/3.0;
    const float million=1.0e6;//十的六次方

    cout<<"tub="<<tub;
    cout<<", a million tubs = "<<million*tub;//这里float只精确到小数点后六位,故显示结果不正确
    cout<<",\nand ten million tubs = ";
    cout<<10*million*tub<<endl;//因为已经超出限制,再乘以十依然不正确

    cout<<"mint = "<<mint<<" and a million mint = ";//double精度到15位,所以13位是有效的
    cout<<million*mint<<endl;
    return 0;
}

//P3.9
#include <iostream>
int main()
{
    using namespace std;
    float a=2.34E+22f;//2.34*10^22,当22变为6时,结果为1
    float b=a+1.0f;//因为float只能表示前6-7位,所以会被系统忽略

    cout<<"a = "<<a<<endl;
    cout<<"b-a = "<<b-a<<endl;
    return 0;
}

//P3.10
#include <iostream>
int main()
{
    using namespace std;
    float hats,heads;

    cout.setf(ios_base::fixed,ios_base::floatfield);//修复小数点后的零,强制显示
    cout<<"Enter a number: ";
    cin>>hats;
    cout<<"Enter another number: ";
    cin>>heads;
    cout<<"hats = "<<hats<<";heads = "<<heads<<endl;
    cout<<"hats + heads = "<<heads+hats<<endl;
    cout<<"hats * heads = "<<hats*heads<<endl;
    cout<<"hats / heads = "<<hats/heads<<endl;
    //cout<<"hats % heads = "<<hats%heads<<endl;该行求模不能使用浮点数
    return 0;
}

//P3.11
#include <iostream>
int main()
{
    using namespace std;
    cout.setf(ios_base::fixed, ios_base::floatfield);
    cout << "Integer division: 9/5 = " << 9 / 5 << endl;
    cout << "Floating-Point division: 9.0/5.0 = ";
    cout << 9.0 / 5.0 << endl;
    cout << "Mixed division: 9.0/5 = " << 9.0 / 5 << endl;
    cout << "Double constant: 1e7/9.0 = ";
    cout << 1.e7 / 9.0 << endl;//1e7和1.e7是等价的。而且不加参数默认double
    cout<<"Float constant: 1e7f/9.0f = ";
    cout<<1.e7f/9.0f<<endl;//两个数都为float,结果只能为float,6位有效数字
    return 0;
}

//P3.12
#include <iostream>
int main()
{
    using namespace std;
    const int Lbs_per_stn=14;
    int lbs;

    cout<<"Enter your weight in pound: ";
    cin>>lbs;
    int stone=lbs/Lbs_per_stn;//计算整的Stone
    int pounds=lbs%Lbs_per_stn;//计算余下的英镑数
    cout<<lbs<<" Pounds are "<<stone<<" Stone , "<<pounds<<" Pound(s).\n";
    return 0;
}

//P3.13
#include <iostream>
int main()
{
    using namespace std;
    cout.setf(ios_base::fixed,ios_base::floatfield);
    float tree=3;
    int guess(3.9832);//括号赋值法
    int debt=7.2E12;//每一个编译器均不同
    cout<<"Tree = "<<tree<<endl;
    cout<<"guess = "<<guess<<endl;
    cout<<"Debt = "<<debt<<endl;
    return 0;
}

//P3.14
#include <iostream>
int main()
{
    using namespace std;
    int auks,bats,coots;
    auks=19.99+11.99;
    bats=(int) 19.99+(int) 11.99;//C 语言老式写法
    coots=int (19.99)+ int (11.99);
    cout<<"auks= "<<auks<<", bats= "<<bats;
    cout<<", coots= "<<coots<<endl;

    char ch='Z';
    cout<<"The Code for "<<ch<<" is ";
    cout<<int(ch)<<endl;
    cout<<"Yes,the code is ";
    cout<<static_cast<int>(ch)<<endl;  
    //调用static_cast<>将ch强制类型转换为int
    return 0;
}

//PP3.7.1
#include <iostream>
int main()
{
    using namespace std;
    cout<<"Enter your height by foot:__\b\b";//两个删除符号
    int height;
    cin>>height;
    const int inchV=12;//转换因子inchV
    int inch=height/inchV;
    int foot=height%inchV;
    cout<<"So your height is "<<inch<<" inch and "
        <<foot<<" foot.";
    return 0;
}

//PP3.7.2
#include <iostream>
int main()
{
    using namespace std;
    cout<<"Please enter your height by inch and foot(Use Space between them): ";
    float inch,foot;//参考身高数据:5 inch&7.9 foot,约1.72m
    cin>>inch>>foot;//参考体重数据:120 pound,约54.5kg
    cout<<"Please enter your Weight by Pound : ";
    float pound;
    cin>>pound;
    const int hcv=12;
    const float wcv=2.2;
    float mi=(inch*hcv+foot)*0.0254;
    float kg=pound/2.2;
    float bmi=kg/(mi*mi);
    cout<<"So your height is "<<mi<<" metre and your weight is "<<kg<<" kg."<<endl;
    cout<<"Then your BMI is "<<bmi<<endl;
    cout<<"Thank you ver much.";
    return 0;
}

//PP3.7.3
#include <iostream>
int main()
{
    using namespace std;
    cout<<"Enter a latitude in degrees,minutes,and seconds: ";
    cout<<"First,enter the degrees: ";
    float degrees;
    cin>>degrees;
    cout<<"Next,enter the minutes: ";
    float minutes;
    cin>>minutes;
    cout<<"Finally,enter the seconds: ";
    float seconds;
    cin>>seconds;
    const int sv=60;//const定义需要指明具体的常量类型
    cout<<degrees<<" degrees, "<<minutes<<" minutes, "<<seconds<<" seconds "
        <<" = "<<degrees+minutes/sv+seconds/sv/sv<<" degrees.";
    return 0;
}

//PP3.7.4
#include <iostream>
int main()
{
    using namespace std;
    cout<<"Enter the numbers of seconds: ";
    long allseconds;
    cin>>allseconds;
    int allhours=allseconds/3600;//全部小时数
    short days=allhours/24;//除法计算天数
    short hours=allhours%24;//求模算出小时数
    long allminutes=allseconds/60;//全部分钟数
    short minutes=allminutes%(allhours*60);//求模算出分钟数
    short seconds=allseconds%60;//求模算出秒数
    cout<<allseconds<<"seconds = "<<days<<" days "<<hours<<" hours "
        <<minutes<<" minutes "<<seconds<<" seconds.";
    return 0;
}

//PP3.7.5
#include <iostream>
int main()
{
    using namespace std;
    cout.setf(ios_base::fixed, ios_base::floatfield);
    cout<<"Enter the world's population: ";
    long double wpplt;
    cin>>wpplt;
    cout<<"Enter the population of the U.S: ";
    long double upplt;
    cin>>upplt;
    cout<<"The population of U.S is "<<(upplt/wpplt)*100<<"% of the world population.";//百分比要*100
    return 0;
}

//PP3.7.6
#include <iostream>
int main()
{
    using namespace std;
    cout<<"Enter your drive distance: ";
    double distance;
    cin>>distance;
    cout<<"Next,enter the oil by 升: ";
    float oil;
    cin>>oil;
    cout<<"100km run out of "<<(oil/distance)*100<<"升。";
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,839评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,543评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,116评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,371评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,384评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,111评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,416评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,053评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,558评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,007评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,117评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,756评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,324评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,315评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,539评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,578评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,877评论 2 345

推荐阅读更多精彩内容