19课作业1
本帖最后由 zaas 于 2011-1-14 22:50 编辑#include <stdio.h>
#include <stdlib.h>
int Numinput(int *p,int i);
void Print(int *p,int i);
void Ascending(int *p,int i);
void Descending(int *p,int i);
void Random(int *p,int i);
int Sort (int *p,int i,char * method);
void Change(int *a,int *b);
int Big(int a,int b);
int Small(int a,int b);
void main()
{
int i;
int flag;
int *p;
charmethod;
printf("how many nums?\n");
flag=scanf("%d",&i);
if (!flag)
{
printf("error num!\n");
return;
}
p= (int * )malloc(i*sizeof(int));
flag=Numinput(p,i);
if (!flag)
{
printf("error num!\n");
free(p);
return;
}
printf("Ascending/Descending/Random?\n");
scanf("%10s",method);
printf("Original:\n");
Print(p,i);
flag=Sort(p,i,method);
if (!flag)
{
printf("error method!\n");
free(p);
return;
}
printf("Sorted:\n");
Print(p,i);
free(p);
}
int Numinput(int *p,int i)
{
int j;
int flag;
printf("input nums:\n");
for (j=0;j<i;j++)
{
flag=scanf("%d",p+j);
if (!flag)
{
return(0);
}
}
return(1);
}
void Print(int *p,int i)
{
int j;
for (j=0;j<i;j++)
{
printf("%d ",*(p+j));
}
printf("\n");
}
int Sort (int *p,int i,char * method)
{
switch (*method)
{
case 'A':
Ascending(p,i);
return(1);
case 'a':
Ascending(p,i);
return(1);
case 'D':
Descending(p,i);
return(1);
case 'd':
Descending(p,i);
return(1);
case 'R':
Random(p,i);
return(1);
case 'r':
Random(p,i);
return(1);
default:
return(0);
}
}
void Ascending(int *p,int i)
{
int j,k;
for (j=0;j<i;j++)
{
for (k=0;k<i-j-1;k++)
{
if (Big(*(p+k),*(p+k+1)))
{
Change(p+k,p+k+1);
}
}
}
}
void Change(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int Big(int a,int b)
{
return a>b?1:0;
}
int Small(int a,int b)
{
return a<b?1:0;
}
void Descending(int *p,int i)
{
int j,k;
for (j=0;j<i;j++)
{
for (k=0;k<i-j-1;k++)
{
if (Small(*(p+k),*(p+k+1)))
{
Change(p+k,p+k+1);
}
}
}
}
void Random(int *p,int i)
{
int j;
int *rnd= (int * )malloc(i*sizeof(int));
Ascending(p,i);
for (j=0;j<=i/2;j++)
{
*(rnd+2*j)=*(p+j);
*(rnd+2*j+1)=*(p+i-j-1);
}
for (j=0;j<i;j++)
{
*(p+j)=*(rnd+j);
}
free(rnd);
}
回复 1# zaas
图如下:
switch的优化:00401190/$8B4424 0C mov eax, dword ptr
00401194|.0FBE00 movsx eax, byte ptr
00401197|.83C0 BF add eax, -41 ;Switch (cases 41..72)
0040119A|.83F8 31 cmp eax, 31
0040119D|.0F87 9F000000 ja 00401242
004011A3|.33C9 xor ecx, ecx
004011A5|.8A88 64124000 mov cl, byte ptr
004011AB|.FF248D 481240>jmp dword ptr
004011B2|>8B5424 08 mov edx, dword ptr ;Case 41 ('A') of switch 00401197
004011B6|.8B4424 04 mov eax, dword ptr
004011BA|.52 push edx
004011BB|.50 push eax
004011BC|.E8 DF000000 call 004012A0
004011C1|.83C4 08 add esp, 8
004011C4|.B8 01000000 mov eax, 1
004011C9|.C3 retn
004011CA|>8B4C24 08 mov ecx, dword ptr ;Case 61 ('a') of switch 00401197
004011CE|.8B5424 04 mov edx, dword ptr
004011D2|.51 push ecx
004011D3|.52 push edx
004011D4|.E8 C7000000 call 004012A0
004011D9|.83C4 08 add esp, 8
004011DC|.B8 01000000 mov eax, 1
004011E1|.C3 retn
004011E2|>8B4424 08 mov eax, dword ptr ;Case 44 ('D') of switch 00401197
004011E6|.8B4C24 04 mov ecx, dword ptr
004011EA|.50 push eax
004011EB|.51 push ecx
004011EC|.E8 4F010000 call 00401340
004011F1|.83C4 08 add esp, 8
004011F4|.B8 01000000 mov eax, 1
004011F9|.C3 retn
004011FA|>8B5424 08 mov edx, dword ptr ;Case 64 ('d') of switch 00401197
004011FE|.8B4424 04 mov eax, dword ptr
00401202|.52 push edx
00401203|.50 push eax
00401204|.E8 37010000 call 00401340
00401209|.83C4 08 add esp, 8
0040120C|.B8 01000000 mov eax, 1
00401211|.C3 retn
00401212|>8B4C24 08 mov ecx, dword ptr ;Case 52 ('R') of switch 00401197
00401216|.8B5424 04 mov edx, dword ptr
0040121A|.51 push ecx
0040121B|.52 push edx
0040121C|.E8 7F010000 call 004013A0
00401221|.83C4 08 add esp, 8
00401224|.B8 01000000 mov eax, 1
00401229|.C3 retn
0040122A|>8B4424 08 mov eax, dword ptr ;Case 72 ('r') of switch 00401197
0040122E|.8B4C24 04 mov ecx, dword ptr
00401232|.50 push eax
00401233|.51 push ecx
00401234|.E8 67010000 call 004013A0
00401239|.83C4 08 add esp, 8
0040123C|.B8 01000000 mov eax, 1
00401241|.C3 retn
00401242|>33C0 xor eax, eax ;Default case of switch 00401197
00401244\.C3 retn 学习了,感觉楼主是对C通了,用的很自如;而自己则是不通,处处碰壁。 过来看帖,顺便膜拜一下!
页:
[1]