Kodi PHP:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct{
char* name;
int num;
}record;
void _initialize(record *);
record ** read(FILE* , int * no_records);
void write(FILE* , record **, int);
void print_record(FILE* , record * r);
record ** selection_sort(record **, int);
int print_str(FILE *, char *);
void __delete(record **, int);
int compare(record ** , int, int );
void swap(record ** , int, int);
int name_acceptable(char c);
int num_acceptable(char c);
void _initialize(record * r)
{ r->name= NULL; r->num = 0; }
/* reading */
record ** read(FILE* in, int * no_records){
record ** records=NULL;
while( !feof(in) ){
record *r=(record*)malloc(sizeof(record));
char * buffer= NULL, c=0, length_name=0, length_buffer=0, name_allocated=0, num_allocated=0;
_initialize(r);
while( (c=fgetc(in)), name_acceptable(c) ){
name_allocated++;
r->name =(char *)realloc(r->name, (length_name + 2)*sizeof(char));
*(r->name+length_name++) = c;
}
while( (c=fgetc(in)), num_acceptable(c)){
num_allocated++;
buffer =(char *)realloc(buffer, (length_buffer + 2)*sizeof(char));
*(buffer+length_buffer++) = c;
}
if(name_allocated && num_allocated){
*(r->name+length_name) = '\0';
*(buffer+length_buffer) = '\0';
r->num= atoi(buffer);
records=(record**)realloc(records, (*no_records + 1)*sizeof(record*));
*(records + *no_records) = r;
*no_records += 1;
}
free(buffer);
r= NULL; buffer=NULL;
length_name= length_buffer = name_allocated = num_allocated = 0;
}
return records;
}
int name_acceptable(char c)
{ return ( (c>='a' && c<='z') || (c>='A' && c<='Z') || c == ' ' ) && c != '\t'; }
int num_acceptable(char c)
{ return ( c>='0' && c<='9' ) && c!='\n'; }
/* writing */
void write(FILE * f, record ** records, int no_records){
int i;for(i=0; i < no_records ; i++)
if( records + i) print_record(f, *(records + i));
}
void print_record(FILE* f, record * r){
print_str(f, r->name);
fprintf(f, "\t%d\n", r->num);
}
int print_str(FILE* f, char * str){
if(str && *str!='\0'){
fprintf(f,"%c", *str);
print_str(f, str+1);
}
}
/* sorting */
record ** selection_sort(record ** records, int no_records){
int tmp_min, i, j;
record ** sorted_records=(record**)malloc(no_records*sizeof(record *));
for(i=0; i< no_records ; i++)
sorted_records [i] = records[i];
for( i = 0; i < no_records; i++) {
tmp_min = i;
for( j = i+1; j < no_records; j++) {
if (compare(sorted_records, j, tmp_min)< 0)
tmp_min=j;
}
if (tmp_min != i)
swap(sorted_records, tmp_min, i);
}
return sorted_records;
}
int compare(record ** sorted_records, int rank1, int rank2){
return strcmp( sorted_records[rank1]->name, sorted_records[rank2]->name);
}
void swap(record ** sorted_records, int rank1, int rank2){
record * tmp = *(sorted_records + rank1);
*(sorted_records + rank1) = *(sorted_records + rank2);
*(sorted_records + rank2) = tmp;
}
void __delete(record ** records, int no_records){
int i; for(i=0; i< no_records ; i++)
if(records+i) free(*(records + i));
}
int main(void){
clock_t a= clock();
FILE* in= fopen("input.txt","r");
FILE* out= fopen("output.txt","w");
int no_records=0;
record ** records = read(in, &no_records);
record ** sorted_records = selection_sort(records, no_records);
write(out, sorted_records, no_records);
__delete(records, no_records);
__delete(sorted_records, no_records);
fclose(in); fclose(out);
printf("Run time: %ld\n", clock() -a);
return 0;
}
Krijoni Kontakt