AddNumber功能麻烦

时间:2016-09-09 23:44:36

标签: c

我无法理解如何从我的程序中获取用户输入并将其传递给函数AddNumber。程序将启动并询问用户输入并将其存储到输入数组中,但我不确定如何从数组中获取信息并将其传递给函数AddNumber

我已根据我的理解更新了代码' AddNumber'功能没有做任何事情。我的印象是我创造了指针' previous'应该是previous = NULL吗?

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

typedef struct A_NewNumber {
  struct A_NewNumber *next;
  char newNum;
} NewNumber;

NewNumber *AddNumber(NewNumber *previous, char *input){

//char input[16];
//double numEntered = 0;

NewNumber *newNum = malloc(sizeof(NewNumber));
sscanf(input, "%lf", &newNum->newNum);

//sscanf(input, "%s", newNum->enterNumber);
//numEntered = atof(input);
//possible code 
/*if (previous != NULL){
    previous->newNum;
}
newNum->next = NULL;
newNum->newNum = 0;
return newNum;

}*/


void PrintList(NewNumber *numStart) {
  NewNumber *currentNumber = numStart;
  int count = 0;

  while (currentNumber != NULL) {
    count++;
    printf("Number Position:%s\n", currentNumber->enterNumber);
    currentNumber = currentNumber->next;
  }
  printf("Total Numbers Entered%d\n", count);
}

void CleanUp(NewNumber *start) {
  NewNumber *freeMe = start;
  NewNumber *holdMe = NULL;

  while (freeMe != NULL) {
    holdMe = freeMe->next;
    free(freeMe);
    freeMe = holdMe;
  }
}

int main(){

   //indexNum = 0;
   char command[16];
   char input[16];
   //float userInput;
   NewNumber *userEnter = NULL;
   NewNumber *start = NULL;
   NewNumber *newest = NULL;

   while(fgets(input, sizeof input, stdin)){
    printf("Please enter a number->");
    printf("Enter 'quit' to stop or 'print' to print/calculate");
    sscanf(input, "%s", command);
    if(newest == NULL){
        start = AddNumber(NULL, input);
        newest = start;
    }else{
        newest = AddNumber(newest, input);
    }
    if( strncmp(command, "print", 5) == 0){
        PrintList(start);
    }else if( strncmp(command, "quit", 4)== 0){
        printf("\n\nQuitting....\n");
        break;
        //userInput = enterNumber;
    }
  }
  CleanUp(start);
  return 0;
}

3 个答案:

答案 0 :(得分:0)

如果不过多修改代码,我建议AddNumber获取指向前一个NewNumber以及用户输入的指针。您的意图似乎也是在double中阅读,因此我将newNum字段修改为double类型。像这样:

typedef struct A_NewNumber{
    struct A_NewNumber *next;
    double newNum;
} NewNumber;

NewNumber *AddNumber(NewNumber *previous, char *input){

    NewNumber *newNum = malloc(sizeof(NewNumber));
    /* use %lf to read in a double */
    sscanf(input, "%lf", &newNum->newNum);

    if (previous != NULL){
        previous->next = newNum;
    }

    return newNum;
}

然后,当您调用AddNumber函数时,您可以传递上一个和输入,如

if(newest == NULL){
    start = AddNumber(NULL, input);
    newest = start;
} else {
    newest = AddNumber(newest, input);
}

答案 1 :(得分:0)

一些问题:

代码无效

if (previous != NULL){
    // Following line does no assignment
    previous->newNum;
}

当然,在分配之后,应该设置所有字段

NewNumber *newNum = malloc(sizeof(NewNumber));
sscanf(input, "%s", newNum->enterNumber);

// add
newNum->next = NULL;
newNum->newNum = 0; // TBD code, but set to something

指针类型错误

NewNumber *AddNumber(NewNumber *previous){
....
char input[16];
....
start = AddNumber(input);

答案 2 :(得分:-1)

通常有一种内置的方法可以在大多数编程语言中获得用户输入。例如,在Java中,您可以使用Scanner对象从命令行提示用户输入。这是命令行应用程序吗?

这是C所以我鼓励你使用scanf()函数,这里有一个简短的代码片段https://en.wikibooks.org/wiki/C_Programming/Simple_input_and_output#Input_using_scanf.28.29

您只需提供数据类型(即字符串的%s)和内存地址。这可以通过指针保存,您可以将其传递给AddNumber()函数。

希望这有帮助!