亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2999 | 回復: 8
打印 上一主題 下一主題

[C] 這道題用C語言怎么寫,真是難倒我了 [復制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2015-10-18 21:51 |只看該作者 |倒序瀏覽
今天參加某司的筆試,要求用C語言實現(xiàn)下面這道題:
把數(shù)據(jù)讀入比較字符串string[],然后讀入一個模式字符串,要求查找string[]中和模式字符串的所有匹配,輸出行號、匹配字符串。匹配時不區(qū)分大小寫,并且可以有一個用中括號表示的模式匹配。如"aa[123]bb", 就是說aa1bb, aa2bb, aa3bb都算匹配。
以下是實例,輸入格式如下
4    //要輸入的比較字符串行數(shù)
Aab  //比較字符串
a2B
ab
ABB
a[a2b]b   //模式字符串
輸出如下
1 Aab
2 a2B
4 ABB
其中,1,2,4是輸入時的比較字符串行號

對于這道題,我感覺就是加了中括號里的模式匹配不好解決,沒想到好的辦法,當時想了半天都不知所措,請大家給點意見

論壇徽章:
95
程序設計版塊每日發(fā)帖之星
日期:2015-09-05 06:20:00程序設計版塊每日發(fā)帖之星
日期:2015-09-17 06:20:00程序設計版塊每日發(fā)帖之星
日期:2015-09-18 06:20:002015亞冠之阿爾艾因
日期:2015-09-18 10:35:08月度論壇發(fā)貼之星
日期:2015-09-30 22:25:002015亞冠之阿爾沙巴布
日期:2015-10-03 08:57:39程序設計版塊每日發(fā)帖之星
日期:2015-10-05 06:20:00每日論壇發(fā)貼之星
日期:2015-10-05 06:20:002015年亞冠紀念徽章
日期:2015-10-06 10:06:482015亞冠之塔什干棉農(nóng)
日期:2015-10-19 19:43:35程序設計版塊每日發(fā)帖之星
日期:2015-10-21 06:20:00每日論壇發(fā)貼之星
日期:2015-09-14 06:20:00
2 [報告]
發(fā)表于 2015-10-18 22:32 |只看該作者
回復 1# justcoding


    允許用正則庫么?

論壇徽章:
7
天秤座
日期:2014-08-07 13:56:30丑牛
日期:2014-08-27 20:34:21雙魚座
日期:2014-08-27 22:02:21天秤座
日期:2014-08-30 10:39:11雙魚座
日期:2014-09-21 20:07:532015年亞洲杯之日本
日期:2015-02-06 14:00:282015亞冠之大阪鋼巴
日期:2015-11-02 14:50:19
3 [報告]
發(fā)表于 2015-10-18 23:26 |只看該作者
如果只有簡單的[]里面沒其他表達式的話,簡單想法直接開一條鏈表記錄整個表達式,碰到[]這種就再建一個子鏈表把可能的值串起來。
  1. struct {
  2.     struct list *next;
  3.     enum type;
  4.     union {
  5.         key;
  6.         list;
  7.     }
  8. };
復制代碼
或者用自動狀態(tài)機吧

論壇徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16賽季CBA聯(lián)賽之青島
日期:2016-07-05 12:36:0515-16賽季CBA聯(lián)賽之廣東
日期:2016-06-29 11:45:542015亞冠之全北現(xiàn)代
日期:2015-07-22 08:09:472015年辭舊歲徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39獅子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技術圖書徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
4 [報告]
發(fā)表于 2015-10-19 08:35 |只看該作者
隨手寫一個,不知道對不對
  1. #include <stdbool.h>
  2. #include <ctype.h>

  3. bool match( const char* s, const char* t )
  4. {
  5.     for( ; *s && *t; ++s,++t )
  6.     {
  7.         if( *t == '[' )
  8.         {
  9.             for( ++t; *t && *t!=']' && tolower(*s)!=tolower(*t); ++t );

  10.             if( *t=='\0' || *t==']' )
  11.                 return false;

  12.             for( ; *t && *t!=']'; ++t );
  13.         }
  14.         else if( tolower(*s)!=tolower(*t) )
  15.             return false;
  16.     }

  17.     return !*s && !*t;
  18. }

  19. #include <stdio.h>

  20. int main( void )
  21. {
  22.     printf( "%s\n", match("Aab","a[a2b]b")?"true":"false" );
  23.     printf( "%s\n", match("a2B","a[a2b]b")?"true":"false" );
  24.     printf( "%s\n", match("ab","a[a2b]b")?"true":"false" );
  25.     printf( "%s\n", match("ABB","a[a2b]b")?"true":"false" );
  26. }
復制代碼

論壇徽章:
0
5 [報告]
發(fā)表于 2015-10-19 12:41 |只看該作者
回復 2# MMMIX
這個我倒沒問,而且不是你說我還不知道有正則庫這個東西,汗


   

論壇徽章:
3
15-16賽季CBA聯(lián)賽之山東
日期:2016-10-30 08:47:3015-16賽季CBA聯(lián)賽之佛山
日期:2016-12-17 00:06:31CU十四周年紀念徽章
日期:2017-12-03 01:04:02
6 [報告]
發(fā)表于 2015-10-25 19:58 |只看該作者
回復 5# justcoding


    這個東西, 如果用正則庫確實不難. 如果不用正則庫, 就相當于自己寫正則庫中的部分實現(xiàn)了.
    考慮到是一個題目的話, 實際上應該是看樓主知不知道正則庫這回事了... 搜索C語言正則庫即可...
    如果是要求正則全匹配而且不準用正則庫的話, HoHo, 就相當于寫一個正則庫了, 這工作量... 不然呢, flex 什么的也是一個選擇吧...

論壇徽章:
3
15-16賽季CBA聯(lián)賽之山東
日期:2016-10-30 08:47:3015-16賽季CBA聯(lián)賽之佛山
日期:2016-12-17 00:06:31CU十四周年紀念徽章
日期:2017-12-03 01:04:02
7 [報告]
發(fā)表于 2015-10-25 20:02 |只看該作者
仔細看了下好像是只要求匹配中括號, 那不用正則庫也是可以處理的... 不過確實還是有些麻煩, 粗略寫來估計 bug 不少.

論壇徽章:
84
每日論壇發(fā)貼之星
日期:2015-12-29 06:20:00每日論壇發(fā)貼之星
日期:2016-01-16 06:20:00每周論壇發(fā)貼之星
日期:2016-01-17 22:22:00程序設計版塊每日發(fā)帖之星
日期:2016-01-20 06:20:00每日論壇發(fā)貼之星
日期:2016-01-20 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-01-21 06:20:00每日論壇發(fā)貼之星
日期:2016-01-21 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-01-23 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-01-31 06:20:00數(shù)據(jù)庫技術版塊每日發(fā)帖之星
日期:2016-01-16 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-01-16 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-01-14 06:20:00
8 [報告]
發(fā)表于 2015-10-27 15:54 |只看該作者
fnmatch(3)

論壇徽章:
84
每日論壇發(fā)貼之星
日期:2015-12-29 06:20:00每日論壇發(fā)貼之星
日期:2016-01-16 06:20:00每周論壇發(fā)貼之星
日期:2016-01-17 22:22:00程序設計版塊每日發(fā)帖之星
日期:2016-01-20 06:20:00每日論壇發(fā)貼之星
日期:2016-01-20 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-01-21 06:20:00每日論壇發(fā)貼之星
日期:2016-01-21 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-01-23 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-01-31 06:20:00數(shù)據(jù)庫技術版塊每日發(fā)帖之星
日期:2016-01-16 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-01-16 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-01-14 06:20:00
9 [報告]
發(fā)表于 2015-10-27 16:02 |只看該作者
  1. /*
  2. * Copyright (c) 1989, 1993, 1994
  3. *        The Regents of the University of California.  All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Guido van Rossum.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. *    notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. *    notice, this list of conditions and the following disclaimer in the
  15. *    documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. *    must display the following acknowledgement:
  18. *        This product includes software developed by the University of
  19. *        California, Berkeley and its contributors.
  20. * 4. Neither the name of the University nor the names of its contributors
  21. *    may be used to endorse or promote products derived from this software
  22. *    without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * From FreeBSD fnmatch.c 1.11
  37. * $Id: fnmatch.c,v 1.3 1997/08/19 02:34:30 jdp Exp $
  38. */

  39. #if defined(LIBC_SCCS) && !defined(lint)
  40. static char sccsid[] = "@(#)fnmatch.c        8.2 (Berkeley) 4/16/94";
  41. #endif /* LIBC_SCCS and not lint */

  42. /*
  43. * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
  44. * Compares a filename or pathname to a pattern.
  45. */

  46. #include <ctype.h>
  47. #include <string.h>
  48. #include <stdio.h>

  49. #include "fnmatch.h"

  50. #define        EOS        '\0'

  51. static const char *rangematch(const char *, char, int);

  52. int
  53. fnmatch(const char *pattern, const char *string, int flags)
  54. {
  55.         const char *stringstart;
  56.         char c, test;

  57.         for (stringstart = string;;)
  58.                 switch (c = *pattern++) {
  59.                 case EOS:
  60.                         if ((flags & FNM_LEADING_DIR) && *string == '/')
  61.                                 return (0);
  62.                         return (*string == EOS ? 0 : FNM_NOMATCH);
  63.                 case '?':
  64.                         if (*string == EOS)
  65.                                 return (FNM_NOMATCH);
  66.                         if (*string == '/' && (flags & FNM_PATHNAME))
  67.                                 return (FNM_NOMATCH);
  68.                         if (*string == '.' && (flags & FNM_PERIOD) &&
  69.                             (string == stringstart ||
  70.                             ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  71.                                 return (FNM_NOMATCH);
  72.                         ++string;
  73.                         break;
  74.                 case '*':
  75.                         c = *pattern;
  76.                         /* Collapse multiple stars. */
  77.                         while (c == '*')
  78.                                 c = *++pattern;

  79.                         if (*string == '.' && (flags & FNM_PERIOD) &&
  80.                             (string == stringstart ||
  81.                             ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
  82.                                 return (FNM_NOMATCH);

  83.                         /* Optimize for pattern with * at end or before /. */
  84.                         if (c == EOS)
  85.                                 if (flags & FNM_PATHNAME)
  86.                                         return ((flags & FNM_LEADING_DIR) ||
  87.                                             strchr(string, '/') == NULL ?
  88.                                             0 : FNM_NOMATCH);
  89.                                 else
  90.                                         return (0);
  91.                         else if (c == '/' && flags & FNM_PATHNAME) {
  92.                                 if ((string = strchr(string, '/')) == NULL)
  93.                                         return (FNM_NOMATCH);
  94.                                 break;
  95.                         }

  96.                         /* General case, use recursion. */
  97.                         while ((test = *string) != EOS) {
  98.                                 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
  99.                                         return (0);
  100.                                 if (test == '/' && flags & FNM_PATHNAME)
  101.                                         break;
  102.                                 ++string;
  103.                         }
  104.                         return (FNM_NOMATCH);
  105.                 case '[':
  106.                         if (*string == EOS)
  107.                                 return (FNM_NOMATCH);
  108.                         if (*string == '/' && flags & FNM_PATHNAME)
  109.                                 return (FNM_NOMATCH);
  110.                         if ((pattern =
  111.                             rangematch(pattern, *string, flags)) == NULL)
  112.                                 return (FNM_NOMATCH);
  113.                         ++string;
  114.                         break;
  115.                 case '\\':
  116.                         if (!(flags & FNM_NOESCAPE)) {
  117.                                 if ((c = *pattern++) == EOS) {
  118.                                         c = '\\';
  119.                                         --pattern;
  120.                                 }
  121.                         }
  122.                         /* FALLTHROUGH */
  123.                 default:
  124.                         if (c == *string)
  125.                                 ;
  126.                         else if ((flags & FNM_CASEFOLD) &&
  127.                                  (tolower((unsigned char)c) ==
  128.                                   tolower((unsigned char)*string)))
  129.                                 ;
  130.                         else if ((flags & FNM_PREFIX_DIRS) && *string == EOS &&
  131.                              ((c == '/' && string != stringstart) ||
  132.                              (string == stringstart+1 && *stringstart == '/')))
  133.                                 return (0);
  134.                         else
  135.                                 return (FNM_NOMATCH);
  136.                         string++;
  137.                         break;
  138.                 }
  139.         /* NOTREACHED */
  140. }

  141. static const char *
  142. rangematch(const char *pattern, char test, int flags)
  143. {
  144.         int negate, ok;
  145.         char c, c2;

  146.         /*
  147.          * A bracket expression starting with an unquoted circumflex
  148.          * character produces unspecified results (IEEE 1003.2-1992,
  149.          * 3.13.2).  This implementation treats it like '!', for
  150.          * consistency with the regular expression syntax.
  151.          * J.T. Conklin (conklin@ngai.kaleida.com)
  152.          */
  153.         if ( (negate = (*pattern == '!' || *pattern == '^')) )
  154.                 ++pattern;

  155.         if (flags & FNM_CASEFOLD)
  156.                 test = tolower((unsigned char)test);

  157.         for (ok = 0; (c = *pattern++) != ']';) {
  158.                 if (c == '\\' && !(flags & FNM_NOESCAPE))
  159.                         c = *pattern++;
  160.                 if (c == EOS)
  161.                         return (NULL);

  162.                 if (flags & FNM_CASEFOLD)
  163.                         c = tolower((unsigned char)c);

  164.                 if (*pattern == '-'
  165.                     && (c2 = *(pattern+1)) != EOS && c2 != ']') {
  166.                         pattern += 2;
  167.                         if (c2 == '\\' && !(flags & FNM_NOESCAPE))
  168.                                 c2 = *pattern++;
  169.                         if (c2 == EOS)
  170.                                 return (NULL);

  171.                         if (flags & FNM_CASEFOLD)
  172.                                 c2 = tolower((unsigned char)c2);

  173.                         if ((unsigned char)c <= (unsigned char)test &&
  174.                             (unsigned char)test <= (unsigned char)c2)
  175.                                 ok = 1;
  176.                 } else if (c == test)
  177.                         ok = 1;
  178.         }
  179.         return (ok == negate ? NULL : pattern);
  180. }
復制代碼
http://web.mit.edu/freebsd/csup/fnmatch.c
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP