| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 686 人关注过本帖
标题:比较复杂的数该怎么排序?
只看楼主 加入收藏
zhangenter
Rank: 6Rank: 6
等 级:贵宾
威 望:25
帖 子:390
专家分:0
注 册:2006-6-5
收藏
 问题点数:0 回复次数:3 
比较复杂的数该怎么排序?

从键盘输入有正负号,有小数点的10个数字(数字位数可能很长,一个字或一个双字存储不下),对这十个数进行排序后输出,不知道谁有好点的方法.有没有代码没关系,关键是先有个算法分析,大家帮帮忙,谢谢了

搜索更多相关主题的帖子: 键盘 小数点 
2006-07-06 16:45
yuki
Rank: 2
等 级:新手上路
威 望:5
帖 子:508
专家分:0
注 册:2005-2-4
收藏
得分:0 

我刚学汇编,复杂的程序还不敢尝试,这里做了个冒泡排序过程,c文件中可以调用,使用方法,在BC3.1或TC3的IDE中建个PRJ把ex.asm和Myex.c导入就行了,当然也可以用到汇编程序里去了,这里我就不写了。

; ex.asm
PUBLIC _bubble_sort

.model small
.186
.code
_bubble_sort PROC C array: word, s: word
;===========================================================
; int i,j,temp;
; for(i=0;i<=s;i++) {
; for(j=0;j<s-1;j++) {
; if(array[j]>array[j+1]) {
; temp = array[j];
; array[j] = array[j+1];
; array[j+1] = temp;
; }
; }
; }
;============================================================

push ax
push bx
push cx
push dx

push di
push si

mov ax, 0 ; set i = 0
mov dx, s ; i <= s

mov di, OFFSET array
mov si, di

L1:
cmp ax, dx
jae OUT_LOOP1
dec dx ; j < s - 1
mov bx, 0
L2:
push dx
cmp bx, dx
jae OUT_INNER_LOOP

push ax
push bx
push cx ; save cx, dx for mul instruction

mov cx, 2

push ax ; save ax
mov ax, bx
mul cx
mov bx, ax ; bx = bx * 2
pop ax

push di
push si

add di, bx ; locate array[j]
add bx, 2
add si, bx ; locate array[j+1]

mov bx, [di]
cmp bx, [si]
ja SWAP

pop si
pop di
pop cx
pop bx
pop ax
pop dx

inc bx
jmp L2

SWAP:
mov ax, [di] ; temp = array[j]
mov bx, [si]
mov [di], bx ; array[j] = array[j+1]
mov [si], ax ; array[j+1] = temp

pop si
pop di
pop cx
pop bx
pop ax
pop dx

inc bx
jmp L2

OUT_INNER_LOOP:
pop dx
inc dx
inc ax
jmp L1

OUT_LOOP1:
pop si
pop di

pop dx
pop cx
pop bx
pop ax

ret

_bubble_sort ENDP

END

/* myex.c */
#include <stdio.h>

#ifdef __cplusplus
#define EXT extern "C"
#else
#define EXT extern
#endif

EXT bubble_sort(int array[], int size);

int main() {
int i;
int a[] = {10,4,3,7,5,8,2,6,9,1};
bubble_sort(a,10);
for(i=0;i<10;i++)
printf("%3d",a[i]);
printf("\n");
getch();
return 0;
}


我们都在命运湖上荡舟划桨,波浪起伏使我们无法逃离孤行;如果我们迷失方向,波浪将指引我们穿过另一天曙光
2006-07-06 22:04
zhangenter
Rank: 6Rank: 6
等 级:贵宾
威 望:25
帖 子:390
专家分:0
注 册:2006-6-5
收藏
得分:0 
c和汇编我都不懂,一个星期都没学到,你说的怎么导入我一点都不懂,不过还是谢谢你了

OldHandle = findobj( \'Name\', \'悲伤\' ) ;if ~isempty(OldHandle),delete(OldHandle) ;end for Time = \'现在\':\'每一天\':\'永远\',set( gco, \'心情\', \'快乐\');end % 这段代码为你天天快乐而存在
2006-07-06 23:34
zhangenter
Rank: 6Rank: 6
等 级:贵宾
威 望:25
帖 子:390
专家分:0
注 册:2006-6-5
收藏
得分:0 

  汇编指令我知道的不多,连子程序都不知道怎么调用,下面是我编的,一路jmp下去
  程序非常乱,我说下基本思想,给10个数字每个预留20位,输入的时候空格作为两数分隔,回车作为输入结束,定义一个存单纯存储数字的数组number,定义一个存储数字位数的数组numlenth(不包括负号和小数点),定义处理负号先定义个10个元素的值为1的数组cmpzero,输入负号则把1改为0,对于小数点的处理,数组pointn记录下小数点之前数字位数,对没小数点的数字的pointn的值即是numlenth值,直接把numlenth值复制上去.排序是用冒泡法,比较大小时,先比负号数组,异号则跳出(大于互换,小于不换),同为负号的话用变量ccnum的值来记下,同正则比较pointn的值,即小数点之前数字位数的比较,数字位数不等则跳出,位数相等则逐位比较,若仍相等则比较小数位之后的数.数据输出时候读取排序模式的变量值,由于数组比较玩保存为升序了,如果选择的降序只要对数组从后往前输出.现在想想排序的时候数字互换太麻烦了,应该拿个指针来代替数字进行互换的.
data segment
number db 200 dup(?)
numlenth dw 10 dup(?)
ccnum db ?
pointn dw 10 dup(0)
cmpzero dw 10 dup(1)
select db 1
count1 dw ?
count2 dw ?
count3 dw ?
count4 dw ?
exit db 0dh,0ah,'please any key to exit','$'
input db 'please input the numbers:(dapart them with keyboard "space" and end with keyboad "enter")',0dh,0ah,'$'
choose db 0dh,0ah,'please choose sort style',0dh,0ah,'1 ascend',0dh,0ah,'2 descend',0dh,0ah,'$'
output db 0dh,0ah,'the sorted numbers are:',0dh,0ah,'$'
data ends

code segment
assume cs:code,ds:data
start: mov ax,data
mov ds,ax
sub si,si
sub bx,bx
sub di,di
sub cx,cx
mov count1,0
mov count2,0
mov ah,09h
lea dx,input
int 21h
sub dx,dx
inputdata:mov ah,01h
int 21h
cmp al,20h
jz nextdata
cmp al,0dh
jz endinput
cmp al,2eh
jnz numsign
mov bx,count1
mov pointn[di],bx
jmp inputdata
numsign: cmp al,2dh
jnz savedata
mov cmpzero[di],0
jmp inputdata
savedata:inc count1
mov number[si],al
inc si
jmp inputdata
nextdata: cmp pointn[di],0
jnz savenum
mov dx,count1
mov pointn[di],dx
savenum:mov dx,count1
mov numlenth[di],dx
add count2,20
mov si,count2
cmp count2,200
jz sortdata
mov count1,0
add di,2
jmp inputdata
endinput:cmp pointn[di],0
jnz nopoint
mov dx,count1
mov pointn[di],dx
nopoint: mov dx,count1
mov numlenth[di],dx
sortdata: add di,2
mov count1,0
selectstyle:
mov ah,09h
lea dx,choose
int 21h
mov ah,01h
int 21h
cmp al,49
jz selected
cmp al,50
jz selected
jmp selectstyle
selected:
mov select,al

mov count1,di
mov count4,di
mov count3,0
sub si,si
sub di,di

nextloop:
sub bx,bx
sub cx,cx
mov ccnum,0
mov dx,0
mov count2,0
mov ax,cmpzero[si]
cmp ax,cmpzero[si+2]
jna eeee
jmp change
eeee:
jnb dddd
jmp over2
dddd:
cmp ax,0
jnz gggg
mov ccnum,1
gggg:
mov ax,pointn[si]
cmp ax,pointn[si+2]
jna ffff
jmp change
ffff:
jnb cmpap
jmp over
cmpap:

mov ch,number[bx+di]
mov cl,number[bx+di+20]
cmp ch,cl

ja change


jb over

inc bx
cmp bx,ax
jnz cmpap

mov cx,numlenth[si]
cmp cx,ax
jnz aaaa
jmp over
aaaa:
mov dx,numlenth[si+2]
cmp dx,ax
jz change
cmpbp:mov ah,number[di+bx]
mov al,number[di+bx+20]
cmp ah,al
ja change
jb over
inc bx
cmp bx,cx
jz over
cmp bx,dx
jz change
jmp cmpbp
change:sub ax,ax
sub bx,bx
sub cx,cx
notchange:cmp ccnum,1
jz over2
mov ax,cmpzero[si]
xchg ax,cmpzero[si+2]
mov cmpzero[si],ax
mov ax,pointn[si]
xchg ax,pointn[si+2]
mov pointn[si],ax
mov ax,numlenth[si]
xchg ax,numlenth[si+2]
mov numlenth[si],ax
changenum:

mov al,number[di+bx]
xchg al,number[di+bx+20]
mov number[di+bx],al


inc bx
cmp bx,20
jnz changenum

over:cmp ccnum,1
jnz over2
mov ccnum,0
jmp change

over2:

add si,2
add di,20
cmp si,count1
jz bbbb
jmp nextloop
bbbb:
sub count1,2
mov count3,0

sub si,si
sub di,di
cmp count1,2
jz cccc
jmp nextloop
cccc:

cmp select,50
jz descend
mov ah,09h
lea dx,output
int 21h
sub si,si
sub bx,bx
sub cx,cx
mov count2,0
sub dx,dx
outputnext:mov cx,pointn[si]
sub di,di
add di,count2
add cx,count2
sub cmpzero[si],0
jnz outputdata
mov dl,2dh
mov ah,02h
int 21h
outputdata:mov dl,number[di]
mov ah,02h
int 21h
inc di
cmp di,cx
jnz outputdata
sub cx,count2
cmp cx,numlenth[si]
jz outputspace
mov dl,2eh
mov ah,02h
int 21h
mov cx,numlenth[si]
add cx,count2
jmp outputdata

outputspace:
add si,2
cmp si,count4
jnz hhhh
jmp exitall
hhhh:
mov dl,20h
mov ah,02h
int 21h
add count2,20
jmp outputnext
jmp exitall

descend:
mov ah,09h
lea dx,output
int 21h
sub si,si
sub bx,bx
sub cx,cx
sub dx,dx
mov ax,count4
mov di,20
mul di
mov count2,ax
mov si,count4
outputnext2:mov cx,pointn[si]
sub di,di
add di,count2
add cx,count2
sub cmpzero[si],0
jnz outputdata
mov dl,2dh
mov ah,02h
int 21h
outputdata2:mov dl,number[di]
mov ah,02h
int 21h
inc di
cmp di,cx
jnz outputdata2
sub cx,count2
cmp cx,numlenth[si]
jz outputspace2
mov dl,2eh
mov ah,02h
int 21h
mov cx,numlenth[si]
add cx,count2
jmp outputdata2

outputspace2:
sub si,2
cmp si,count4
jz exitall
mov dl,20h
mov ah,02h
int 21h
sub count2,20
jmp outputnext2

exitall:
mov ah,09h
lea dx,exit
int 21h
mov ah,01h
int 21h
mov ah,4ch
int 21h
code ends
end start
运行的时候还有问题,现在这段只能实现输满10个数字的升序排序,降序有乱码,不输满10位回车也有乱码,问题还在找,哪位高手发现错在哪的话也请指出.


OldHandle = findobj( \'Name\', \'悲伤\' ) ;if ~isempty(OldHandle),delete(OldHandle) ;end for Time = \'现在\':\'每一天\':\'永远\',set( gco, \'心情\', \'快乐\');end % 这段代码为你天天快乐而存在
2006-07-07 23:50
快速回复:比较复杂的数该怎么排序?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045229 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved