美文网首页
CPP_Basic_Code_P2.1-PP2.7.7

CPP_Basic_Code_P2.1-PP2.7.7

作者: 贴墙上的咖啡 | 来源:发表于2017-03-26 22:19 被阅读0次

    CPP_Basic_Code_P2.1-PP2.7.7

    //  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
    
    //P2.1
    #include <iostream>//Hello,C++
    int main()
    {
        using namespace std;
        cout <<"Come up and C++ me some time.";
        cout <<endl;
        cout <<"You won't regret it"<<endl;
        return 0;
    }
    
    //P2.2
    #include <iostream>
    int main()
    {
        using namespace std;
        int carrots;
        carrots=25;
        cout<<"I Have ";
        cout<<carrots;
        cout<<" carrots.";//注意前后空格
        cout<<endl;
        carrots=carrots-1;
        cout<<"Crunch,crunch,Now I have "<<carrots<<" carrots."<<endl;
        return 0;
    }
    
    //P2.3
    #include <iostream>
    int main()
    {
        using namespace std;
        int carrots;
        cout<<"How many Carrots do you have?"<<endl;
        cin>>carrots;
        cout<<"Here are two more. ";
        carrots=carrots+2;
        cout<<"Now you have "<<carrots<<" carrots."<<endl;//输出流
        return 0;
    }
    
    //P2.4
    #include <iostream>
    #include <cmath>//是C中math.h的升级
    int main()
    {
        using namespace std;
        double area;
        cout<<"Enter the floor area,in square feet,of your home:";
        cin>>area;
        double side=sqrt(area);//开方计算
        cout<<"That's the equvalent of a square  "<<side
            <<" feet to the side."<<endl;
        cout<<"How Fascinating"<<endl;
        return 0;
    }
    
    //P2.5
    #include <iostream>
    void simon(int)//函数声明
    int main()
    {
        using namespace std;
        simon(3);
        cout<<"Pick an integer: ";
        int count;
        cin>>count;
        simon(count);
        cout<<"Done"<<endl;
        return 0;
    }
    void simon(int n)//打印函数,无返回值。
    {
        using namespace std;
        cout<<"Simon says touch your toes "<<n<<" times"<<endl;
    }
    
    //P2.6
    #include <iostream>
    int stonetolb(int);
    int main()
    {
        using namespace std;
        int stone;
        cout<<"Enter the weight in stone:";
        cin>>stone;
        int pounds=stonetolb(stone);
        cout<<stone<<" stone= ";
        cout<<pounds<<" pounds."<<endl;
        return 0;
    }
    int stonetolb(int sts)
    {
        return 14*sts;//1英石=14磅,使用表达式避免创建新变量
    }
    
    //PP2.7.2
    #include <iostream>
    int main()
    {
        using  namespace std;
        cout<<"Please input a distance:";
        double xLong;
        cin>>xLong;
        double  Ma=220*xLong;
        cout<<xLong<<"long = "<<Ma<<"Ma";
        return 0;
    }
    
    //PP2.7.3
    #include <iostream>
    void z_tech1();
    void z_tech2();
    int main()
    {
        using namespace std;
        z_tech1();//无参数无返回值
        z_tech1();
        z_tech2();
        z_tech2();
        return 0;
    }
    void z_tech1()
    {
        using namespace std;
        cout<<"Three blind mice "<<endl;
    }
    void z_tech2()
    {
        using namespace std;
        cout<<"See how they run "<<endl;
    }
    
    //PP2.7.4
    #include<iostream>
    int main()
    {
        using namespace std;
        cout<<"Please enter your age(years):"<<endl;
        int Year;
        cin>>Year;
        cout<<Year<<" years is about "<<12*Year<<" months.";
        return 0;
    }
    
    //PP2.7.5
    #include <iostream>
    double Fahrenheit(double);
    int main()
    {
        using namespace std;
        cout<<"Please enter a celsius value: "<<endl;
        double Celsius;
        cin>>Celsius;
        cout<<Celsius<<" degrees Celsius is "<<Fahrenheit(Celsius)<<" degrees Fahrenheit.";
        return 0;
    }
    double Fahrenheit(double n)
    {
        n=1.8*n+32.0;
        return n;
    }
    
    //PP2.7.6
    #include <iostream>
    double astronomical_units(double);
    int main()
    {
        using namespace std;
        cout<<"Please enter the number of light year: "<<endl;
        double Lightyear;
        cin>>Lightyear;
        double astr_units=astronomical_units(Lightyear);
        cout<<Lightyear<<" light year = "<<astr_units<<" astronomical units.";
        return 0;
    }
    double astronomical_units(double x)
    {
        x=63240*x;
        return x;
    }
    
    //PP2.7.7
    #include <iostream>
    void time(int,int);//多参数申明
    int main()
    {
        using namespace std;
        cout<<"Please enter the number of hours: ";
        int hours;
        cin>>hours;
        cout<<"Please enter the number of minutes: ";
        int minutes;
        cin>>minutes;
        time(hours,minutes);
        return 0;
    }
    void time(int a,int b)//多参数函数实体
    {
        using namespace std;
        cout<<"Time: "<<a<<":"<<b;
    }
    

    相关文章

      网友评论

          本文标题:CPP_Basic_Code_P2.1-PP2.7.7

          本文链接:https://www.haomeiwen.com/subject/jaflottx.html