-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
147 lines (126 loc) · 3.86 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*H**********************************************************************
* AUTHOR : Razie Hyria START DATE : JAN 12TH 2023
* FILENAME : ConvDecimal_to_OtherBasesc
* COURSE NAME: CMPSC 472 Section 001: Operating Systems
* SEMESTER: SPRING 2023
*
* DESCRIPTION :
* A C Programming Lab to Convert Decimal to Hex, Binary, and Octal.
*
* PUBLIC FUNCTIONS :
* int Binary converter
* int Hexadecimal converter
* int Octal converter
*
* NOTES :
* These functions feature C Arithmetic Operators
* Copyright PSU Abington
*
*
*
* CHANGES :
*
* REF NO VERSION DATE WHO DETAIL
*
*H*/
#include <stdio.h>
#include <ctype.h> // header file for checking character types for user input
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
// defining the min and max input values for cleaner code
#define min -2
#define max 2000000
#define exit -1
int userInput;
bool isRunning = true;
void flush_stream();
void ConvDecimal(int decimal, int base);
// conv function
/* q = 200
while (quotient > 0)
r = q%2
q = q/2
convDec(int x, int divbase);
Using formula from in class discussion, and classmate Anthonys suggestion/solution
using format identifires for printf
*/
void ConvDecimal(int decimal, int base)
{
// storing the remainder
int remainder = decimal%base;
if(decimal == 0) // exist clause for the while loop
{
printf("0"); //printing the value
return; // exiting the function
}
// recursively calls itself until the value is 0, and then prints the solution backwards (due to princ. of recurrsion)
ConvDecimal(decimal/base,base);
if(remainder < 10) // for octol and binary bases
{
printf("%d", remainder);
}
else // hexadecimal bases check using the format identifiers mentioned above
{
printf("%c", remainder-10+'A');
}
}
/*
The scanf buffer is used with the %d integer format specfifier so that the program or scanner knows to accept
an integer. When this doesnt occur, the program gets caught in an infinite loop, since the scanner doesnt know what to do with that
unexpected value. The function below "flushes" the stdin input stream so that the user can enter another value
Borrowed from StackOverflow:
https://stackoverflow.com/a/26081123/17628672
*/
void flush_stream(void)// clearing the scanf buffer
{
int c;
while ( (c = getchar()) != '\n' && c != EOF ) { }
}
int main()
{
// welcome message
printf("\nWelcome to the Decimal converter! Type in -1 to exit. \n");
printf("Your number will be converted into Binary, Hex, and Octal! \n");
// will loop until isRunning = false
while(isRunning)
{
printf("Input an integer from 0 -> 2,000,000 to convert: \n"); //ask user input
bool isValidating = true;
while(isValidating)
{
if(scanf("%d", &userInput) != 1) // checks to see if the character is not an int
{
fputs("\nInvalid entry! Try Again! \n", stderr);
flush_stream();
}
else if(userInput <= min) // validation process to check if its less than 0
{
printf("%d Too Small! Try Again! \n", userInput);
}
else if (userInput > max) // checks if greater than max
{
printf("%d Too big! Try Again!\n", userInput);
}
else if (userInput == exit) // exit condition
{
printf("Thank you! Come again.");
isValidating = false; // exits current program
}
else // otherwise call on the ConvDec function and return output to user
{
printf("\nConverting %d..\n", userInput);
printf("\nBinary: ");
ConvDecimal(userInput,2);
printf("\nHexadecimal: ");
ConvDecimal(userInput,16);
printf("\nOctal: ");
ConvDecimal(userInput,8);
printf("\nInsert another number to try again or -1 to exit: ");
}
} // is validating
isRunning = false; // used to exit function
} // is running
}
// end of main function