第二次上机作业letterCounting&lettercoutingWithFile&&EOJ 2844
来源:互联网 发布:腾讯视频网络设置在哪 编辑:程序博客网 时间:2024/06/06 01:45
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;}
阅读全文
0 0
- 第二次上机作业letterCounting&lettercoutingWithFile&&EOJ 2844
- 第二次上机作业
- 第二次上机作业
- 第二次上机作业
- 第二次C++上机作业
- c++第二次上机作业
- 第二次上机作业
- 第二次上机作业
- C++第二次上机作业
- C++第二次上机-作业
- 第二次上机作业
- C++第二次上机作业
- 第二次上机作业
- 第二次上机作业
- c++第二次上机作业
- 第二次上机作业
- 第二次上机作业
- C++第二次上机作业
- Mysql 索引的理解,总结与优化注意事项
- 连接数据库失败:Access denied for user 'root'@'(ip地址)' (using password: YES)
- maven class not found no qualifying bean of type
- JQuery的获得属性
- Android 中的dm-verity原理分析
- 第二次上机作业letterCounting&lettercoutingWithFile&&EOJ 2844
- LeetCode 476. Number Complement
- pwn工具箱之fastbin attack
- python操作Excel读写之使用xlrd
- ELK+Filebeat搭建实时日志分析平台
- PowerShell实现IE Web自动化
- 获取银行卡所属银行,验证银行卡号是否正确
- openCV之中值滤波&均值滤波(及代码实现)
- pwn工具箱之house of lore