第四次作业2

本文介绍了一个使用C++进行文本处理的示例程序,该程序读取文件中的单词,并将其转换为小写形式,然后根据特定条件筛选并排序这些单词。程序还包括字符串操作、文件读写等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<fstream>
#include<cstring>
#include<string>
using namespace std;

struct Word{      
    int Count;
     Word() : Str(""), Count(0) {}  
    string Str; 
    char *p;
};


void exchange(Word &word)      
{  
    string tStr = word.Str;  
    int tCount = word.Count;  
    word.Str = Str;  
    word.Count = Count;  
    Str = tStr;  
    Count = tCount;  
}  

Words test[100];

void lwr(char x[])     
{   int k = 0;
    while (x[k] != '\0')
    {
        if (x[k] >= 'A'&&x[k] <= 'Z')
            x[k] = x[k] + 32;
        k++;
    }
}

int identify(char a[])    
{   int m=(strlen(a)>=4)?1:0;
    int n=(a[0]>='a'&&a[0]<='z')?1:0;
    if(!m||!n)
        return 0;
    else
        while(a)
        {   for(int i=1;;i++)
           {  
               if(!(a[i]>='a'&&a[i]<='z')||!(a[i]>='0'&&a[i]<='9'))
                        return 0;
               else
                        return 1;
           }
        }
}

void SortWordDown(Word * words, int size)  
{  
    for(int i=0;i<size;i++)  
    {
        for(int j=0;j <size-1;j++)  
        { 
            if(words[j].Count<words[j+1].Count)  
            {  
                words[j].exchange(words[j+1]);  
            }  
        }  
    }  
}  

int counting(char b[],int num)   
{   for(int j=0;j<num;j++)
    {

        if(!strcmp(b,test[j].p))
             test[j].count++;
        else
            return 0;
    }
}


int main()
{   char c[200];
    ifstream fin("D:/A_Tale_of_Two_Cities.txt");   
    for(int f=0;;f++)
        fin>>c[f];
    fin.close();
    cin.get();
    lwr(c);
    const char *delim = ",”“.''!?";      
    char *q;
    int n=0;
    q = strtok(c, delim);
    SortWordDown(words, wCount);  
    while (q)
    {
        if (identify(q))
        {
            
                strcpy(test[n].p,q);
                n++;
            
        }

        ofstream Result1; 
        Result1.open("Result1.txt"); 
        Result1<< test[n].p << ":" << test[n].count << '\n';
       
        
        q=strtok(NULL,delim);
    }
    return 0;
}

转载于:https://www.cnblogs.com/zjcx8775/p/5335412.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值