想知道怎么简单的横向输出日历
只运用数组循环选择可以写出吗下面是我写的 但是只会竖向输出
#include <stdio.h>
int main()
{
int month, day, year, sum, a, b, c, i;
sum = 0;
printf("输入年份:");
scanf("%d", &year);
for (i = 1900; i < year; i++)
{
if ((i% 4 == 0 && i % 100 != 0) ||i% 400 == 0)
sum += 366;
else sum += 365;
}sum+=31;
for (month = 1; month <= 12; month++)
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: day = 31; break;
case 4:
case 6:
case 9:
case 11:day = 30; break;
case 2: if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) day = 29;
else day = 28; break;
default:day = 31; break;
}
printf("==============================================%d月================================================\n", month),
printf("日\t\t一\t\t二\t\t三\t\t四\t\t五\t\t六\n");
a = (sum) % 7;
for (c = 0; c < a; c++)
{
printf("\t\t");
}
for (i = 1; i <= day; i++)
{
if ((i+sum)%7 == 0 || i == day)
printf("%d\n", i);
else printf("%d\t\t", i);
}sum += day;
}
return 0;
}
代码只能一行行打印下来,做不到竖着打过去
想达到所谓的横向效果,就只能像图片显示的那样,先打印3个月的第一周,然后打印第二周......
风车呼呼呼 发表于 2022-5-3 17:06
代码只能一行行打印下来,做不到竖着打过去
想达到所谓的横向效果,就只能像图片显示的那样,先打印3个月 ...
可以的吧,这是我们班的作业,我周围的同学都是用不到100行代码,实现了这个效果 addendum777 发表于 2022-5-3 17:29
可以的吧,这是我们班的作业,我周围的同学都是用不到100行代码,实现了这个效果
可以什么?可以实现这个效果吗?当然可以啊
我只是说需要转变思路,代码逻辑上不可能一次性在左边一块打印整个1月份的日历,然后又在它右边打印整个2月份,只能从上到下
另外,你们这个只要求格式吗 目前你实现的具体日期和每周并没有对齐,至少它不是2022的日历 6666666666666666666 3个月3个月地打印咯,先提前把所有月份的天数计算好。
先打印一月 + 二月 + 三月的第一行,当然月与月直接做好间隔,
再打印一月 + 二月 + 三月的第二行...
...
四月 + 五月 + 六月的第一行...
...
以此类推。 来学习下! 其实不难,只是用 C 语言打印比较麻烦,C 语言不太适合用来打印这种格式输出,用 Python 相对简单多。
代码:#include <stdio.h>
#define endl printf("\n");
#define space printf(" ");
#define _3dot printf("***");
int isLeapYear(int year) { return (!(year % 4) && (year % 100)) || !(year % 400); }
int firstDay(int year) {
int days = 0;
for (int y = 1900; y < year; y++) {
days += isLeapYear(y) ? 366 : 365;
}
return days + 1;
}
void line1(int year) {
char* str = "*************";
printf("%*s%d年日历%s%s%d年日历%s\n\n", 31, str, year, str, str, year, str);
}
void line2() { for (int i = 0; i < 32; ++i) printf("*"); }
void line3(int year, int month) { printf("%*s%d年%d月%*s", 12, "", year, month, month > 9 ? 13 : 14, ""); }
void line4() { printf("%s%6s%6s%6s%6s%6s%6s", "一", "二", "三", "四", "五", "六", "日"); }
void line5(int max, int w, int line) {
int l = 0, r = 6, i = 0;
w = w ? w : 7;
char str;
for (int block = 1, day = 1; block <= 42; ++block) {
if (block < w) {
if (!l) { snprintf(str, 3, "%5s", ""); l += 2; }
else { snprintf(str + l, r, "%5s", ""); l += (r - 1); }
}
else if (day <= max) {
if (!l) { snprintf(str + l, 3, "%2d", day++); l += 2; }
else { snprintf(str + l, r, "%5d", day++); l += (r - 1); }
if (!(block % 7)) {
str = '\0';
l = 0;
}
}
else {
if (!l) { snprintf(str + l, 3, "%2s", ""); l += 2; }
else { snprintf(str + l, r, "%5s", ""); l += (r - 1); }
if (!(block % 7)) {
str = '\0';
l = 0;
}
}
}
printf("%s", str);
}
void calendar(int year) {
int days = { 31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int week, sum = 0;
for (int m = 0; m < 12; ++m) {
week = (firstDay(year) + sum) % 7;
sum += days;
}
line1(year);
endl
for (int m = 1; m <= 12; m += 3) {
if (!((m - 1) % 3)) {
_3dot
for (int i = 0; i < 3; ++i) {
line2();
space
}
endl
}
space
line3(year, m); line3(year, m + 1); line3(year, m + 2);
endl
space
line4(); _3dot
line4(); _3dot
line4();
endl
for (int i = 0; i < 6; ++i) {
space
line5(days, week, i); space
line5(days, week, i); space
line5(days, week, i); space
endl
}
}
}
int main(void) {
int year = 2022;
calendar(year);
return 0;
} 让我想起了wttr.in这个页面(浏览器或curl程序都能访问) 每一行存放在数组里,再集中输出? 你猜我在干嘛?{:5_97:} {:5_109:} 学习 $ cat screen.h
#ifndef _SCREEN_H_
#define _SCREEN_H_
#include <stdio.h>
#include <stddef.h>
typedef struct {
size_t width, height;
char *buff;
} screen_t;
screen_t *screen_init(size_t width, size_t height);
void screen_deinit(screen_t *screen);
void screen_putchar(screen_t *screen, size_t x, size_t y, char ch);
void screen_refresh(screen_t *screen, FILE *fp);
void screen_draw_border(screen_t *screen, char ch);
#endif
$ cat screen.c
#include "screen.h"
#include <stdlib.h>
#include <string.h>
screen_t *screen_init(size_t width, size_t height) {
screen_t *screen = malloc(sizeof(*screen));
*screen = (screen_t){width, height, malloc(width * height)};
memset(screen->buff, ' ', screen->width * screen->height);
return screen;
}
void screen_deinit(screen_t *screen) {
free(screen->buff);
free(screen);
}
void screen_putchar(screen_t *screen, size_t x, size_t y, char ch) {
if(x < screen->width && y < screen->height)
screen->buff = ch;
}
void screen_refresh(screen_t *screen, FILE *fp) {
for(size_t y = 0; y < screen->height; ++y) {
for(size_t x = 0; x < screen->width; ++x) {
fputc(screen->buff, fp);
}
fputc('\n', fp);
}
}
void screen_draw_border(screen_t *screen, char ch) {
for(size_t y = 0; y < screen->height; ++y) {
for(size_t x = 0; x < screen->width; ++x) {
if(y == 0 || x == 0 || y == screen->height - 1 || x == screen->width - 1)
screen_putchar(screen, x, y, ch);
}
}
}
$ cat main.c
#include "screen.h"
void output_string(screen_t *screen, size_t x, size_t y, const char *str) {
for(size_t i = 0; str; ++i) {
screen_putchar(screen, x + i, y, str);
}
}
int main(void) {
screen_t *screen = screen_init(130, 41);
//screen_draw_border(screen, '#');
output_string(screen, 10, 3, "*********2022年日历**********");
output_string(screen, 30, 5, "*********2026年日历**********");
output_string(screen, 50, 12, "*********2029年日历**********");
screen_refresh(screen, stdout);
screen_deinit(screen);
return 0;
}
$ cat Makefile
CFLAGS = -g -Wall -fsanitize=undefined -fsanitize=leak -fsanitize=address -fno-omit-frame-pointer
LDFLAGS = -lasan -lubsan -lm
all: main
main: main.o screen.o
clean:
rm -f *.o main
$
善用(char)8和(char)13可能会有效。
页:
[1]