# 模擬成績 in c ## Akuan ```c #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> int main() { int scords[30]; int i,j,h,c,x,s; srand(time(NULL)); for(i=0;i<30;i++){ scords[i]=(60+rand()%40+1); printf("%d ",scords[i]); } for(j=29;j>=1;j--){ for(h=0;h<j;h++){ if(scords[h+1]<scords[h]){ c=scords[h+1]; scords[h+1]=scords[h]; scords[h]=c; } } } printf("\n"); for(x=0;x<30;x++){ printf("%d ",scords[x]) ; } printf("\n"); system("pause"); return 0; } ``` ```c #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> int bubbleSort(int scores[],int scores_size){ int j,h,c; for(j=0;j<29;j++){ for(h=0;h<29-j;h++){ if(scores[h]>scores[h+1]){ c=scores[h]; scores[h]=scores[h+1]; scores[h+1]=c; } } } } int main() { int scores[30]; int i,x; srand(time(NULL)); for(i=0;i<30;i++){ scores[i]=(60+rand()%40+1); printf("%d ",scores[i]); } printf("\n"); bubbleSort(scores,30); for(x=0;x<30;x++){ printf("%d ",scores[x]) ; } printf("\n"); system("pause"); return 0; } ``` ```c // 在這裡貼上 #include <stdio.h> #include <stdlib.h> #include <time.h> #define MAX 30 #define SWAP(x,y) {int t; t = x; x = y; y = t;} #define Size 60 #define Num 30 void bubblesort(int scores[],int scores_size); int binarySearch(int scores[],int find); int main() { int i, scores[Size], temp, pos,x,find; srand(time(NULL)); scores[0] = 100; for(i = 1 ; i < Size; ++i) { scores[i] = i+40; } for(i = 0; i < Size; ++i) { pos = 59 * rand() / RAND_MAX; temp = scores[i]; scores[i] = scores[pos]; scores[pos] = temp; } printf("學生成績:"); printf("\n"); for(i = 0; i < MAX; i++) { printf("%d ", scores[i]); } bubblesort(scores,30); printf("\n"); for(x=0;x<30;x++) { printf("%d ",scores[x]); } printf("\n"); printf("\n輸入尋找分數:"); scanf("%d", &find); if((i = binarySearch(scores, find)) >= 0) printf("找到第 %d名學生的分數 ", i+1); else printf("\n找不到指定分數"); return 0; system("pause"); } int bubblesort(int scores[],int scores_size) { int h, j, k; for(h=0;h<29;h++) { for(j=0;j<29-h;j++) { if(scores[j]<scores[j+1]) { k=scores[j]; scores[j]=scores[j+1]; scores[j+1]=k; } } } } int binarySearch(int scores[],int find) { int low = MAX-1; int upper = 0; while(low >= upper) { int mid = (low+upper) / 2; if(scores[mid] < find) low = mid-1; else if(scores[mid] > find) upper = mid+1; else return mid; } return -1; } ``` ## Russel ```c #include <stdio.h> #include <stdlib.h> #include <time.h> ``` ```c struct Student { int studentID; int studentScore; }; typedef struct Student Student_t; #define NUMBER_OF_STUDENT 30 ``` ```c void bubbleSort_Scores (Student_t* students[]) { Student_t* tmp; for (int i = 0; i < (NUMBER_OF_STUDENT-1); ++i) { for (int j=0; j < ((NUMBER_OF_STUDENT-1)-i); ++j ) { if (students[j]->studentScore > students[j+1]->studentScore) { tmp = students[j]; students[j] = students[j+1]; students[j+1] = tmp; } } } } ``` ```c void bubbleSort_IDs (Student_t* students[]) { Student_t* tmp; for (int i = 0; i < (NUMBER_OF_STUDENT-1); ++i) { for (int j=0; j < ((NUMBER_OF_STUDENT-1)-i); ++j ) { if (students[j]->studentID > students[j+1]->studentID) { tmp = students[j]; students[j] = students[j+1]; students[j+1] = tmp; } } } } ``` ```c Student_t* binarySearch_Scores (Student_t* students[], int begin, int end, int key) { /* Assume that students is sorted */ if (begin > end) return NULL; if (begin == end) return students[begin]; int middle = (begin + end)/ 2; if (students[middle]->studentScore == key) return students[middle]; else if (key < students[middle]->studentScore) return binarySearch_Scores(students, begin, middle-1, key); else // if (key > students[middle]->studentScore) return binarySearch_Scores(students, middle+1, end, key); return NULL; } ``` ```c Student_t* binarySearch_IDs (Student_t* students[], int begin, int end, int key) { /* Assume that students is sorted */ if (begin > end) return NULL; if (begin == end) return students[begin]; int middle = (begin + end)/ 2; if (students[middle]->studentID == key) return students[middle]; else if (key < students[middle]->studentID) return binarySearch_IDs(students, begin, middle-1, key); else // if (key > students[middle]->studentScore) return binarySearch_IDs(students, middle+1, end, key); return NULL; } ``` ```c void simulateScores(Student_t* students[]) { srand(time(NULL)); for (int i = 0; i < NUMBER_OF_STUDENT; ++i) { students[i]->studentID = (i+1); students[i]->studentScore = rand()%40 + 60; } } ``` ```c void printAll(Student_t* students[]) { printf("Student ID : "); for(int i = 0; i < NUMBER_OF_STUDENT; ++i) { if (students[i]->studentID < 10) printf(" %d", students[i]->studentID); else printf(" %d", students[i]->studentID); } printf("\n\r"); printf("Student Score : "); for(int i = 0; i < NUMBER_OF_STUDENT; ++i) { printf("%d ", students[i]->studentScore); } printf("\n\r"); } ``` ```c void txt_write (Student_t* students[]) { char buffer[500]; strcat(buffer, "Student ID : ") for(int i = 0; i < NUMBER_OF_STUDENT; ++i) { if (students[i]->studentID < 10) { strcat(buffer, " "); strcat(buffer, students[i]->studentID); } else { strcat(buffer, " "); strcat(buffer, students[i]->studentID); } } strcat(buffer, "\n\r"); strcat(buffer, "Student Score : "); for(int i = 0; i < NUMBER_OF_STUDENT; ++i) { strcat(buffer, students[i]->studentScore); strcat(buffer, " "); } strcat(buffer, "\n\r"); FILE *file; file = fopen( "score.txt","w" ); fwrite(buffer,1,sizeof(buffer),file); fclose(file); } ``` ```c void setScore(Student_t* students[], int id, int score) { bubbleSort_IDs(students); Student_t* student = binarySearch_IDs(students, 0, NUMBER_OF_STUDENT-1, id); student->score = score; bubbleSors_Scores(students); } ``` ```c int main() { Student_t* students[NUMBER_OF_STUDENT]; for (int i = 0; i < NUMBER_OF_STUDENT; ++i) students[i] = malloc(sizeof(Student_t)); simulateScores(students); bubbleSort_Scores(students); printAll(students); txt_write(students); Student_t* student = binarySearch_Scores(students, 0, NUMBER_OF_STUDENT-1, 77); if (student){ printf("Studnet ID: %d\n\r", student->studentID); printf("Studnet Score: %d\n\r", student->studentScore); } setScore(students, 13, 77); txt_write(students); for (int i = 0; i < NUMBER_OF_STUDENT; ++i) free(students[i]); return 0; } ```