赞
踩
设计并实现一个管理系统(学生管理、图书管理、产品管理等等),系统中需包含1000条以上的记录,系统至少包含以下功能:
(1) 自动生成记录信息(或者从文件中读取);
(2) 逐条显示所有记录信息;
(3) 利用时间复杂度O(n2)的排序算法(例如直接插入排序或者折半插入排序)按照关键字进行排序;
(4) 利用时间复杂度O(nlogn)的排序算法(例如快速排序)按照关键字进行排序;
(5) 程序中统计(3)和(4)的算法在数据正序、逆序、乱序情况下的比较次数与移动次数并输出,在报告中对比较次数与移动次数进行分析。
提示:以下是本篇文章正文内容,下面案例可供参考
#include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> #include<ctime> #include<iomanip> using namespace std; struct student { int num; char name[5]; int grade; }a[2000],b[2000],c[2000],d[2000],e[2000],f[2000],g[2000],h[2000]; int stunum; //学生总数 int cmp2 = 0, mov2 = 0;//乱序 int cmppp2 = 0, movvv2 = 0;//逆序 void Assignment1(student *a,student *b){ for (int i = 0; i < stunum; i++){ a[i].num = b[i].num; strcpy(a[i].name,b[i].name); a[i].grade = b[i].grade; } } void Assignment2(student *a,student *b){ for (int i = 1; i <=stunum; i++){ a[i].num = b[i].num; strcpy(a[i].name,b[i].name); a[i].grade = b[i].grade; } } void Bubble_sort(student* a)//乱序冒泡排序 { int cmp=0; int mov=0; for (int i = 1;i < stunum;i++) { for (int j = 0;j < stunum - i;j++) { cmp++; if (a[j].grade < a[j + 1].grade) { student tt = a[j]; a[j] = a[j + 1]; a[j + 1] = tt; mov += 2; } else if (a[j].grade == a[j + 1].grade) { cmp++; if (a[j].num > a[j + 1].num) { student tt = a[j]; a[j] = a[j + 1]; a[j + 1] = tt; mov += 2; } else if (a[j].num == a[j + 1].num) { cmp++; if (strcmp(a[j].name,a[j + 1].name)>0) { student tt = a[j]; a[j] = a[j + 1]; a[j + 1] = tt; mov += 2; } } } } } cout <<setw(10)<<"序号" <<setw(10)<< "学生学号" <<setw(10)<< "学生姓名" <<setw(10)<< "学生成绩" <<endl; for (int i = 0;i < stunum;i++) cout <<setw(10)<<i + 1<<setw(11)<<a[i].num <<setw(12)<< a[i].name <<setw(12)<< a[i].grade << endl; cout << "乱序冒泡排序比较的次数:" << cmp << endl; cout << "乱序冒泡排序移动的次数:" << mov << endl; } void Bubble_sort_reverse(student* a)//逆序冒泡排序 { int cmp=0; int mov=0; for (int i = 1;i < stunum;i++) { for (int j = 0;j < stunum - i;j++) { cmp++; if (a[j].grade > a[j + 1].grade) { student tt = a[j]; a[j] = a[j + 1]; a[j + 1] = tt; mov += 2; } else if (a[j].grade == a[j + 1].grade) { cmp++; if (a[j].num > a[j + 1].num) { student tt = a[j]; a[j] = a[j + 1]; a[j + 1] = tt; mov += 2; } else if (a[j].num == a[j + 1].num) { cmp++; if (strcmp(a[j].name , a[j + 1].name)>0) { student tt = a[j]; a[j] = a[j + 1]; a[j + 1] = tt; mov += 2; } } } } } cout <<setw(10)<<"序号" <<setw(10)<< "学生学号" <<setw(10)<< "学生姓名" <<setw(10)<< "学生成绩" <<endl; for (int i = 0;i < stunum;i++) cout <<setw(10)<<i + 1<<setw(11)<<a[i].num <<setw(12)<< a[i].name <<setw(12)<< a[i].grade << endl; cout << "逆序冒泡排序比较的次数:" << cmp << endl; cout << "逆序冒泡排序移动的次数:" << mov << endl; } void Quick_Sort(student* arr, int begin, int end)//乱序快速排序 { if (begin > end) return; student tmp = arr[begin]; int i = begin; int j = end; while (i != j) { while (arr[j].grade <= tmp.grade && j > i) { cmp2++; if (arr[j].grade == tmp.grade && arr[j].num < tmp.num)break; j--; } while (arr[i].grade >= tmp.grade && j > i) { cmp2++; if (arr[i].grade == tmp.grade && arr[i].num > tmp.num)break; i++; } if (j > i) { mov2 += 2; swap(arr[i], arr[j]); } } arr[begin] = arr[i]; arr[i] = tmp; Quick_Sort(arr, begin, i - 1); Quick_Sort(arr, i + 1, end); } void Quick_Sort_Reverse(student* arr, int begin, int end)//逆序快速排序 { if (begin > end) return; student tmp = arr[begin]; int i = begin; int j = end; while (i != j) { while (arr[j].grade >= tmp.grade && j > i) { cmppp2++; if (arr[j].grade == tmp.grade && arr[j].num > tmp.num)break; j--; } while (arr[i].grade <= tmp.grade && j > i) { cmppp2++; if (arr[i].grade == tmp.grade && arr[i].num < tmp.num)break; i++; } if (j > i) { movvv2 += 2; swap(arr[i], arr[j]); } } arr[begin] = arr[i]; arr[i] = tmp; Quick_Sort_Reverse(arr, begin, i - 1); Quick_Sort_Reverse(arr, i + 1, end); } void System_Interface() { cout << " <<学生管理系统>>" << endl; cout << "请输入需要生成的学生信息数量:" << endl; cin >> stunum; cout << "显示生成的学生信息:" << endl; cout <<setw(10)<<"序号" <<setw(10)<< "学生学号" <<setw(10)<< "学生姓名" <<setw(10)<< "学生成绩" <<endl; srand((unsigned)time(NULL)); for (int i = 0; i < stunum; i++) { a[i].num = rand()%(901)+100; a[i].name[0] = rand() % 26 + 'a'; a[i].name[1] = rand() % 26 + 'a'; a[i].name[2] = rand() % 26 + 'a'; a[i].grade = rand() % 101; cout <<setw(10)<<i + 1<<setw(11)<<a[i].num <<setw(12)<< a[i].name <<setw(12)<< a[i].grade << endl; b[i+1].num = a[i].num; b[i+1].name[0] = a[i].name[0]; b[i+1].name[1] = a[i].name[1]; b[i+1].name[2] = a[i].name[2]; b[i+1].grade = a[i].grade; } int choice=-1; while(choice!=0){ cout<<"----------------------------------------"<<endl; cout<<"请选择想要进行的操作:"<<endl; cout<<"1、乱序冒泡排序"<<endl; cout<<"2、乱序快速排序"<<endl; cout<<"3、正序冒泡排序"<<endl; cout<<"4、正序快速排序"<<endl; cout<<"5、逆序冒泡排序"<<endl; cout<<"6、逆序快速排序"<<endl; cout<<"0、退出排序操作"<<endl; cout<<"----------------------------------------"<<endl; cin>>choice; switch (choice) { case 1: Assignment1(c,a); Bubble_sort(c); break; case 2: Assignment2(d,b); Quick_Sort(d,1,stunum); cout <<setw(10)<<"序号" <<setw(10)<< "学生学号" <<setw(10)<< "学生姓名" <<setw(10)<< "学生成绩" <<endl; for (int i = 1;i <=stunum ;i++) cout <<setw(10)<<i << setw(11) << d[i].num << setw(12) << d[i].name << setw(10) << d[i].grade << endl; cout << "乱序快速排序比较的次数:" << cmp2 << endl; cout << "乱序快速排序移动的次数:" << mov2 << endl; break; case 3: Assignment1(e,a); Bubble_sort(e); break; case 4: Assignment2(f,b); Quick_Sort(f,1,stunum); cout <<setw(10)<<"序号" <<setw(10)<< "学生学号" <<setw(10)<< "学生姓名" <<setw(10)<< "学生成绩" <<endl; for (int i = 1;i <= stunum ;i++) cout <<setw(10)<<i << setw(11) << f[i].num << setw(12) << f[i].name << setw(10) << f[i].grade << endl; cout << "正序快速排序比较的次数:" << cmp2 << endl; cout << "正序快速排序移动的次数:" << mov2 << endl; break; case 5: Assignment1(g,a); Bubble_sort_reverse(g); break; case 6: Assignment2(h,b); Quick_Sort_Reverse(h,1,stunum); cout <<setw(10)<<"序号" <<setw(10)<< "学生学号" <<setw(10)<< "学生姓名" <<setw(10)<< "学生成绩" <<endl; for (int i =1;i <= stunum ;i++) cout <<setw(10)<<i << setw(11) << h[i].num << setw(12) << h[i].name << setw(10) << h[i].grade << endl; cout << "逆序快速排序比较的次数:" << cmppp2 << endl; cout << "逆序快速排序移动的次数:" << movvv2 << endl; break; case 0: exit(100); break; } } } int main() { cout.setf(std::ios::left); System_Interface(); return 0; }
生成数据
乱序冒泡
乱序快排
正序冒泡
正序快排
逆序冒泡
逆序快排
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。