| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 834 人关注过本帖
标题:[原创]To Flier,字符串操作演示系统
取消只看楼主 加入收藏
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
 问题点数:0 回复次数:3 
[原创]To Flier,字符串操作演示系统

我仔细看了下Flier给出的字符串演示程序的代码,觉得之间漏了很多很多"{","}",而且看上去并不像Flier本人写出的代码,由于修改代码过于繁琐,我重新写了一份,顺便练习一下我最近自学的链表。。。。 <源代码> #include <stdio.h> #include <math.h> #include <conio.h> #include <malloc.h> #include <ctype.h>

#define TRUE 1 #define FALSE 0

#define MAX_CMD_COUNT 4 #define MAX_STRING_LEN 255

typedef unsigned char BOOL;

struct strlst { char *strname; char *content; struct strlst *nextAddr; struct strlst *prevAddr; };

struct strlst *node_head; struct strlst *node_tail; struct strlst *node_curr;

BOOL isnumeric(char *); /* 测试该串是否仅包含数字 */ int strlen(char *); /* 去串长度 */ int strtoint(char *); /* 将串转换为整数 */ BOOL equal(char *,char *); /* 测试两串是否相等 */ char *mid(char *,char *,int,int); /* 模仿VB中的Mid函数取子串 */ int strcmp(char *,char *); /* 比较两字符串,返回第一次出现的位置 */ char *strcpy(char *,char *); /* 复制串 */ char *inverse(char *); /* 将一个串逆转 */ char *substr(char *,char *,int,int); /* 在指定范围取子串 */ char *convert(char *,char [][MAX_STRING_LEN],int); /* 将一个指定行的二位数组转换成指针 */ char *lowercase(char *); /* 将指定串转换成小写 */

struct strlst *cmd_create(char *,char *); /* 创建一个串变量结构,返回结构指针 */ struct strlst *cmd_searchbyname(char *); /* 通过串名查找一个串变量结构,返回结构指针 */ struct strlst *cmd_searchbycont(char *); /* 通过串内容查找一个串变量结构,返回结构指针 */ void display(struct strlst *); /* 输出单个串变量结构 */ void dispall(); /* 输出整条链表 */ void addtolst(struct strlst *); /* 将一个串变量结构添加至链表 */ void delbyname(char *); /* 通过串名删除链表中的指定串变量结构 */ void delbycont(char *); /* 通过串内容删除链表中的指定串变量结构 */ void total(); /* 统计链表占用的内存数 */ void freeall(); /* 释放整条链表 */ void locate(int); /* 将当前指针指向指定链表位置 */ void cmd(char *); /* 执行命令 */

int main() { char *command; command=(char *)malloc(MAX_STRING_LEN*sizeof(char)); if(!command) { printf("Cannot allocate memory!\n"); exit(1); } printf("String operating demo version 0.1\n"); printf("Created by yuki\n\n"); while(1) { printf("CMD>"); gets(command); cmd(command); } return 1; }

BOOL isnumeric(char *s) { if(!s) return FALSE; while(*s) if(!isdigit(*s++)) return FALSE; return TRUE; }

int strlen(char *s) { int result=0; if(!s) return 0; while(*s++) result++; return result; } int strtoint(char *s) { int sl=strlen(s)-1; int result=0; if(!s||!isnumeric(s)) return -1; while(*s) { result+=(*s-'0')*(int)(pow(10.0,(double)(sl--))); s++; } return result; }

BOOL equal(char *s1,char *s2) { if(!s1||!s2) return FALSE; if(strlen(s1)!=strlen(s2)) return FALSE; while(*s1) if(*s1++!=*s2++) return FALSE; return TRUE; }

char *mid(char *des,char *src,int begin,int length) { char *p=des; if(!src||begin>strlen(src)||begin+length-1>strlen(src)) return NULL; src+=begin-1; while(length--) *des++=*src++; *des='\0'; return p; }

int strcmp(char *s1,char *s2) { char *p,*bakup=s2; BOOL flag; int slen1=strlen(s1),slen2=strlen(s2); int i=1; if(!s1||!s2||!slen1||!slen2) return 0; if(slen1>slen2) { p=(char*)malloc((slen2+1)*sizeof(char)); if(!p) { printf("Cannot allocate memory!\n"); exit(1); } while(i<=slen1) { p=mid(p,s1,i,slen2); if(!p) {free(p); return 0;} flag=TRUE; while(*p) { if(*p++!=*s2++) { flag=FALSE; s2=bakup; break; } } if(flag) {free(p); return i;} i++; } return 0; } else if(slen1==slen2) { if(equal(s1,s2)) return 1; return 0; } else return 0; }

char *strcpy(char *des,char *src) { if(!src) return NULL; char *p=des; while(*des++=*src++); *des='\0'; return p; }

char *inverse(char *s) { if(!s) return NULL; char *p=s; char temp; char *sbegin=s,*send=s+strlen(s)-1; while(sbegin<=send) { temp=*sbegin; *sbegin=*send; *send=temp; sbegin++; send--; } return p; }

char *substr(char *des,char *src,int begin,int end) { if(!src) return NULL; if(begin<1||end>strlen(src)||begin>end) return NULL; char *p=des,*eAddr; eAddr=src+end-1; src+=begin-1; while(src<=eAddr) *des++=*src++; *des='\0'; return p; }

char *convert(char *des,char src[][MAX_STRING_LEN],int row) { char *p=des; int i=0; while(src[row][i]) *des++=src[row][i++]; *des='\0'; return p; }

char *lowercase(char *s) { char *p=s; if(!s) return NULL; while(*s) { if(*s>='A'&&*s<='Z') *s=*s+32; s++; } return p; }

struct strlst *cmd_create(char *name,char *content) { struct strlst *p; p=(struct strlst *)malloc(sizeof(struct strlst)); if(!p) { printf("Cannot allocate for structure!\n"); exit(1); } p->strname=name; p->content=content; p->nextAddr=NULL; p->prevAddr=NULL; return p; }

struct strlst *cmd_searchbyname(char *name) { struct strlst *p; p=node_head; while(p) { if(strcmp(p->strname,name)) {node_curr=p; return p;} p=p->nextAddr; } return NULL; }

struct strlst *cmd_searchbycont(char *content) { struct strlst *p; p=node_head; while(p) { if(strcmp(p->content,content)) {node_curr=p; return p;} p=p->nextAddr; } return NULL; }

void display(struct strlst *s) { printf("Varible name of string : %s\n",s->strname); printf("Varible content : %s\n",s->content); }

void dispall() { struct strlst *p; p=node_head; if(!p) printf("The list is empty!\n"); else { printf("String name Content\n"); while(p) { printf("%4s %12s\n",p->strname,p->content); p=p->nextAddr; } } }

void addtolst(struct strlst *s) { if(!node_head) /* 如果表头为空的,则将结构添加至表头 */ { node_head=s; /* 注意:首先使用cmd_create()函数创建结构 */ node_tail=node_head; node_curr=node_head; } else /* 否则在链表尾部添加新的结构 */ { s->prevAddr=node_tail; node_tail->nextAddr=s; node_tail=s; /* 将链表尾巴移到新添加的结构体地址上 */ node_curr=node_tail; } }

void delbyname(char *name) { struct strlst *p=cmd_searchbyname(name); if(!p) printf("No such varible structure can be got rid of!\n"); else { struct strlst *bakup,*waste=p; if(!p->nextAddr&&!p->prevAddr) { node_head=NULL; node_tail=NULL; node_curr=NULL; } else if(p==node_head) { node_head=node_head->nextAddr; node_head->prevAddr=NULL; node_curr=node_head; } else if(p==node_tail) { node_tail=node_tail->prevAddr; node_tail->nextAddr=NULL; node_curr=node_tail; } else if(p->prevAddr&&p->nextAddr) { bakup=p->prevAddr; /* 备份当前节点的前一个地址 */ p=p->nextAddr; /* 当前位置后移一个节点 */ p->prevAddr=bakup; /* 将新位置的前一个地址设为备份位置,这样就断开了和欲删除地址处的连接 */ bakup->nextAddr=p; /* 同样将备份位置的后一个地址和后移后的节点地址相连 */ node_curr=p; } printf("Varible %s is deleted!\n",waste->strname); free(waste); } }

void delbycont(char *content) { struct strlst *p=cmd_searchbycont(content); if(!p) printf("No such varible structure can be got rid of!\n"); else { struct strlst *bakup,*waste=p; if(!p->nextAddr&&!p->prevAddr) { node_head=NULL; node_tail=NULL; node_curr=NULL; } else if(p==node_head) { node_head=node_head->nextAddr; node_head->prevAddr=NULL; node_curr=node_head; } else if(p==node_tail) { node_tail=node_tail->prevAddr; node_tail->nextAddr=NULL; node_curr=node_tail; } else if(p->prevAddr&&p->nextAddr) { bakup=p->prevAddr; /* 备份当前节点的前一个地址 */ p=p->nextAddr; /* 当前位置后移一个节点 */ p->prevAddr=bakup; /* 将新位置的前一个地址设为备份位置,这样就断开了和欲删除地址处的连接 */ bakup->nextAddr=p; /* 同样将备份位置的后一个地址和后移后的节点地址相连 */ node_curr=p; } printf("Varible %s is deleted!\n",waste->strname); free(waste); } }

void total() { long sum=0,count=0; struct strlst *p; p=node_head; while(p) { sum+=sizeof(p); count++; p=p->nextAddr; } printf("Total %ld structure(s) found in the linked list!\n",count); printf("Total %ld byte(s) memory have been allocated!\n",sum); }

void freeall() { struct strlst *p,*bak; p=node_head; while(p) { bak=p->nextAddr; free(p); p=bak; } }

void locate(int pos) { struct strlst *p; if(pos<1) printf("Impossible to locate the position which is illegal!\n"); else { p=node_head; while(p) { if(!pos) {node_curr=p; break;} p=p->nextAddr; } } }

搜索更多相关主题的帖子: Flier 字符 系统 演示 
2005-05-28 13:01
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 
void cmd(char *command)
{
    char argment[MAX_CMD_COUNT][MAX_STRING_LEN]={0};   
    int i=0,j=0;
   
    char *temp;
    char *strname,*content,*s;
   
    struct strlst *p;
   
    temp=(char *)malloc(MAX_STRING_LEN*sizeof(char));
    if(!temp)
    {
        printf("Cannot allocate memory!\n");
        exit(1);
    }
            
    while(*command)
    {
        if(*command==32)
        {
            i++;
            j=0;
            command++;
            continue;
        }
        argment[i][j++]=*command++;
    }
    temp=convert(temp,argment,0);
    if(strcmp(lowercase(temp),"create"))
    {
        strname=(char *)malloc(MAX_STRING_LEN*sizeof(char));
        if(!strname)
        {
            printf("Cannot allocate memory!\n");
            exit(1);
        }
        strname=convert(strname,argment,1);
        if(!strlen(strname))
            printf("Synx error - The length of varible name cannot be ZERO!\n");
        else
        {
            content=(char *)malloc(MAX_STRING_LEN*sizeof(char));
            if(!content)
            {
                printf("Cannot allocate memory!\n");
                exit(1);
            }
            content=convert(content,argment,2);
            if(!strlen(content))
                printf("Synx error - Cannot create a empty string!\n");
            else
            {
                p=cmd_create(strname,content);
                addtolst(p);
                printf("A new varible is created!\n");
            }
        }
    }
    else if(strcmp(lowercase(temp),"display"))
    {
        strname=(char *)malloc(MAX_STRING_LEN*sizeof(char));
        if(!strname)
        {
            printf("Cannot allocate memory!\n");
            exit(1);
        }
        strname=convert(strname,argment,1);
        
        content=(char *)malloc(MAX_STRING_LEN*sizeof(char));
        if(!content)
        {
            printf("Cannot allocate memory!\n");
            exit(1);
        }
        content=convert(content,argment,2);
        
        if(strcmp(lowercase(strname),"byname"))
        {
            if(!strlen(content))
                printf("Synx error - Varible name for searching cannot be empty!\n");
            else
            {
                p=cmd_searchbyname(content);
                if(!p)
                    printf("Cannot find any similar structure!\n");
                else
                {
                    display(p);
                    free(strname);
                    free(content);
                }   
            }
        }
        else if(strcmp(lowercase(strname),"bycont"))
        {
            if(!strlen(content))
                printf("Synx error - Content for searching cannot be empty!\n");
            else
            {
                p=cmd_searchbycont(content);
                if(!p)
                    printf("Cannot find any similar structure!\n");
                else
                {
                    display(p);
                    free(strname);
                    free(content);
                }
            }
        }
        else if(strcmp(lowercase(strname),"current"))
        {
            if(!node_curr)
                printf("Nothing!\n");
            else
            {
                display(node_curr);
                free(strname);
                free(content);
            }        
        }
        else if(strcmp(lowercase(strname),"all"))
        {
            dispall();
            free(strname);
            free(content);
        }   
        else
            printf("Synx error - Method for searching is invaild!\n");
    }
    else if(strcmp(lowercase(temp),"del"))
    {
        strname=(char *)malloc(MAX_STRING_LEN*sizeof(char));
        if(!strname)
        {
            printf("Cannot allocate memory!\n");
            exit(1);
        }
        strname=convert(strname,argment,1);
        
        content=(char *)malloc(MAX_STRING_LEN*sizeof(char));
        if(!content)
        {
            printf("Cannot allocate memory!\n");
            exit(1);
        }
        content=convert(content,argment,2);
        if(strcmp(lowercase(strname),"byname"))
        {
            if(!strlen(content))
                printf("Synx error - Varible name for deleting cannot be empty!\n");
            else
            {
                delbyname(content);
                free(strname);
                free(content);
            }
        }
        else if(strcmp(lowercase(strname),"bycont"))
        {
            if(!strlen(content))           
                printf("Synx error - Content for deleting cannot be empty!\n");
            else
            {
                delbycont(content);
                free(strname);
                free(content);
            }
        }
        else
            printf("Synx error - Method for deleting is invaild!\n");
    }
    else if(strcmp(lowercase(temp),"strlen"))
    {
        if(!node_curr)
        {
            printf("The current structure points to nothing!\n");
            printf("Using 'display current' to check the current structure!\n");
        }
        else
        {
            printf("Varible name : %s\n",node_curr-&gt;strname);
            printf("Content length : %d\n",strlen(node_curr-&gt;content));
        }        
    }
    else if(strcmp(lowercase(temp),"strcpy"))
    {
        if(!node_curr)
            printf("Current structure has not created yet!\n");
        else
        {
            strname=(char *)malloc(MAX_STRING_LEN*sizeof(char));
            if(!strname)
            {
                printf("Cannot allocate memory!\n");
                exit(1);
            }
            strname=convert(strname,argment,1);
            if(!strname)
                printf("No content can be copyed to the current structure!");
            else
            {
                printf("Varible name : %s\n",node_curr-&gt;strname);
                printf("Orginial content : %s\n",node_curr-&gt;content);
                node_curr-&gt;content=strcpy(node_curr-&gt;content,strname);
                printf("Current content : %s\n",node_curr-&gt;content);
            }
        }
    }
    else if(strcmp(lowercase(temp),"inverse"))
    {
        if(!node_curr)
        {
            printf("The current structure points to nothing!\n");
            printf("Using 'display current' to check the current structure!\n");
        }
        else
        {
            printf("Varible name : %s\n",node_curr-&gt;strname);
            printf("Orginial content : %s\n",node_curr-&gt;content);
            node_curr-&gt;content=inverse(node_curr-&gt;content);
            printf("Inversing result : %s\n",node_curr-&gt;content);
        }
    }
    else if(strcmp(lowercase(temp),"substr"))
    {
        if(!node_curr)
        {
            printf("The current structure points to nothing!\n");
            printf("Using 'display current' to check the current structure!\n");
        }
        else
        {
            strname=(char *)malloc(MAX_STRING_LEN*sizeof(char));
            if(!strname)
            {
                printf("Cannot allocate memory!\n");
                exit(1);
            }
            strname=convert(strname,argment,1);
        
            content=(char *)malloc(MAX_STRING_LEN*sizeof(char));
            if(!content)
            {
                printf("Cannot allocate memory!\n");
                exit(1);
            }
            content=convert(content,argment,2);
            
            if(!isnumeric(strname)||!isnumeric(content))
            {
                printf("Synx error - Invaild parameter!\n");
                free(strname);
                free(content);
            }
            else
            {
                s=(char *)malloc(MAX_STRING_LEN*sizeof(char));
                if(!s)
                {
                    printf("Cannot allocate memory!\n");
                    exit(1);
                }   
                s=substr(s,node_curr-&gt;content,strtoint(strname),strtoint(content));
                if(!s)
                    printf("Impossible!\n");
                else
                {
                    printf("The sub string is %s\n",s);
                    free(s);
                    free(strname);
                    free(content);
                }
            }
        }
    }
    else if(strcmp(lowercase(temp),"locate"))
    {
        strname=(char *)malloc(MAX_STRING_LEN*sizeof(char));
        if(!strname)
        {
            printf("Cannot allocate memory!\n");
            exit(1);
        }
        strname=convert(strname,argment,1);
        
        if(!isnumeric(strname))
            printf("Synx error - invaild parameter!\n");
        else
        {
            p=node_curr;
            locate(strtoint(strname));
            if(node_curr==p)
                printf("Cannot find new position!\n");
            else
                printf("Locate OK!\n");
            free(strname);
        }   
    }
    else if(strcmp(lowercase(temp),"total"))
    {
        total();
    }
    else if(strcmp(lowercase(temp),"exit")||strcmp(lowercase(temp),"quit"))
    {
        freeall();
        printf("Bye! Bye!\n");
        printf("&lt;Press any key to exit&gt;");
        free(command);
        getch();
        exit(1);
    }
    else
        printf("Invaild command!\n");                 
    free(temp);
}

我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2005-05-28 13:02
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 
我发现一个问题,就是我在输入关键字的时候(命令关键字),如果字符串太长的话会造成莫名其妙的非法操作然后就中止了程序(在Dev-C++编译的情况下),而我在TC3.0编译该程序后,没有任何问题。不知道这是为什么,请高手给个指点谢谢啦。。。。。。。

以下是错误截图
图片附件: 游客没有浏览图片的权限,请 登录注册



[此贴子已经被作者于2005-5-28 19:22:05编辑过]



我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2005-05-28 19:17
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 
修正一个错误

请看locate函数

void locate(int pos)
{
   ..........
   while(p)
   {
       if(!pos) { .... } //这个判断有错,应该改称pos==1,否则永远也找不到想要定位的记录。
       ....
   }
}

[此贴子已经被作者于2005-5-28 19:45:50编辑过]



我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2005-05-28 19:43
快速回复:[原创]To Flier,字符串操作演示系统
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034836 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved