Write a program that, given a list of names, prints the names in order,one name per line. Names are provided as command-line arguments. Each
name is a first name followed by a last name; the order of names is alphabetical by last name, then alphabetical by first name if the last
names match. You may assume that all names are in uppercase. For example, yourprog MICHAEL DOUGLAS CHARLIE SHEEN DARYL HANNAH MARTIN
SHEEN will print
MICHAEL DOUGLAS
DARYL HANNAH
CHARLIE SHEEN
MARTIN SHEEN
You will not receive extra credit for identifying the film
|
void printstring(char *x)
{
while (*x)
putchar(*x++);
}
/* 1 if the string s is larger than the string t; 0 otherwise. */
int strlarger(char *s,char *t)
{
for (;;) {
if (!*s) return 0;
if (!*t) return 1;
if (*s > *t) return 1;
if (*s < *t) return 0;
++s;
++t;
}
}
/* 1 if the name p[0] p[1] is smaller than the name q[0] q[1]; 0 otherwise. */
int namesmaller(char *p[2],char *q[2])
{
if (strlarger(q[1],p[1]))
return 1;
if (strlarger(p[1],q[1]))
return 0;
if (strlarger(q[0],p[0]))
return 1;
return 0;
}
int main(int argc,char **argv)
{
int i;
int smallest;
while (argc > 2) {
smallest = 1;
for (i = 3;i + 1 < argc;i += 2)
if (namesmaller(argv + i,argv + smallest))
smallest = i;
printstring(argv[smallest]);
printstring(" ");
printstring(argv[smallest + 1]);
printstring("n");
argc -= 2;
argv[smallest] = argv[argc];
argv[smallest + 1] = argv[argc + 1];
}
return 0;
}
|