编写程序,输入文本,并打印一个表格,显示每个不同单词在文本中出现的次数

本文介绍了一个简单的C++程序,用于统计并输出输入文本中每个单词的出现次数。通过使用标准库中的map容器,该程序能够有效地跟踪和更新单词频率。

题目

编写程序,输入文本,并打印一个表格,显示每个不同单词在文本中出现的次数。
例如:
输入:
Welcome to C++ world ! Happy birthday to you .
输出:
单词 C++ 的个数为 : 1
单词 Happy 的个数为 : 1
单词 Welcome 的个数为 : 1
单词 birthday 的个数为 : 1
单词 to 的个数为 : 2
单词 world 的个数为 : 1
单词 you 的个数为 : 1

/*
输入几行文本,并打印一个表格,显示每个不同单词在文本中出现的次数
*/


#include <iostream>
#include <map>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    string str,s;
    map<string,int> m;
    map<string,int>::iterator iter;

    cout<<"please input test: "<<endl;
    getline(cin,str);           //输入文本
    istringstream is(str);
    while(is>>s){               //提取字符串

        iter=m.find(s);         //查找s,并赋给迭代器
        if(iter!=m.end()){        //若字符串存在,则value+1
            iter->second++;
        }else{
            m.insert(make_pair(s,1));   //不存在则插入
        }

    }

    //把标点符号键值对删除
    m.erase("!");
    m.erase(",");
    m.erase(".");
    m.erase("?");

    //结束后遍历map
    for(map<string,int>::iterator it= m.begin();it!=m.end();it++){

        cout<<"单词 "<<it->first<<" 的个数为 : "<<it->second<<endl;

    }

    return 0;
}

编写一个具有前后端的大数据(MapReduce)程序来实现英语文章单词统计功能,并将结果以表格形式显示在前端 Vue 程序界面上,可以按照以下步骤进: ### 后端(Java MapReduce 程序) 以下是一个简单的 Java MapReduce 程序示例,用于统计英语文章中的单词数量: ```java import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } } public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context ) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 这个 Java 程序实现了一个简单的 MapReduce 单词统计功能。Mapper 将输入文本分割成单词,并为每个单词输出一个键值对(单词,1)。Reducer 对相同单词的计数进求和。 ### 后端(Spring Boot API) 为了将 MapReduce 的结果提供给前端,需要创建一个 Spring Boot 应用程序来提供 API 接口。以下是一个简单的示例: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; @SpringBootApplication @RestController public class WordCountApi { @GetMapping("/wordcount") public List<String[]> getWordCount() throws IOException { List<String[]> wordCountList = new ArrayList<>(); BufferedReader reader = new BufferedReader(new FileReader("output/part-r-00000")); String line; while ((line = reader.readLine()) != null) { String[] parts = line.split("\t"); wordCountList.add(parts); } reader.close(); return wordCountList; } public static void main(String[] args) { SpringApplication.run(WordCountApi.class, args); } } ``` 这个 Spring Boot 应用程序提供了一个 `/wordcount` 的 API 接口,用于返回 MapReduce 结果。 ### 前端(Vue.js) 在前端,使用 Vue.js 来创建一个简单的界面,并通过 API 接口获取数据并显示表格中: ```vue <template> <div> <h1>单词统计结果</h1> <table> <thead> <tr> <th>单词</th> <th>数量</th> </tr> </thead> <tbody> <tr v-for="(wordCount, index) in wordCountList" :key="index"> <td>{{ wordCount[0] }}</td> <td>{{ wordCount[1] }}</td> </tr> </tbody> </table> </div> </template> <script> export default { data() { return { wordCountList: [] }; }, mounted() { this.fetchWordCount(); }, methods: { fetchWordCount() { fetch('/wordcount') .then(response => response.json()) .then(data => { this.wordCountList = data; }); } } }; </script> ``` 这个 Vue.js 组件通过 `fetch` 方法调用后端的 `/wordcount` API 接口,并将返回的数据显示表格中。 ### 运步骤 1. 运 MapReduce 程序,将英语文章作为输入,指定输出路径。 2. 运 Spring Boot 应用程序。 3. 运 Vue.js 应用程序。 4. 在浏览器中打开 Vue.js 应用程序的页面,即可看到单词统计结果的表格
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值