语言联邦-常用算法、可存成代码段

//1.查找单个字符
//2.连续查找单个字符
//3.查找多个连续字符
//4.字符区间查找
//5.字符串对齐
//6.搜索连续相对的元素
//7.去重、排序
//8.生成随机数
//9.求正整数的质数
// 9.正整数的阶乘
//10.四舍五入
//16.最大公约数、最小公倍数
//11.set or map
//12.一行输入 + 解析单个字符串 + 数值变得
//13.字符串检查
//14.字符串的替换
//15.字符中的字符的个数
//17.等差数列
//18.数组与容器
//19.byte
//21.文件读写
//22.超长正整数

// ALTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include  
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

//1.查找单个字符
#if 0
int main()
{
	std::string str;
	std::cout << "please input the string :";
	while (std::getline(std::cin, str) && str.length()>0)
	{
		int nStrLen{ 0 };
		const auto nPos = str.find_last_of(' ');
		if(nPos<0) continue;
		
		std::cout << str.length() - nPos-1 << std::endl;
		//break;
	}
    return 0;
}
#endif

//2.连续查找单个字符
#if 0
int main()
{
	std::cout << "please input the string:" << std::endl;
	std::string str;
	std::getline(std::cin, str);
	if (str.length() < 1)
	{
		std::cout << "please input the string:" << std::endl;
		return 1;
	}

	std::cout << "please input one char as the separator:" << std::endl;
	char cSep;
	std::cin >> cSep;

	int nSpecialCharCount = 0;

	//again
#if 1
	std::for_each(str.begin(), str.end(), [&](char & cItem) {if (cItem == cSep) { nSpecialCharCount++;}});
#endif

#if 0
	std::string::iterator itr = std::find_if(str.begin(), str.end(), std::bind2nd(std::equal_to<char>(), cSep));
	while (itr != str.end())
	{
		++nSpecialCharCount;
		itr = std::find(++itr, str.end(), cSep);
	}
#endif
	cout << "the special char num is :" << nSpecialCharCount << endl;

	return 1;
}
#endif

//3.查找多个连续字符
#if 0
int main()
{
	cout << "please input the string :" << endl;
	std::string str;
	std::getline(std::cin, str);
	while (str.length()<1)
	{
		cout << "please input the string :" << endl;
		std::getline(std::cin, str);
	}

	std::cout << "please input the special string:" << std::endl;
	char cSep;
	std::cin >> cSep;

	std::cout << "please input the number of the special str:" << std::endl;
	int nCout{0};
	std::cin >> nCout;
	
	int nNumber = 0;
	std::string::iterator itF = std::search_n(str.begin(), str.end(), nCout, cSep);
	while (itF!=str.end())
	{
		++nNumber;
		itF = std::search_n(++itF, str.end(), nCout, cSep);
	}
	cout << "the number of the Adjacnt strings is :" << nNumber << endl;
	return 1;
}

#endif

//4.字符区间查找
#if 0
int main()
{
	cout << "please input the string :" << endl;
	std::string str;
	std::getline(std::cin, str);

	while (str.length() < 1)
	{
		cout << "please input the string :" << endl;
		std::getline(std::cin, str);
	}

	cout << "please input the strings which need to find:" << endl;
	std::string strSub;
	std::getline(std::cin, strSub);

	std::string::iterator itrF= std::search(str.begin(), str.end(), strSub.begin(), strSub.end());
	int nCount = 0;
	while (itrF!=str.end())
	{
		nCount++;
		itrF= std::search(++itrF, str.end(), strSub.begin(), strSub.end());
	}
	cout << "the number of special strings is:" << nCount << endl;
	return 1;
}
#endif

//5.字符串对齐
#if 0
int main()
{
	std::string str1[2];
	cout << "please input 2 strings " << endl;
	cin >> str1[0];
	cin >> str1[1];

	for (int i = 0;i < 2;i++)
	{if (str1[i].length() >= 100) { str1[i] = str1[i].substr(0, 99); }}
	
	std::vector<std::string> vArray;
	for (int i = 0; i < 2; i++)
	{
		std::string::iterator itrF = str1[i].begin();
		while (itrF != str1[i].end())
		{
			const auto nDis = distance(itrF, str1[i].end());
			std::string strTemp = str1[i].substr(distance(str1[i].begin(), itrF), __min(8, nDis));
			if (8 > nDis)
			{	strTemp.append((8 - nDis), '0');vArray.emplace_back(strTemp);break;}
			else
			{	vArray.emplace_back(strTemp); itrF += 8;}
		}
	}

	cout << "the uniform string array is :" << endl;
	cout << std::setprecision(10)<<std::scientific;
	std::copy(vArray.begin(), vArray.end(), std::ostream_iterator<std::string>(cout, "\n"));

	cout << "the uniform string array is(oct) :" << endl;
	std::for_each(vArray.begin(), vArray.end(), [](const auto & str) {cout << oct <<setw(10)<< atoi(str.c_str())<< endl;});

	cout << "the uniform string array is(dec) :" << endl;
	std::for_each(vArray.begin(), vArray.end(), [](const auto & str) {cout << dec << setw(10) << atoi(str.c_str()) << endl;});

	cout << "the uniform string array is(hex) :" << endl;
	std::for_each(vArray.begin(), vArray.end(), [](const auto & str) {cout << hex << setw(10) << atoi(str.c_str()) << endl;});

	cout << "the uniform string array is :" << endl;
}
#endif

//6.搜索连续相对的元素
#if 0
int main()
{
	cout << "please input the string :" << endl;
	std::string str;
	std::getline(std::cin, str);

	while (str.length() < 1)
	{
		cout << "please input the string :" << endl;
		std::getline(std::cin, str);
	}

	std::string::iterator itrF = std::adjacent_find(str.begin(), str.end()/*, [](char char1, char char2) {return char1=='a'&&char2=='s';}*/);
	int nPos = itrF!=str.end() ? std::distance(str.begin(), itrF) : -1;
	cout << "the position is :" << nPos << endl;
	return 1;
}
#endif

//7.去重、排序
#if 0
int main()
{
	cout << "please input numbers separated by commas:" << endl;
	std::string strNumbers;
	getline(std::cin, strNumbers);
	while (strNumbers.length()<1)
	{
		cout << "please input numbers separated by commas:" << endl;
		getline(std::cin, strNumbers);
	}

	std::vector<long> vNum;
	std::string::iterator itrF = std::find(strNumbers.begin(), strNumbers.end(), ',');
	std::string::iterator itrS = strNumbers.begin();
	while (itrF!=strNumbers.end())
	{
		auto offset = std::distance(strNumbers.begin(), itrS);
		auto nCount = std::distance(itrS, itrF);
		std::string strSub = strNumbers.substr(offset, nCount);
		vNum.emplace_back(atoi(strSub.c_str()));
		itrS = ++itrF;
		itrF= std::find(itrS, strNumbers.end(), ',');
	}

	auto offset = std::distance(strNumbers.begin(), itrS);
	auto nCount = std::distance(itrS, strNumbers.end());
	if (nCount > 0)
	{
		std::string strSub = strNumbers.substr(offset, nCount);
		vNum.emplace_back(atoi(strSub.c_str()));
	}

	std::cout << "the number list is:" << endl;
	std::for_each(vNum.begin(), vNum.end(), [=](long &nNum) {cout << nNum << ",";});

	std::sort(vNum.begin(), vNum.end());
	vNum.erase(std::unique(vNum.begin(),vNum.end()), vNum.end());
	std::cout << endl << "the uniqued number list is:" << endl;
	std::for_each(vNum.begin(), vNum.end(), [=](long &nNum) {cout << nNum << ",";});

	return 1;
}
#endif

//8.生成随机数
#if 0
int main()
{
	/*
	1.random
	2.array
	3.erase+unique
	*/
	cout << "please input the number: " << endl;
	int nNumber = 0;
	cin >> nNumber;
	while (nNumber<1)
	{
		cout << "please input the number(>0): " << endl;
		cin >> nNumber;
	}

	//generate numbers 
	std::list<int> vNum;
	std::generate_n(back_inserter(vNum), nNumber, []() {static int nS = 0;int nTemp = rand();return 1000 >= nTemp ? nTemp : ++nS; });
	cout << "the number list is:"<<endl;
	for_each(vNum.begin(), vNum.end(), [=](auto& nNum) {cout << nNum << ",";});
	cout << endl;
	
	//erase+unique
	vNum.sort();
	cout << "the uniqued number list is:" << endl;
	std::copy(vNum.begin(), unique(vNum.begin(), vNum.end()), ostream_iterator<int>(cout, ","));
	vNum.erase(unique(vNum.begin(), vNum.end()), vNum.end());
	cout << endl << "erase:" << endl;
	std::copy(vNum.begin(), unique(vNum.begin(), vNum.end()), ostream_iterator<int>(cout, ","));

	cout << endl;
}

#endif // 1

//9.求正整数的质数
#if 0
std::string GetResult(long ulDataInput)
{
	if (ulDataInput < 1) return "";

	std::vector<int> vNumArray;
	while (ulDataInput>1)
	{
		for (int i = 2;i <= ulDataInput;i++)
		{
			if (ulDataInput%i == 0)
			{
				vNumArray.emplace_back(i);
				ulDataInput /= i;
				break;
			}
		}
	}

	std::string strResult;
	std::for_each(vNumArray.begin(), vNumArray.end(),
		[&](int & num) {strResult.append(std::to_string(num));strResult.append(",");});

	return strResult;
}

int main()
{
	cout << "please input a long number:" << endl;
	long nInput{0};
	cin >> nInput;

	if (nInput < 2) {cout << nInput;return 1;}

	std::list<int> vResult;
	while (nInput>1)
	{
		for (int k = 2;k <= nInput;k++)
		{
			if (nInput%k == 0)
			{
				nInput /= k;
				vResult.emplace_back(k);
				break;
			}
		}
	}

	cout << "the result is :\n" ;
	std::for_each(vResult.begin(), vResult.end(), [](int & aItem) {cout << aItem << ",";});
	cout << "\n";
	return 1;
}
#endif

// 9.正整数的阶乘
#if 0
template<unsigned int n>
struct sCalc
{
	enum { start=n+sCalc<n-1>::start};
};
template<>
struct sCalc<0>
{
	enum { start = 0 };
};

int main()
{
	cout << "please input a number:" << endl;
	int nInput{ 0 };
	cin >> nInput;

	std::list<int> vArray;
	std::generate_n(back_inserter(vArray), nInput, [=]() {static int nStart = nInput; return nStart--;});
	std::copy(vArray.begin(), vArray.end(), ostream_iterator<int>(cout, ","));

	cout << "\n阶乘是:" << std::accumulate(vArray.begin(), vArray.end(), 0, multiplies<int>()) << endl;

	std::cout << "\nn的阶乘是:" << sCalc<5>::start << std::endl;
	return 0;
}

#endif

//10.四舍五入
#if 0
int main()
{
	double dInputNumber = 0;
	cout << "please input a number(>0):" << endl;
	
	do 
	{
		cin >> dInputNumber;
		if(!(dInputNumber>0.0)) break;

		cout << "the Result is :" << round(dInputNumber) << endl;
	} while (dInputNumber >0);
}
#endif

//16.最大公约数、最小公倍数
#if 0
/*
while (nNum1%nNum2)
{
int nTemp = nNum1;
nNum1 = nNum2;
nNum2 = nTemp%nNum2;
}
*/
int main()
{
	cout << "Please input two numbers :" << endl;
	int nNum1 = 0; int nNum2 = 0;
	cin >> nNum1;
	cin >> nNum2;

	//test
	cout << "The Input is :" << nNum1 << "," << nNum2 << endl;
	//test

	const int nProduct = nNum1*nNum2;
	if (nProduct == 0) { cout << nProduct << endl; };

	// gcd
	while (nNum1%nNum2)
	{
		int nTemp = nNum1;
		nNum1 = nNum2;
		nNum2 = nTemp%nNum2;
	}

	//test
	cout << "gcd is :" << nNum2 << endl;
	//test

	cout << "the result is :" << nProduct / nNum2 << endl;
}
#endif

//11.set or map
#if 0
class CalcMapVale : public std::binary_function<typename multimap<int, int>::value_type, int,int>
{
public:
	int operator()(std::multimap<int, int>::value_type & aItem, int &key)
	{
		if (aItem.first == key) return aItem.second;
	}
};

template<typename K, typename V>
class valueEquals
{
public:
	valueEquals() {}
	V operator()(std::pair<K, V> elem)
	{
		return (elem.second == elem.first);
	}
};

int main()
{
	int nPairs = 0;
	cout << "please input the number:" << endl;
	cin >> nPairs;
	if (nPairs < 1) return 1;

	cout << "please input the key and vale:" << endl;

	std::multimap<int, int> vMap;
	std::list<int> vKey;
	std::string strInput;
	while (vMap.size() != nPairs && getline(std::cin, strInput))
	{
		const int npos = strInput.find_first_of(" ");
		const int nPosRev = strInput.find_last_of(" ");
		if(npos<0 || nPosRev<0 || nPosRev!=npos) continue;

		vKey.emplace_back(atoi(strInput.substr(0, npos).c_str()));
		vMap.emplace(atoi(strInput.substr(0, npos).c_str()), atoi(strInput.substr(nPosRev + 1, string::npos).c_str()));
	}

	using MapValue = std::multimap<int, int>::value_type;
	using MapIter = std::multimap<int, int>::iterator;

	cout << "the original map is :\n";
	for_each(vMap.begin(), vMap.end(), [](MapValue & aItem) {cout << aItem.first << "," << aItem.second << endl;});

	MapIter IterEqu=std::find_if(vMap.begin(), vMap.end(), valueEquals<int, int>());
	if (IterEqu != vMap.end())
	{	cout << "K=V :" << IterEqu->first << IterEqu->second << endl;}

	//merge
	vKey.sort();
	vKey.erase(std::unique(vKey.begin(), vKey.end()), vKey.end());

	std::map<int, int> vMapRes;
	for_each(vKey.begin(), vKey.end(), [&](int &key) {vMapRes.emplace(key, 0);});

	for(auto & aItem : vKey)
	{
		auto & itrFs = vMap.equal_range(aItem);
		int nSum = 0;
		for_each(itrFs.first, itrFs.second, [&](MapValue & aItem) {nSum += aItem.second;});
		vMapRes[aItem] = nSum;
	}

	cout << "the merged map is :\n";
	cout.setf(ios::left, ios::adjustfield);
	for_each(vMapRes.begin(), vMapRes.end(), [](MapValue & aItem) {cout <<setw(10) << aItem.first << "," << aItem.second << endl;});

	return 1;
}
#endif

//12.一行输入 + 解析单个字符串 + 数值变得
#if 0
int main()
{
	/*
	Note:
	1.input
	2.parse
	3.change value
	*/

	cout << "please input the orderstring :"<<endl;
	std::string strOrder;
	getline(std::cin, strOrder);
	while (strOrder.length()<1)
	{
		cout << "please input the orderstring :" << endl;
		getline(std::cin, strOrder);
	}

	std::vector<std::string> vSingleOrders;
	using StrIter = std::string::iterator;
	StrIter sIterS = strOrder.begin();
	StrIter sIterF = std::find_if(sIterS, strOrder.end(), bind2nd(equal_to<char>(), ';'));
	while (sIterF !=strOrder.end())
	{
		std::string strSub = strOrder.substr(distance(strOrder.begin(), sIterS), sIterF-sIterS);
		vSingleOrders.emplace_back(strSub);
		sIterS = ++sIterF;
		sIterF = std::find_if(sIterS, strOrder.end(), bind2nd(equal_to<char>(), ';'));
	}

	//test
	cout << "the order list is :\n";
	for_each(vSingleOrders.begin(), vSingleOrders.end(), [](auto& sItem) {cout << sItem << endl;});
	//test

	int X = 0, Y = 0;
	for_each(vSingleOrders.begin(), vSingleOrders.end(), [&](string & sItem) {

		if (sItem.length() < 1) return;
		if (sItem.at(0) != 'A' && sItem.at(0) != 'D' && sItem.at(0) != 'W' && sItem.at(0) != 'S') return;

		string strData = sItem.substr(1, sItem.length() - 1);
		if (strspn(strData.c_str(), "0123456789") != strlen(strData.c_str())) return;
		const int nOffset=stoi(strData);

		switch (sItem.at(0))
		{
		case 'A': {X -= nOffset;break;};
		case 'D': {X += nOffset;break;};
		case 'W': {Y += nOffset;break;};
		case 'S': {Y -= nOffset;break;};
		default:break;
		}
	});

	cout << "the coordinate is :" << X << "," << Y << endl;;
}
#endif

//13.字符串检查
#if 0
int main()
{
	/*
	Note:
	1.input several passwords
	2.check every one
	*/
	cout << "please input passwords:" << endl;

	std::vector<std::string> vStrArray;
	string strTemp;
	while (getline(cin, strTemp) && strTemp.length()>0)
	{vStrArray.emplace_back(strTemp);}

	//test-check input 
	cout << "the Input is:\n";
	for_each(vStrArray.begin(), vStrArray.end(), [](string& sItem) {cout << sItem << endl;});
	//test

	cout << "\ncheck:\n";
	for_each(vStrArray.begin(), vStrArray.end(), [](string& sItem) {
		bool bLength = sItem.length() > 8;
		bool bHaveUpper = false;
		bool bHaveLower = false;
		bool bHaveNumber = false;
		bool bHavePunct = false;
		for (char cChar : sItem)
		{
			if (!bHaveNumber && isdigit(cChar)) bHaveNumber = true;
			if (!bHaveUpper && isupper(cChar)) bHaveUpper = true;
			if (!bHaveLower && islower(cChar)) bHaveLower = true;
			if (!bHavePunct && ispunct(cChar)) bHavePunct = true;
		}

		bool bSameSubStr = false;
		using cIter = std::string::iterator;
		cIter cIterS = sItem.begin();
		cIter cIterF = sItem.begin();
		do 
		{
			cIterF = std::search(cIterS+3, sItem.end(), cIterS, cIterS + 2);
			if (cIterF != sItem.end()) { bSameSubStr = true;break; }
			++cIterS;
		} while (cIterS!=sItem.end() && distance(cIterS,sItem.end())>2);

		bool bValid = bLength &&
			(static_cast<int>(bHavePunct)+ static_cast<int>(bHaveNumber)+ static_cast<int>(bHaveLower)+ static_cast<int>(bHaveUpper))>2
			&& !bSameSubStr;
		bValid ? cout << "OK" : cout << "NG";cout << endl;
	});


	return 1;
}
#endif

//14.字符串的替换
#if 0
int main()
{
	/*
	Note:
	1.input
	2.parse+chage
	*/
	std::string strInput;
	cout << "please input the string:\n";
	//getline(cin, strInput);
	//while (strInput.length()<1)
	//{
	//	cout << "please input the string:\n";
	//	getline(cin, strInput);
	//}
	//
	//if (strInput.length() > 100)
	//{
	//	strInput = strInput.substr(0, 100);
	//	cout << "cut off: " << strInput << endl;
	//}
	char cChar = ' ';
	while (strInput.length()<10 && cin.get(cChar) && cChar!='\n')
	{
		strInput.push_back(cChar);
	}

	//test -input
	cout << "the input is " << strInput <<endl<<ends<<flush;
	//test

	//move afterword
	std::for_each(strInput.begin(), strInput.end(), [](char& cChar) {
		if (isdigit(cChar) || ispunct(cChar)) return;
		if (isupper(cChar)){ cChar=tolower(cChar); cChar= cChar=='z'?'a': cChar +1;}
		else if(islower(cChar))
		{
			int nRange = 1;
			for (char cC = 'a';'z' >= cC;cC+=3)
			{
				if (cChar >= cC && cChar < static_cast<char>(cC + 3)){ cChar = cChar == 'z' ? '9' : '1'+ nRange;break;}
				nRange++;
			}
		}
	});

	//test -input
	cout << "after move: " << strInput << endl;;
	//test
}
#endif

//15.字符中的字符的个数
#if 0
int main()
{
	/*
	Note:
	1.input
	2.count
	3.delete
	*/

	cout << "please input the string:" << endl;
	std::string strInput;
	getline(cin, strInput);
	
	// until the Invalid string
	while (strInput.length()<1)
	{
		cout << "please input the string:" << endl;
		getline(cin, strInput);
	}
	// cut off
	if (strInput.length() > 20) {strInput = strInput.substr(0, 20);}

	//test
	cout << "the input is :" << strInput << endl;
	//test

	static int nCount = INT_MAX;
	for_each(strInput.begin(), strInput.end(), [&](char & cChar) {
		nCount = __min(nCount, std::count(strInput.begin(), strInput.end(),cChar));
	});

	//test
	cout << "the minimum is :" << nCount << endl;
	//test

	//delete the first one 
	using sSter = std::string::iterator;
	sSter sNewEnd= std::remove_if(strInput.begin(), strInput.end(), [&](char & cChar) {
		if (std::count(strInput.begin(), strInput.end(), cChar) == nCount) return true;
		return false;
	});

	// delete others
	while (sNewEnd != strInput.end())
	{
		strInput = strInput.substr(0, distance(strInput.begin(), sNewEnd));
		nCount--;
		sNewEnd = std::remove_if(strInput.begin(), strInput.end(), [&](char & cChar) {
			if (std::count(strInput.begin(), strInput.end(), cChar) == nCount) return true;
			return false;
		});
	}

	cout << "the result is :" << strInput << endl;
}
#endif

//17.等差数列
#if 0
int main()
{
	const int nStart = 2;
	const int nStep = 3;
	cout << "Please input the item size:" << endl;
	int N = 0;
	cin >> N;

	if (N == 0) { cout <<"unvalidated Input:"<< N << endl;return -1; };

	//generate the sequence
	std::vector<int> vArray(N);
	for_each(vArray.begin(), vArray.end(), [=](int & nItem) {static int nTemp = nStart;nItem = nTemp; nTemp += nStep;});
	//test
	cout << "the number array is : \n";
	for_each(vArray.begin(), vArray.end(), [](int &nItem) {cout << nItem << ",";});
	cout << endl;
	//test
	//accumlate
	cout << "the sum is :" << std::accumulate(vArray.begin(), vArray.end(), 0) << endl;
	//product
	cout << "the product is :" << std::accumulate(vArray.begin(), vArray.end(), 1,std::multiplies<long>()) << endl;

	return 0;
}
#endif

//18.数组
#if 0
int main()
{
	/**
	 Note:
	 1.input size
	 2.generate sequence
	 3.sort
	 */
	cout << "please enter the size of Sequence: " << endl;
	string strN;
	getline(cin, strN);
	if (strN.empty()) { cerr << "Invalid entry  : " << strN << endl;return -1; };
	const int N = atoi(strN.c_str());
	if (N == 0) { cerr << "Invalid entry  : " << N << endl;return -1; };

	cout << "please enter a sequence separated by spaces : " << endl;
	std::string strInput;
	getline(cin, strInput); cin.clear();
	if(strInput.empty()) { cerr << "Invalid entry : " << N << endl;return -1; };

	int nSort = 0;
	cout << "Please enter order rule(0,1):"<<endl;
	cin >> nSort;
	if (nSort!=0 && nSort!=1) { cerr << "Invalid entry : " << nSort << endl;return -1; };

	int* nArray = new int[N];

	//generate the number array
	strInput.append(" ");
	using SIter = std::string::iterator;
	SIter sIterS = strInput.begin();
	SIter sIterF = std::find_if(sIterS, strInput.end(), bind2nd(equal_to<char>(), ' '));
	int nIndex = 0;
	while (sIterF != strInput.end())
	{
		std::string strSub = strInput.substr(distance(strInput.begin(), sIterS), sIterF - sIterS);
		if (!strSub.empty()) { nArray[nIndex++] = atoi(strSub.c_str()); }
		sIterS = ++sIterF;
		sIterF = std::find_if(sIterS, strInput.end(), bind2nd(equal_to<char>(), ' '));
	}

	//test
	cout << "the input array is:" << endl;
	std::for_each(nArray, nArray + N, [](int & nItem) {cout << nItem << ",";});
	//test

	cout << "\nthe sorted array is:" << endl;
	if (nSort == 0) { std::sort(nArray, nArray + N, less<int>()); }
	else { std::sort(nArray, nArray + N, greater<int>()); }
	std::for_each(nArray, nArray + N, [](int & nItem) {cout << nItem << ",";});

	return 1;
}
#endif

//19.byte
#if 0
int main()
{
	/*
	Note:
	1.input a int 
	2.compute byte 1
	*/
	cout << "Please input a Number:" << endl;
	std::string strInput;
	getline(cin, strInput);
	if (strInput.empty()) { cerr << "Invalid entry!" << endl;return -1; };

	int nByte = atoi(strInput.c_str());
	if (nByte == 0) { cout << "The Result is : 0" << endl;return 1; };
	cout << "The Input Number is :" <<bitset<8>(nByte) << endl;

	int k = 0;
	while (nByte!=0)
	{
		nByte &= (nByte << 1);
		k++;
	}

	cout << "The size of Byte 1 is :" << k << endl;
}
#endif

//again
#if 1
int main()
{
	return 1;
}
#endif

//20.计算算术表达式的值
#if 0
int main()
{
	/*
	Note:
	1.input a string
	2.parse the string ,then push to stack
	3.change mid to end
	4.calculate
	*/
	cout << "Please input a express : " << endl;
	string strInput;
	getline(cin, strInput);
	if (strInput.empty()) { cout << "unvalidated input !" << endl;return -1; };
	
	std::replace(strInput.begin(), strInput.end(), '[','(');
	std::replace(strInput.begin(), strInput.end(), ']', ')');
	std::replace(strInput.begin(), strInput.end(), '{', '(');
	std::replace(strInput.begin(), strInput.end(), '}', ')');
	strInput.append("#");

	//test
	cout << "The input express is :" <<strInput<< endl;	
	//test

	const std::string strOperator("(*/+-)#");
	using sDis = string::iterator::difference_type;
	using sNumPair = std::pair<int, sDis>;
	using sOperPair = std::pair<char, sDis>;
	std::stack<sNumPair> sNumbers;
	std::stack<sOperPair> sOperators;

	using SIter = std::string::iterator;
	SIter sIterS = strInput.begin();
	SIter sIterF = sIterS;

	auto CheckOperator = [&](char & cTest)->bool {

		for (int k = 0;k < strOperator.size();k++)
		{
			char cChar = strOperator.at(k);
			if (cChar == ' ') continue;
			if (cTest == cChar) return true;
		}
		return false;
	};

	char cChar = ' ';
	while (sIterF != strInput.end())
	{
		bool bIsOperator = CheckOperator(*sIterF);
		if (!bIsOperator) {++sIterF; continue;}
		
		sOperators.push(sOperPair(*sIterF,distance(strInput.begin(),sIterF)));

		std::string strSub = strInput.substr(distance(strInput.begin(), sIterS), (sIterF - sIterS));
		if (!strSub.empty()) { int nNum = atoi(strSub.c_str()); sNumbers.push(sNumPair(nNum, distance(strInput.begin(), sIterS))); };

		sIterS = ++sIterF;
		if (sIterS == strInput.end()||*sIterS=='#') { break; };
	}

	//test
	cout << "the number stack is :" << endl;
	while (!sNumbers.empty())
	{
		cout << sNumbers.top().first<<","<<sNumbers.top().second << endl; sNumbers.pop();
	}
	cout << "the Operator stack is :" << endl;
	while (!sOperators.empty())
	{
		cout << sOperators.top().first<<"," <<sOperators.top().second << endl;sOperators.pop();
	}
	//test

}
#endif

#if 0
//3+2*{1+2*[-4/(8-6)+7]}

std::string CalcExpress(const std::string & strExpress/*无括号*/)
{
	string strResult("1");
	const std::string strOperator("*/+-");
	string strTemp(strExpress);

	using SIter = std::string::iterator;

	auto IsHaveOperator = [&](const std::string &szExpress)->bool {

		if (szExpress.length() < 1) return false;
		for (auto & aChar : szExpress)
		{
			for (int k = 0;k < strOperator.size();k++)
			{
				char cChar = strOperator.at(k);
				if (cChar == ' ') continue;
				if (aChar == cChar) return true;

			}
		}
		return false;
	};

	while (IsHaveOperator(strTemp))
	{
		for (const char & cOper:strOperator)
		{
			size_t nPos = strTemp.find(cOper);
			if(nPos==string::npos) continue;
			if(nPos<1) continue;

			int nNum1 = strTemp.at(nPos - 1) - '0';
			int nNum2 = strTemp.at(nPos + 1) - '0';
			int nResult = 0;
			if (cOper == '*') nResult = nNum1*nNum2;
			else if(cOper=='/')nResult = nNum2==0?0 : nNum1/nNum2;
			else if (cOper == '+')nResult = nNum1 + nNum2;
			else if (cOper == '-')nResult = nNum1 - nNum2;
			strTemp.replace(strTemp.begin()+nPos-1, strTemp.begin() + nPos + 2, to_string(nResult));
		}
	}

	return strTemp;
}

int main()
{
	cout << "Please input a express : " << endl;
	string strInput;
	getline(cin, strInput);
	if (strInput.empty()) { cout << "unvalidated input !" << endl;return -1; };

	std::replace(strInput.begin(), strInput.end(), '[', '(');
	std::replace(strInput.begin(), strInput.end(), ']', ')');
	std::replace(strInput.begin(), strInput.end(), '{', '(');
	std::replace(strInput.begin(), strInput.end(), '}', ')');

	//test
	cout << "The input express is :" << strInput << endl;
	//test

	const std::string strOperator("(*/+-)");
	auto IsHaveOperator = [&](const std::string &szExpress)->bool {

		if (szExpress.length() < 1) return false;
		for (auto & aChar : szExpress)
		{
			for (int k = 0;k < strOperator.size();k++)
			{
				char cChar = strOperator.at(k);
				if (cChar == ' ') continue;
				if (aChar == cChar) return true;

			}
		}
		return false;
	};

	using SIter = std::string::iterator;

	while (IsHaveOperator(strInput))
	{
		std::string::size_type nPos1 = strInput.find_last_of('(');
		std::string::size_type nPos2 = strInput.find_first_of(')');
		//if (nPos1 != std::string::npos && nPos2 != std::string::npos)
		{
			SIter IterS = strInput.begin();
			if(nPos1!=std::string::npos){	std::advance(IterS, nPos1);}
			else{ IterS = strInput.end();}

			SIter IterF = strInput.begin();
			if (nPos2+1 != std::string::npos){ std::advance(IterF, nPos2 + 1);}
			else {IterF = strInput.end();}

			if (IterS == strInput.end()) break;

			//calculate
			std::string strSubExpress = strInput.substr(nPos1+1, nPos2 - nPos1-1);
			if (strSubExpress.at(0) == '-') { strSubExpress.insert(strSubExpress.begin(), '0'); }

			//test
			cout << "sub express is :" << strSubExpress << endl;
			//test
			std::string strSubRes = CalcExpress(strSubExpress);
			//test
			cout << "the sub express 's result is :" << strSubRes << endl;
			//test

			strInput.replace(IterS, IterF, strSubRes);

			//test
			cout << "the new express is :" << strInput << endl;
			//test
		}
	}

	cout << "\nThe result of the express is :" << strInput << endl;
	return 1;
}
#endif

//21.文件读写
#if 0
int main()
{
	std::string strFileName = "D:\\osg365vs2015x64\\Build64VC140\\bin\\Data\\1.txt";
	std::fstream file(strFileName.c_str());
	if (!file)
	{
		cerr << "can't open the file :" << strFileName << endl;
		return -1;
	}
	// input
	for (int i = 32;i < 256;i++)
	{
		file << "Value:" << setw(3) << i << " " << "char:" << static_cast<char>(i) << endl;
	}
	file.close();


	//output
	std::fstream file1(strFileName.c_str());
	if (!file1)
	{
		cerr << "can't open the file :" << strFileName << endl;
		return -1;
	}

	char cChar = ' ';
	while (file1.get(cChar))
	{
		cout.put(cChar);
	}

	char szline[1000];
	while (file1.getline(szline, 1000))
	{
		cout << szline;
	}
	cout << "getloc = " << file1.getloc().classic().c_str() << endl;

	std::string sFileData((std::istreambuf_iterator<char>(file1)), std::istreambuf_iterator<char>());
	std::cout << sFileData.c_str() << std::endl;

	return 1;
}
#endif
// again
#if 0
int main()
{
	string strFileName = "D:\\osg365vs2015x64\\Build64VC140\\bin\\Data\\2.txt";
	std::fstream file(strFileName);
	if (!file)
	{
		cout << "Invalid Entry!" << endl;
		return 0;
	}

	cout <<"tellp:"<< file.tellp() << endl;
	file << "123456789" << endl;
	const auto nPos = file.tellp();
	cout << "tellp:" << nPos << endl;

	for (int i = 32; i < 45;i++)
	{
		file << "Value:" <<setw(3)<< i << "\n";
	}
	file.close();

	std::fstream Rfile(strFileName);
	if (!Rfile)
	{
		cout << "Invalid Entry!" << endl;
		return 0;
	}

	Rfile.seekg(nPos);
	std::string strFilecontent((std::istreambuf_iterator<char>(Rfile)), std::istreambuf_iterator<char>());
	cout << strFilecontent;

	return 1;
}
#endif

//22.超长正整数
#if 0
int main()
{
	string s1, s2;
	cout << "please enter two numbers:" << endl;
	if (cin >> s1 >> s2)
	{
		if (s1.size() > s2.size()){ s2 = string(s1.size() - s2.size(), '0') + s2;}
		else{ s1 = string(s2.size() - s1.size(), '0') + s1;}

		cout << "reset s1 s2:" << endl;
		cout << s1 << endl;
		cout << s2 << endl;

		int carry = 0;
		string he;
		for (int i = s1.size() - 1;i >= 0;i--)
		{
			char c = (carry + s1[i] - '0' + s2[i] - '0') % 10 + '0';
			he = c + he;
			cout << "char is :" << c << ";" << "he is :" << he << endl;
			carry = (carry + s1[i] - '0' + s2[i] - '0') / 10;
		}
		if (carry) he = '1' + he;

		cout <<"the sum is :\n"<< he << endl;
	}
	return 0;
}
#endif

你可能感兴趣的:(算法,c++,开发语言,stl)