Browse Source

上传文件至 '随机数排序'

lisihan 6 tháng trước cách đây
mục cha
commit
663ee0b15c
4 tập tin đã thay đổi với 46 bổ sung14 xóa
  1. 4 4
      随机数排序/algo.h
  2. 9 1
      随机数排序/io.c
  3. 2 2
      随机数排序/io.h
  4. 31 7
      随机数排序/main.c

+ 4 - 4
随机数排序/algo.h

@@ -1,8 +1,8 @@
 #pragma once
 #ifndef ALGO_H
 #define ALGO_H
-void QuickSort(int* arr, int left, int right);
-int  PartSort(int* arr, int left, int right);
-void swap(int* arr, int left, int right);
-int* RandomArrayCreate(int Arraylength);
+void QuickSort(int* arr/*O*/, int left/*I*/, int right/*I*/);
+int  PartSort(int* arr/*IO*/, int left/*I*/, int right/*I*/);
+void swap(int* arr/*IO*/, int left/*I*/, int right/*I*/);
+int* RandomArrayCreate(int Arraylength/*I*/);
 #endif

+ 9 - 1
随机数排序/io.c

@@ -1,5 +1,6 @@
 #include"io.h"
 #include<stdio.h>
+#include <assert.h>
 void OutPut(int* arr, int Arraylength)
 {
     for (int i = 0; i < Arraylength; i++)
@@ -21,5 +22,12 @@ void OutPut(int* arr, int Arraylength)
 void InPutArrayLength(int* Arraylength)
 {
     printf("%s","请输入您想要获得的随机数组长度:\n");
-    scanf_s("%d",Arraylength);
+
+    // 05/21/2024 Li SiHan Added Start
+    //函数assert用于确认数组长度输入的合法性
+    //确认输入为数字
+    assert(scanf_s("%d",Arraylength)!=0);
+    //确认输入为正数
+    assert(*Arraylength >= 0);
+    // 05/21/2024 Li SiHan Added End
 }

+ 2 - 2
随机数排序/io.h

@@ -1,6 +1,6 @@
 #pragma once
 #ifndef IO_H
 #define IO_H
-void OutPut(int* arr, int Arraylength);
-void InPutArrayLength(int *Arraylength);
+void OutPut(int* arr/*I*/, int Arraylength/*I*/);
+void InPutArrayLength(int *Arraylength/*O*/);
 #endif  

+ 31 - 7
随机数排序/main.c

@@ -1,17 +1,41 @@
+/*HEAD  lisihan_homework/随机数排序/main.c */
+ /*==============================================================================
+ 2024年度面向对象程序设计作业
+================================================================================
+ File description:
+
+  这是随机数排序的程序。
+    提交者:李思翰
+    邮 箱:1711871773@qq.com
+
+ ================================================================================
+ Date               Name             Description of Change
+ 05/19/2024         Li SiHan         Created
+ 05/21/2024         Li SiHan         Added Code comments and Assert function
+ $HISTORY$
+ ================================================================================
+ */
 #include<stdio.h>
 #include"algo.h"
 #include"io.h"
-int main()
+void main()
 {
-    int Arraylength=0;
+    //对于数组的长度这一变量进行声明及初始化
+    int Arraylength=0; 
 
-    InPutArrayLength(&Arraylength);
+    //用户对数组长度这一变量进行赋值
+    InPutArrayLength(&Arraylength); 
    
-    int* arr = RandomArrayCreate(Arraylength);
-    OutPut(arr,Arraylength);
+    //生成并返回所需长度的随机数组
+    int* arr = RandomArrayCreate(Arraylength); 
+    //输出当前所生成的数组
+    OutPut(arr,Arraylength); 
+
+    //对数组进行快速排序
+    QuickSort(arr,0, Arraylength-1); 
+    //输出排序后的数组
+    OutPut(arr,Arraylength); 
 
-    QuickSort(arr,0, Arraylength-1);
-    OutPut(arr,Arraylength);
 }