553C++笔试笔记2012

目录

1. 求最小公倍数

2.求一系列整数之和,个数也由io给出。

3.读文本统计单词个数(用到了map和regex)

补充1:regex

补充2:map

4.Person类

 5.Conclusion


1. 求最小公倍数

#include<iostream>

using namespace std;
int main() {
	int a, b = 0;
	cin >> a >> b;
	if (a > b) {
		int temp = a;
		a = b;
		b = temp;
	}

	int i = 1;
	while ((b * i) % a != 0)
		i++;
	cout << a << "和" << b << "的最小公倍数为" << b * i;
	return 0;
}

2.求一系列整数之和,个数也由io给出。

#include<iostream>
using namespace std;

int main() {
	int n;
	cin >> n;
	int sum = 0;
	int num;
	for (int i = 0; i < n; i++) {
		cin >> num;
		sum += num;
	}
	cout << n << "个整数之和为:" << sum << endl;

	return 0;
}
3
1
22
34
3个整数之和为:57

3.读文本统计单词个数(用到了map和regex)

#include<map>
#include<fstream>
#include<iostream>
#include<string>
#include<regex>
using namespace std;

void count(map<string, int> wamp) {
	map<string, int>::iterator it;
	for (it = wamp.begin(); it !=wamp.end(); it++)
		cout <<"("<< it->first<<"," << it->second<<")" << endl;
}
int main() {
	ifstream is("D:/word.txt");
	string mStr;
	map<string,int> mMAP;
	regex regWord("[a-zA-Z]+");
	while (getline(is, mStr)) {
		const std::sregex_token_iterator end;
		for (sregex_token_iterator wordIter(mStr.begin(), mStr.end(), regWord); wordIter != end; wordIter++){
			mMAP[*wordIter]++;
			/*cout << *wordIter << endl;迭代器不是指针,是类模板,表现的像指针。 
			模拟了指针的一些功能,通过重载了指针的一些操作符,->,++ --等封装了指针,
			是一个“可遍历STL( Standard Template Library)容器内全部或部分元素”的对象。*/
		}		
	}
	count(mMAP);
	return 0;
}

Pattern pattern = Pattern.compile("[a-zA-Z]+");//“+”:表示可以有一对多
Pattern pattern = Pattern.compile("[0-9]+"); 

补充1:regex

(101条消息) C++正则表达式regex库使用方法总结_#include _Asia-Lee的博客-CSDN博客icon-default.png?t=N176https://blog.csdn.net/asialee_bird/article/details/103420321

补充2:map

(101条消息) C++ map用法总结(整理)_sevencheng798的博客-CSDN博客icon-default.png?t=N176https://blog.csdn.net/sevenjoin/article/details/81943864

4.Person类

#include<string>
#include<iostream>
using namespace std;

class Name {
private:
	string first;
	string middle;
	string last;
public:
	Name(string f = "", string m = "", string l = "") {
		first = f;
		middle = m;
		last = l;
	}
	string printName()const {
		cout << "名字:" << first << ends << middle << ends << last << endl;
		return first + middle + last;
	}
};

class Person {
private:
	Name name;
	string nationality;
	string sex;
public:
	Person(const Name &na, string n, string s) {
		name = na;
		nationality = n;
		sex = s;
	}
	void printName() {
		name.printName();
	}
	void printNationality() {
		cout <<  "国籍:" << nationality << endl;
	}
};


int main() {
	Name n("Liu", "Ming", "Zhe");
	Person p(n, "China", "Male");
	n.printName();
	p.printName();
	p.printNationality();

}
名字:LiuMingZhe
名字:LiuMingZhe
国籍 : China

 5.Conclusion

既没有考多态,也没有考继承,感觉像是少了两题。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值