第二次上机作业letterCountinglettercoutingWithFileEOJ 2844

本文主要是介绍第二次上机作业letterCountinglettercoutingWithFileEOJ 2844,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. letterCounting.cpp

   Write a function that countsthe number of occurrences of a pair of letters in a string.

   Then write a programthatreads a pair and a string to test the function.

   Blanks may be in the string.

第一题作业就是简单的输入两个字符串,并在第二个字符串中找到第一个字符串出现次数。

直接用string类的find函数即可。

要注意的是,find函数返回类型为string::size_type并不是简单的int类,只知道是一种unsigned类型,所以直接将它赋值给int不妥。如果找不到则返回string::npos.

代码如下:

#include <iostream>
#include <string>
using namespace std;
int main()
{string buf,str;int num=0;cout << "Enter a pair of letters:";getline(cin,buf);cout << "Enter a string to be couted:";getline(cin,str);string::size_type pos=str.find(buf);//定义一个string::size_type类型的变量while(pos!=string::npos){num++;pos=str.find(buf,pos+buf.size());//从当前找到的位置加上单词长度的位置为下一次寻找起始地址}cout<<"The number of occurences of a pair of letters "<<'"';cout<<buf<<'"'<<"in the string "<<'"';cout<<str<<'"'<<" is "<<num<<endl;return 0;
}

第二题
   Modify letterCount.cpp, read a pair and a string from a file.Specify filename in the command line. Use STL fstream. 二题的要求是要用命令行从文本读入输入,方法是使用STL中的fstream 文件流
<fstream>库包含了三个基本的类:ifstream, ofstream和fstream。这三个类分别代表一个输入文件,一个输出文件,以及一个输入输出文件。Ifstream类支持>>操作 符,ofstream类支持<<操作符,fstream类同时支持>>和<<操作符。所 有<fstream>对象都能够把一个文件名当成构造函数的变量,并能够自动的打开文件,如: 
std::ofstream dictionary("myfile.txt"); 
此外关于fstream的使用可见我转载的另一篇博客
所以我们只需要顶一个ifstream的变量,再将命令行参数传入并打开文件即可。

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc,char* argv[])//参数是由操作系统给的
{//argc为命令行参数个数,且至少为1(本身文件名也算)//argc为指针数组ifstream fin;fin.open(argv[1]);string buf,str;int num=0;cout << "Enter a pair of letters:";getline(fin,buf);cout << "Enter a string to be couted:";getline(fin,str);string::size_type pos=str.find(buf);while(pos!=string::npos){num++;pos=str.find(buf,pos+buf.size());}cout<<"The number of occurences of a pair of letters "<<'"';cout<<buf<<'"'<<"in the string "<<'"';cout<<str<<'"'<<" is "<<num<<endl;fin.close();return 0;
}
作业第三问是一道EOJ上的题目,其实是对unique_copy的使用。
题目地址http://acm.ecnu.edu.cn/problem/2844/
此题较易,不再解释。具体代码如下:


#include <bits/stdc++.h>
using namespace std;
struct Des
{bool operator()(const int a,const int b)const{return a>b;}
};
int main(int argc,char* argv[])//参数是由操作系统给的
{vector<int> vi,vv;char c;cin>>c;int tmp;while(cin>>tmp)vi.push_back(tmp);if(c=='A')sort(vi.begin(),vi.end());elsesort(vi.begin(),vi.end(),Des());unique_copy(vi.begin(),vi.end(),back_inserter(vv));for(int i=0;i<vv.size()-1;i++)cout<<vv[i]<<' ';cout<<vv[vv.size()-1]<<endl;}




这篇关于第二次上机作业letterCountinglettercoutingWithFileEOJ 2844的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/544754

相关文章

作业提交过程之HDFSMapReduce

作业提交全过程详解 (1)作业提交 第1步:Client调用job.waitForCompletion方法,向整个集群提交MapReduce作业。 第2步:Client向RM申请一个作业id。 第3步:RM给Client返回该job资源的提交路径和作业id。 第4步:Client提交jar包、切片信息和配置文件到指定的资源提交路径。 第5步:Client提交完资源后,向RM申请运行MrAp

毕业前第二次面试的感慨

距面试已经过去了有几天了,我现在想起来都有说多的恨感慨。 我一直都是想找刚刚起步的企业,因为这能让我学到更多的东西,然而正好有一家企业是刚起步的,而且他还有自己的产品专利,可以说这是一家,即是创业又是刚起步的公司,这家公司回复了我投给他的简历,这家企业想进一步了解我的情况,因为简历上我符合这家企业的基本要求,所以要进一步了解。 虽然面试的过程中,他给我的面试题,我做得并不是很理想,

Java高级Day38-网络编程作业

112.网络编程作业 //1.使用字符流的方式,编写一个客户端程序和服务器端程序//2.客户端发送"name",服务器端接收到后,返回"我是nova"//3.客户端发送"hobby",服务器端接收到后,返回"编写java程序"//4.不是这两个问题,回复"你说啥呢"​​===============//客户端//===============public class SocketT

0906作业+思维导图梳理

一、作业: 1、创捷一个类似于qq登录的界面 1)源代码 #include "widget.h"#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget){ui->setupUi(this);//QPushbutton:登录、退出this->join = new QP

2024.9.6 作业

1> 手写unique_ptr指针指针 #include <iostream>using namespace std;template <typename T>class my_unique_ptr{public:explicit my_unique_ptr(T *p = nullptr) noexcept // 构造函数{ptr = p;}~my_unique_ptr() noexcep

9月6号作业

1:.h文件 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QWidget> #include<QIcon> //图标类 #include<QLabel> //标签类 #include<QMovie> //动图类 #include<QLineEdit> //行编辑器类

Flink实例(六十九): flink 作业提交(四)总结

独立集群提交 # 启动集群bin/start-cluster.sh# 提交job./bin/flink run ./examples/batch/WordCount.jar --input hdfs:/user/yuan/input/wc.count --output hdfs:/user/yuan/swwwttt yarn session # 启动集群./bin/

【#第三期实战营闯关作业 ## 茴香豆:企业级知识库问答工具】

今天学习了《 茴香豆:企业级知识库问答工具》这一课,对大模型的应用有了更深得认识。以下是记录本课实操过程及截图: 搭建茴香豆虚拟环境: 输入以下命令 ``studio-conda -o internlm-base -t huixiangdou 成功安装虚拟环境截图 安装茴香豆 cd /root 克隆代码仓库 git clone https://github.com/internlm/h

Quartz 作业调度器

1、Quartz  java实现  注:这里使用的是Quartz1.6.5版本(包:quartz-1.6.5.jar)   [java]  view plain copy //测试main函数   //QuartzTest.java   package quartzPackage;         import java.text.SimpleDateFormat

清华MEM作业-利用管理运筹学的分析工具slover求解最优解的实现 及 通过使用文件或者套节字来识别进程的fuser命令

一、清华MEM作业-利用管理运筹学的分析工具slover求解最优解的实现         最近又接触了一些线性求解的问题,以前主要都是在高中数学里接触到,都是使用笔算,最后通过一些函数式得出最小或者最大值,最近的研究生学业上接触到了一个Excel solver分析工具,对这种线性求最优解的问题感觉使用起来真是得心应手。在使用这个工具前,EXCEL里需要先装上solver工具,装起来很也简单,网上