C程序读取输入

时间:2017-11-04 22:43:23

标签: c input

对于我的程序,目标是读取一个字符串并使用我的代码输入。但是,我们必须注意我的一些命令只需要一个输入,有些需要两个。例如:" ina"," inb"," del"," rep"命令需要2个输入,其中prn只需要1.除了我一直使用的方式(scanf两次)之外,还有另一种方法可以将它应用于我的代码。因为当我想使用prn时,我必须包含一个不必要的int。

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

struct node {
  int data;
  char *item;
  struct node* next;
};

//Global Variables
struct node* root = NULL;

//Prototypes
void ina();
void inb();
int length();
void prn();
void del();
void rep();

//Main
void main () {
  char command[4];
  int num;
  char str[255];


  while(1) {
    printf("Command? ");
    fflush(stdout);
    scanf("%s", &command); //This is where I want to generalize
    scanf("%d", &num);     //and clean up


      //Reads input and selects which command to execute
    if (strcmp(command, "ina")==0) {
      ina(num);
    } else
    if(strcmp(command, "inb")==0) {
      inb(num);
    } else
    if (strcmp(command, "prn")==0) {
      prn();
    } else
    if (strcmp(command, "del")==0) {
      del(num);
    } else
    if (strcmp(command, "rep")==0) {
      rep(num);
    } else
    if (strcmp(command, "end")==0) {
      exit(1);
    }
    else {
      return;
    }
  }

}




//Command Insert After
void ina(int n) {
  struct node* temp;
  temp = (struct node*)malloc(sizeof(struct node));

  temp->data = n;
  temp->next = NULL;

  if(root==NULL) {
    root = temp;
    printf("Text inserted at beginning\n");

  }
  else {
    struct node* p;
    p = root;

    while(p->next != NULL) {
      p = p->next;
    }
    p->next = temp;
      printf("Ok\n");

  }
}


//Command Insert Before
void inb(int n) {
  struct node* temp;
  temp = (struct node*)malloc(sizeof(struct node));
  temp->data = n;
  temp->next=NULL;

  if (root == NULL) {
    root = temp;
    printf("Text inserted at beginning\n");
    fflush(stdout);
  }
  else {
    temp->next=root;
    root = temp;
    printf("Ok\n");
    fflush(stdout) ;
  }

}

//Command Length, not necessary but use for delete command
int length() {
  struct node* temp;
  int count = 0;
  temp = root;

  while(temp != NULL) {
    count++;
    temp = temp->next;
  }
  return count;
}

//Command Print
void prn() {
  struct node* temp;
  temp = root;

  if(temp == NULL) {
    printf("List is empty\n");
    fflush(stdout);
  }
  else {
    while(temp != NULL) {
      printf("%d\n",temp->data);
      temp = temp->next;
    }
    printf("\n");
  }
}

//Command Delete
void del(int n) {
  struct node* temp;

  if(n > length()) {
    printf("No Such Index\n");
    fflush(stdout);
    return;
  }
  else if(n==1) {
    temp = root;
    root = temp->next;
    temp->next = NULL;
    free(temp);
  }
  else {
    struct node* p = root, *q;
    int i = 1;
    while(i<n-1) {
      p = p->next;
      i++;
    }
    q = p->next;
    p->next = q->next;
    q->next = NULL;
    free(q);
  }
    printf("Deleted\n");
    fflush(stdout);
}

//Command Replace FIX
void rep(int i) { 
  int old, n;
  struct node* temp;
  temp = root;
  old = i;

  printf("\nEnter what you want to replace with: ");
  scanf("%d", &n);

  if(temp == NULL) {
    printf("No such index\n");
    return NULL;
}
  while (temp != NULL) {
  if(temp->data == old) {
    temp->data = n;
      printf("Replaced\n");

  }
  temp = temp->next;
}
}

输出 请注意&#34; prn&#34;命令我必须添加一个不必要的1和#34; end&#34;命令我最初没有添加任何内容而且没有处理任何内容。 Output image

2 个答案:

答案 0 :(得分:2)

如果没有代码,请给出一个想法。

将输入行分成argv []类型列表(不要忘记末尾的NULL指针)。然后你可以使用xxx(argc,&amp; argv)将整个列表传递给你的每个函数,让他们调用getopt()来解析自己的参数。他们现在可以处理任意数量的参数(artc = 1表示无参数,只是命令名称),最多可以处理YOUR_ARG_MAX值。

如果您没有可重入版本,可以在空白分隔符“\ t \ r \ n”或strtok()上使用strtok_r()。由于strtok操作在每个令牌之后放置\ 0,您只需要将令牌地址保存在 char * argv [1 + YOUR_ARG_MAX] 数组中,并在argc变量中使用NULL终止计数参数。

答案 1 :(得分:1)

您可以在输入command后检查其值,如果它是带有一个参数的函数之一,则发出第二个scanf,否则只调用带有0个参数的函数:

scanf("%s", &command); //This is where I want to generalize

if (strcmp(command, "ina") == 0){
    scanf("%d", &num);
    ina(num);
}
else
    if(strcmp(command, "inb" ) == 0){
      scanf("%d", &num);
      inb(num);
    }
else
    if (strcmp(command, "prn") == 0)
      prn();
else
    if (strcmp(command, "del") == 0){
        scanf("%d", &num);
        del(num);
} 
else
    if (strcmp(command, "rep")==0)
        scanf("%d", &num);
        rep(num);
} 
else
if (strcmp(command, "end") == 0) {
  exit(1);
  • 还要注意代码没有编译:你声明:ina, inb, del, rep作为函数接受0参数但是在调用它们时你传递一个整数:rep(num) ...所以正确地宣布它们。
相关问题