You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
2.9 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "my_funcs.h"
#define typed_scan(type) for (unsigned int i = 0; i < size; i++) \
{ \
if (scanf(specifier, ((type *) p_array) + i)) \
scan_el_cnt++; \
}
#define typed_print(type) for (unsigned int i = 0; i < size; i++) printf(specifier, *(((type *) p_array) + i));
int scan_array(void * p_array, unsigned int size, char * specifier)
{
int scan_el_cnt = 0;
size_t spec_size = strlen(specifier);
if (spec_size > 1 && specifier[0] == '%')
{
switch (specifier[1])
{
case 'c':
typed_scan(char);
break;
case 'd':
typed_scan(int);
break;
case 'f':
typed_scan(float);
break;
case 'l':
{
if (spec_size > 2)
{
switch (specifier[2])
{
case 'l':
typed_scan(long);
break;
case 'd':
typed_scan(double);
break;
}
}
}
}
}
return scan_el_cnt;
}
void print_array(void * p_array, unsigned int size, char * type, char * specifier)
{
switch (type[0])
{
case 'c':
typed_print(char);
break;
case 'd':
{
typed_print(int);
break;
} break;
case 'f':
{
typed_print(float);
break;
} break;
case 'l':
{
if (strlen(type) > 1)
{
switch (type[1])
{
case 'd':
typed_print(long);
break;
case 'f':
typed_print(double);
break;
}
}
} break;
}
}
void print_array_table(void * p_array, unsigned int size, char * type, char * specifier, unsigned int colomn)
{
unsigned int type_size = 0;
switch (type[0])
{
case 'c':
type_size = sizeof(char);
break;
case 'd':
type_size = sizeof(int);
break;
case 'f':
type_size = sizeof(float);
break;
case 'l':
{
if (strlen(type) > 1)
{
if (type[1] == 'd')
type_size = sizeof(long);
if (type[1] == 'f')
type_size = sizeof(double);
}
} break;
}
if (type_size) {
char * new_p = p_array;
size_t row_count = size / colomn;
size_t last_row_size = size % colomn;
for (unsigned int i = 0; i < row_count; i++)
{
print_array(new_p, colomn, type, specifier);
printf("\n");
new_p += colomn * type_size;
}
if (last_row_size) {
print_array(new_p, last_row_size, type, specifier);
printf("\n");
}
}
}
void clear_input(void)
{
scanf("%*[^\n]");
}