forked from Suxyuuu/buaa-991
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2017_2.c
91 lines (81 loc) · 1.98 KB
/
2017_2.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* 读取一个字符串 识别其中的数字字符并将其转化为整型(不可使用atoi函数) 将数字从大到小输出
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void stoi(char(* nums)[20], int numi[20], int count); //字符转数字
void output(int* numi, int count); //排序并输出
int comp(const void *a, const void *b); //快排比较函数
int main(void){
char str[100]="abc123de45f*#6?*789&";
//scanf("%s",str);
//printf("%s\n",str);
char num[20][20];
char buff[20];
int start=0, end=0;
int pos=-1;
int count=0;
for (int i = 0; i < strlen(str)+1; i++)
{
if (str[i]>='0' && str[i]<='9' && pos==-1)
{
start=i;
pos=i;
}
else if (str[i]>='0' && str[i]<='9' && pos!=-1)
{
pos++;
}
else
{
if (pos==i-1 && i!=0)
{
end=i-1;
pos=-1;
int j=0;
for (int i = start; i < end+1; i++,j++)
{
buff[j]=str[i];
}
buff[j]='\0';
strncpy(num[count],buff,20);
count++;
}
}
}
for (int i = 0; i < count; i++)
{
printf("%s\n",num[i]);
}
printf("\n");
int numi[20];
stoi(num, numi, count);
output(numi, count);
getchar();
getchar();
return 0;
}
void stoi(char(* nums)[20], int* numi, int count){
for (int i = 0; i < count; i++)
{
int sum=0;
int j=0;
while (nums[i][j]!='\0')
{
sum=sum*10+nums[i][j]-'0';
j++;
}
numi[i]=sum;
}
}
int comp(const void *a, const void *b){
return *(int*)b-*(int*)a;
}
void output(int* numi, int count){
qsort(numi, count, sizeof(int), comp);
for (int i = 0; i < count; i++)
{
printf("%d\n", numi[i]);
}
}