C++练手小项目//日历生成器---可供新手学习

C++练手小项目//日历生成器

项目概述:
用户输入年月,程序输出该月份的日历到屏幕上。
用到的知识有:

  • 函数
  • 指针
  • 条件语句
  • 字符串
    代码如下:
/*C++练手小项目//日历生成器*/

include <iostream>
#include<iomanip>
#include<windows.h>
#include<string>
#include<vector>
#include<array>
using namespace std;


bool judge_runnian(int year) {/*judge_runnian:判断是否是闰年*/
    bool ifrun;
    if ((year % 4 == 0) & (year % 100 != 0)) {
        ifrun = true;
    }
    else if ((year % 4 == 0) & (year % 400 == 0)) {
        ifrun = true;
    }
    else {
        ifrun = false;
    }
    return ifrun;
}

vector<string> splitstring(const string s, const string c)//利用空格分隔字符串
{
    string :: size_type pos1,pos2;//::标明类的变量
    vector<string> v;
    pos1 = 0;
    pos2 = s.find(c);
    while (string::npos != pos2) {
        v.push_back(s.substr(pos1, pos2 - pos1));
        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if (pos1 != s.length())
        v.push_back(s.substr(pos1));
    return v;
}

int main()//主函数代码如下
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);//设置cmd窗口字体颜色
    cout << "请输入年月(以一个空格分开)" << endl;
    string day;
    getline(cin, day);
    vector<string> newday;
    newday= splitstring(day," "); 
    int year, month;
    year = atoi(newday[0].c_str());
    month= atoi(newday[1].c_str());
    cout << year << "年" << month << "月日历如下:\n";
    for (int i = 0; i < 10; i++) {//上下表头*****
        cout << "  *  ";
    }
    cout << endl;
    if (!judge_runnian(year)) {//如果不是闰年
        
        switch (month) 
        {
            case 2:
                for (int i = 0; i < 28; i++) {
                    cout << setw(3) << i + 1;
                    cout << "  ";
                    if ((i + 1) % 10 == 0)
                        cout << endl;
                }
                break;
            case 4:        
                for (int i = 0; i < 30; i++) {
                    cout << setw(3) << i + 1;
                    cout << "  ";
                    if ((i + 1) % 10 == 0)
                        cout << endl;
                }
                break;
            case 6:
                for (int i = 0; i < 30; i++) {
                    cout << setw(3) << i + 1;
                    cout << "  ";
                    if ((i + 1) % 10 == 0)
                        cout << endl;
                }
                break;
            case 9:
                for (int i = 0; i < 30; i++) {
                    cout << setw(3) << i + 1;
                    cout << "  ";
                    if ((i + 1) % 10 == 0)
                        cout << endl;
                }
                break;
            case 11:
                for (int i = 0; i < 30; i++) {
                    cout << setw(3) << i + 1;
                    cout << "  ";
                    if ((i + 1) % 10 == 0)
                        cout << endl;
                }
                break;
            default://1,3,5,7,8,10,12
                for (int i = 0; i < 31; i++) {
                    cout << setw(3) << i + 1;
                    cout << "  ";
                    if ((i + 1) % 10 == 0)
                        cout << endl;
                }
                break;
        }
    
    }
    else {
        switch (month)
        {
        case 2:
            for (int i = 0; i < 29; i++) {
                cout << setw(3) << i + 1;
                cout << "  ";
                if ((i + 1) % 10 == 0)
                    cout << endl;
            }
            break;
        case 4:
            for (int i = 0; i < 30; i++) {
                cout << setw(3) << i + 1;
                cout << "  ";
                if ((i + 1) % 10 == 0)
                    cout << endl;
            }
            break;
        case 6:
            for (int i = 0; i < 30; i++) {
                cout << setw(3) << i + 1;
                cout << "  ";
                if ((i + 1) % 10 == 0)
                    cout << endl;
            }
            break;
        case 9:
            for (int i = 0; i < 30; i++) {
                cout << setw(3) << i + 1;
                cout << "  ";
                if ((i + 1) % 10 == 0)
                    cout << endl;
            }
            break;
        case 11:
            for (int i = 0; i < 30; i++) {
                cout << setw(3) << i + 1;
                cout << "  ";
                if ((i + 1) % 10 == 0)
                    cout << endl;
            }
            break;
        default://1,3,5,7,8,10,12
            for (int i = 0; i < 31; i++) {
                cout << setw(3) << i + 1;
                cout << "  ";
                if ((i + 1) % 10 == 0)
                    cout << endl;
            }
            break;
        }
    }
    return 0;
}


输出结果如下:
在这里插入图片描述

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读