user-0 提问: c语言编程求解

回复:
user: user-1
content: 参考楼上版主

user: user-2
content: 你这个问题好多： 1.输入a以后之所以输入b有错误，是因为输入缓冲区中有残留的回车符，所以getchar会直接读走，所以要在get_option开头加一句：fflush(stdin); 2.main函数里你遍历head,也不能直接操作head,否则你遍历打印完以后，head已经是NULL了，之后再进行别的操作就会因为访问空指针而报错，所以需要用个临时变量temp去遍历就行了。 3.你的check函数里，for循环内q = q->next;这句应该放到if后面，而不是前面，否则也会出现空指针而报错 PS：还有别的问题，楼主自己单步调试看看，单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。  #include<string.h> #include<stdio.h> #include<stdlib.h> #include "ctype.h"//字符处理函数 #define LEN sizeof(struct list)  struct list *head; static unsigned int count;  char get_option(void); int check(char arr[], unsigned int count, struct list *head); struct list *createlist(void); struct list *sortlist(struct list *head);    /*************选择，接受用户选项*********************/ char get_option(void) { 	fflush(stdin); 	char ch; 	puts("请输入a,b,c或者d."); 	while ((ch = getchar())<'a' || ch>'d') 	{ 		while ((ch = getchar()) != '\n'); 		puts("输入错误，请输入a,b,c或者d."); 	} 	return ch; }   struct list { 	char data[15]; 	struct list *next; }; /************创建链表**********/  struct list *createlist(void) { 	unsigned int i = 0; 	struct list *head, *q, *s; 	char a[15]; 	head = (struct list*)malloc(LEN); 	q = head; 	puts("开始创建字典1"); 	puts("请输入单词（每行一个，END结束输入）"); 	while (scanf("%s", a) == 1) 	{ 		if (check(a, i, head)) 			continue; 		if (strncmp(a, "END", 3) == 0) 		{ 			count = i; 			break; 		} 		else 		{ 			s = (struct list *)malloc(LEN); 			strcpy(s->data, a); 			q->next = s; 			q = s; 			q->next = NULL; 			i++; 		}  	} 	head = head->next; 	return(head); }  /******************检查是否有重复单词*******************/ int check(char arr[], unsigned int count, struct list *head) { 	unsigned int i; unsigned int flag = 0; 	struct list *q; 	for (i = 0; i < strlen(arr); i++) 	if (isalpha(arr[i]) == 0) 	{ 		printf("%s不是一个单词。\n", arr); 		flag = 1; 		break; 	} 	q = head; 	for (i = 0; i < count; i++) 	{ 		 		if (strncmp(q->data, arr, strlen(arr) + 1) == 0) 		{ 			puts("重复的单词！"); 			flag = 1; 		} 		q = q->next; 	} 	return flag; } /************排序函数*************/ struct list *sortlist(struct list *head) { 	struct list *head1, *p, *q, *s, *r; 	head1 = NULL; 	p = head; 	while (p != NULL) 	{ 		s = (struct list*)malloc(LEN); 		strcpy(s->data, p->data); 		s->next = NULL; 		if (head1 == NULL || strcmp(s->data, head1->data)<0)//链为空或着s值小于头结点，s插入表头 		{ 			s->next = head1; 			head1 = s; 		} 		else//寻找s的插入位置，r指向待比较的结点，q指向r的前驱结点 		{ 			q = head1; r = q; 			while (r != NULL&&strcmp(s->data, r->data)>0) 			if (strcmp(s->data, r->data) > 0) 			{ 				q = r; 				r = r->next; 			} 			if (r = NULL) 			{ 				q->next = s; 				s->next = NULL; 			} 			else 			{ 				s->next = r; 				q->next = s; 			} 		} 		p = p->next; 	} 	return(head1); }  /**************主函数***********/ int main() { 	struct list *head; 	struct list *p, *s; 	char a[15]; 	char input; 	puts("\t************************欢迎使用字典排版系统*************\n\n"); 	puts("\ta.构建字典系统并显示出来"); 	puts("\tb.添加新单词"); 	puts("\tc.对已有单词进行排序"); 	puts("\td.退出系统"); 	puts("\n\n\t***************************************************\n"); 	input = get_option(); 	while (input != 'd')//循环不同选择实现不同功能 	{ 		if (input == 'a') 		{ 			head = createlist(); 			struct list *temp = head; 			puts("temp"); 			while (temp != NULL) 			{ 				printf("\t%s\n", temp->data); 				temp = temp->next; 			} 		} 		if (input == 'b') 		{ 			puts("添加词库"); 			puts("请输入新的单词（每行一个，END结束输入）"); 			while (scanf("%s", a)) 			{ 				if (check(a, count, head))  					continue;  				if (strncmp(a, "END", 3) == 0) 					break; 				else 				{ 					s = (struct list *)malloc(LEN); 					strcpy(s->data, a); 					s->next = head; 					head = s; 					count++; 				} 				puts("已有单词"); 				while (head != NULL) 				{ 					printf("\t%s\n", head->data); 					head = head->next; 				}   			}  		} 		if (input == 'c') 		{ 			puts("对单词进行排序："); 			p = sortlist(head); 			p = head; 			printf("排序:\n"); 			while (p != NULL) 			{ 				printf("\t%s\n", p->data); 				p = p->next; 			}  		} 		puts("还要做些什么？"); 		input = get_option(); 	}  	puts("谢谢使用，再见！"); 	return 0; }

user: user-0
content: 没人吗？ 呜呜呜呜