9.50
int main() { vector<string > vecStr = {"12","3","4","5"}; int theSum = 0; for(auto str:vecStr){ int tmpInt = stoi(str); theSum += tmpInt; } cout<< theSum << endl; for (;;); return 0; }
int main() { vector<string > vecStr = {"12.1","3.2","4.3","6.5"}; float theSum = 0; for(auto str:vecStr){ float tmpInt = stof(str); theSum += tmpInt; } cout<< theSum << endl; for (;;); return 0; }
9.51
Date::Date(const string& str) { const string numbers = "0123456789"; int number_begin = 0; int number_end = 0; number_begin = str.find_first_of(numbers, number_begin); number_end = str.find_first_not_of(numbers, number_end); string dayStr(str, number_begin, number_end - number_begin); day = stoi(dayStr); number_begin++; number_end++; number_begin = str.find_first_of(numbers, number_begin); number_end = str.find_first_not_of(numbers, number_end); string monthStr(str, number_begin, number_end - number_begin); month = stoi(monthStr); number_begin++; number_end++; number_begin = str.find_first_of(numbers, number_begin); number_end = str.find_first_not_of(numbers, number_end); if(number_end == string::npos){ number_end = str.size(); } string yearStr(str, number_begin, number_end - number_begin); year = stoi(yearStr); }
9.51
class Date { public: Date() = default; Date(const string&) ; int year; int month; int day; }; Date::Date(const string& str) { const string numbers = "0123456789"; int number_begin = 0; int number_end = 0; number_begin = str.find_first_of(numbers, number_begin); number_end = str.find_first_not_of(numbers, number_end); string dayStr(str, number_begin, number_end - number_begin); day = stoi(dayStr); number_begin++; number_end++; number_begin = str.find_first_of(numbers, number_begin); number_end = str.find_first_not_of(numbers, number_end); string monthStr(str, number_begin, number_end - number_begin); month = stoi(monthStr); number_begin++; number_end++; number_begin = str.find_first_of(numbers, number_begin); number_end = str.find_first_not_of(numbers, number_end); if(number_end == string::npos){ number_end = str.size(); } string yearStr(str, number_begin, number_end - number_begin); year = stoi(yearStr); }
9.52
仅以加法为例
int getSum (const string& left, const string& right){ return stoi(left) + stoi(right); } int main() { string theExpression = "1+(2+3)+4"; stack<string > sta; int meet = 0; for(auto tmp : theExpression){ if(tmp == '('){ meet = 1; } if (tmp == ')') { sta.push(string(1, tmp)); meet = 0; } if(meet){ sta.push(string(1, tmp)); } } string left, right, symbol; int popState = 0; while (!sta.empty()) { if(sta.top().find(" ") != string::npos){ sta.pop(); continue; } if(sta.top().find_first_of("0123456789") == string::npos){ if (sta.top() != "(" && sta.top() != ")") { symbol+= sta.top(); } sta.pop(); popState++; continue; } if(popState == 1){ right += sta.top(); } if(popState == 2){ left += sta.top(); } sta.pop(); } if(symbol == "+"){ sta.push(to_string(getSum(left,right))); } cout << sta.top() << endl; for (;;); return 0; }