This program is relatively small. It runs in TC2 and implements a variety of hexadecimal conversions. I checked that the file was modified on February, which was probably written when I was a beginner in C language, I remember that I wrote a large number addition C program in the same period, which is a little interesting. I posted it to make everyone laugh.
PS: I have also posted some debugging code in the program. After a long time, I cannot remember what the purpose is, but the program is not difficult to understand. I am lazy and will not study the comments.
/* Hexadecimal conversion
Author: Zhou Zhiming
*/
# Include <stdio. h>
# Include <stdlib. h>
# Include <process. h>
# Include <string. h>
# Include <conio. h>
# Include <graphics. h>
# Define stack_init_size 10
# Define size_increment 5
# Define array_size 33
# Define null 0
# Define OK 1
# Define error 0
# Define overflow 0
/* # Define leftup 201
# Define leftdown 200
# Define Line 205
# Define rightup 187
# Define rightdown 188 */
/******************************* Typedef ******* ***********************/
Typedef int selemtype, status;
Typedef struct {
Selemtype * base;
Selemtype * top;
Int stacksize;
} Sqstack, * sqstack;
/**************************** Data_structure ********* *********************/
Status init_stack (sqstack s ){
(* S). base = (selemtype *) malloc (stack_init_size * sizeof (selemtype ));
If (! (* S). Base) Exit (overflow );
(* S). Top = (* s). base;
(* S). stacksize = stack_init_size;
Return OK;
}/* Init */
Status push_stack (sqstack S, selemtype e ){
If (* s). Base + (* s). stacksize = (* s). Top ){
(* S). base = (selemtype *) realloc (* s). Base, (size_increment + (* s). stacksize) * sizeof (selemtype ));
(* S). Top = (* s). Base + (* s). stacksize;
(* S). stacksize + = size_increment;
}
* (* S). Top = E;
(* S). Top + = 1;
Return OK;
}/* Push */
Status pop_stack (sqstack S, selemtype * P ){
If (* s). base = (* s). Top) return error;
* P = * (* s). Top-1 );
(* S). Top-= 1;
Return OK;
}/* Pop */
Status gettop_stack (sqstack S, selemtype * E ){
If (S. base = S. Top) return error;
* E = * (S. Top-1 );
Return OK;
}/* Gettop */
Status length_stack (sqstack s ){
Int y;
If (S. base = S. Top) return 0;
/* Size = sizeof (selemtype );*/
Y = S. top-s.base;
Return y;
}/* Length */
Status free_stack (sqstack s ){
Free (* s). Base );
(* S). Top = (* s). base = NULL;
Return OK;
}/* Free */
******** ****************************/
Status transform (Num, sys, S)
Unsigned long num;
Int sys;
Sqstack S;
{
Unsigned long dividend = num;
Int divisor = sys, REM;
/* Int I, * P = NULL, E, length ;*/
If (! Init_stack (s) Exit (0 );
/**********-Algorithm -***********/
Do {
If (dividend <sys) {REM = dividend;
Push_stack (S, REM );
Break ;}
Else {
Rem = dividend % divisor;
Push_stack (S, REM );
Dividend = (dividend-REM)/divisor;/* dividend = dividend/divisor ;*/
}/* Else */
} While (dividend );
}/* // Transform //*/
/******************************/
Status output (sqstack s ){
Int I, length, * P = NULL;
Length = length_stack (* s);/* error :*/
For (I = 1; I <= length; I ++) {/* for (I = 1; I <= length_stack (* s); I ++) {....} */
Pop_stack (S, P );/**/
If (* P <10) printf ("% d", * P );
Else printf ("% C", * P + 55 );
}
Free_stack (s );
Return OK;
}/* // Output //*/
/*************************************** ********/
Status output_bm (char * mark, sqstack s ){
Int I, j, length, * P = NULL, zerro = 0;
Int array [32];
Length = length_stack (* s);/* printf ("% d", length );*/
If (* mark = 45 ){
For (I = 0; I <length; I ++ ){
Pop_stack (S, P );
If (* P) {array [I] = 0; zerro = 1 ;}
Else array [I] = 1;
/* Printf ("% d", array [I]); */
}
/* Length = length_array (array); printf ("% d", length );*/
If (array [length-1] = 0) array [length-1] = 1;
Else {array [length-1] = 0;
For (I = 1; I <length; I ++ ){
Array [length-1-i] + = 1;
If (array [length-1-i] = 2)
Array [length-1-i] = 0;
Else break;
}/* */
}/* Else */
If (length = 32) array [0] = 1;
/* Length = length_array (array); printf ("% d", length );*/
For (I = 1, j = 1; I <= 32-length; I ++, J ++ ){
If (! Zerro) putchar ('0');/********* attention * P ********/
Else putchar ('1 ');
If (! (J % 8) putchar ('');
/* Putchar ('1 ');*/
}
For (I = 0; I <length; I ++, J ++ ){
Printf ("% d", array [I]);
If (! (J) % 8) putchar ('');
/* Printf ("% d", array [I]); */
}
}/* If */
Else {
For (I = 1, j = 1; I <= 32-length; I ++, J ++ ){
Putchar ('0 ');
If (J % 8 = 0) putchar ('');
}
For (I = 1; I <= length; I ++, J ++ ){
Pop_stack (S, P );
Printf ("% d", * P );
If (J % 8 = 0) putchar ('');
}
}/* Else */
Free_stack (s );
}/* // Output //*/
/*************************************** ****************/
Status length_array (int * P) {/* When array [I]! = 0, the function is OK ;*/
Int I;
If (P = NULL) Exit (0 );
For (I = 0; * P ++! = NULL; I ++ );
Return I;
}/* Lengtharray */
Status input_transd (INT sys, unsigned long * P, char * mark ){
Int I, J;
Unsigned int length;
Unsigned long sum = 0, POW;
Char array_char [array_size];
Int array_int [array_size];
Scanf ("% s", array_char );
/* For (I = 0; I <array_size; I ++ ){
Array_char [I] = getchar ();
If (array_char [I] = '/N') break;
}*/
Length = strlen (array_char );
If (array_char [0] = 45 ){
* Mark = array_char [0];
For (I = 0; I <length-1; I ++)
Array_char [I] = array_char [I + 1];
Array_char [length-1] = '/0';/* The statement missing wocould attract error! */
}
Length = strlen (array_char );
For (I = 0; I <length; I ++ ){
If (array_char [length-1-i]> = 48 & array_char [length-1-i] <= 57)
Array_int [I] = array_char [length-1-i]-48;
Else if (array_char [length-1-i]> = 65 & array_char [length-1-i] <= 90)
Array_int [I] = array_char [length-1-i]-55;
Else if (array_char [length-1-i]> = 97 & array_char [length-1-i] <= 122)
Array_int [I] = array_char [length-1-i]-87;
Else exit (0 );
}
For (I = 0; I <length; I ++ ){
If (array_int [I]> = sys ){
Gotoxy (20,13); printf ("inupt error! ");
Gotoxy (20, 15); printf ("inupt error! ");
Gotoxy (20, 17); printf ("inupt error! ");
Gotoxy (20, 19); printf ("inupt error! ");
Gotoxy (20, 22 );
Exit (0 );
}
/* If ()*/
For (j = 1, POW = 1; j <= I; j ++)/** error: For (j = 1; j <= I; j ++) {...} */
Pow * = sys;
Sum + = array_int [I] * POW;
}
* P = sum;
Return OK;
}/* Input */
/****************************** Main ******* *************************/
Main ()
{Sqstack S;
Int sys;
Int I, j, length;
Char mark = 0;
Unsigned long p_num, * P = NULL;
Char array [32];
Do {
Clrscr ();
Textmode (C80 );
Textbackground (2 );
Textcolor (14 );
For (I = 1; I <= 24; I ++ ){
/* Gotoxy (1, I );
Printf ("% d", I );*/
For (j = 3; j <80; j ++ ){
/* Gotoxy (J, 1 );
Printf ("% d", J );*/
Gotoxy (J, I );
Putch ('');
}
}
Gotoxy (30,4 );
Printf ("number transform ");
Textattr (Red | blink | blue * 16 );
For (I = 8; I <= 20; I ++)
For (j = 6; j <= 52; j ++ ){
Gotoxy (7, I );
Putch (186 );
Gotoxy (53, I );
Putch (186 );
If (I % 2 = 0 ){
Gotoxy (J, I );
Putch (205 );
}
}
Gotoxy (9,9); printf ("select number system :");
Gotoxy (9,11); printf ("inport :");
Gotoxy (9,13); printf ("binary -- 2 :");
Gotoxy (9,15); printf ("octal --- 8 :");
Gotoxy (9,17); printf ("Dec ---- 10 :");
Gotoxy (9,19); printf ("hex ---- 16 :");
Gotoxy (); printf ("select from ");
Gotoxy (56,10); printf ("input confine :");
Gotoxy (56,11); printf ("binary 0 to 111 .. 1 (32 )");
Gotoxy (56,12); printf ("octal 0 to 37777777777 ");
Gotoxy (56,13); printf ("Dec 0 to 4294967295 ");
Gotoxy (56,14); printf ("hex 0 to ffffffff ");
Gotoxy (30,9 );
Scanf ("% d", & Sys );
Switch (sys ){
Case 2:/* gotoxy (17,11 );
Input_transd (sys, P );
Break ;*/
Case 8:/* gotoxy (17,11 );
Input_transd (sys, P );
Break ;*/
Case 10:
Case 16: gotoxy (17,11 );
Input_transd (sys, P, & mark );
P_num = * P;
Break;
Default: exit (0 );
}
Gotoxy (20, 13);/* printf ("% lu", * P );*/
Transform (* P, 2, & S );
/* Length = length_stack (s );*/
If (mark = 45) putchar (Mark );
/* {Putchar ('1 ');
For (I = 1; I <32-length; I ++)
Putchar ('0 ');
}
Else for (I = 1; I <= 32-length; I ++)
Putchar ('0 ');*/
Output (& S );
Gotoxy (20, 15);/* printf ("% lu, % lu", * P, p_num );*/
Transform (p_num, 8, & S );
If (mark = 45) putchar (Mark );
Output (& S );
Gotoxy (20, 17 );
Transform (p_num, 10, & S );
If (mark = 45) putchar (Mark );
Output (& S );
Gotoxy (20, 19 );
Transform (p_num, 16, & S );
If (mark = 45) putchar (Mark );
Output (& S );
Gotoxy (16, 21 );
Printf ("BM:"); transform (p_num, 2, & S );
/* If (mark = 45) putchar (Mark );*/
Output_bm (& mark, & S );
Gotoxy (20, 23 );
Printf ("continiue? Yes ---> 1, No ---> 0 :");
Scanf ("% d", & mark );
} While (mark! = 0 );
Restorecrtmode ();
}/* Main */
--------
Site recommended by bloggers:
Www.itstand.net
"Itstand. Net-Focus on the Chinese IT community for software development, providing the most comprehensive information dissemination and service platform for software developers"
--------