我的代码打印空白,而不是我给出的输入

  • 本文关键字:代码 打印 空白 c
  • 更新时间 :
  • 英文 :


我有下面的代码,它应该打印我给出的名称,但它打印空格。

我可能在数据类型上犯了一个错误,因为我打印的是整数而不是名称。我想不明白。

这是完整的代码,但我认为主要问题是在printStack函数。

编辑:我已经把所有东西都改成英文了,很抱歉造成了混乱。

#include <stdio.h>
#include <stdlib.h>
#define SIZE 25
char stack[SIZE];
int top=-1;
int isFull(){
if (top>=SIZE-1){
return 1;
}
return 0;    
}
struct n{
n * next;
char data;
};
typedef n node;
void printStack(char person){
printf("Entered persons: n");
for(int i=0;i<=top;i++)
printf("%cn",stack[person]);
printf("n");
}
void push(node * root, char person){
if(root == NULL || isFull()){
root = (node *)malloc(sizeof(node));
root -> data = person;
root -> next = NULL;
stack[++top]=person;
}
node * iter = root;
while(iter->next !=NULL || isFull()){
iter = iter -> next;
stack[++top]=person;
}
}

int main()
{
int pick;
char person;
node * s=NULL;
while (1){

printf("1 - Add personn");
printf("2 - Show personsn");
printf("3 - Exitn");

scanf("%d",&pick);
switch (pick){
case 1: 
printf ("Person name:  ");
scanf("%s",&person);
push(s, person);
break;
case 2:
printStack(person);
break;
case 3:
exit(0);
break;
}

}
return 0;
}

printStack()在堆栈上迭代,但在printf()调用中,您打印stack[yolcu],因此top + 1次同样的事情。我想你的意思是打印stack[i]。1和后面的元素之间存在逻辑错误(第一个元素顶部是1,第二个元素顶部是3)。我建议您使用正常的c范围[0;Top[其中Top现在是大小而不是(可能无效的)索引,因此initialize是0而不是-1。迭代i<上面。如果top >= BOYUT,则为满。您希望使用scanf(" %c", ..)来忽略前一个提示符中的换行符:>

#include <stdio.h>
#include <stdlib.h>
#define BOYUT 25
char stack[BOYUT];
int top=0;
int isFull(){
return top >= BOYUT;
}
typedef struct node {
struct node *next;
char data;
} node;
void printStack(){
printf("Girilen yolcular: n");
for(int i=0;i<top;i++)
printf("%i: %cn",i, stack[i]);
printf("n");
}
void push(node * root, char yolcu){
if(root == NULL || isFull()){
root = malloc(sizeof(node));
root -> data = yolcu;
root -> next = NULL;
stack[top++]=yolcu;
}
node * iter = root;
while(iter->next !=NULL || isFull()){
iter = iter -> next;
stack[top++]=yolcu;
}
}
int main() {
int secim;
char yolcu;
node *s=NULL;
while (1){
printf("1 - Yolcu Eklemen");
printf("2 - Yolculari Gostern");
printf("3 - Cikisn");
scanf("%d",&secim);
switch (secim){
case 1:
printf ("Yolcu Adi: ");
scanf(" %c",&yolcu);
push(s, yolcu);
printf("n");
break;
case 2:
printStack();
break;
case 3:
return 0;
break;
}
}
}

和示例会话:

1 - Yolcu Ekleme
2 - Yolculari Goster
3 - Cikis
1
Yolcu Adi: a
1 - Yolcu Ekleme
2 - Yolculari Goster
3 - Cikis
1
Yolcu Adi: b
1 - Yolcu Ekleme
2 - Yolculari Goster
3 - Cikis
2
Girilen yolcular: 
0: a
1: b
1 - Yolcu Ekleme
2 - Yolculari Goster
3 - Cikis

最新更新