1. 程式人生 > >氣泡排序--c語言面向物件的使用

氣泡排序--c語言面向物件的使用

/**************************************************

File Name: maopao.c Author: hebowen Mail: [email protected] Created Time: 2018年07月15日 星期日 09時39分36秒 **************************************************/

#include <stdio.h> #include <stdlib.h> #define MAX 10 struct maopao { int a[MAX]; void (*get_value)(int *,int); void (*maopao)(int *,int); void (*show_value)(int *,int); }L;

void get_value(int a[],int len) { int i = 0; printf("\033[41m請輸入10個數字\033[0m\n"); for(i = 0;i < len;i++) scanf("%d",&a[i]); }

void maopao(int a[],int len) { int tmp = 0; int i = 0 ,j = 0; for(i = 0;i < len-1; i++) { for(j = 0;j < len-i-1;j++) { if(a[j] > a[j+1]) { tmp = a[j+1]; a[j+1] = a[j]; a[j] = tmp; } } } void show_value(int a[],int len) { int i = 0; for(;i < len;i++) printf("%d “,a[i]); printf(”\n"); } void init_maopao(struct maopao A) { A->get_value = get_value; A->maopao = maopao; A->show_value = show_value; } int main(int argc,const char

argv[]) { struct maopao A = (struct maopao)malloc(sizeof(L)); init_maopao(A); A->get_value(A->a,MAX); A->show_value(A->a,MAX); A->maopao(A->a,MAX); A->show_value(A->a,MAX); free(A);

return 0;

}