From ff38d0b31445834b1387fe2f514d3ad76b776ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=A1=E5=9D=82=E6=98=B4?= Date: Wed, 23 Oct 2024 21:27:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E9=A2=98=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework3/Stack.cpp | 1 + homework3/test1.cpp | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/homework3/Stack.cpp b/homework3/Stack.cpp index 66c3450..b6766df 100644 --- a/homework3/Stack.cpp +++ b/homework3/Stack.cpp @@ -37,5 +37,6 @@ int pop(stack &s) { return NULL; } int e = s.data[s.top]; + s.top--; return e; } diff --git a/homework3/test1.cpp b/homework3/test1.cpp index 7a19bbc..d6f5983 100644 --- a/homework3/test1.cpp +++ b/homework3/test1.cpp @@ -2,7 +2,23 @@ #include using namespace std; -int main() { +string getBinary(int i) { + stack s; + initStack(s); + while (i != 0) { + push(s, i % 2); + i /= 2; + } + string str = ""; + while (!isEmpty(s)) { + str += pop(s) + '0'; + } + return str; +} +int main() { + int decimal; + cin >> decimal; + cout << getBinary(decimal) << endl;; return 0; }