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,一经查实,立即删除!

相关文章

前端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(…

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…

为什么要学Java?

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

基于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 …

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;就会出现黏包、半包…

Python3 学习笔记——基本知识入门 | 菜鸟教程

Python3 学习笔记——基本知识入门 | 菜鸟教程 目录 Python3 学习笔记——基本知识入门 | 菜鸟教程基础知识标识符python保留字注释行与缩进多行语句数字(Number)类型字符串(String)空行等待用户输入同一行显示多条语句多个语句构成代码组print 输出import 与 from...import命令…

DDR3控制器(一)DDR3 IP调用

目录 一、DDR3 IP核简介 二、DDR3 IP核调用 在千兆以太网通信中用到了DDR3控制器&#xff0c;但是并没有对其做相关介绍。这次准备重新整理一下DDR3控制相关知识&#xff0c;复习巩固一下。 一、DDR3 IP核简介 MIG IP核&#xff08;Memory Interface Generator&#xff09;是…

气膜游泳馆建造成本要多少—轻空间

随着人们对健康生活的追求和游泳运动的普及&#xff0c;游泳馆的需求不断增加。传统游泳馆的建设周期长、成本高&#xff0c;而气膜游泳馆以其独特的优势成为一种新的选择。轻空间将详细分析气膜游泳馆的建造成本及其优势。 气膜游泳馆的基本结构 气膜游泳馆主要由以下几个部分…

静电场的基本方程

目录 场积分方程 通量&#xff08;高斯定理&#xff09; 环量 场微分方程 散度 旋度 小结 补充知识 立体角 场积分方程 通量&#xff08;高斯定理&#xff09; 环量 场微分方程 散度 旋度 小结 补充知识 立体角

Open3D 删除点云中重复的点

目录 一、算法原理1、重叠点2、主要函数二、代码实现三、结果展示本文由CSDN点云侠原创,原文链接。如果你不是在点云侠的博客中看到该文章,那么此处便是不要脸的爬虫与GPT。 一、算法原理 1、重叠点 原始点云克隆一份   构造重叠区域   合并点云获得重叠点 2、主要…

玄子Share-本地部署 AI 大模型与构建知识库

玄子Share-本地部署 AI 大模型与构建知识库 部署环境概述 警告&#xff01;OpenAI 宣布全面封锁中国 API 接入 昨天&#xff0c;许多开发者从 OpenAI 那收到了一份警告信 您好&#xff0c; 据我们的数据监测&#xff0c;贵组织正从 OpenAl 当前未支持的区域产生 API 访问流量…

ai智能写作助手有哪些?3款AI工具推荐

ai智能写作助手有哪些&#xff1f;在数字化时代的浪潮中&#xff0c;AI智能写作助手如同智慧的灯塔&#xff0c;照亮了创作者们的道路。它们不仅极大地提升了写作效率&#xff0c;让文字流淌更加顺畅&#xff0c;更能够深入挖掘和激发创作者的内在灵感&#xff0c;将创意的火花…

github配置可拉取项目到本地

首先配置用户名和邮箱&#xff1a; git config --global user.name 自己的名字git config --global user.email 自己的邮箱配置完之后检查一下&#xff1a; git config --global user.namegit config --global user.email如果提示的是自己配置好的名字和邮箱就Ok 然后拉取githu…

Luminar Neo 1.20.0 (macOS Universal) - 创新 AI 图像编辑器

Luminar Neo 1.20.0 (macOS Universal) - 创新 AI 图像编辑器 利用尖端的人工智能生成技术&#xff0c;轻松增强照片效果 请访问原文链接&#xff1a;https://sysin.org/blog/luminar-neo/&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1…