【C++】几个简单课本例题

//
//  main.cpp
//  2_1
//
//  Created by T.P on 2018/2/28.
//  Copyright © 2018年 T.P. All rights reserved.
//

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    cout<<"Hello!"<<endl;
    cout<<"Welcome to C++!"<<endl;
    cout<<setw(10)<<setprecision(3)<<3.1415<<endl;
    
    int score=61;
    cout<<(score>=60? "pass":"fail")<<endl
    <<endl;
    
    return 0;
}


//
//  main.cpp
//  2_2
//
//  Created by T.P on 2018/3/4.
//  Copyright © 2018年 T.P. All rights reserved.
//

//判断闰年
//:闰年可以被4整除而不能被100整除,或者能被400整除。

//表达式((year%4==0&&year%100!=0)||(year%400==0))

#include <iostream>
#include <iomanip>      //包含IO类库操作符
using namespace std;    //没写 using namespace std; 用 std::cout
                        //写了 using namespace std; 用 cout 就可以了。

int main(int argc, const char * argv[]) {
    // insert code here...
    //std::cout << "Hello, World!\n";
    int year;
    bool isLeapYear;
    
    cout<<"Enter the year:";
    cin>>year;
    isLeapYear=((year%4==0&&year%100!=0)||(year%400==0));
    
    if(isLeapYear)
        cout<<year<<"is a leap year"<<endl;
    else
        cout<<year<<" is not a leap year"<<endl;
    
    
    
    return 0;
}


//
//  main.cpp
//  2_3
//
//  Created by T.P on 2018/3/4.
//  Copyright © 2018年 T.P. All rights reserved.
//


//比较两个数的大小
#include <iostream>
#include <iomanip>
using namespace std;


int main(int argc, const char * argv[]) {
    int x,y;
    cout<<"Please Enter x and y : ";
    cin>>x>>y;
    cout<<endl;
    if(x!=y){
        if(x>y)
            cout << "x>y";
        else
            cout << "x<y";
    }
    else
        cout << "x=y";
    cout << endl;
    return 0;
}


//
//  main.cpp
//  2_4
//
//  Created by T.P on 2018/3/4.
//  Copyright © 2018年 T.P. All rights reserved.
//

//输入一个0~6的整数,转换成星期输出
#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, const char * argv[]) {
    // insert code here...
    //std::cout << "Hello, World!\n";
    int day;
    cin>>day;
    switch (day) {
        case 0:
            cout<<"Sunday";
            break;
        case 1:
            cout<<"Monday";
            break;
        case 2:
            cout<<"Tuesday";
            break;
        case 3:
            cout<<"Wednesday";
            break;
        case 4:
            cout<<"thursday";
            break;
        case 5:
            cout<<"Friday";
            break;
        case 6:
            cout<<"Saturday";
            break;
        default:
            cout<<"Day out of range Sunday...Saturday";
            break;
    }
    cout<<endl;
    return 0;
}

posted @ 2018-03-04 18:22  T.P  阅读(242)  评论(0编辑  收藏  举报