Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question: The program sorts user input in ascending order. It accepts a -n argument to indicate that the input will be numeric. The default is

Question:

The program sorts user input in ascending order. It accepts a -n argument to indicate that the input will be numeric. The default is a string of characters. Modify the program to handle a -r flag, which indicates sorting in reverse (decreasing) order. Be sure that -r work with -n.

code:

#include #include #include

#define MAXLINES 5000 char *lineptr[MAXLINES];

int readlines(char *lineptr[], int nlines); void writelines(char *linptr[], int nlines);

void qsort_(void *lineptr[], int left, int right, int (*comp)(void *, void *));

int numcmp(char *, char *);

int main(int argc, char* argv[]) { int nlines; int numeric =0; if(argc > 1 && strcmp(argv[1], "-n") ==0) numeric=1; if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { qsort_((void **) lineptr, 0, nlines-1, (int (*)(void*, void*))(numeric ? numcmp : strcmp)); writelines(lineptr, nlines); return 0; } else { printf("input too big to sort "); return 1; }

return 0; }

void qsort_(void *v[], int left, int right, int (*comp)(void *, void *)) { int i, last; void swap(void *v[], int, int); if (left >= right) return; swap(v, left, (left +right)/2); last = left; for (i = left +1; i

int numcmp(char *s1, char *s2){ double v1, v2; v1 = atof(s1); v2 = atof(s2); if (v1v2) return 1; else return 0; }

void swap(void *v[], int i, int j){ void *temp; temp = v[i]; v[i] = v[j]; v[j] = temp; }

#define MAXLEN 1000 int getline_(char *, int); char *alloc(int);

int readlines(char *lineptr[], int maxlines){ int len, nlines; char *p, line[MAXLEN]; nlines = 0; while ((len = getline_(line, MAXLEN)) > 0){ //printf("%s\t%d ", line, len); if (nlines >= maxlines || (p = alloc(len)) == NULL){ return -1; } else{ line[len - 1] = '\0'; strcpy(p, line); lineptr[nlines++] = p; } //printf("p=%s\t%p ", p, p); } return nlines; }

void writelines(char *lineptr[], int nlines){ int i; while (nlines-- > 0) printf ("%s ", *lineptr++); }

int getline_(char *s, int lim){ int c, i; for (i=0; i

#define ALLOCSIZE 10000

static char allocbuf[ALLOCSIZE]; static char* allocp = allocbuf;

char *alloc(int n){ //return point to n characters if (allocbuf + ALLOCSIZE - allocp >= n){ allocp+=n; return allocp - n; } /ot enough room in the buffer return 0; }

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

I am as specific as possible, this was actually the question and these changes are necessary. So I explained what format the code was in and what changes are required.

9 1 #include 2 #include 3 #include 4 5 #define MAXLINES 5000 6 char *lineptr[MAXLINES]; 7 8 int readlines(char *lineptr[], int nlines); 9 void writelines(char *linptr[], int nlines); 10 11 void asort_(void *lineptr[], int left, int right, 12 int ("comp)(void *, void *)); 13 14 int numcmp(char char *); 15 16 int main(int argc, char* argv[]) 17 { int nlines; 19 int numeric =0; 20 21 if(argc > 1 && strcmp(argv[1], "-n") ==0) 22 numeric=1; 23 if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { 24 qsort_((void **) lineptr, 0, nlines-1, 25 (int (*)(void*, void)) (numeric ? numcmp : strcmp)); 26 writelines(lineptr, nlines); 27 return 0; 28 } else { 29 printf("input too big to sort "); 18 42 43 30 return 1; 31 } 32 33 return 0; 34 } 35 36 void asort_(void *v[], int left, int right, 37 int (*comp)(void *, void *)) 38 - { 39 int i, last; 40 void swap(void *v[], int, int); 41 if (left >= right) return; 44 swap(v, left, (left +right)/2); 45 last = left; 46 for (i = left +1; i 0){ 85 //printf("%s\t%d ", line, len); 86 if (nlines >= maxlines 11 (p = alloc(len)) 87 return -1; NULL) { 88 } 89 else{ 90 line[len - 1] = '\0'; 91 strcpcp, line); 92 lineptr[nlines++] = p; 93 } } 94 //printf("p=%s\t% ", p, p); 95 } 96 return nlines; 97 } 98 99 void writelines(char *lineptr[], int nlines){ 100 int i; 101 102 while (nlines-- > 0) 103 printf ("%s ", *lineptr++); 104 105} 106 107. int getline_(char *s, int lim){ 108 int c, i; 109 for (i=0; i= 128 allocp+=n; 129 return allocp - n; 130 } 131 132 /ot enough room in the buffer 133 return 0; 134 } n) {

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands On Database

Authors: Steve Conger

1st Edition

013610827X, 978-0136108276

More Books

Students also viewed these Databases questions