c-ncurses链接列表未显示在屏幕中



我制作了一个链表,并用4"字符,我想用ncurses库在屏幕上打印它们,因为我正试图制作一个蛇游戏,但我想知道为什么它不打印,下面是代码:

#include<ncurses.h>
#include<stdlib.h>
struct snake{
char piece;
struct snake *nextPiece;
};
struct snake *addNewPiece(const char piece){
struct snake *newPiece = NULL;
newPiece = malloc(sizeof(struct snake));
if(newPiece != NULL){
newPiece->nextPiece = NULL;
newPiece->piece = piece;
}else{
endwin();
printf("mem aloc failure, the program couldn't alloc the memory correctly");
}
return newPiece;
}
int main(){
int i = 0;
struct snake *first = NULL;
struct snake *added = NULL;
initscr();
noecho();
int yMaxStd,xMaxStd;
getmaxyx(stdscr,yMaxStd,xMaxStd);
WINDOW* win = newwin(20,50,(yMaxStd/2)-10,10);
box(win,0,0);
refresh();
wrefresh(win);
leaveok(win, true);
int yMaxWin,xMaxWin;
getmaxyx(win,yMaxWin,xMaxWin);
wmove(win,yMaxWin/2,yMaxWin/2);
//llenar lista con 4 #
while(i<=4){
if(first == NULL){
first = addNewPiece("#");
if(first != NULL){
added = first;
}
}else{
added->nextPiece = addNewPiece("#");
if(added->nextPiece != NULL){
added = added->nextPiece;
}
}
}
if(first == NULL){
endwin();
printf("Men alloc failure.");
}
printinscreen(first,win);

getch();
endwin();
return 0;
}
void printinscreen(struct snake *piece,WINDOW* win){ 
struct snake *t;
t = piece;
int y = 5,x = 5;
if(t == NULL){
endwin();
}else{
while(t != NULL){
mvwprintw(win,y,x,t);
t = t->nextPiece;
y--;
}
}

}



我知道我很好地完成了链接列表的链接过程,因为我用stdio.h在另一个文件中测试了它的打印,它在中工作

您的程序有几个错误,我已经修复了它们。最严重的是你的代码无法用C++编译。如果打算在C++下构建它,首先需要修复编译错误。

  1. newPiece = malloc(sizeof(struct snake));,这里需要转换:newPiece = (snake *)malloc(sizeof(struct snake));,通常不建议在c++中使用malloc,更自然的是使用new

  2. first = addNewPiece("#");将字符串文本作为char参数传递,需要在此处传递一个字符。

  3. mvwprintw(win, y, x, t);,你误解了API,应该固定为mvwprintw(win, y, x, "%c", t->piece);,你需要在使用API库之前查看文档,看看它期望使用什么类型的参数

  4. 打印屏幕后忘记刷新屏幕!

  5. 您没有在for循环中增加索引,这是一个无限循环。

你的代码有点c风格,如果你试图用c++编写,它需要重构,一些建议:

  • 使用std::vector来存储蛇身,这样我们就不需要手动管理内存了。并避免了容易出错的链表处理。然后可以简化代码的大部分
  • 使用日志库和打印日志来帮助调试

修复的代码应该可以工作,我的控制台上有一条垂直的蛇。

#include <ncurses.h>
#include <stdlib.h>
struct snake {
char piece;
struct snake *nextPiece;
};
struct snake *addNewPiece(const char piece) {
struct snake *newPiece = NULL;
newPiece = (snake *)malloc(sizeof(struct snake));
if (newPiece != NULL) {
newPiece->nextPiece = NULL;
newPiece->piece = piece;
} else {
endwin();
printf("mem aloc failure, the program couldn't alloc the memory correctly");
}
return newPiece;
}
void printinscreen(struct snake *piece, WINDOW *win);
int main(int argc, char *argv[]) {
int i = 0;
struct snake *first = NULL;
struct snake *added = NULL;
initscr();
noecho();
int yMaxStd, xMaxStd;
getmaxyx(stdscr, yMaxStd, xMaxStd);
WINDOW *win = newwin(20, 50, (yMaxStd / 2) - 10, 10);
box(win, 0, 0);
refresh();
wrefresh(win);
leaveok(win, true);
int yMaxWin, xMaxWin;
getmaxyx(win, yMaxWin, xMaxWin);
wmove(win, yMaxWin / 2, yMaxWin / 2);
// llenar lista con 4 #
while (i <= 4) {
if (first == NULL) {
first = addNewPiece('#');
if (first != NULL) {
added = first;
}
} else {
added->nextPiece = addNewPiece('#');
if (added->nextPiece != NULL) {
added = added->nextPiece;
}
}
++i;
}
if (first == NULL) {
endwin();
printf("Men alloc failure.");
}
printinscreen(first, win);
refresh();
wrefresh(win);
getch();
endwin();
return 0;
}
void printinscreen(struct snake *piece, WINDOW *win) {
struct snake *t;
t = piece;
int y = 5, x = 5;
if (t == NULL) {
endwin();
} else {
while (t != NULL) {
mvwprintw(win, y, x, "%c", t->piece);
t = t->nextPiece;
y--;
}
}
}

在看到PO的评论后,我们知道该问题被错误地标记为c++,c版本代码:

#include <ncurses.h>
#include <stdlib.h>
struct snake {
char piece;
struct snake *nextPiece;
};
struct snake *addNewPiece(const char piece) {
struct snake *newPiece = NULL;
newPiece = malloc(sizeof(struct snake));
if (newPiece != NULL) {
newPiece->nextPiece = NULL;
newPiece->piece = piece;
} else {
endwin();
printf("mem aloc failure, the program couldn't alloc the memory correctly");
}
return newPiece;
}
void printinscreen(struct snake *piece, WINDOW *win);
int main(int argc, char *argv[]) {
int i = 0;
struct snake *first = NULL;
struct snake *added = NULL;
initscr();
noecho();
int yMaxStd, xMaxStd;
getmaxyx(stdscr, yMaxStd, xMaxStd);
WINDOW *win = newwin(20, 50, (yMaxStd / 2) - 10, 10);
box(win, 0, 0);
refresh();
wrefresh(win);
leaveok(win, true);
int yMaxWin, xMaxWin;
getmaxyx(win, yMaxWin, xMaxWin);
wmove(win, yMaxWin / 2, yMaxWin / 2);
// llenar lista con 4 #
while (i <= 4) {
if (first == NULL) {
first = addNewPiece('#');
if (first != NULL) {
added = first;
}
} else {
added->nextPiece = addNewPiece('#');
if (added->nextPiece != NULL) {
added = added->nextPiece;
}
}
++i;
}
if (first == NULL) {
endwin();
printf("Men alloc failure.");
}
printinscreen(first, win);
refresh();
wrefresh(win);
getch();
endwin();
return 0;
}
void printinscreen(struct snake *piece, WINDOW *win) {
struct snake *t;
t = piece;
int y = 5, x = 5;
if (t == NULL) {
endwin();
} else {
while (t != NULL) {
mvwprintw(win, y, x, "%c", t->piece);
t = t->nextPiece;
y--;
}
}
}

相关内容

  • 没有找到相关文章

最新更新