gpt4 book ai didi

c - 我想消除我在程序中绘制的表格的重复外观。有没有办法可以将其放入单独的函数中?

转载 作者:行者123 更新时间:2023-11-30 17:29:45 25 4
gpt4 key购买 nike

我正在创建一个程序,要求用户提供坐标,然后在 table 上用大写 O 标记该坐标。我已经让程序在单元格 A1-A3 上标记字母 O(如果用户输入过这些字母)。但是我编写的程序会很长,因为我在每个条件下重复输入表格。有没有办法可以将表分开并将其放入函数中?

#include <stdio.h>
#include <string.h>

int main(){
char first [3], second [3], third [3];
char A1 = ' ', A2 = ' ', A3 = ' ';
char B1 = ' ', B2 = ' ', B3 = ' ';
char C1 = ' ', C2 = ' ', C3 = ' ';

static const char a1[] = "a1";
static const char a2[] = "a2";
static const char a3[] = "a3";
static const char b1[] = "b1";
static const char b2[] = "b2";
static const char b3[] = "b3";
static const char c1[] = "c1";
static const char c2[] = "c2";
static const char c3[] = "c3";

printf(" 1 2 3");
printf("\n +---+---+---+");
printf("\nA| %c | %c | %c |", A1, A2, A3);
printf("\n +---+---+---+");
printf("\nB| %c | %c | %c |", B1, B2, B3);
printf("\n +---+---+---+");
printf("\nC| %c | %c | %c |", C1, C2, C3);
printf("\n +---+---+---+");

printf("\n\nCoordinates should be entered in this format, lower cap for columns followed by row number.\nExample: a1, b2, c3, etc.");
printf("\n\nPlease enter 1st coordinate: ");
scanf("%3s", &first);

if (!strcmp(first,a1)){
A1 = 'O';
system("cls");
printf(" 1 2 3");
printf("\n +---+---+---+");
printf("\nA| %c | %c | %c |", A1, A2, A3);
printf("\n +---+---+---+");
printf("\nB| %c | %c | %c |", B1, B2, B3);
printf("\n +---+---+---+");
printf("\nC| %c | %c | %c |", C1, C2, C3);
printf("\n +---+---+---+");
}

if (!strcmp(first,a2)){
A2 = 'O';
system("cls");
printf(" 1 2 3");
printf("\n +---+---+---+");
printf("\nA| %c | %c | %c |", A1, A2, A3);
printf("\n +---+---+---+");
printf("\nB| %c | %c | %c |", B1, B2, B3);
printf("\n +---+---+---+");
printf("\nC| %c | %c | %c |", C1, C2, C3);
printf("\n +---+---+---+");
}

if (!strcmp(first,a3)){
A3 = 'O';
system("cls");
printf(" 1 2 3");
printf("\n +---+---+---+");
printf("\nA| %c | %c | %c |", A1, A2, A3);
printf("\n +---+---+---+");
printf("\nB| %c | %c | %c |", B1, B2, B3);
printf("\n +---+---+---+");
printf("\nC| %c | %c | %c |", C1, C2, C3);
printf("\n +---+---+---+");
}

getch();
return 0;
}

最佳答案

添加函数printTable

void printTable(char A1, char A2, char A3,
char B1, char B2, char B3,
char C1, char C2, char C3)
{
system("cls");
printf(" 1 2 3");
printf("\n +---+---+---+");
printf("\nA| %c | %c | %c |", A1, A2, A3);
printf("\n +---+---+---+");
printf("\nB| %c | %c | %c |", B1, B2, B3);
printf("\n +---+---+---+");
printf("\nC| %c | %c | %c |", C1, C2, C3);
printf("\n +---+---+---+");
}

在适当的位置从 main 调用该函数。

int main(){
char first [3], second [3], third [3];
char A1 = ' ', A2 = ' ', A3 = ' ';
char B1 = ' ', B2 = ' ', B3 = ' ';
char C1 = ' ', C2 = ' ', C3 = ' ';

static const char a1[] = "a1";
static const char a2[] = "a2";
static const char a3[] = "a3";
static const char b1[] = "b1";
static const char b2[] = "b2";
static const char b3[] = "b3";
static const char c1[] = "c1";
static const char c2[] = "c2";
static const char c3[] = "c3";

printTable(A1, A2, A3, B1, B2, B3, C1, C2, C3);

printf("\n\nCoordinates should be entered in this format, lower cap for columns followed by row number.\nExample: a1, b2, c3, etc.");
printf("\n\nPlease enter 1st coordinate: ");
scanf("%3s", &first);

if (!strcmp(first,a1)){
A1 = 'O';
printTable(A1, A2, A3, B1, B2, B3, C1, C2, C3);
}

if (!strcmp(first,a2)){
A2 = 'O';
printTable(A1, A2, A3, B1, B2, B3, C1, C2, C3);
}

if (!strcmp(first,a3)){
A3 = 'O';
printTable(A1, A2, A3, B1, B2, B3, C1, C2, C3);
}

getch();
return 0;
}

进一步简化

创建一个字符数组来存储数据并使用该数组而不是有这么多变量。

void printTable(char data[][3])
{
char* A = data[0];
char* B = data[1];
char* C = data[2];

system("cls");
printf(" 1 2 3");
printf("\n +---+---+---+");
printf("\nA| %c | %c | %c |", A[0], A[1], A[2]);
printf("\n +---+---+---+");
printf("\nB| %c | %c | %c |", B[0], B[1], B[2]);
printf("\n +---+---+---+");
printf("\nC| %c | %c | %c |", C[0], C[1], C[2]);
printf("\n +---+---+---+");
}

int main(){
char first [3], second [3], third [3];
char data[][3] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};

static const char a1[] = "a1";
static const char a2[] = "a2";
static const char a3[] = "a3";
static const char b1[] = "b1";
static const char b2[] = "b2";
static const char b3[] = "b3";
static const char c1[] = "c1";
static const char c2[] = "c2";
static const char c3[] = "c3";

printTable(data);

printf("\n\nCoordinates should be entered in this format, lower cap for columns followed by row number.\nExample: a1, b2, c3, etc.");
printf("\n\nPlease enter 1st coordinate: ");
scanf("%3s", &first);

if (!strcmp(first,a1)){
data[0][0] = 'O';
printTable(data);
}

if (!strcmp(first,a2)){
data[0][1] = 'O';
printTable(data);
}

if (!strcmp(first,a3)){
data[0][2] = 'O';
printTable(data);
}

getch();
return 0;
}

关于c - 我想消除我在程序中绘制的表格的重复外观。有没有办法可以将其放入单独的函数中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25531150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com