Created
July 12, 2016 12:43
-
-
Save 52coder/7802d6a3ae66f370aa74a358d5d18032 to your computer and use it in GitHub Desktop.
编程练习代码实现.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.c | |
// HelloWorld | |
// | |
// Created by 52coder on 16/5/22. | |
// Copyright (c) 2016年 52coder. All rights reserved. | |
// | |
#include <ctype.h> | |
#include "set.h" | |
#define NUM_LEN 101 /**< 定义整数最大长度 >*/ | |
int main() | |
{ | |
char input[500]; | |
char command='o'; | |
char str[NUM_LEN]; | |
Node *head; | |
SetNodeInitiate(&head); | |
while(1) | |
{ | |
memset(input,'\0',500*sizeof(char)); | |
memset(str,'\0',NUM_LEN*sizeof(char)); | |
fgets(input,500,stdin); | |
if(-1==CheckLegal(input,&command,str)) | |
{ | |
printf("Invalid Command\n"); | |
continue; | |
} | |
switch(command) | |
{ | |
case 'a': | |
SetNodeInsert(head,str); | |
break; | |
case 'd': | |
SetNodeDelete(head,str); | |
break; | |
case 'p': | |
PrintSetNode(head); | |
break; | |
case 'q': | |
SetNodeRelease(head); | |
return 0; | |
default: | |
printf("Invalid Command\n"); | |
break; | |
} | |
} | |
SetNodeRelease(head); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* COPYRIGHT NOTICE | |
* Copyright (c) 2016 | |
* All rights reserved. | |
* | |
* @file set.h | |
* 本文件主要具体实现链表和相关操作,插入结点,删除结点,打印结点对应的内容 | |
检查数据合法性,如果合法提取指令和整数内容 | |
Compare比较两个提取的整数的大小 | |
* | |
* 版本 作者 日期 修订说明 | |
* | |
* 1.05 52coder 2016-05-26 最终版本 | |
*/ | |
#include "set.h" | |
#include "ctype.h" | |
/* | |
* @param[in] in为输入字符串 | |
@param[out] ch返回对应指令 str返回格式化后的字符串 | |
* @return 函数执行成功返回1如果op1>op2.返回0,如果op1<=op2,否则返回相应的错误码 | |
* @note 输入字符串合法返回1,输入字符串非法返回-1 | |
* | |
*/ | |
int CheckLegal(char *in,char *ch,char *str) | |
{ | |
int i=0; | |
int len=strlen(in); | |
int start=0; | |
int end=0; | |
int flag=0; | |
int zero=0; // 对 -0和0进行特别处理 | |
while(in[i]==' ') | |
{ | |
i++; | |
} | |
*ch=in[i]; | |
if('p'==*ch||'q'==*ch) | |
{ | |
i++; | |
while(' '==in[i]) | |
{ | |
i++; | |
} | |
if(len-1==i) | |
{ | |
return 1; | |
} | |
else | |
{ | |
return -1; | |
} | |
} | |
else if('a'==*ch||'d'==*ch) | |
{ | |
i++; | |
if(in[i]!=' ') | |
return -1; | |
while(' '==in[i]) | |
{ | |
i++; | |
} | |
//处理a/d空格 空格 空格 回车情形 | |
if(len-1==i) | |
{ | |
return -1; | |
} | |
if('-'==in[i]) | |
{ | |
i++; | |
flag=1; | |
} | |
while('0'==in[i]) | |
{ | |
zero = 1; | |
i++; | |
} | |
start=i; | |
while(isdigit(in[i])) | |
{ | |
i++; | |
} | |
end=i; | |
while(' '==in[i]) | |
{ | |
i++; | |
} | |
if('\n'==in[i]) | |
{ | |
if(1==flag) | |
{ | |
if(end-start>99) | |
{ | |
return -1; | |
} | |
str[0]='-'; | |
strncpy(str+1,in+start,end-start); | |
str[end-start+1]='\0'; | |
if (end == start && 1 == zero) | |
{ | |
str[0] = '0'; | |
str[1] = '\0'; | |
} | |
} | |
else | |
{ | |
if(end-start>100) | |
{ | |
return -1; | |
} | |
strncpy(str, in+start, end-start); | |
str[end-start]='\0'; | |
if (end == start && zero == 1) | |
{ | |
str[0] = '0'; | |
str[1] = '\0'; | |
} | |
} | |
return 1; | |
} | |
else | |
return -1; | |
} | |
else | |
return -1; | |
} | |
/* | |
* @param[in] op1 op2 为待比较的两个规格化后字符串,字符串格式完全符合整数形式 | |
* @return 函数执行成功,如果op1>op2.返回1,如果op1<=op2,则返回0 | |
* | |
* @note disk_info为输出参数,在调用本函数时,需要先申请disk_info的内存; | |
* | |
*/ | |
int Compare(char *op1,char *op2) | |
{ | |
int i=0; | |
if(('-'==op1[i])&&(op2[i]!='-')) | |
{ | |
return 0; | |
} | |
else if((op1[i]!='-')&&('-'==op2[i])) | |
{ | |
return 1; | |
} | |
else if(('-'==op1[i])&&('-'==op2[i])) | |
{ | |
if(strlen(op1)>strlen(op2)) | |
{ | |
return 0; | |
} | |
else if(strlen(op1)<strlen(op2)) | |
{ | |
return 1; | |
} | |
else | |
{ | |
if(strncmp(op1,op2,strlen(op1))>=0) | |
{ | |
return 0; | |
} | |
else | |
{ | |
return 1; | |
} | |
} | |
} | |
else | |
{ | |
if(strlen(op1)>strlen(op2)) | |
{ | |
return 1; | |
} | |
else if(strlen(op1)<strlen(op2)) | |
{ | |
return 0; | |
} | |
else | |
{ | |
if(strncmp(op1,op2,strlen(op1))>0) | |
{ | |
return 1; | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
} | |
} | |
//链表初始化 | |
int SetNodeInitiate(Node **head) | |
{ | |
*head=(Node *)malloc(sizeof(Node)); | |
if(NULL==*head) | |
{ | |
return -1; | |
} | |
(*head)->next=NULL; | |
memset((*head)->data,'\0',101*sizeof(char)); | |
return 0; | |
} | |
int SetNodeInsert(Node *head,char *str) | |
{ | |
Node *p, *r; | |
p=head; | |
//只有头结点,直接插入结点 | |
if(p->next==NULL) | |
{ | |
Node *q=(Node *)malloc(sizeof(Node)); | |
strcpy(q->data,str); | |
p->next=q; | |
q->next=NULL; | |
return 0; | |
} | |
while (p->next != NULL) | |
{ | |
if (Compare(p->next->data, str)>0) | |
{ | |
break; | |
} | |
p=p->next; | |
} | |
r = (Node *)malloc(sizeof(Node)); | |
strcpy(r->data,str); | |
r->next=p->next; | |
p->next=r; | |
return 0; | |
} | |
//删除结点 | |
int SetNodeDelete(Node *head,char *str) | |
{ | |
Node *p; | |
Node *s; | |
p=head; | |
while(p->next!=NULL) | |
{ | |
if(strcmp(str,p->next->data)!=0) | |
{ | |
p=p->next; | |
} | |
else | |
{ | |
s=p->next; | |
p->next=p->next->next; | |
free(s); | |
s=NULL; | |
return 1; | |
} | |
} | |
return -1; | |
} | |
//打印整条链表 | |
void PrintSetNode(Node *head) | |
{ | |
Node *p=head; | |
while(p->next!=NULL) | |
{ | |
printf("%s ",p->next->data); | |
p=p->next; | |
} | |
printf("\n"); | |
} | |
//释放整个链表 | |
void SetNodeRelease(Node *head) | |
{ | |
Node *p = head; | |
if ( NULL== head ) | |
{ | |
return; | |
} | |
while (head != NULL) | |
{ | |
head = head->next; | |
free(p); | |
p = head; | |
} | |
free(head); | |
head = NULL; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* COPYRIGHT NOTICE | |
* Copyright (c) 2016 | |
* All rights reserved. | |
* | |
* @file set.h | |
* 本文件主要定义链表和相关操作,插入结点,删除结点,打印结点对应的内容 | |
* | |
* 版本 作者 日期 修订说明 | |
* | |
* 1.05 52coder 2016-05-26 最重版本 | |
*/ | |
#ifndef _set_H | |
#define _set_H | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
typedef struct SetNode | |
{ | |
struct SetNode *next; | |
char data[101]; | |
}Node; | |
int checklegal(char *in,char *ch,char *str); | |
int SetNodeInitiate(Node **head); | |
int compare(char *op1,char *op2); | |
int SetNodeInsert(Node *head,char *str); | |
int SetNodeDelete(Node *head,char *str); | |
void PrintSetNode(Node *head); | |
void SetNodeRelease(Node *head); | |
#endif /* defined(____set__) */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment