?? 6-2.c
字號:
#include <REG52.H>
#include <stdio.h>
#ifdef MONITOR51
char code reserve [3] _at_ 0x23; // 定義一個絕對地址
#endif
#define N 10
void main (void) {
void input(int arr[],int n); //函數聲明
void sort(int *ptr,int n);
void output(int arr[],int n);
int a[N],*p; //定義一維數組和指針變量
#ifndef MONITOR51
SCON = 0x50;
TMOD |= 0x20;
TH1 = 221;
TR1 = 1;
TI = 1;
#endif
input(a,N) ; //數據輸入函數調用,實參a 是數組名
p = a ; //指針變量指向數組的首地址
sort(p,N) ; //排序,實參p 是指針變量
output(p,N) ; //輸出,實參p 是指針變量
while (1) {};
}
void input(int arr[],int n) //無需返回值的輸入數據函數定義,形參a r //r 是數組
{
int i;
printf("input data:\n");
for( i = 0 ; i < n ; i++ ) //采用傳統的下標法
scanf( "%d" , &arr[i] ) ;
}
void sort(int *ptr,int n) //冒泡排序,形參p t r 是指針變量、
{
int i,j,t;
for( i = 0 ; i < n - 1 ; i++ )
for( j = 0 ; j < n - 1 - i ; j++ )
if (*(ptr+j)>*(ptr+j+1)) //相臨兩個元素進行比較
{
t = * (ptr+ j ) ; //兩個元素進行交換
*(ptr+ j ) = * (ptr +j+1) ;
*(ptr+ j + 1 ) = t ;
}
}
void output(int arr[],int n) //數據輸出
{
int *ptr=arr; //利用指針指向數組的首地址
printf("output data:\n");
for( ; ptr-arr<n ; ptr++ ) //輸出數組的n 個元素
printf("%4d",*ptr) ;
printf("\n");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -