The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
untitled C Code
Posted by: dan | January 2, 2010 @ 10:41pm
C Code
[
Download
]
#include <stdio.h> #include <string.h> #define MAXLEN 1000 #define MAXLINES 5000 char *lineptr[MAXLINES]; void writelines(char *[], int nlines); void writelines1(char *[], int nlines); int readlines(char *[], int); int readlines1(char *[], int); void qsort(char *[], int, int); main() { int nlines; char line[MAXLEN]; char *lines[MAXLINES]; //read lines and print them while ((nlines = readlines1(lines, MAXLINES)) > 0) writelines(lines, nlines); } //print input lines void writelines(char *lines[], int nlines) { int i; for (i = 0; i < nlines; ++i) printf("%s\n", lines[i]); } //print input lines void writelines1(char *lines[], int nlines) { int i; for (i = 0; i < nlines; ++i) printf("%s\n", lines[i]); } void my_strcpy(char *, char *); int readline(char [], int); char *alloc(int); //get input lines int readlines(char *lines[], int maxlines) { int len, nlines; char *p, line[MAXLEN]; nlines = 0; while ((len = readline(line, MAXLEN)) > 0) if (nlines >= maxlines || (p = alloc(len)) == NULL) return -1; else { line[len-1] = '\0'; my_strcpy(p, line); lines[nlines++] = p; } return nlines; } //get input lines int readlines1(char *lines[], int maxlines) { int len, nlines; char *p, line[MAXLEN]; char b[MAXLEN]; p = b; nlines = 0; while ((len = readline(line, MAXLEN)) > 0) if (nlines >= maxlines) return -1; else { line[len-1] = '\0'; my_strcpy(p, line); lines[nlines++] = p; } return nlines; } //copy t to s void my_strcpy(char *s, char *t) { while (*t != '\0') *s++ = *t++; *s = '\0'; } //get input line int readline(char s[], int max) { int c, i; for (i = 0; i<max-1 && (c = getchar())!=EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return i; } #define ALLOCSIZE 1000 static char allocbuf[ALLOCSIZE]; static char *allocp = allocbuf; //allocate storage char *alloc(int n) { if (allocbuf + ALLOCSIZE - allocp >= n) { allocp += n; return allocp - n; } else return 0; } int my_strcmp(char *, char *); void swap(char *[], int, int); //quicksort lines void qsort(char *v[], int left, int right) { int i, last; if (left >= right) return; swap(v, left, (left + right) / 2); last = left; for (i = left + 1; i <= right; ++i) if (my_strcmp(v[i], v[left]) < 0) swap(v, ++last, i); swap(v, left, last); qsort(v, left, last - 1); qsort(v, last + 1, right); } //compare lines int my_strcmp(char *s, char *t) { while (*s == *t) { ++s, ++t; if (*s == '\0') return 0; } return *s - *t; } //swap lines void swap(char *v[], int i, int j) { char *temp = v[i]; v[i] = v[j]; v[j] = temp; }
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.73 / 8
45 Votes
http://codebin.yi.org/442
page generated in 0.01 seconds