-
Notifications
You must be signed in to change notification settings - Fork 0
/
selnode.c
58 lines (48 loc) · 1.06 KB
/
selnode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int id[65536];
int cmpfunc (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}
int main(int argc, char *argv[]){
int n, i;
int a, b, l, s;
char *token;
if (argc < 3){
return 0;
}
n = atoi(argv[1]);
token = strtok(argv[2], ",");
s = 0;
while(token != NULL){
l = sscanf(token, "%d-%d", &a, &b);
if (l == 1){
id[s] = a;
s++;
}
else if (l == 2){
for(i = a; i <= b; i++){
id[s] = i;
s++;
}
}
else{
printf("Error parsing token: %s\n", token);
break;
}
token = strtok (NULL, ",");
}
if (n > s){
printf("Error: n > s\n");
n = s;
}
qsort(id, s, sizeof(int), cmpfunc);
for(i = 0; i < n; i++){
if (i > 0){
printf(",");
}
printf("%d", id[i]);
}
return 0;
}