[英]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.