Skip to content

Instantly share code, notes, and snippets.

@inish777
Created June 27, 2016 10:06
Show Gist options
  • Save inish777/325379654a79083cb12a01edf83db625 to your computer and use it in GitHub Desktop.
Save inish777/325379654a79083cb12a01edf83db625 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
void select_sort_rows (int** array, int n)
{
int x, y;
int min = 0;
for (x = 0; x < n - 1; x++) {
min = x;
for (y = x; y < n; y++) {
if (array[y][0] < array[min][0])
min = y;
}
if (min != x) {
int* t = array[x];
array[x] = array[min];
array[min] = t;
}
}
}
void select_sort_columns (int** array, int n, int m)
{
int x, y, j;
int min = 0;
for (j = 0; j < n; j++) {
for (x = 0; x < m - 1; x++) {
min = x;
for (y = x; y < m; y++) {
if (array[j][y] < array[j][min])
min = y;
}
if (min != x) {
int t = array[j][x];
array[j][x] = array[j][min];
array[j][min] = t;
}
}
}
}
void print_array (int** array, int n, int m)
{
int x, y;
for (x = 0; x < n; x ++) {
for (y = 0; y < m; y++) {
printf("%d ", array[x][y]);
}
printf ("\n");
}
}
int** allocate (int* n, int* m)
{
printf ("Введите количество строк и столбцов массива через пробел\n");
scanf ("%d %d", n, m);
int** array = (int**)malloc (sizeof(int*) * (*n));
int x;
for (x = 0; x < *n; x++) {
array[x] = (int*) malloc (sizeof(int) * (*m));
}
return array;
}
void file_input ()
{
int n, m;
int** array = allocate (&n, &m);
FILE* in = fopen ("textfile", "r");
if (! in) {
printf ("textfile not found\f");
exit (-1);
}
char buf[256];
int x, y = 0;
char* token;
for (x = 0; x < n; x++) {
fgets(buf, 255, in);
token = strtok(buf, " \n");
while(token != NULL) {
sscanf(token, "%d", &array[x][y]);
token = strtok(NULL, " \n");
y ++;
}
y = 0;
}
select_sort_columns(array, n, m);
select_sort_rows(array, n);
print_array(array, n, m);
}
void binary_input ()
{
int n, m;
int** array = allocate (&n, &m);
FILE* file = fopen("binary.txt", "rb");
if (! file) {
printf ("binary.txt not found\f");
exit (-1);
}
int x;
for (x = 0; x < n; x++) {
fread(array[x], sizeof(int), m, file);
}
fclose(file);
select_sort_columns(array, n, m);
select_sort_rows(array, n);
print_array(array, n, m);
}
void keyboard_input ()
{
int n, m;
int** array = allocate (&n, &m);
int x, y;
for (x = 0; x < n; x++) {
for (y = 0; y < m; y++) {
scanf ("%d", &array[x][y]);
}
}
select_sort_columns(array, n, m);
select_sort_rows(array, n);
print_array (array, n, m);
}
void random_input()
{
int n, m;
int** array = allocate (&n, &m);
srand (time ());
int x,y;
for(x = 0; x < n; x++) {
for (y = 0; y < m; y++) {
array[x][y] = 1 + rand() % 20;
}
}
select_sort_columns(array, n, m);
select_sort_rows(array, n);
print_array (array, n, m);
}
int main (int argc, char** argv)
{
setlocale(LC_ALL, "");
if (argc == 1) {
printf ("Не задан тип ввода\n");
exit (-1);
}
switch (*argv[1]) {
case 'b':
binary_input ();
break;
case 'k':
keyboard_input ();
break;
case 'f':
file_input();
break;
case 'r':
random_input ();
break;
default:
printf ("Неизвестный тип ввода\n");
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment