本文提供了ICS HW4补充作业中需要的初始代码。
main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include"add.h" #include"BubbleSort.h" #include"printResult.h" #include<time.h>
#define bool char #define true 1 #define false 0 #define LENGTH 10
int main() { int a[LENGTH],i; int b[LENGTH]; int randValue = 0; srand(time(NULL)); for(i=0;i<LENGTH;i++){ randValue = 1 + (int)rand()%LENGTH; a[i] = randValue; b[i] = a[i]; } printResult(a,LENGTH,"\nrandom array: "); bool flag = true; while(flag){ printf("\n1.Bubble Sort\n2.sum\n3.print result\n4.exit"); printf("\nchoose a number:"); int number = 0; scanf("%d",&number); int sum = 0; switch(number) { case 1: BubbleSort(a,LENGTH); break; case 2: sum = add(a,LENGTH); printf("\nresult of sum: %d\n",sum); break; case 3: printResult(b,LENGTH,"\noriginal array:\t"); printResult(a,LENGTH,"\nsorted array:\t"); break; case 4: flag = false; break; default: printf("\nplease choose a correct number and continue!"); break; } printf("\nDone!\n\n"); } return 0; }
|
add.c
1 2 3 4
| #include"add.h" int add(int s[],int n){ }
|
add.h
1 2
| #include<stdio.h> int add(int s[],int n);
|
BubbleSort.c
1 2 3 4
| #include"BubbleSort.h" void BubbleSort(int s[], int n){ }
|
BubbleSort.h
1 2
| #include<stdio.h> void BubbleSort(int s[], int n);
|
printResult.c
1 2 3 4 5 6 7 8 9
| #include"printResult.h" void printResult(int s[],int n,char* str){ printf("%s",str); int i; for(i = 0;i<n;i++){ printf("%5d",s[i]); } printf("\n"); }
|
printResult.h
1 2
| #include<stdio.h> void printResult(int s[],int n,char* str);
|
愿天堂没有用word图片发布的作业代码。