| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1611 人关注过本帖
标题:求助字符串的基本操作演示程序。或帮忙改改程序,谢谢。急需。比有重谢
只看楼主 加入收藏
flier
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2005-3-6
收藏
 问题点数:0 回复次数:6 
求助字符串的基本操作演示程序。或帮忙改改程序,谢谢。急需。比有重谢
务须帮帮忙。。。。
搜索更多相关主题的帖子: 演示 字符 改改 
2005-05-25 14:14
flier
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2005-3-6
收藏
得分:0 
【C源程序】 
 #include "stdlib.h"
 #include "stdio.h"
 typedef char *STRING;  /*定义新的字符串类型,即指向字符的指针*/
 struct strheadlist
  {STRING strhead[100];
   STRING strname[100];
   int curnum;     
  }; /*定义指向字符串的字针数组,并存放各字符串的串名(串名也用字符串来表示),及当前堆中串的数目*/
 struct result
 {int num;
  int nametype[6];
  STRING str[6];
  };                /*定义命令行编译以后形成的命令及参数等数据*/
  int strtoint(STRING s);
  void creat(STRING s,STRING ss);
  void assign(STRING s,STRING t);
  int equal(STRING s,STRING t);
  int length(STRING s);
  void concat(STRING s,STRING t,STRING v);
  void substr(STRING s,int start,int len,STRING v);
  int index(STRING s,STRING t,int start);
  struct result cmdsyna(int row);
  int lookup(STRING s);
  struct strheadlist head;
  char cmd[40][80];
  main()
  {
    struct result comm;
    int row=0,i;
    char c;
    int equ;
head.curnum=0;
while(1)                /*重复输入并处理相应命令*/
    {
     printf("CMD>");
     for (i=0;(c=getchar())!='\n ';i++)      cmd[row][i]=c;
     cmd[row][i]= '\0 ';
     comm=cmdsyna(row);/*对各命令动词进行判数并作参数分析*/
     if (strcmp(comm.str[0],"creat")==0)  /*字符串创建*/
        {if (comm.num!=2)  printf("Parameters is wrong!\n");
          else  if (lookup(comm.str[1])!=-1) printf("String %s cannot be creat!\n");
            else {if (comm.nametype[1]==4 && comm.nametype[2]==2)
                       creat(comm.str[1],comm.str[2]);
               }
        }
     else   if (strcmp(comm.str[0],"assign")==0)  /*字符串赋值*/
                {if (comm.num!=2) printf("Parameters is wrong!\n");
                     else  assign(comm.str[1],comm.str[2]);
                    }
           else if (strcmp(comm.str[0],"equal")==0)  /*字符串判等*/
               {if (comm.num!=2) printf("Parameters is wrong!\n");
                 else
              {if (lookup(comm.str[1])==-1||lookup(comm.str[2])==-1)
                   printf("String %s or %s is not found!\n",comm.str[1],comm. str[2]);
                        else {if  (equal(head.strhead[lookup(comm.str[1])],
                        head.strhead[lookup(comm.str[2])])==1) printf("Equal!!!\n");
                           else printf("Not equal!!!\n");
                                                }
                           }
                     }
           else if (strcmp(comm.str[0],"length")==0) /*字符串求长*/
                  {if (comm.num!=1) printf("Parameters is wrong!\n");
                   else {if (lookup(comm.str[1])==-1)
                         printf("The string %s connot be found!\n");
                         else  printf("String %s :%s length is %d\n",comm.str[1],
head.strhead [lookup(comm.str[1])],length(head.strhead[lookup(comm.str[1])]));
                             }
                   }
         else    if (strcmp(comm.str[0],"concat")==0) /*字符串连接并形成新的字符串*/
                    {if (comm.num!=3) printf("Parameters is wrong!\n");
                       else
                        { if (lookup(comm.str[1])==-1||lookup(comm.str[2])==-1)
                            printf("The string %s or %s is not found!\n", comm.str[1],comm.str[2]);
                          else  if (lookup(comm.str[3])!=-1) printf("String %s connot be creat!\n", comm.str[3]);
                                 else          concat(head.strhead[lookup(comm.str[1])],                                             
head.strhead[lookup(comm.str[2])],comm.str[3]);
                           }
                    }
        else if (strcmp(comm.str[0],"substr")==0) /*取字符串子串,并形成新的字符串*/
                {if (comm.num!=4) printf("Parameters wrong!\n");
                  else
                   {if (lookup(comm.str[1])==-1)
                         printf("String %s is not found!\n",comm.str[1]);
                      else
                         {if (lookup(comm.str[4])!=-1)
       printf("String %s connot be creat!\n",comm.str[4]);
                          else
                             {int start,len;
                              start=strtoint(comm.str[2]);
                                  len=strtoint(comm.str[3]);                         substr(head.strhead[lookup(comm.str[1])],start,
len,comm.str[4]);
                             }
                        }
                   }
                }
            else    if (strcmp(comm.str[0],"index")==0)   /*字符串定位*/
                     {if (comm.num!=3) printf("Parameters wrong!\n");
                      else
                         {if (lookup(comm.str[1])==-1||lookup(comm.str[2])==-1)
printf("String %s or %s is not found!\n",
 comm.str[1],comm.str[2]);
                          else
                              {int start,flag=-1;
                               start=strtoint(comm.str[3]);                               flag=index(head.strhead[lookup(comm.str[1])],head.strhead[lookup(comm.str[2])],start);
                         if  (flag==-1)    printf("String %s  start %d ,
String %s is not index!!!\n",comm.str[1],start,comm.str[2]);
else printf("String %s start %d ,String %s is  index %d!!!\n",
             comm.str[1],start,comm.str[2],flag);
                              }         
                       }
                   }
        else    if (strcmp(comm.str[0],"quit")==0) /*退出演示系统*/
                  {printf("Bye Bye!\n");
                   break;
                   }
               else
                   printf("Bad filename or no command!\n"); /*错误命令动词提示*/
     row++;
     }
   }
  int strtoint(STRING s) /*将字符串转换成整数常量*/
  {
   int i=0;
   int total=0;
   while(s[i]!= '\0 ')
   {total=total*10+s[i]-48;
    i++;
   }
   return(total);
  }
  int lookup(STRING s) 
   /*查找串表中是否存在串名所对应的字符串,如存在则返回所对应的位置,否则返回-1*/
  {int i;
   int look=-1;
   for (i=0;i       if (strcmp(head.strname[i],s)==0)
          {look=i;
           break;
          }
    if (look==head.curnum)  look=-1;
    return(look);
   }
  struct result cmdsyna(int row) /*对输入的命令行进行编译*/
   {int i;
    struct result res;
    char c;
    int word=0,start;
    res.num=-1;
    for(i=0;(c=cmd[row][i])!= '\0 ';i++)
      {  if(c== '  ')
           {word=0;
            cmd[row][i]= '\0 ';
           }
         else
            if (word==0)
   /*出现了一个新的参数,将分离出的各参数以及命令动词放置于参数表中*/
              {res.num++;
               start=i;word=1;
               res.str[res.num]=&cmd[row][start];
               if (res.num==0)  res.nametype[res.num]=1;
          else
                   if   (cmd[row][start]==39)
                   {cmd[row][start]= '\0 ';
                           res.str[res.num]=&cmd[row][start+1];
                           res.nametype[res.num]=2;
                }
               else  
                    {if   (cmd[row][start]>= '0 '&&cmd[row][start]<= '9 ')
                          res.nametype[res.num]=3;
               else     res.nametype[res.num]=4;
                    }
          }
     else
         {if    (res.nametype[res.num]==2 && c==39)     cmd[row][i]= '\0 '; 
                     /*处理参数中字符串常量*/
             if    (res.nametype[res.num]==3 &&(c<'0'||c>'9'))  cmd[row][i]= '\0';
                   /*处理参数中整数常量*/
           }
       }
     return(res);
   }
  int index(STRING s,STRING t,int start)
   /*从串S起始置START开始查找串T,如找到,则返回第一次出现的位置,否则返回-1*/
  {
   int i,j;
   if  ((start<0)||(start+length(t)>length(s))||(length(t)==0))
       return(-1);
   else
    {i=start;
     j=0;
     while  (i     {
      if (s[i]==t[j])
         {i++;j++;}
      else
         {i=i-j+1;j=0;}
      }
    }
   if (j==length(t))
     return(i-length(t));
   else
     return(-1);
   }
  void  substr(STRING s,int start,int len,STRING v)
   /*为新串开辟新的空间,并将从串S中START起始位置起取LEN长度的子串存放起来,把新串的串名和首地址存放于串表中*/
  {int i;
   char ch[80];
   if   (start<0&&start>=length(s))
        printf("Cannot be substr!!!\n");
   else
      {if  (len>=1&&len<=length(s)-start)
         { head.strname[head.curnum]=v;
           head.strhead[head.curnum]=ch;
           for(i=0;i         ch[i]=s[start+i];
           ch[i]= '\0';
      head.curnum++;
           printf("The string %s :%s\n",v,ch);
         }
       else
      printf("Cannot be substr!!!\n");
      }
   }
  void concat(STRING s,STRING t,STRING v)
  /*为新串开辟新的空间,并将两串连接起来存放起来,并输出连接后的字符串,将新串的串名和首地址存放到串表中*/
  {
   int i,j;
   char ch[80];
   head.strname[head.curnum]=v;
   head.strhead[head.curnum]=ch;
   for (i=0;s[i]!= '\0';i++)
        ch[i]=s[i];
   for (j=i,i=0;t[i]!= '\0';i++,j++)
        ch[j]=t[i];
   ch[j]= '\0';
   head.curnum++;
   printf("The string %s :%s\n",v,ch);
  }
 int length(STRING s)
 {
  return(strlen(s));
 }
 int equal(STRING s,STRING t)
  /*比较两串是否相等,如相等则返加整数1,否则返回0*/
 {
  if (strcmp(s,t)==0) return 1;
  else   return 0;
 }
void assign(STRING s,STRING t)
  /*将字符串T所指向的字符串的首地址和新串S的名称存入于串表相应的位置上,并输出赋值后的字符串*/
 {
  int look1,look2;
  if   ((look1=lookup(t))==-1)
     printf("The string %s is not found!\n",t);
  else  {if ((look2=lookup(s))==-1)
                 {head.strhead[head.curnum]=head.strhead[look1];
                  head.strname[head.curnum]=s;
                  head.curnum++;
                 }
          else
                 {head.strhead[look2]=head.strhead[look1];}
                  printf("The string %s : %s\n",s,head.strhead[look2]);}
                 }
  void creat(STRING s,STRING ss)
   /*将字符串常量的首地址和串的名称存放于串表相应的位置上,并输出新建后的字符串*/
  {
   head.strhead[head.curnum]=ss;
   head.strname[head.curnum]=s;
   head.curnum++;
   printf("The string %s :%s\n",s,ss);
  }
【测试数据】可以自定义一些测试数据,如:
1. CMD>creat   ss  'abcdef' ↙
2. CMD>assign  tt  ss↙
3. CMD>length  ss↙
4. CMD>concat  abc  'abc'  tt↙
5. CMD>index   ss  tt   0↙
6. CMD>quit↙
2005-05-25 14:15
edgeman
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2005-5-11
收藏
得分:0 

当你有了想飞的冲动,就再也不会满足在地上爬
2005-05-27 15:20
luyulin
Rank: 1
等 级:新手上路
帖 子:66
专家分:0
注 册:2004-12-18
收藏
得分:0 
这么长的代码 我晕~~~~~
2005-06-13 11:12
sforzaafa
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2005-5-18
收藏
得分:0 
'strcmp' : undeclared identifier
2005-07-11 11:14
sforzaafa
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2005-5-18
收藏
得分:0 
  if (strcmp(comm.str[0],"creat")==0)请问这句是怎么实现的?
2005-07-11 11:21
georgezf
Rank: 1
等 级:新手上路
帖 子:275
专家分:0
注 册:2004-11-20
收藏
得分:0 
比较comm.str[0]和creat是否相等(说白了就是看comm.str[0]的数据是不是creat)

2005-07-11 20:47
快速回复:求助字符串的基本操作演示程序。或帮忙改改程序,谢谢。急需。比有重谢 ...
数据加载中...
 
   



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

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