1.编写程序将自己学号后面的8位同学的学号、姓名以及数学、英语和数据结构的成绩信息保存到学生成绩表中。如果学号后面没有8位同学,就用学号在班级最前面的学生依次补齐,例如学号为189000248号张二同学,后面只有2位同学,那再表中就存入如下几条学生信息。
| 学号 | 姓名 | 数学 | 英语 | 数据结构 |
| 189000249 | 张三 | 80 | 75 | 86 |
| 189000250 | 李四 | 55 | 63 | 72 |
| 189000201 | 王一 | 88 | 75 | 85 |
| 189000202 | 王二 | 79 | 96 | 83 |
| 189000203 | 王三 | 87 | 45 | 77 |
| 189000204 | 王四 | 66 | 56 | 50 |
| 189000205 | 王五 | 35 | 55 | 68 |
| 189000206 | 王六 | 89 | 98 | 96 |
2.用顺序表插入算法,将自己的学号信息插入到表的第一个位置;再应用删除算法删除现在表中第五位同学的信息,然后输出表里面所有数据信息。
3.用顺序查找算法,查找自己的学号在表中是否存在,如果存在请输出学号、姓名以及各科成绩信息。
4.用直接插入排序算法,对学生成绩表里面信息,按照数学成绩升序排序,并显示输出排序结果。然后用二分查找,查找数学成绩为80的同学是否存在,如果存在,请输出该学生的学号和姓名。
5.用冒泡排序算法,对学生成绩表里面信息,按照英语成绩升序排序,并显示输出排序结果。然后用二分查找,查找英语成绩为90的同学是否存在,如果存在,请输出该学生的学号和姓名。
6.用直接选择排序算法,对学生成绩表里面信息,按照数据结构成绩升序排序,并显示输出排序结果。然后用二分查找,查找数据结构成绩为50的同学是否存在,如果存在,请输出该学生的学号和姓名。
7.分别统计数学、英语、数据结构三门课程的最低分、最高分以及平均分,并输出结果。
直接插入排序算法
基本思想:插入排序通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入 ,如此重复,直至完成序列排序。
void dirInsertSort(){
for(int i = 1;i < count;i++){
student[0] = student[i+1]; //将student[0]当作中间变量
int j = i;
while(j > 0 && student[0].math < student[j].math){
student[j+1] = student[j];
student[j] = student[0];
student[0] = student[j];
j--;
}
}
冒泡排序算法
基本思想:从待排序序列中找出一个最大值或最小值,这样的操作执行 n-1 次,最终就可以得到一个有序序列。
void bubbleSort(){
int flag = 1; //设置flag在于监测排序是否完成
for(int i = 1;i < count && flag == 1;i++){
flag = 0;
for(int j = 1;j < count-i+1;j++){
if(student[j].english > student[j+1].english){
flag = 1;
student[0] = student[j];
student[j] = student[j+1];
student[j+1] = student[0];
}
}
}
}
简单选择排序算法
基本思想:每次从左至右扫描序列,记下最小值的位置。然后将最小值与当前位置的值交换
void directSelectionSort(){
int small; //记录最小值的位置
for(int i = 1;i < count;i++){
small = i;
for(int j = i+1;j <= count;j++){
if(student[j].dataStructure < student[small].dataStructure)
small = j;
}
if(small != i){ //若small == i则表示当前small位置的值为最小值
student[0] = student[i];
student[i] = student[small];
student[small] = student[0];
}
}
}
完整代码
#include#include #include #define Max 10 struct Student{ char num[20]; char name[20]; int math; int english; int dataStructure; }student[Max]; int count = Max-2; int min = 0,max = 0,sum = 0; double avg = 0; void print(int i); void init(); void insert(Student stu,int location); void del(int location); void sequentialSearch(char a[]); void dirInsertSort(); void twoPointSearchMath(int math); void bubbleSort(); void twoPointSearchEnglish(int english); void directSelectionSort(); void twoPointSearchDataStructure(int dataStructure); void calculate(); void minSwap(int value); void maxSwap(int value); void avgSwap(int value,int i); void main(){ init(); strcpy(student[0].num,"209000630"); strcpy(student[0].name,"杨帆"); student[0].math = 100; student[0].english = 100; student[0].dataStructure = 100; insert(student[0],1); printf("-----------------------分隔符-----------------------n"); del(5); printf("-----------------------分隔符-----------------------n"); sequentialSearch("209000630"); printf("-----------------------分隔符-----------------------n"); dirInsertSort(); twoPointSearchMath(80); printf("-----------------------分隔符-----------------------n"); bubbleSort(); twoPointSearchEnglish(90); printf("-----------------------分隔符-----------------------n"); directSelectionSort(); twoPointSearchDataStructure(50); printf("-----------------------分隔符-----------------------n"); calculate(); system("pause"); } void print(int i){ printf("%st%st%dt%dt%dn",student[i].num,student[i].name,student[i].math,student[i].english,student[i].dataStructure); } void init(){ strcpy(student[1].num,"189000249"); strcpy(student[1].name,"张三"); student[1].math = 80; student[1].english = 75; student[1].dataStructure = 86; strcpy(student[2].num,"189000250"); strcpy(student[2].name,"李四"); student[2].math = 55; student[2].english = 63; student[2].dataStructure = 72; strcpy(student[3].num,"189000201"); strcpy(student[3].name,"王一"); student[3].math = 88; student[3].english = 75; student[3].dataStructure = 85; strcpy(student[4].num,"189000202"); strcpy(student[4].name,"王二"); student[4].math = 79; student[4].english = 96; student[4].dataStructure = 83; strcpy(student[5].num,"189000203"); strcpy(student[5].name,"王三"); student[5].math = 87; student[5].english = 45; student[5].dataStructure = 77; strcpy(student[6].num,"189000204"); strcpy(student[6].name,"王四"); student[6].math = 66; student[6].english = 56; student[6].dataStructure = 50; strcpy(student[7].num,"189000205"); strcpy(student[7].name,"王五"); student[7].math = 35; student[7].english = 55; student[7].dataStructure = 68; strcpy(student[8].num,"189000206"); strcpy(student[8].name,"王六"); student[8].math = 89; student[8].english = 98; student[8].dataStructure = 96; } void insert(Student stu,int location){ for(int i = count;i >= location;i--){ student[i+1] =student[i]; } student[location] = stu; count++; printf("插入成功!n"); } void del(int location){ student[0] = student[location]; for(int i = location;i <= count;i++){ student[i] = student[i+1]; } printf("%s数据已被删除!n",student[0].name); print(0); count--; } void sequentialSearch(char a[]){ for(int i = 1;i <= count;i++){ if(strcmp(student[i].num,a) == 0){ print(i); break; }else if(i == count){ printf("没有找到你的信息"); } } } void dirInsertSort(){ for(int i = 1;i < count;i++){ student[0] = student[i+1]; int j = i; while(j > 0 && student[0].math < student[j].math){ student[j+1] = student[j]; student[j] = student[0]; student[0] = student[j]; j--; } } } void twoPointSearchMath(int math){ int low = 1,high = count,mid = (low+high)/2; while(1){ if(student[mid].math > math && mid-1 >= low){ high = mid-1; mid = (low+high)/2; }else if(student[mid].math < math && mid+1 <= high){ low = mid+1; mid = (low+high)/2; }else if(student[mid].math == math){ print(mid); break; }else{ printf("没有找到信息n"); break; } } } void bubbleSort(){ int flag = 1; for(int i = 1;i < count && flag == 1;i++){ flag = 0; for(int j = 1;j < count-i+1;j++){ if(student[j].english > student[j+1].english){ flag = 1; student[0] = student[j]; student[j] = student[j+1]; student[j+1] = student[0]; } } } } void twoPointSearchEnglish(int english){ int low = 1,high = count,mid = (low+high)/2; while(1){ if(student[mid].english > english && mid-1 >= low){ high = mid-1; mid = (low+high)/2; }else if(student[mid].english < english && mid+1 <= high){ low = mid+1; mid = (low+high)/2; }else if(student[mid].english == english){ print(mid); break; }else{ printf("没有找到信息n"); break; } } } void directSelectionSort(){ int small; for(int i = 1;i < count;i++){ small = i; for(int j = i+1;j <= count;j++){ if(student[j].dataStructure < student[small].dataStructure) small = j; } if(small != i){ student[0] = student[i]; student[i] = student[small]; student[small] = student[0]; } } } void twoPointSearchDataStructure(int dataStructure){ int low = 1,high = count,mid = (low+high)/2; while(1){ if(student[mid].dataStructure > dataStructure && mid-1 >= low){ high = mid-1; mid = (low+high)/2; }else if(student[mid].dataStructure < dataStructure && mid+1 <= high){ low = mid+1; mid = (low+high)/2; }else if(student[mid].dataStructure == dataStructure){ print(mid); break; }else{ printf("没有找到信息n"); break; } } // printf("low = %d,mid = %d,high = %dn",low,mid,high); } void calculate(){ //数学结果 int i; min = student[1].math; max = student[1].math; sum = 0; for(i = 1;i <= count;i++){ minSwap(student[i].math); maxSwap(student[i].math); avgSwap(student[i].math,i); } printf("数学最低分:%dn最高分:%dn平均分:%lfn",min,max,avg); //英语结果 min = student[1].english; max = student[1].english; sum = 0; for(i = 1;i <= count;i++){ minSwap(student[i].english); maxSwap(student[i].english); avgSwap(student[i].english,i); } printf("英语最低分:%dn最高分:%dn平均分:%lfn",min,max,avg); //数据结构结果 min = student[1].dataStructure; max = student[1].dataStructure; sum = 0; for(i = 1;i <= count;i++){ minSwap(student[i].dataStructure); maxSwap(student[i].dataStructure); avgSwap(student[i].dataStructure,i); } printf("数据结构最低分:%dn最高分:%dn平均分:%lfn",min,max,avg); } void minSwap(int value){ if(min > value){ min = value; } } void maxSwap(int value){ if(max < value){ max = value; } } void avgSwap(int value,int i){ sum += value; avg = sum/i; }



