ivy-jie

progress ...

C++博客 首页 新随笔 联系 聚合 管理
  9 Posts :: 41 Stories :: 6 Comments :: 0 Trackbacks

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>  //for cout
#include <ctype.h>//使用字符函数
//isalpha(ch),islower(ch),isdigital(ch)
//从一个文件中取出字符串与另一个文件比较,计算字符串在该文件中出现的次数
//(字符串包含匹配性验证,比如?代表小写英文字母,*代表小写英文字母和0-9的数字),
//第一问要求你用程序实现该题目,第二问要求你对第一问中的程序写测试用例,包括基本
//测试、边界测试,*代表小写英文字母和0-9的数字
bool equal(char a,char b)
{
  if(a==b) return true;
  if(a == '?' && b == '*' || a=='*' && b=='?')
   return true;
  if((a=='?' && b>=97 && b<=122) || (a>=97 && a<=123 && b=='?'))
   return true;
  if((a=='*' && b>=97 && b<=122) || (a>=97 && a<=122 && b=='*') || (a=='*' && b>=48 &&b<=57) || (a>=48 && a<=57 && b=='*'))
   return true;
  return false;
}
int main(int argc,char** argv)
{
  char line[30];
  FILE *fp;
  int i=0,j=0;
  if((fp = fopen(argv[1],"r")) != NULL)
  {
   if(fgets(line,100,fp)== NULL)//实际上取29个字符,最后加'\0'
   {
    printf("fgets error\n");
   }
   fclose(fp);
  }
 
   int length = strlen(line);
   int sum = 0;//次数初始为0
   char ch;
   if(fopen(argv[2],"r") != NULL)
   //在文件2中查找字符串,并计数
   {
     while((ch = fgetc(fp))!= EOF)
  {
    int test;
       test = ftell(fp);
    printf("file pointer value:%d\n",test);
    if(equal(ch,line[j]))
    {
     j++;
     if(j == length) //字符串匹配到尾部,成功,总数加1
     {
      sum+=1;
      j =0;
     }
    }

    else           //否则的话回溯,重新匹配
    {
     i= ftell(fp);
     fseek(fp,j,1);//向前回溯一个字符
     j= 0;
    }
 }
   }
    printf("string %s occurs %d times in file2\n",line,sum);
    return sum;
}
/*边界测试
设计测试用例时要考虑边界输入和非法输入,这里统称为特殊输入,程序

员在编写代码时也要考虑特殊输入,要为特殊输入编写处理代码。在实际工作中,程序员没有考虑到某些

特殊输入是很常见的,这也是程序错误的一个重要来源。
*/

posted on 2009-05-23 10:45 ivy-jie 阅读(694) 评论(0)  编辑 收藏 引用 所属分类: MFC

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理