[英]C Program Reading an 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;
}
}
输出请注意,“ prn”命令我必须添加一个不必要的1,而对于“ end”命令,我一开始没有添加任何东西,也没有进行任何处理。 输出图像
给出一个想法,如果没有代码。
将输入行分成argv []类型列表(不要忘了最后的NULL指针)。 然后,您可以使用xxx(argc,&argv)将整个列表传递给每个函数,让它们调用getopt()来解析自己的参数。 现在,他们可以处理多达YOUR_ARG_MAX值的任意数量的参数(artc = 1,不带参数,仅命令名称)。
如果没有可重入版本,则可以在空格定界符“ \\ t \\ r \\ n”或strtok()上使用strtok_r()。 由于strtok操作在每个令牌后放置\\ 0,因此您只需要将令牌地址保存在char * argv [1 + YOUR_ARG_MAX]数组中,并使用NULL终止计数argc变量中的参数。
您可以在输入command
检查其值,以便确定它是否是scanf
一个参数的函数之一,然后发出第二个scanf
否则仅调用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)
...因此,请正确声明它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.