scanf 如何处理字符串和整数? C
How does scanf work with both strings and integers? C
所以我有一个程序,它接受一个数据字符串和一个数字,这是它在打印时的优先级位置。我需要使用链表,我已经弄清楚如何使用它,但是这个程序的执行方式是在数据字符串的末尾,用户应该输入 NONE 和优先级程序执行。问题是我对 strcmp 的检查强制用户输入 NONE 两次以执行程序。我认为我没有正确使用 scanf 获取字符串和 int 值,这就是我的问题所在,但我不确定。
这是一个正确的示例输入:
andk81739wewe 7
qweod125632ao 3
lenlc93012wasd 0
093deaeiao12 5
13jadacas291 3
...
NONE
这是程序执行实际必须输入的内容
andk81739wewe 7
qweod125632ao 3
lenlc93012wasd 0
093deaeiao12 5
13jadacas291 3
...
NONE
NONE
关于为什么必须键入第二个 NONE 程序才能识别已键入 none 的任何想法?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LARGE 100
struct node
{
char data[LARGE];
int position;
struct node* next;
};
void sortedInsert(struct node** first, struct node* new_node)
{
struct node* current;
if (*first == NULL || (*first)->position <= new_node->position)
{
new_node->next = *first;
*first = new_node;
}
else
{
current = *first;
while (current->next!=NULL &&
current->next->position > new_node->position)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
struct node *newNode(char *new_data,int position)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
strcpy(new_node->data,new_data);
new_node->position=position;
new_node->next = NULL;
return new_node;
}
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%s \n", temp->data);
temp = temp->next;
}
}
int main(void) {
char job[LARGE],blank[1]={' '},*p,*q;
int number=0,x=0;
q=&blank[1];
struct node* first = NULL;
struct node *new_node = newNode(q,0);
printf("Please enter printing jobs\n");
while(x!=1){
if(strcmp(job,"NONE")==0){
x=1;
}
else{
scanf("%s", job);
scanf("%d", &number);
p=&job[0];
sortedInsert(&first, new_node);
new_node = newNode(p,number);
}
}
printf("Print Job in order from 9-0\n");
printList(first);
return 0;
}
或者,您可以使用以下代码段。这是一种更加简化和简化的方法。:
int main(void) {
char job[LARGE];
struct node *first = NULL;
struct node *new_node = NULL;
int number;
printf("Please enter printing jobs\n");
while(1)
{
scanf("%s", job);
if(!strcmp(job, "NONE"))
break;
scanf("%d", &number);
new_node = newNode(job, number);
sortedInsert(&first, new_node);
}
printf("Print Job in order from 9-0\n");
printList(first);
return 0;
}
所以我有一个程序,它接受一个数据字符串和一个数字,这是它在打印时的优先级位置。我需要使用链表,我已经弄清楚如何使用它,但是这个程序的执行方式是在数据字符串的末尾,用户应该输入 NONE 和优先级程序执行。问题是我对 strcmp 的检查强制用户输入 NONE 两次以执行程序。我认为我没有正确使用 scanf 获取字符串和 int 值,这就是我的问题所在,但我不确定。
这是一个正确的示例输入:
andk81739wewe 7
qweod125632ao 3
lenlc93012wasd 0
093deaeiao12 5
13jadacas291 3
...
NONE
这是程序执行实际必须输入的内容
andk81739wewe 7
qweod125632ao 3
lenlc93012wasd 0
093deaeiao12 5
13jadacas291 3
...
NONE
NONE
关于为什么必须键入第二个 NONE 程序才能识别已键入 none 的任何想法?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LARGE 100
struct node
{
char data[LARGE];
int position;
struct node* next;
};
void sortedInsert(struct node** first, struct node* new_node)
{
struct node* current;
if (*first == NULL || (*first)->position <= new_node->position)
{
new_node->next = *first;
*first = new_node;
}
else
{
current = *first;
while (current->next!=NULL &&
current->next->position > new_node->position)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
struct node *newNode(char *new_data,int position)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
strcpy(new_node->data,new_data);
new_node->position=position;
new_node->next = NULL;
return new_node;
}
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%s \n", temp->data);
temp = temp->next;
}
}
int main(void) {
char job[LARGE],blank[1]={' '},*p,*q;
int number=0,x=0;
q=&blank[1];
struct node* first = NULL;
struct node *new_node = newNode(q,0);
printf("Please enter printing jobs\n");
while(x!=1){
if(strcmp(job,"NONE")==0){
x=1;
}
else{
scanf("%s", job);
scanf("%d", &number);
p=&job[0];
sortedInsert(&first, new_node);
new_node = newNode(p,number);
}
}
printf("Print Job in order from 9-0\n");
printList(first);
return 0;
}
或者,您可以使用以下代码段。这是一种更加简化和简化的方法。:
int main(void) {
char job[LARGE];
struct node *first = NULL;
struct node *new_node = NULL;
int number;
printf("Please enter printing jobs\n");
while(1)
{
scanf("%s", job);
if(!strcmp(job, "NONE"))
break;
scanf("%d", &number);
new_node = newNode(job, number);
sortedInsert(&first, new_node);
}
printf("Print Job in order from 9-0\n");
printList(first);
return 0;
}