Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
|
Implementation of a function similar to printf
More...
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
Go to the source code of this file.
Data Structures | |
struct | buffer |
struct used to store character in certain times More... | |
Macros | |
#define | INT_MAX_LENGTH 10 |
for malloc and free functions | |
#define | PRECISION_FOR_FLOAT 8 |
Typedefs | |
typedef struct buffer | Buffer |
struct used to store character in certain times | |
Functions | |
int | power_of_ten (int a) |
int | is_number (char *c) |
Checks if a character is a number. | |
char | get_ch (char *p, Buffer *buffer) |
Returns specific required next character. | |
void | unget_ch (char *c, Buffer *buffer) |
Stores character to the buffer->buffr_char | |
int | get_number_of_digits (int n) |
Calculates the number of digits in a number. | |
void | put_char (char s) |
Prints one character on screen. | |
void | reverse_str (char *p) |
Reverses a string using two pointer algorithm | |
void | print_int_value (int n, int width, int precision) |
void | print_double_value (double dval, int width, int precision) |
The algorithm here is also the same as the print_int_value function. | |
void | print_string (char *p, int width, int precision) |
char * | get_width_and_precision (char *p, Buffer *buffer, int *width, int *precision) |
Takes width and precision specified from the format of the string. | |
void | min_printf (char *fmt,...) |
min_printf is the function same as printf | |
Implementation of a function similar to printf
printf
statement rewritten (as min_printf
) in C without using the stdio.h
library Syntax of min_printf
is same as printf
Currently min_printf handles: Integers, Doubles, floats, characters and strings The format specifiers and escape sequence is the same as for printf
User can also specify the width and precision if required, just like in the case of printf
How to use it:
min_printf()
, and pass required parameters to it#define INT_MAX_LENGTH 10 |
for malloc
and free
functions
for write
function for va_start
and va_arg
functions
char get_ch | ( | char * | p, |
Buffer * | buffer | ||
) |
Returns specific required next character.
p | pointer to a format string of min_printf() |
buffer | struct for checking if buffr_char character is present or not |
buffer->buffr_char
, if buffer->buf_size
is one buffer->buf_size
is zero < Since character is used, this sets buffer->buf_size
to zero
int get_number_of_digits | ( | int | n | ) |
Calculates the number of digits in a number.
n | number whose digits are to be counted |
char * get_width_and_precision | ( | char * | p, |
Buffer * | buffer, | ||
int * | width, | ||
int * | precision | ||
) |
Takes width and precision specified from the format of the string.
p | pointer of the format string |
width | variable in which width will be stored |
precision | variable in which precision will be stored |
int is_number | ( | char * | c | ) |
Checks if a character is a number.
c | character to be checked if it's a number or not |
true
(1) if the character is a number false
(0) if the character is NOT a number void min_printf | ( | char * | fmt, |
... | |||
) |
min_printf is the function same as printf
fmt | format of string |
... | arguments passed according to the format |
int power_of_ten | ( | int | a | ) |
This function return ten to the power a(The parameter specified to it) like: if the parameter specified is 4 i.e. -> power_of_ten(4) is called then this function will return ten to the power four (10000);
a | The power of ten which is to be returned |
< This number will be returned as ten to power of a
void print_double_value | ( | double | dval, |
int | width, | ||
int | precision | ||
) |
The algorithm here is also the same as the print_int_value
function.
First, the digits before decimal is printed by converting the double to int. Then after printed a .
, the double number is subtracted with the integer value of the number, leaving us with 0 before the decimal. Then, we multiply the number with 10 raised to the power precision ( precision means how many digits to be printed after the decimal.) By default, the precision is 8 if it is not specified. Then, the remaining number is printed on the screen.
dval | double number to be printed |
width | similar to width parameter of print_int_value() |
precision | tells the number of digits to be printed after the decimal (By default it is 8) |
void print_int_value | ( | int | n, |
int | width, | ||
int | precision | ||
) |
The algorithm here is to first convert the number into string and then reverse it be passing it to reverse_str function and then printing on the screen
n | Number to be printed |
width | Total characters to be printed (Prints ' ' if (size < width) |
precision | Total character of number to be printed (prints 0 before number if size of number < precision) |
< Used to store number of digits in number
The next two conditions check weather it is required to add blanks before printing the number (ie: width)and is it specified how many zeros to be printed before the number is printed (ie: precision)
void print_string | ( | char * | p, |
int | width, | ||
int | precision | ||
) |
First size of the string is calculated to check whether width and precision are to be taken into account or not. Then, the string is printed in accordingly.
p | pointer to string to be printed |
width | if (width > sizeof string) then, blanks will be printed before sting to cover up the width |
precision | total characters of the string to be printed (prints the whole string if 0 or greater than size of string) |
void put_char | ( | char | s | ) |
void reverse_str | ( | char * | p | ) |
Reverses a string using two pointer algorithm
p | pointer to the string which is to be reversed |
void unget_ch | ( | char * | c, |
Buffer * | buffer | ||
) |
Stores character to the buffer->buffr_char
c | character to be stored in the buffer->buffr_char |
buffer | struct where character will be stored |