C++编程逻辑讲解step by step:根据关键词自动判断文章的类别,去掉无关词后统计文章中每个单词出现的频率。

题目分析

根据关键词自动判断文章的类别。共2篇文章(用char s1[]char s2[]保存),去掉无关词后(用char irrelevant[][10]保存),统计文章里面每个单词出现的频率:关键词个数¸去掉无关词后的文章单词个数;

判断条件:文章中关键词"computer"的出现频率超过10%,被认为是计算机类;关键词" Earth "的出现频率超过10%,被认为是地球科学类。

char s1[]="An amazing machine! We are living in the computer age today and most of our day to day activities cannot be accomplished without using computers. Sometimes knowingly and sometimes unknowingly we use computers. Computer has become an indispensable and multipurpose tool. We are breathing in the computer age and gradually computer has become such a desire necessity of life that it is difficult to imagine life without it. ";
char s2[]="Earth not only orbits the sun but spins on its own axis as it does so. At the equator the Earth is spinning on its axis at a speed of about 1,038 miles (1,670 kilometres) an hour. Earth's spin or rotation is a relic of its origin as a hot. Earth science or geology is the science of the Earth. During the time when Earth was forming, the largest mass of planet Earth is called. ";
char irrelevant [][10]= {"a","an","the","that","what","which","then","to","for","of","in","as","and","on","is","are","this","by","every","so","was","were","at","most","we","it","without","such","cannot","be","has",,"have","it","today"};//允许重复

最后,编写函数。完整的代码如下:

#include <iostream>
#include <string.h>
using namespace std;
char s1[]="An amazing machine! We are living in the computer age today and most of our day to day activities cannot be accomplished without using computers. Sometimes knowingly and sometimes unknowingly we use computers. Computer has become an indispensable and multipurpose tool. We are breathing in the computer age and gradually computer has become such a desire necessity of life that it is difficult to imagine life without it. ";
char s2[]="Earth not only orbits the sun but spins on its own axis as it does so. At the equator the Earth is spinning on its axis at a speed of about 1,038 miles (1,670 kilometres) an hour. Earth's spin or rotation is a relic of its origin as a hot. Earth science or geology is the earth science of the Earth. During the time when Earth was forming, the largest mass of planet Earth is called. ";
char irrelevant[][10]={"a","an","the","that","what","which","then","to","for","of","in","as","and","on","is","are","this","by","every","so","was","were","at","most","we","it","without","such","cannot","be","has","have","it","today"};
int DivWords(char str[], char div[][20]);
int Cntirrelevant (char div[][20], int n, char irrelevant[][10], int m);
int CntKeyWords (char div[][20], int n, char keyword[]);
int JudgeClass(int a, int b, int c, char keyword[]);
int CalWordFreq(char str[], char keyword[]);int main()
{char keyword1[20]="computer";char keyword2[20]="earth";CalWordFreq(s1, keyword1);CalWordFreq(s2, keyword2);	return 0;
}
int CalWordFreq(char str[], char keyword[])
{char d[100][20];int numofWords,numofirrlwords,numofkeywords;numofWords =DivWords(str,d);numofirrlwords = Cntirrelevant(d, numofWords, irrelevant, 34);numofkeywords = CntKeyWords(d, numofWords, keyword);JudgeClass(numofkeywords,numofWords,numofirrlwords,keyword);
}
int JudgeClass(int a, int b, int c, char keyword[])
{if  (float(a) / (b - c)>=0.1) 	cout<< "This article looks like "<< keyword<< endl;elsecout<< "This article does not look like "<< keyword<< endl;return 1;}
int CntKeyWords (char div[][20], int n, char keyword[])
{int i,j,cnt=0;for(i=0;i<n;i++){if(strlen(div[i])<strlen(keyword)) continue;for(j=0;j<strlen(keyword);j++)if (div[i][j]!=keyword[j]) break;	if (j==strlen(keyword)) cnt++;	}return cnt;
}int Cntirrelevant(char div[][20], int n, char irrelevant[][10], int m)
{int i,j,cnt=0;for(i=0;i<n;i++)for(j=0;j<m;j++)if(strcmp(div[i],irrelevant[j])==0) {cnt++;	}return cnt;
}int DivWords(char str[], char div[][20])
{int i=0,j=0,k=0;	                         for(i=0;i<strlen(str);i++)               {if(str[i]>='A'&&str[i]<='Z') str[i]=str[i]+32;if(str[i]>='a'&&str[i]<='z'){div[j][k]=str[i];k++;}else{	div[j][k]=0;j++;k=0;}} j++;k=0;return j;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/35335.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

go编译报错

missing go.sum entry for module providing package gihub.com/xxxxxxxxxxx; to add:xxxx 在go项目目录下执行命令&#xff1a; go mod tidy

前端JS必用工具【js-tool-big-box】学习,数值型数组的正向排序和倒向排序

这一小节&#xff0c;我们说一下前端 js-tool-big-box 这个工具库&#xff0c;添加的数值型数组的正向排序和倒向排序。 以前呢&#xff0c;我们的数组需要排序的时候&#xff0c;都是在项目的utils目录里&#xff0c;写一段公共方法&#xff0c;弄个冒泡排序啦&#xff0c;弄…

已解决java.security.GeneralSecurityException: 安全性相关的通用异常的正确解决方法,亲测有效!!!

已解决java.security.GeneralSecurityException: 安全性相关的通用异常的正确解决方法&#xff0c;亲测有效&#xff01;&#xff01;&#xff01; 目录 问题分析 报错原因 解决思路 解决方法 确定具体异常类型 检查输入参数 验证算法支持性 调整安全策略 确保资源可…

uniapp 小程序 堆叠轮播图 左滑 右滑 自动翻页 点击停止自动翻页

uniapp 小程序 堆叠轮播图 左滑 右滑 自动翻页 点击停止自动翻页 超过指定时间未点击滑动 则继续开始滚动 直接上代码 componentSwiper.vue 需要注意页面切换时清除计时器 <template><view><view class"swiperPanel" touchstart"startMove"…

Verilog刷题笔记48——FSM1型异步复位

题目: 解题&#xff1a; module top_module(input clk,input areset, // Asynchronous reset to state Binput in,output out);// parameter A0, B1; reg state, next_state;always (*) begin // This is a combinational always block// State transition logiccase(…

Python Type Hint有啥用

Python 的类型提示&#xff08;Type Hint&#xff09;是 Python 3.5 引入的一种静态类型检查功能。类型提示的主要目的是增强代码的可读性、可维护性和错误检测能力。虽然 Python 仍然是动态类型的语言&#xff0c;但类型提示可以帮助开发者在编码和维护过程中受益匪浅。 类型…

VMware vSphere 8.0 Update 3 发布下载 - 企业级工作负载平台

VMware vSphere 8.0 Update 3 发布下载 - 企业级工作负载平台 vSphere 8.0U3 | ESXi 8.0U3 & vCenter Server 8.0U3 请访问原文链接&#xff1a;https://sysin.org/blog/vmware-vsphere-8-u3/&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&am…

【yolov8:报错AttributeError: ‘Segment‘ object has no attribute ‘detect‘】

今天在运行yolov8的Segment模式的val模型测验时报错&#xff1a; AttributeError: ‘Segment’ object has no attribute ‘detect’ 原因是yolov8的版本问题&#xff0c;更新至最新版就可以了&#xff1a; 输入ultralytics查看版本 卸载当前的老版本 然后下载最新版本就可以…

【前端】Vue项目和微信小程序生成二维码和条形码

前言&#xff1a;哈喽&#xff0c;大家好&#xff0c;我是前端菜鸟的自我修养&#xff01;今天给大家分享Vue项目和微信小程序如何生成二维码和条形码&#xff0c;介绍了JsBarcode、wxbarcode等插件&#xff0c;并提供具体代码帮助大家深入理解&#xff0c;彻底掌握&#xff01…

5G与自动驾驶

上篇&#xff08;5G与4G的区别-CSDN博客&#xff09;讲了4G与5G的区别&#xff0c;大家可以看到5G具备高带宽、低时延的特性&#xff0c;可以广泛应用于各种物联网场景。 今天和大家简单聊聊5G与自动驾驶。 自动驾驶依靠人工智能、视觉计算、雷达、监控装置和全球定位系统协同…

Android Gradle开发与应用:Gradle基础

Android Gradle开发与应用&#xff1a;Gradle基础 Gradle是一款开源的自动化构建工具&#xff0c;已成为Android官方首推的自动化构建工具线。以下是关于Gradle在Android开发中的基础知识和应用&#xff0c;将按照几个关键点进行分点表示和归纳。 1. Gradle的基本概念 Proje…

关于mybatis中Mapper对应xml要写参数名的

1. 问题: 当我们在Mapper和xml之中传递参数时,必须要用Param注解来标识参数名, 否则参数就对应不上, 但每个参数都写Param就很烦人, 情况如下: mapper: User findById(Param("id") Long id); xml: <select id"findById" resultType"com.hz.doma…

为什么要学Java?

想要自己教会自己java&#xff0c;从小白成长到架构师。实现硬实力就业&#xff01; 因为Java是全球排名第一的编程语言&#xff0c;Java工程师也是市场需求最大的软件工程师&#xff0c;选择Java&#xff0c;就是选择了高薪。 为什么Java应用最广泛&#xff1f; 从互联网到…

软件测试:实验一 黑盒测试技术

一、实验目的 掌握黑盒测试的基本概念和原理&#xff0c;基本方法和技术。学会运用边界值、等价类划分方法对应用程序进行测试。学会使用测试用例对应用程序进行实际测试&#xff0c;并记录测试结果。 二、实验要求 分析被测应用程序&#xff0c;选定合适的黑盒测试方法。根…

基于FreeRTOS+STM32CubeMX+LCD1602+MCP4141(SPI接口)的数字电位器Proteus仿真

一、仿真原理图: 二、运行效果: 三、软件部分: 1)、SPI读写: 2)、初始化部分: void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the CPU, AHB and APB busses clocks …

基于python爬虫对豆瓣影评分析系统的设计与实现

基于python爬虫对豆瓣影评分析系统的设计与实现 Design and Implementation of a Python-based Web Crawler for Analyzing Douban Movie Reviews 完整下载链接:基于python爬虫对豆瓣影评分析系统的设计与实现 文章目录 基于python爬虫对豆瓣影评分析系统的设计与实现摘要第一…

SQL连接与筛选:解析left join on和where的区别及典型案例分析

文章目录 前言数据库在运行时的执行顺序一、left join on和where条件的定义和作用left join on条件where条件 二、left join on和where条件的区别原理不同left join原理&#xff1a;where原理&#xff1a; 应用场景不同执行顺序不同&#xff08;作用阶段不同&#xff09;结果集…

Netty学习(二)——黏包半包、协议设计解析、聊天室

一、粘包与半包 1.1 粘包和半包复现 1、粘包复现&#xff1a; Server代码&#xff1a; public class ProblemServer {public static void main(String[] args) throws InterruptedException {new ServerBootstrap()//若是指定接收缓冲区大小&#xff1a;就会出现黏包、半包…

测试零一万物大模型

听闻李开复先生创业的零一大模型很牛逼&#xff0c;一大早测试了一下。 代码 &#xff08;python) from openai import OpenAI API_BASE "https://api.lingyiwanwu.com/v1" API_KEY "61310vvvvvvc975" client OpenAI(api_keyAPI_KEY,base_urlAPI_BASE…

搜维尔科技:CyberGlove Systems 是数据手套技术的全球领导者,提供市场上最先进的以手为中心的 3D 动作捕捉解决方案

CyberGlove Systems 是数据手套技术的全球领导者&#xff0c;提供市场上最先进的以手为中心的 3D 动作捕捉解决方案 搜维尔科技&#xff1a;快速了解 SenseGlove 的 Nova VR 触觉力反馈手套