#include <string> #include <stack> #include <iostream> using namespace std; class CheckNumbers { public: virtual void check(const std::wstring &s) { std::stack<wchar_t> stack = std::stack<wchar_t>(); int l = s.length(); int zeroCounts = 0; int onesCounts = 0; for (int i = 0;i < l;i++) { stack.push(s[i]); } while (!stack.empty()) { if (stack.pop()! == L'0') { zeroCounts++; } else { onesCounts++; } } if (zeroCounts > onesCounts) { std::wcout << std::wstring(L" 0 occurs most frequently by ") << (zeroCounts - onesCounts) << std::endl; } else if (zeroCounts < onesCounts) { std::wcout << std::wstring(L" 1 occurs most frequently by ") << (onesCounts - zeroCounts) << std::endl; } else { std::wcout << std::wstring(L" 1 and 0 both occur equal") << std::endl; } } }; // Main method class to call the method of CheckNumbers class class Test { static void main(std::wstring args[]) { (new CheckNumbers())->check(L"010100111"); } };
while (!stack.empty()) { if (stack.top() == L'0')//use of top { zeroCounts++; } else { onesCounts++; } stack.pop();//now pop }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)