3.从用户处接收长度并输出X形状。
3.从用户处接收长度并输出X形状。 咋还有韩语? wp231957 发表于 2021-4-18 17:59咋还有韩语?
在韩国上学,教授给的题,你看你能帮我写一下程序吗? 851850865 发表于 2021-4-18 18:20
在韩国上学,教授给的题,你看你能帮我写一下程序吗?
你先帮我们翻译一下,^_^ 人造人 发表于 2021-4-18 18:31
你先帮我们翻译一下,^_^
题的要求是:
1.从用户处输入X的长度(整数)。
2.利用@和空白字符输出X型。
3参考执行示例1和执行示例2输出X型。 #include "screen.h"
#include <math.h>
void draw(screen_t *screen, size_t n) {
size_t x = 0, y = 0;
for(y = 0; y < n; ++y) {
screen_putchar(screen, x++, y, '@');
}
x = n - 1;
for(y = 0; y < n; ++y) {
screen_putchar(screen, x--, y, '@');
}
}
int main(void) {
screen_t *screen = screen_init(130, 40);
size_t n;
scanf("%lu", &n);
draw(screen, n);
screen_refresh(screen, stdout);
screen_deinit(screen);
return 0;
}
#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
#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);
}
}
}
人造人 发表于 2021-4-18 18:42
#include<stdio.h>
int main()
{
char a;
int i, j, max;
printf("输入一个数:");
scanf_s("%d", &max);
for(i=0;i<max;i++)
for(j=0;j<max;j++)
a=' ';
for(i=0,j=max-1;i<max;i++,j--)
{
a='@';
a='@';
}
for(i=0;i<max;i++)
{for(j=0;j<max;j++)
printf("%c",a);
printf("\n");
}
}
我是这样写的你看看 851850865 发表于 2021-4-18 19:50
#include
int main()
{
嗯,能实现要求就行
页:
[1]