SEU.2010.553

Problem1

输入 n 个十进制数转换成二进制写到文件,n 是随机得到的,用随机数进行输入n个十进制数,然后再进行转换。

#include "iostream"
#include "fstream"
#include <string>
using namespace std;
int main()
{
	int x,y,n;
	string str;
	srand(0);
	n = rand()%5;
	ofstream file;
	file.open("1.txt", ios::out);
	if (!file)
	{
		cerr << "File cannot be opened";
		exit(EXIT_FAILURE);
	}
	cout << "一共输入" << n << "个数字" << endl;
	while (n--)
	{
		cout << "input:";
		cin >> x;
		while (x!=0)
		{
			y = x % 2;
			str += to_string(y);
			x /= 2;
		}
		reverse(str.begin(), str.end()); //   //字符串逆置
		file << str << endl;
	}
	file.close();
	}

Problem2

#include "stdafx.h"
#include <iostream>
using namespace std;
//迭代插入排序
template<typename T>
void insertSort(T* t, int length)
{
	for (int i = 1; i < length; i++)
	{
		T p = t[i];
		int j;
		for (j = i - 1; j >= 0; j--)
		{
			if (t[j] > p)
				t[j + 1] = t[j];
			else
				break;
		}
		t[j+1] = p;
	}
}
//递归插入排序
template<typename T>
void InsertSort(T* t,int n)
{
	int i;
	if (n <= 0) return ;
	InsertSort(t, n - 1);
	T p = t[n];
	for (i = n - 1; i >= 0; i--)
	{
		if (t[i] > p)
			t[i + 1] = t[i];
		else
			break;
	}
	t[i+1] = p;
}
int main()
{
	int a[] = { 5,2,0,1,3,1,4 };
	int len = sizeof(a) / sizeof(a[0]);
	for (int i = 0; i < len; ++i) 
		cout << a[i] << " ";
	cout << endl;
	//insertSort(a,len);
	//for (int i = 0; i < len; ++i)
	//	cout << a[i] << " ";
	//cout << endl;
	InsertSort(a, len-1);
	for (int i = 0; i < len; ++i)
		cout << a[i] << " ";
	return 0;
}

Problem3

字符串的解析,文件中有类似的一行行字符串“(010)(15012345678)|123|(430070)”,按以下格式输出:区号|电话号码|城市编号|邮编
此处参考了https://blog.csdn.net/qq_32925781/article/details/79377073

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void getDataSimple(ifstream& in) {			// 如果输入是简单的固定长度的数字序列
	string str;								
	while (in >> str) {
		cout << str.substr(1, 3) << "|"
			<< str.substr(6, 11) << "|"
			<< str.substr(19, 3) << "|"
			<< str.substr(24, 6) << endl;
	}
}
int main() {
	ifstream input("info.txt", ios::in);
	getDataSimple(input);
	return 0;
}
// 不固定长度
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
	ifstream is("/Users/.../info.txt");
	if (!is) {
		cerr << "File cannot be opened!\n";
		exit(EXIT_FAILURE);
	}
	string info;
	string area, telephone, city, mail;
	while (is >> info) {
		area = info.substr(info.find("(") + 1, info.find(")") - info.find("(") - 1);   //提取找到的第一对括号之间的字符串
		info.erase(info.find("("), info.find(")") - info.find("(") + 1);      //删除该括号之间的字符串,括号一起删
		telephone = info.substr(info.find("(") + 1, info.find(")") - info.find("(") - 1); //此时找到的“第一对“括号实际上是下一对
		info.erase(info.find("("), info.find(")") - info.find("(") + 1);     //删除该括号之间的字符串,括号一起删
		mail = info.substr(info.find("(") + 1, info.find(")") - info.find("(") - 1);  //注意这里先处理mail
		info.erase(info.find("("), info.find(")") - info.find("(") + 1);
		city = info.substr(1, info.length() - 2);        //city包含在||中间,最后再处理
		cout << area << "| " << telephone << "| " << city << "| " << mail << endl;
	}
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值