gpt4 book ai didi

c - 用多个 if on curses 替换 switch

转载 作者:太空宇宙 更新时间:2023-11-04 01:33:07 25 4
gpt4 key购买 nike

我通过教程学习 curses 库,我在 curses 教程中用多个 if 替换了 switch 语句(switch 部分仍在注释中),因为我更好地理解了 if 语句我的问题是,现在每次我按下一个键时,都会在新键之前使用之前输入的键,这是怎么回事?

int             main(int argc, char *argv[])
{WINDOW *my_win;
int startx, starty, width, height;
int ch;

initscr();/* Start curses mode */
cbreak();/* Line buffering disabled, Pass on
* everty thing to me */
keypad(stdscr, TRUE);/* I need that nifty F1 */

height = 3;
width = 12;
starty = (LINES - height) / 2;/* Calculating for a center placement */
startx = (COLS - width) / 2;/* of the window*/
printw("Press F1 to exit");
refresh();
my_win = create_newwin(height, width, starty, startx);
/*
while((ch = getch()) != KEY_F(1))
{switch(ch)
{case KEY_LEFT:
destroy_win(my_win);
my_win = create_newwin(height, width, starty,--startx);
break;
case KEY_RIGHT:
destroy_win(my_win);
my_win = create_newwin(height, width, starty,++startx);
break;
case KEY_UP:
destroy_win(my_win);
my_win = create_newwin(height, width, --starty,startx);
break;
case KEY_DOWN:
destroy_win(my_win);
my_win = create_newwin(height, width, ++starty,startx);
break;
}
}
*/
while(42)
{
ch = getch();
if(ch == KEY_LEFT)
{
destroy_win(my_win);
my_win = create_newwin(height, width, starty,startx--);
}
else if(ch == KEY_RIGHT)
{
destroy_win(my_win);
my_win = create_newwin(height, width, starty,startx++);
}
else if(ch == KEY_UP)
{
destroy_win(my_win);
my_win = create_newwin(height, width, starty--,startx);
}
else if(ch == KEY_DOWN)
{
destroy_win(my_win);
my_win = create_newwin(height, width, starty++, startx);
}
else
{
endwin();
exit(0);
}

}
endwin();/* End curses mode */
return 0;
}

WINDOW *create_newwin(int height, int width, int starty, int startx)
{WINDOW *local_win;

local_win = newwin(height, width, starty, startx);
box(local_win, 0 , 0);/* 0, 0 gives default characters
* for the vertical and horizontal
* lines*/
wrefresh(local_win);/* Show that box */

return local_win;
}

void destroy_win(WINDOW *local_win)
{
/* box(local_win, ' ', ' '); : This won't produce the desired
* result of erasing the window. It will leave it's four corners
* and so an ugly remnant of window.
*/
wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
/* The parameters taken are
* 1. win: the window on which to operate
* 2. ls: character to be used for the left side of the window
* 3. rs: character to be used for the right side of the window
* 4. ts: character to be used for the top side of the window
* 5. bs: character to be used for the bottom side of the window
* 6. tl: character to be used for the top left corner of the window
* 7. tr: character to be used for the top right corner of the window
* 8. bl: character to be used for the bottom left corner of the window
* 9. br: character to be used for the bottom right corner of the window
*/
wrefresh(local_win);
delwin(local_win);
}

最佳答案

switchif 转换并不是您所做的唯一更改。您引入了如下错误,其中:

my_win = create_newwin(height, width, starty, --startx);

变成了:

my_win = create_newwin(height, width, starty, startx--);

也就是说,前缀减量变成了后缀,改变了传递给create_newwin()的值;前缀减量计算减量后的值,而后缀计算减量前的值。这意味着旧位置用于绘制窗口,产生的效果就好像错误与输入键有关,而问题实际上与输入键无关。

您对原始代码中的所有其他前缀增量执行了相同的操作。您应该将它们全部从后缀更改为前缀,代码将表现相同。

关于c - 用多个 if on curses 替换 switch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19613104/

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