C 是否可以让用户从命令行中选择一个值?
在C语言中,用户是否可以通过将光标移动到所需的值,然后按Enter或space键来确认选择,从而让用户从先前在屏幕上打印的数据中选择一个值 举例说明: 在以下代码中:C 是否可以让用户从命令行中选择一个值?,c,C,在C语言中,用户是否可以通过将光标移动到所需的值,然后按Enter或space键来确认选择,从而让用户从先前在屏幕上打印的数据中选择一个值 举例说明: 在以下代码中: int x[10] = {1,2,3,4,5,6,7,8,9,10}; int i; for(i = 0; i < 10; ++i){ printf("%i ", x[i]); } 现在,用户看到了输出,是否可以让他使用箭头键将光标移动到所需位置,并让输入成为用户选择的内容
int x[10] = {1,2,3,4,5,6,7,8,9,10};
int i;
for(i = 0; i < 10; ++i){
printf("%i ", x[i]);
}
现在,用户看到了输出,是否可以让他使用箭头键将光标移动到所需位置,并让输入成为用户选择的内容?不容易,这将取决于系统。您将需要一个光标定位库。例如,诅咒或ncurses。使用某种编程库,允许程序员以独立于终端的方式编写基于文本的用户界面。例如,.我知道让您的程序识别正在使用的箭头键的最简单方法是ncurses。”It’这是一个意大利语教程。谢谢大家的支持。在你向我指出图书馆诅咒之后,我能够实现我想要的。h所以我将在这里与你分享结果 一些注意事项: h仅与类似UNIX的OSs兼容。我读到可以将程序移植到Windows,但我没有研究这个问题 编译源代码时,有必要链接curses.h库 ->g++fileName.c-lcurses 有些变量和函数的名称不是英文的,但我确保对它们都进行了注释
#include <stdio.h>
#include <curses.h>
#include <stdlib.h>
WINDOW *janela; // Points to a Windows Object
int xPos; // current x cursor position
int yPos; // current y cursor position
int main(void){
// Declaration of all functions
void moverEsquerda(void); //move left
void moverDireita(void); //move right
void moverCima(void); //move up
void moverBaixo(void); //move down
int lerInt(void); //read value
char c; // This variable stores the user input(up, down, left, etc...)
janela = initscr(); // curses call to initialize window
noecho(); // curses call to set no echoing
cbreak(); // curses call to set no waiting for Enter key
int tabela[4][4]; // This array is just for demonstration purposes
tabela = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
// places the cursor at 0,0
xPos = 0;
yPos = 0;
move(yPos, xPos);
int num; // Stores the select by the user
// The following while is executed until the user presses an
// "invalid key"
while (1) {
c = getch();
if (c == 'w') moverCima();
else if(c == 's') moverBaixo();
else if(c == 'a') moverEsquerda();
else if(c == 'd') moverDireita();
else if(c == '\n'){ // If user presses Enter the caracter is writen in a txt file
FILE *file = fopen("test.txt", "a");
num = (int)inch();
fprintf(file, "Voce selecinou o numero %c\n", num);
fclose(file);
}
else {
endwin(); //ends window object
break; //exit the loop
}
}
return 0;
}
void moverCima(void){
--yPos;
move(yPos, xPos);
}
void moverBaixo(void){
++yPos;
move(yPos, xPos);
}
void moverDireita(void){
++xPos;
move(yPos, xPos);
}
void moverEsquerda(void){
--xPos;
move(yPos, xPos);
}
可能吗?是的,但这需要大量的工作。如果你想要更具交互性的东西,有像ncurses这样的工具包,它们可以帮助你只使用C语言,就像语言提供的一样,我想说不。但是有很多框架可以用来制作终端用户界面。也许你可以看看ncurses?
#include <stdio.h>
#include <curses.h>
#include <stdlib.h>
WINDOW *janela; // Points to a Windows Object
int xPos; // current x cursor position
int yPos; // current y cursor position
int main(void){
// Declaration of all functions
void moverEsquerda(void); //move left
void moverDireita(void); //move right
void moverCima(void); //move up
void moverBaixo(void); //move down
int lerInt(void); //read value
char c; // This variable stores the user input(up, down, left, etc...)
janela = initscr(); // curses call to initialize window
noecho(); // curses call to set no echoing
cbreak(); // curses call to set no waiting for Enter key
int tabela[4][4]; // This array is just for demonstration purposes
tabela = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
// places the cursor at 0,0
xPos = 0;
yPos = 0;
move(yPos, xPos);
int num; // Stores the select by the user
// The following while is executed until the user presses an
// "invalid key"
while (1) {
c = getch();
if (c == 'w') moverCima();
else if(c == 's') moverBaixo();
else if(c == 'a') moverEsquerda();
else if(c == 'd') moverDireita();
else if(c == '\n'){ // If user presses Enter the caracter is writen in a txt file
FILE *file = fopen("test.txt", "a");
num = (int)inch();
fprintf(file, "Voce selecinou o numero %c\n", num);
fclose(file);
}
else {
endwin(); //ends window object
break; //exit the loop
}
}
return 0;
}
void moverCima(void){
--yPos;
move(yPos, xPos);
}
void moverBaixo(void){
++yPos;
move(yPos, xPos);
}
void moverDireita(void){
++xPos;
move(yPos, xPos);
}
void moverEsquerda(void){
--xPos;
move(yPos, xPos);
}