惊讶了!不同语言差别这么大!Python和C++

曾经做过c++的打印月历程序,输入哪年哪月,打印出那一天月历
最近学Python,惊呆了,import里面自带打印月历,👍真是人生苦短,就学Python!
我们来对比一下呢:
打印月历的C++代码:

#include <iostream>
#include <iomanip>//输出控制
#include <cstdlib>
#include <string>
#include <sstream>
#include <windows.h>

using namespace std;

int zeller(int yy, int mm)
{
	int m = mm;
	if (mm <= 2) {
		yy = yy - 1;
		m = 12 + mm;
	}
	else
	{
		m = mm;
	}
	int c = yy / 100;
	int y = yy % 100;
	int w = 700 + y + (y / 4) + (c / 4) - 2 * c + 26 * (m + 1) / 10 + 1 - 1;
	//day 1; 
	w = w % 7;
	return w;
}

int calcday1(int y, int m)
{
	bool t;
	if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
		t = true;
	else
		t = false;
	int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	if (t)
		a[1] = 29;
	return a[m - 1];
}

int calcday2(int y, int m)
{
	bool t;
	if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
		t = true;
	else
		t = false;
	int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	if (t)
		a[1] = 29;
	return a[m - 1];
}

void gotoxy(unsigned char x, unsigned char y) {
	COORD cor;
	HANDLE hout;
	cor.X = x;
	cor.Y = y;
	hout = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hout, cor);
}

string putSpaceInt2str(int aNum)
{
	stringstream res;
	string s;
	res << aNum;
	res >> s;
	s = " " + s;
	return s;
}

int main()
{
	int year, month;
	cout << "输入年 月" << endl;
	cin >> year >> month;
	int cc = zeller(year, month);//0->周日
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
	cout << "一 二 三 四 五 六 日" << endl;
	int d1, d2;
	if (month == 1)
		d1 = 31;
	else
		d1 = calcday1(year, month - 1);//上个月
	d2 = calcday2(year, month);//本月
	if (cc == 0)cc = 7; int s = 0;
	d1 = d1 - cc + 2;
	string str;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
	for (int i = cc - 1; i >= 1; i--) {
		s++;
		if (s % 7 == 0)
			cout << endl;
		if (d1 / 10 != 0)
			cout << d1 << " ", d1++;
		else {
			//char c = d1 - '0';
			//str = str + " " + c;
			cout << "  " << d1 << " ";
			d1++;
		}
	}
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
	for (int i = 1; i <= d2; i++)
	{
		if (i / 10 != 0)
			std::cout << i << " ";
		else {
			str = putSpaceInt2str(i);
			std::cout << str << " ";
			str = "";
		}
		s++;
		if (s % 7 == 0)
			std::cout << endl;
	}
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
	for (int i = 1; s % 7 != 0; i++)
	{
		s++;
		cout << " " << i << " ";
	}
	cout << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);

	return EXIT_SUCCESS;
}

有一点可怕,很长对不对?
下面再来看Python代码:

# -*- coding = utf-8 -*-
 
# 引入日历模块
import calendar
 
# 输入指定年月
yy = int(input("输入年份: "))
mm = int(input("输入月份: "))
 
# 显示日历
print(calendar.month(yy,mm))

输出结果:

输入年份: 2015
输入月份: 6
     June 2015
Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

就这么牛皮 ❗️
虽然C++中加了一点颜色,但还是比Python多的多

posted @ 2020-02-20 14:08  Aeterna_Gungnir  阅读(417)  评论(0编辑  收藏  举报