第二次上机作业letterCounting&lettercoutingWithFile&&EOJ 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());
    else
        sort(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;

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值