09/01/09 18:40:15
>>515
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1000
#define BUF 100
typedef struct XX { int number; int data;} X;
enum {FALSE = 0};
void get(char *input){
fgets(input, BUF, stdin);
strtok(input, "\n");}
int cmp_num(const void *a_, const void *b_){
const int a = ((X *)a_)->number, b = ((X *)b_)->number; return a < b ? -1 : a > b ? 1 : 0;}
int cmp_data(const void *a_, const void *b_){
const int a = ((X *)a_)->data, b = ((X *)b_)->data; return a < b ? -1 : a > b ? 1 : 0;}
int main()
{ X a[MAX] = {{0, 0}}; char input[BUF]; int number = 1, i;
while (1) {
puts("1.入力順でソートする, 2.値でソートする, " "3.表示, 4. 数字入力, それ以外は終了");
get(input);
if (!strcmp(input, "1")) qsort(a, MAX, sizeof(X), cmp_num);
else if (!strcmp(input, "2")) qsort(a, MAX, sizeof(X), cmp_data);
else if (!strcmp(input, "3")) {
for (i = 0; i < MAX; ++i) if (a[i].number != 0) printf("%3d. %10d\n", a[i].number, a[i].data);
} else if (!strcmp(input, "4")) { get(input);
for (i = 0; i < MAX; ++i)
if (a[i].number == 0) {
a[i].number = number++;
a[i].data = atoi(input);
break; }
} else break; }
return 0;}