gpt4 book ai didi

根据用户输入的字符创建一个矩形

转载 作者:行者123 更新时间:2023-11-30 20:55:07 25 4
gpt4 key购买 nike

我必须用 C 语言(我是新来的)编写一个程序,以便用户输入行数和列数以及他们想要显示为矩形的字符,例如 6 行和 6 列星号矩形。

这是详细的作业。

The first thing your program will do is print a menu of choices forthe user. You may choose your own version of the wording or order ofchoices presented, but each choice given in the menu must match thefollowing:A function that prompts the user to enter a singlecharacter. The return value of the function be a char and will returnthe character value entered by the user.This return value will bestored in a local variable, C, in main(). The initial default value ofthis character will be ' ' (blank or space character).

A function that prompts the user to enter a numerical value between 1and 15 (inclusive). If the user enters a value outside this range, theuser is prompted to re-enter a value until a proper value is entered.The return value of the function be an int and will return the valueentered by the user. This return value will be stored in a localvariable, N, in main(). The initial default value of this characterwill be 0.

Two "Print Rectangle" functions. Each function will take the currentinteger value N and character value C as input parameters. The returnvalues of these functions will be void. The functions will printrectangles of N lines and columns using the input character C. TheBorder Only function will print the rectangle with the just theborder. The Filled In function will print the rectangle as a solidrectangle. For example, if the integer value N = 6, and the charactervalue C = '*' and the Filled In type is called, the followingrectangle will be printed:

这是到目前为止我的代码。我做错了什么以及我的程序编码是否正确?

       #include <stdio.h>
#include <stdlib.h>

char enterSingleChar();
int enterNumValue();
void printRectanlgeOne(int N, char C);
void printRectangleTwo(int N, char C);

int main()
`enter code here`{
char userChoice;
int N = 0;
char C = ' ';

fprintf(stdout, "Please choose one of the following choices below \n");
fprintf(stdout, "Enter/Change Character (C/c)\n");
fprintf(stdout, "Enter/Change Number (N/n) \n");
fprintf(stdout, "Print Rectangle Type 1 (Border Only), enter 1 \n");
fprintf(stdout, "Print Rectangle Type 2 (Filled in), etner 2 \n");
fprintf(stdout, "Quit Program (Q/q) \n");

scanf("%c", &userChoice);

switch(userChoice)
{
case 'C':
case 'c':
enterSingleChar();
break;
case 'N':
case 'n':
enterNumValue();
break;
case '1':
printRectangleOne(N,C);
break;
case '2':
printRectangleTwo(N,C);
break;
break;
case 'Q':
case 'q':
fprintf(stdout, "The program will now quit\n");
exit(1);
default:
break;
}
}

char enterSingleChar()
{
char singleChar = ' ';
fprintf(stdout, "Please enter a single character \n");
scanf("%c", &singleCharC);
return singleChar;
}

int enterNumValue()
{
int numValue;
fprintf(stdout, "Please enter a numerical value between 1 and 15 inclusively \n");

scanf("%d", &numValue);

while(numValue <= 1 || numValue >= 15)
{
fprintf(stdout, "You have entered an invalid num \n");
fprintf(stdout, "Please try again \n");
fprintf(stdout, "Please enter a numerical value between 1 and 15 inclusively \n");
scanf("%d", &numValue);
}
return numValue;
}

void printRectangleOne(int N, char C)
{
int i;
int j;
for(i = 0; i <= N; i++)
{
fprintf(stdout, &C);

for(j = 0; i <= N; i++)
{
fprintf(stdout, &C);
}
}
printf();
}

void printRectangleTwo(int N, char C)
{

}



最佳答案

使用你的编译器

如果您尝试在启用所有警告的情况下编译该代码,它会开始抛出错误,您应该在继续之前更正这些错误:

test.c:10:5: error: stray ‘`’ in program
`enter code here`{
^
test.c: In function ‘main’:
test.c:10:6: error: unknown type name ‘enter’
`enter code here`{
^

还有:

test.c: In function ‘main’:
test.c:34:25: warning: implicit declaration of function ‘printRectangleOne’ [-Wimplicit-function-declaration]
printRectangleOne(N,C);
^
test.c: In function ‘enterSingleChar’:
test.c:53:22: error: ‘singleCharC’ undeclared (first use in this function)
scanf("%c", &singleCharC);

检查您的输入

我看到的一个问题是,当使用 scanf 从标准输入读取时,Enter 键也会被放入队列

所以有人问你,

Enter one character:
Enter another character:

然后您输入:

A 输入 B 输入

并且您期望这两个字符是 A 和 B。输入队列中的实际内容可能是,具体取决于平台(我不确定):

A\r\nB\r\n  (*six* characters!)
A\nB\n four characters
A\rB\r four characters

此外,如果您使用这些虚假字符之一(例如“\n”,即换行符)在字符“C”周围绘制一个 3x3 矩形,您实际上将发送到输出三个空行(顶行)、一个空行(中线左边框)、一个“C”、另外四个空行。

这可能非常令人费解。

您没有在问题中说明您的问题实际上是什么,但如果出现上述任何症状,请检查您的输入例程。

其他问题

void printRectangleOne(int N, char C)
{
int i;
int j;
for (i = 0; i <= N; i++)
{
// Why do you output this C here?
fprintf(stdout, &C);
// You are checking and incrementing i here, not j.
for(j = 0; i <= N; i++)
{
// fprintf does not work this way. Use
// putchar(C);
// instead.
fprintf(stdout, &C);
}
}
// I don't think this is going to work. Use either
// putchar('\n') or printf("\n") to get a newline.
printf();
}

关于根据用户输入的字符创建一个矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35255260/

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