我想通过scanf在动态数组中存储一些值,我想显示该动态数组。我究竟做错了什么? [英] I want to store some values in a dynamic array by scanf and I want to display that dynamic array. What am I doing wrong?

查看:72
本文介绍了我想通过scanf在动态数组中存储一些值,我想显示该动态数组。我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将这些数字存储在动态数组中并显示它们。我在这段代码中做错了什么?



我尝试了什么:



I want to store these numbers in a dynamic array and display them. What am I doing wrong in this piece of code?

What I have tried:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void push(int*,int,int*);
void display(int*,int,int*);
void main(){
	int done=0,choice,n,top=-1;
	int stack;
	while(!done){
		printf("1.Push\t2.Pop\t3.display\t4.exit\n");
		printf("enter the choice\n");
		scanf("%d",&choice);
		switch(choice){
			case 1:printf("enter the number of elements you want to insert\n");
			scanf("%d",&n);
			push(&stack,n,&top);
			break;
			case 3:display(&stack,n,&top);break;
			case 4:done=1;break;
			default:printf("wrong value\n");break;

		}
	}
}

void push(int *stack,int n,int *top){
	int i;
	stack=malloc(n*sizeof(stack));
	printf("enter the elements\n");
	for(i=0;i<n;i++){
		scanf("%d\n",(stack+i));
		top++;
	}
}

void display(int *stack,int n,int *top){
	int i;
	if(*top==-1){
		printf("stack is empty\n");
	}
	for(i=0;i<n;i++){
		printf("%d",*(stack+i));
	}
}

推荐答案

几乎所有东西。

stack 不是int:它应该是指向int的指针。为什么?因为您无法将动态数组值加载到单个int中!

每次尝试推送一组值时,为什么要废弃所有内容?



帮自己一个忙,打破调试器。

在main函数的第一行放置一个断点,然后通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但是我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
Pretty much everything.
stack isn't an int: it should be a pointer to an int. Why? Because you can't load a dynamic array of values into a single int!
Why do you scrap everything each time you try to "push" a set of values?

Do yourself a favour, and break out the debugger.
Put a breakpoint on the first line in the main function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


我会用 struct for maintining:值的数组,数组的大小(即可用堆栈槽的数量)和堆栈的当前顶部。

I would use a struct for maintining: the array of values, the size of the array (that is the number of available stack slots) and the current top of the stack.
typedef struct _Stack
{
  int * value; // the array of values
  int SLOTS; // the number of available slots
  int top; // the current top of the stack
} Stack;





此外我会将推送操作拆分为 create 一个和一个实际的推送一个(即插入一个项目)。



尝试,例如(请注意,代码只是在错误条件下断言,根据您的需要,您可能会使用更复杂的错误处理)



Moreover I would split your push operation into a create one and an actual push one (that is insert one item).

Try, for instance (please note, the code simply asserts on error conditions, depending on your needs, you might use a more sophisticated error handling)

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

typedef struct _Stack
{
  int * value;
  int SLOTS;
  int top;
} Stack;


Stack * create(int SLOTS);
void destroy(Stack * stack);
void push (Stack * stack, int i);
int pop (Stack * stack);
void display(Stack * stack);


int main()
{
  int choice = 0;
  Stack * stack = NULL;
  do
  {
    printf("1.Create\n2.Push\n3.Pop\n4.Display\n5.Exit\n");
    printf("Please, enter your choice\n");
    if ( scanf("%d", &choice) == 1)
    {
      switch (choice)
      {
      case 1:
        {
          int SLOTS;
          printf("Please enter the number of slots\n");
          if ( scanf("%d", &SLOTS) == 1)
          {
            stack = create( SLOTS );
          }
        }
        break;
      case 2:
        {
          int i;
          printf("Please enter the value to push\n");
          if ( scanf("%d", &i) == 1)
          {
            push(stack, i);
          }
        }
        break;
      case 3:
        {
          int i = pop(stack);
          printf("Popped value is %d\n", i);
        }
        break;
      case 4:
        printf("Stack content is\n");
        display(stack);
        break;
      case 5:
      default:
        break;
      }
    }
  } while ( choice != 5);

  if ( stack ) destroy( stack );

  return 0;
}

Stack * create( int SLOTS )
{
  assert(SLOTS > 0);
  Stack * stack = (Stack * ) malloc(sizeof(Stack));
  assert(stack);
  stack->value = (int *) malloc(sizeof(int) * SLOTS);
  if ( ! stack->value )
  {
    free(stack);
    assert(0);
  }
  stack->SLOTS = SLOTS;
  stack->top = 0;
  return stack;
}

void push(Stack * stack, int i)
{
  assert( stack && stack->top < stack->SLOTS);
  stack->value[stack->top] = i;
  ++stack->top;
}

int pop(Stack * stack)
{
  assert(stack && stack->top > 0);
  --stack->top;
  return stack->value[stack->top];
}

void destroy( Stack * stack )
{
  assert(stack);
  free(stack->value);
  free(stack);
}

void display(Stack * stack)
{
  assert(stack);
  int n;
  for (n=0; n<stack->top; ++n)
  {
    printf("stack[%d] = %d\n", n, stack->value[n]);
  }
}


这篇关于我想通过scanf在动态数组中存储一些值,我想显示该动态数组。我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆