我正在尝试从用户那里读取关于客户的输入。C,双向链表问题
输入:迈克,404禁止日,北卡罗来纳州,27607,123.78
然后客户添加到按字母顺序排序,双向链表。用户可以选择插入条目,删除和条目,或查看列表。添加用户控件后...我的sscanf无法正常工作了。我无法弄清楚为什么。我不明白为什么我无法在用户控制版本中打印客户价值。
此外,任何意见/关于更新节点在前场的语法联系将是非常有益的:P
预先感谢您
工作(没有用户控制):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXNAMELEN 19
#define MAXADDRLEN 49
#define MAXCITYLEN 19
#define MAXSTATELEN 2
struct aNode {
char name[MAXNAMELEN];
char address[MAXADDRLEN];
char city[MAXCITYLEN];
char state[MAXSTATELEN];
int zip;
float balance;
struct aNode *next;
struct aNode *prev;
};
typedef struct aNode node;
struct aNode *dLList;
void read();
void input();
void print();
void delete();
int insertSort(node*);
int main() {
//from text file
read();
input();
/*
int choice;
while(1) {
printf("\n1.INSERT\n2.DELETE\n3.DISPLAY\n4.END\n");
printf("Enter choice:");
scanf("%d",&choice);
switch(choice) {
case 1:
input();
break;
case 2:
delete();
break;
case 3:
print();
break;
case 4:
exit(0);
}
}
*/
return(0);
}
//TODO
void read() {
}
void input() {
struct aNode *current;
current = (struct aNode *)malloc(sizeof(struct aNode));
int buff = 120;
char temp[buff];
printf("Enter data: ");
fgets(temp, buff, stdin);
sscanf(temp, "%[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %d%*c %f",
current->name, current->address, current->city, current->state,
¤t->zip, ¤t->balance);
insertSort(current);
//only for testing
printf("%s, %s, %s, %s, %5d, $%9.2f\n",
current->name, current->address, current->city,
current->state, current->zip, current->balance);
}
void print() {
struct aNode *p;
for(p = dLList; p != NULL; p = p->next) {
printf("%s, %s, %s, %s, %5d, $%9.2f\n",
p->name, p->address, p->city,
p->state, p->zip, p->balance);
}
}
//TODO
void delete() {
}
int insertSort(node * current) {
struct aNode *p;
struct aNode *q;
p = (struct aNode *)malloc(sizeof(struct aNode));
p = current;
//need to link to previous node
if(dLList == NULL || strcmp(dLList->name, p->name) > 0) {
p->next = dLList;
p->prev = NULL;
return(0);
}else {
q = dLList;
while(q->next != NULL && strcmp(q->next->name, q->name) < 0) {
q = q->next;
}
p->next = q->next;
q->next = p;
return(0);
}
}
输出:mike,404禁止st,raleigh,nc,27607,$ 123.78
不工作(用户控制):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXNAMELEN 19
#define MAXADDRLEN 49
#define MAXCITYLEN 19
#define MAXSTATELEN 2
struct aNode {
char name[MAXNAMELEN];
char address[MAXADDRLEN];
char city[MAXCITYLEN];
char state[MAXSTATELEN];
int zip;
float balance;
struct aNode *next;
struct aNode *prev;
};
typedef struct aNode node;
struct aNode *dLList;
void read();
void input();
void print();
void delete();
int insertSort(node*);
int main() {
//from text file
read();
//input();
int choice;
while(1) {
printf("\n1.INSERT\n2.DELETE\n3.DISPLAY\n4.END\n");
printf("Enter choice:");
scanf("%d",&choice);
switch(choice) {
case 1:
input();
break;
case 2:
delete();
break;
case 3:
print();
break;
case 4:
exit(0);
}
}
return(0);
}
//TODO
void read() {
}
void input() {
struct aNode *current;
current = (struct aNode *)malloc(sizeof(struct aNode));
int buff = 120;
char temp[buff];
printf("Enter data: ");
fgets(temp, buff, stdin);
sscanf(temp, "%[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %d%*c %f",
current->name, current->address, current->city, current->state,
¤t->zip, ¤t->balance);
insertSort(current);
//only for testing
printf("%s, %s, %s, %s, %5d, $%9.2f\n",
current->name, current->address, current->city,
current->state, current->zip, current->balance);
}
void print() {
struct aNode *p;
for(p = dLList; p != NULL; p = p->next) {
printf("%s, %s, %s, %s, %5d, $%9.2f\n",
p->name, p->address, p->city,
p->state, p->zip, p->balance);
}
}
//TODO
void delete() {
}
int insertSort(node * current) {
struct aNode *p;
struct aNode *q;
p = (struct aNode *)malloc(sizeof(struct aNode));
p = current;
//need to link to previous node
if(dLList == NULL || strcmp(dLList->name, p->name) > 0) {
p->next = dLList;
p->prev = NULL;
return(0);
}else {
q = dLList;
while(q->next != NULL && strcmp(q->next->name, q->name) < 0) {
q = q->next;
}
p->next = q->next;
q->next = p;
return(0);
}
}
输出:,,,,0,$ 0.00包装
工作,谢谢:)你说“为你工作”?你在做同样的事情吗? :P 有关AShelly在下面写下的任何建议将会有所帮助。我不太明白他/她在暗示什么。 – beowulf3 2011-04-27 19:20:16
@ beowulf3不,我的意思是我在发布前试过:) – NicolasP 2011-04-27 19:22:16
错误:P感谢 – beowulf3 2011-04-27 19:24:35