文章统计及单词搜索

c++大作业

一、课题介绍:

本次课题用c++程序解决如下问题:

选定一篇英文文章,对文章中的单词及句子进行统计:

1.      统计大写字母的个数,单词个数,句子个数;

2.      全拼搜索单词,简拼搜索单词。

 

二、需求分析:

1.在文章检索方面有意义;

2.可用来分析数据进行数据可视化;

3.可用于快速统计。

 

三、课题意义:

1.对于一篇很长的文章,可以借助程序快速统计信息,解放人力,节省时间,提高准确率;

2.促使我们将所学应用到生活当中,加深对c++的理解,也锻炼我们的逻辑思维能力。

 

四、任务总体思路:

1.分别设计函数dx,dc,jz统计大写字母,单词,句子个数;

2.设计函数判断单词a是否与单词b相同,或单词a是否为单词b的简拼;

3.设计函数通过循环嵌套将两个字符组里的单词取出一一进行比较并统计判断结果;

4.设计函数能删除字符组里的某个单词防止重复计算;

5.在主函数中完成各项要求。

 

五、关键函数或关键功能设计描述:

1.句子统计



void jz(char wz[])

{

        inti=0,t=0;

        while(wz[i]!='\0')

        {

                 if(wz[i]=='.'||wz[i]=='!'||wz[i]=='?')

                         t++,i++;

                 elseif(wz[i]=='"')

                 {

                     i++;

                         while(wz[i]!='"')

                                  i++;

                 }

                 else

                         i++; 

        }

        cout<<"句数是"<<t<<endl;

}


通过判断有多少个句号来判断句子个数,并排除“”中标点符号的影响。

运行直接打印结果。

 

2.单词统计


void dc(char wz[]) 

{

        intt=1;

        for(inti=0;i>-1;i++)

        {

                 if(wz[i]=='')

                         t++;

                 elseif(wz[i]=='\0')

                         break;

        }

        cout<<"单词个数是"<<t<<endl;

}


由于英语文章中每个单词前面均为一个空格,所以通过判断空格多少即可统计单词个数,运行直接打印结果。

 

3.大写字母个数统计


void dx(char wz[])   //大写字母个数函数

{

        intt=0;

        for(inti=0;i>-1;i++)

        {

                 if(wz[i]>='A'&&wz[i]<='Z')

                         t++;

                 elseif(wz[i]=='\0')

                         break;

        }

        cout<<"大写字母个数是"<<t<<endl;

}


通过判断字符的ASCII码来判断字符是否为大写字母并进行统计,运行直接打印结果。

 

3.      删除字符组a中a[b]


void del(char a[],int b)  //删除a[b]

{

   int i=b;

        while(a[i]!='\0')

            a[i++]=a[i+1];

        a[i]='\0';

}


运用for循环使a[b]后每一位提前一位,并在最后补充\0。

 

4.      删除字符组a中某个单词


void cc(char c[],char juzi[])

{

        inti=0,j=0,qq;

        charstr[20];

        while(juzi[i]!='\0')

        {

             while(juzi[i]>='A'&&juzi[i]<='z')

                   {

                       str[j]=juzi[i];

                       i++,j++;

                   }

                   str[j]='\0';

                   if(str[0]!='\0')

                   {

                           qq=qp(str,c);

                           if(qq==1)

                           {

                                    for(i=i-1;juzi[i]!=' ';i--)

                                    {

                                            del(juzi,i);

                                    }

                           }

                   }

                   j=0;

                   i++;                    

        }

}


将单词输入到字符组c中,取出b中每个单词与之进行比较,相同则通过for循环与del函数删除该单词,循环从该单词前的空格开始继续运行直到删除全部该单词。

 

5.      判断两个单词是否相同


int qp(char a[],char b[])  //全拼比较,相同为1

{

        inti=0,flag=1,p,q;

        if(strlen(a)==strlen(b))

        {

 

             while(a[i]!='\0')

                  {

                       p=int (a[i]);

                       q=int (b[i]);

                          if(p==q||p==q+32||p==q-32)

                                 i++;

                  else

                           {

                                    flag=0;

                                    break;

                           }

                  }

        }

        else

                 flag=0;

        returnflag;

}


将两个单词中每个字母按顺序一一比较,全部相同则返回1。

 

6.判断单词a是否为单词b的简拼


int jp(char a[],char b[])

{

        inti,j=0,t,p,q;

        t=strlen(b);

        for(i=0;i<t;i++)

        {

                 p=a[j],q=b[i];

                 if(a[j]=='\0')

                         break;

                 if(p==q||q==p+32||q==p-32)

                         j++;

        }

        if(j==int(strlen(a)))

                 return1;

        else

                 return0;

}


按顺序将b中每个单词取出,期间与a的字母进行比较,相同则a取下一位,若b取完后a取到\0,则返回1。

 

7.全拼搜索单词


void qpss(char a[],char b[])//b是原文,全拼搜索函数

{

        inti=0,j=0;

        intm=0,n=0;

        intgs;//gs用来统计某个单词出现的次数

        charstr1[100],str2[100];//str1为取a的地址,str2为取b的地址

   while(a[i]!='\0')//取a开始

        {

              while(a[i]>='A'&&a[i]<='z')

                   {

                       str1[j]=a[i];

                       i++,j++;

                   }

                   str1[j]='\0';

                   if(str1[0]!='\0')   //取出a中的一个单词,存储在str1中,为了防止取空设置此举

                  

                   //以下开始一一查找b中的词语

                   {

                           gs=0;

                           m=0;

                           cout<<"全拼搜索"<<str1<<"的结果为:"<<endl;

                           while(b[m]!='\0')//取b开始

                           {

                      while(b[m]>='A'&&b[m]<='z')

                                     {

                                  str2[n]=b[m];

                                  m++,n++;

                                     }

                            str2[n]='\0';

                            if(str2[0]!='\0')   //取出b中的一个单词,存储在str2中

                                     {

                                             if(qp(str1,str2)==1)

                                                     gs++;

                                     }

                                     n=0; //取b的下一个单词

                            m++;

                           }

                          

                                    cout<<str1<<"在文中出现了"<<gs<<"次"<<endl;

                   }

                   j=0; //取a的下一个单词

                   i++;                    

        }

}


先用一个循环一一取出字符组a中的每个单词,嵌套一个循环一一取出b中的每个单词,相同则gs+1,b取完后打印a中某单词的统计个数,之后a继续循环,b重新循环,直到a中的每个单词都取出且进行了统计。

 

8.简拼搜索单词


void jpss(char a[],char b [])

{

  intm=0,n=0;

  inti=0,j=0;

 char str1[50],str2[50];

 //

  intn1=0,m1=0,gs1=0;

 char str3[50];

 /

  intc;

 char b1[10000];

 ///

 while(a[i]!='\0')

  {

          while(a[i]>='A'&&a[i]<='z')

          {

                    str1[j]=a[i];

                    i++,j++;

          }

          str1[j]='\0';

          if(str1[0]!='\0')   //成功取出a中的一个单词,存储在str1中

          {

                    for(c=0;b[c]!='\0';c++)

                            b1[c]=b[c];

                    b1[c]='\0';

                   cout<<"简拼搜索"<<str1<<"的结果为:"<<endl;

                    m=0;

                    while(b1[m]!='\0')//取b开始

                   {

                  while(b1[m]>='A'&&b1[m]<='z')

                            {

                           str2[n]=b1[m];

                           m++,n++;

                            }

                   str2[n]='\0';

                   if(str2[0]!='\0')   //成功取出b中的一个单词,存储在str2中

                            {

                           if(jp(str1,str2)==1)

                                    {

                          m1=gs1=0;

                                   while(b1[m1]!='\0')//取b开始

                                            {

                                   while(b1[m1]>='A'&&b1[m1]<='z')

                                                                   {

                                              str3[n1]=b1[m1];

                                              m1++,n1++;

                                                                   }

                                         str3[n]='\0';

                                         if(str3[0]!='\0')   //取出b中的一个单词,存储在str2中

                                                                   {

                                                          if(qp(str2,str3)==1)

                                                                   gs1++;

                                                                   }

                                                  n1=0;  //取b的下一个单词

                                         m1++;

                                            }

                                  

                                                    cout<<str2<<"在文中出现了"<<gs1<<"次"<<endl;

                                           

                                            /

                                            cc(str2,b1);

                                           

                                    }

                            }

 

                   n=0; //取b的下一个单词

                   m++;

                   }

          } //str1完成一次检索

          j=0;

          i++;

  }

}


与全拼搜索类似,不同的是在每统计一个单词后在复制后的原文里删除已统计的单词防止重复统计。

 

六、测试数据

(详见附录1)

 

七、个人工作小结和心得体会

做这个大作业的时候除了简拼时时打印乱码外其他的都很顺利,期间也由于对书上的内容不是很熟悉而绕了些远路,可见读书很重要。当然上机练习同样重要,经过这次的大作业,我对书上内容理解得愈发深刻,对语法的运用也更得心应手,这是干读书几遍也得不到的。

对于这次大作业,虽然结果完美运行了出来,但尚未改进人机交互界面,之后要多多自学类如easyX这样的书上没有的知识,且在简拼搜索中没有成功做到引用全拼搜索函数,程序有些赘余,尚可做到进一步简化,可见在对于函数我仍掌握不够不能熟练结合使用。当然也有令我十分满意的地方,比如做出了del和cc函数可以精准搜索出每一个简拼单词,在循环嵌套取单词中也运行完美等等。大作业让我更加热爱c++,点燃了我对编程的兴趣。

 

八、程序源代码清单

(详见附录2)

 

 

 

 

 

 

 

附录1

 

附录2

#include<iostream.h>

#include<string.h>

 

void jz(char wz[])  //句子个数函数

{

        inti=0,t=0;

        while(wz[i]!='\0')

        {

                 if(wz[i]=='.'||wz[i]=='!'||wz[i]=='?')

                         t++,i++;

                 elseif(wz[i]=='"')

                 {

                     i++;

                         while(wz[i]!='"')

                                  i++;

                 }

                 else

                         i++; 

        }

        cout<<"句数是"<<t<<endl;

}

 

void dc(char wz[])  //单词个数函数

{

        intt=1;

        for(inti=0;i>-1;i++)

        {

                 if(wz[i]=='')

                         t++;

                 elseif(wz[i]=='\0')

                         break;

        }

        cout<<"单词个数是"<<t<<endl;

}

 

void dx(char wz[])   //大写字母个数函数

{

        intt=0;

        for(inti=0;i>-1;i++)

        {

                 if(wz[i]>='A'&&wz[i]<='Z')

                         t++;

                 elseif(wz[i]=='\0')

                         break;

        }

        cout<<"大写字母个数是"<<t<<endl;

}

 

void del(char a[],int b)  //删除a[b]

{

   int i=b;

        while(a[i]!='\0')

            a[i++]=a[i+1];

        a[i]='\0';

}

 

 

 

int jp(char a[],char b[])

{

        inti,j=0,t,p,q;

        t=strlen(b);

        for(i=0;i<t;i++)

        {

                 p=a[j],q=b[i];

                 if(a[j]=='\0')

                         break;

                 if(p==q||q==p+32||q==p-32)

                         j++;

        }

        if(j==int(strlen(a)))

                 return1;

        else

                 return0;

}

 

int qp(char a[],char b[])  //全拼比较,相同为1

{

        inti=0,flag=1,p,q;

        if(strlen(a)==strlen(b))

        {

 

             while(a[i]!='\0')

                  {

                       p=int (a[i]);

                       q=int (b[i]);

                          if(p==q||p==q+32||p==q-32)

                                 i++;

                  else

                           {

                                    flag=0;

                                    break;

                           }

                  }

        }

        else

                 flag=0;

        returnflag;

}

 

void cc(char c[],char juzi[])  //在文章中去除某个单词

{

        inti=0,j=0,qq;

        charstr[20];

        while(juzi[i]!='\0')

        {

             while(juzi[i]>='A'&&juzi[i]<='z')

                   {

                       str[j]=juzi[i];

                       i++,j++;

                   }

                   str[j]='\0';

                   if(str[0]!='\0')

                   {

                           qq=qp(str,c);

                           if(qq==1)

                           {

                                    for(i=i-1;juzi[i]!=' ';i--)

                                    {

                                            del(juzi,i);

                                    }

                           }

                   }

                   j=0;

                   i++;                    

        }

}

 

///

 

 

/

//

void qpss(char a[],char b[])//b是原文,全拼搜索函数

{

        inti=0,j=0;

        intm=0,n=0;

        intgs;//gs用来统计某个单词出现的次数

        charstr1[100],str2[100];//str1为取a的地址,str2为取b的地址

   while(a[i]!='\0')//取a开始

        {

              while(a[i]>='A'&&a[i]<='z')

                   {

                       str1[j]=a[i];

                       i++,j++;

                   }

                   str1[j]='\0';

                   if(str1[0]!='\0')   //取出a中的一个单词,存储在str1中,为了防止取空设置此举

                  

                   //以下开始一一查找b中的词语

                   {

                           gs=0;

                           m=0;

                           cout<<"全拼搜索"<<str1<<"的结果为:"<<endl;

                           while(b[m]!='\0')//取b开始

                           {

                      while(b[m]>='A'&&b[m]<='z')

                                     {

                                  str2[n]=b[m];

                                  m++,n++;

                                     }

                            str2[n]='\0';

                            if(str2[0]!='\0')   //取出b中的一个单词,存储在str2中

                                     {

                                             if(qp(str1,str2)==1)

                                                     gs++;

                                     }

                                     n=0; //取b的下一个单词

                            m++;

                           }

                          

                                    cout<<str1<<"在文中出现了"<<gs<<"次"<<endl;

                   }

                   j=0; //取a的下一个单词

                   i++;                    

        }

}

 

/

void jpss(char a[],char b [])

{

  intm=0,n=0;

  inti=0,j=0;

 char str1[50],str2[50];

 //

  intn1=0,m1=0,gs1=0;

  charstr3[50];

 /

  intc;

 char b1[10000];

 ///

 while(a[i]!='\0')

  {

          while(a[i]>='A'&&a[i]<='z')

          {

                    str1[j]=a[i];

                    i++,j++;

          }

          str1[j]='\0';

          if(str1[0]!='\0')   //成功取出a中的一个单词,存储在str1中

          {

                    for(c=0;b[c]!='\0';c++)

                            b1[c]=b[c];

                    b1[c]='\0';

                   cout<<"简拼搜索"<<str1<<"的结果为:"<<endl;

                    m=0;

                    while(b1[m]!='\0')//取b开始

                   {

                  while(b1[m]>='A'&&b1[m]<='z')

                            {

                           str2[n]=b1[m];

                           m++,n++;

                            }

                   str2[n]='\0';

                   if(str2[0]!='\0')   //成功取出b中的一个单词,存储在str2中

                            {

                           if(jp(str1,str2)==1)

                                    {

                          m1=gs1=0;

                                   while(b1[m1]!='\0')//取b开始

                                            {

                                   while(b1[m1]>='A'&&b1[m1]<='z')

                                                                   {

                                              str3[n1]=b1[m1];

                                              m1++,n1++;

                                                                   }

                                         str3[n]='\0';

                                         if(str3[0]!='\0')   //取出b中的一个单词存储在str2中

                                                                   {

                                                          if(qp(str2,str3)==1)

                                                                   gs1++;

                                                                   }

                                                  n1=0;  //取b的下一个单词

                                         m1++;

                                            }

                                  

                                                    cout<<str2<<"在文中出现了"<<gs1<<"次"<<endl;

                                           

                                            /

                                            cc(str2,b1);

                                           

                                    }

                            }

 

                   n=0; //取b的下一个单词

                   m++;

                   }

          } //str1完成一次检索

          j=0;

          i++;

  }

}

 

void main()

{

 

   char c[100];

        chara[100];

        charb[]={"During my onemonth English learning in SEU, I have got a newexperience of English classroom which quite differ from the one in my highschool or the one in my dream. In contrast, the current classroom is moreflexible while the dreamed one is rather free. First of all, my ability doesnot fit in my current class in some way, so in my dreamed one, there should beelementary learning , speaking, reading practise and more primary vocabularycourses. Second, the current one contains too many people, and I desire asmaller one so as to have more opportunities to communicate with others inEnglish. Third, the current one is still like the former one full of Chiness,yet I hope I can study in a circumstance similiar with a real Englishspeakingcountry. As an example, we can learn by chatting with foreigners, watchingEnglish films, reading rhythmic poems, singing songs and so on. LearningEnglish like a baby living in America is always my dream. Also, I am sure thatthe current one will keep up with my desirable one some day. With thedevelopment of human society, we have caused plenty of bad impacts on ourenvironment. Although we get to know the necessity of being in tune with thenature, what we are doing is still destroying the environment. For instance,airpollution from the burning of fossil fuel, white pollution by unlimitedproducing and ineffective recycling, water pollution by our casual emissionsand so on are always the byproduct coming along with modern lifestyles. Soliving green is an urgent task."};

   cout<<b<<endl;

        cout<<endl;

        jz(b);

        dx(b);

        dc(b);

        cout<<endl;

       

        cout<<"请输入你想通过全拼搜索的单词:";

        cin.getline(a,99);

        qpss(a,b);

        cout<<endl;

        cout<<"请输入你想通过简拼搜索的单词:";

        cin.getline(c,99);

        jpss(c,b);

        cout<<endl;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值