惊讶了!不同语言差别这么大!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多的多
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 想让你多爱自己一些的开源计时器
· Cursor预测程序员行业倒计时:CTO应做好50%裁员计划
· 大模型 Token 究竟是啥:图解大模型Token
· 如何在 .NET 中 使用 ANTLR4
· HttpClient使用方法总结及工具类封装