Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a complete C code that uses stack to realize binary conversion to hexadecimal

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how to write a complete C code that uses the stack to convert hexadecimal to hexadecimal. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

/ * convert binary to decimal * / # include#include#define INCREMENT 8typedef char Elemtype;typedef struct Stack {Elemtype * top; Elemtype * base; int stackSize;} Stack;Stack * initStack (int n) / / stack initialization function {Stack * s = (Stack *) malloc (sizeof (Stack)) S-> base = (Elemtype *) malloc (sizeof (Elemtype) * n); if (s-> base = = NULL) exit (0); s-> top = s-> base; s-> stackSize = n; return s } void Push (Stack * s, Elemtype e) / / stack function {if (s-> top-s-> base > = s-> stackSize) {s-> base = (Elemtype *) realloc (s-> base, s-> stackSize + INCREMENT * sizeof (Elemtype)); if (! s-> base) exit (0); s-> top = s-> base + s-> stackSize S-> stackSize = s-> stackSize + INCREMENT;} * (s-> top) = e; s-> top++;} Elemtype Pop (Stack * s) / / unstack function {if (s-> top = = s-> base) exit (0); return (*-- (s-> top)) } int StackLen (Stack * s) / / find the number of elements in the stack {return (s-> top-s-> base);} int main () {int length; int i; short int sum = 0; int index; int temp; Elemtype element; Stack * s = initStack (16); Stack * p = initStack (8); printf ("Please enter binary numbers:\ n") Scanf ("% c", & element); while (element! ='\ n') {/ / Stack the binary number in character type, enter Push (s, element) at the end of the carriage return number; scanf ("% c", & element);} length = StackLen (s) While (StackLen (s)) {for (I ='0' & & temp)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report