插入排序的性能测试对比(C与C++实现)

发表于:2017-10-09 15:02

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:pengyang    来源:51Testing软件测试网采编

  一、概述:
  【标题】学生成绩管理的设计与实现
  【开发语言】C、C++
  【主要技术】结构体、STL
  【基本功能】实现对学生成绩类的基本操作:增加、删除、查询、排序
  【测试数据】功能测试:按提示输入5组正确的正确的数据和5组有问题的数据,查看程序能否运行正确
        性能测试:随机生成1、5、10、15万条数据,查看程序完成按总分排序所用的时间及打印完成的时间
  【测试结果】功能测试:基本功能运行正确,没有进行异常处理
   
  【C++版本性能数据】
 
   【C语言版本新性能数据】
   
  【结论】使用C语言实现排序和打印所用时间少,消耗内存更多
  二、C语言版本详细设计
    1 #include<stdio.h>
    2 #include<stdlib.h>
    3 #include<string.h>
    4 #include<time.h>
    5 
    6 /*学生结构体*/
    7 struct student{
    8     char name[10];
    9     char no[10];
   10     double couse[4];
   11     double sum ;
   12 };
   13 
   14 /*链表节点*/
   15 typedef struct node{
   16     struct student st;
   17     struct node *Next;
   18 }Node;
   19 
   20 int Add(Node *Head);/*增加学生*/
   21 void Print(Node *Head);/*打印学生信息*/
   22 int Find(Node *Head);/*查询学生*/
   23 int Del(Node *Head);/*删除学生*/
   24 void sort_sum(Node *Head);/*按总成绩排序*/
   25 void sort_no(Node *Head);/*按学号排序*/
   26 void main_remid();/*主要提示信息*/
   27 void score_remind();/*成绩提示信息*/
   28 
   29 int main()
   30 {
   31     Node* Head = (struct node *)malloc(sizeof (Node));
   32     Head->Next = NULL;
   33     char op = ' ';
   34     main_remid();
   35     while(op != 'q')
   36     {
   37         scanf("%c", &op);
   38         switch(op)
   39         {
   40             case '1':
   41                 sort_no(Head);
   42                 break;
   43             case '2':    
   44                 Add(Head);
   45                 break;
   46             case '3':
   47                 Find(Head);
   48                 break;
   49             case '4':
   50                 Del(Head);
   51                 break;
   52             case '5':
   53                 sort_sum(Head);
   54                 break;
   55             case '6':
   56                 main_remid();
   57                 break;
   58             default:
   59                 printf("输入指令未知,请重新输入\n");
   60                 break;            
   61         }
   62         printf("请继续选择您想要的操作:\n");
   63         fflush(stdin);
   64     }
   65     return 0;
   66 }
   67 
   68 /*增加学生*/
   69 int Add(Node *Head)
   70 {        
   71     Node* p = (struct node*)malloc(sizeof (Node));
   72     p->st.sum = 0.0;
   73     printf("请输入学生的学号\n");
   74     scanf("%s", p->st.no);
   75     printf("请输入学生的姓名\n");
   76     scanf("%s", p->st.name);
   77     printf("请输入学生的英语成绩\n");
   78     scanf("%lf", &p->st.couse[0]);
   79     p->st.sum += p->st.couse[0];
   80     printf("请输入学生的数学成绩\n");
   81     scanf("%lf", &p->st.couse[1]);
   82     p->st.sum += p->st.couse[1];
   83     printf("请输入学生的语文成绩\n");
   84     scanf("%lf", &p->st.couse[2]);
   85     p->st.sum += p->st.couse[2];
   86     printf("请输入学生的C语言成绩\n");
   87     scanf("%lf", &p->st.couse[3]);
   88     fflush(stdin);
   89     p->st.sum += p->st.couse[3];
   90     if(Head->Next == NULL)
   91     {
   92         p->Next = NULL;
   93         Head ->Next = p;
   94     }
   95     else
   96     {
   97         p->Next = Head ->Next;
   98         Head->Next = p;
   99     }
  100     return 0;
  101 }
  102 
  103 /*按总成绩排序*/
  104 void sort_sum(Node *Head)
  105 {
  106     Node *p = Head;
  107     Node *temp = (struct node*)malloc(sizeof (Node));
  108     Node *first = (struct node*)malloc(sizeof (Node));
  109     memcpy(first, p->Next ,sizeof (Node));
  110     temp->Next = first;
  111     first->Next = NULL;
  112     p = p->Next;
  113     while(p->Next != NULL)
  114     {
  115         Node *t = temp;
  116         while(t->Next!=NULL && p->Next->st.sum < t->Next->st.sum)
  117             t = t->Next;
  118         Node *q = (struct node*)malloc(sizeof (Node));
  119         memcpy(q, p->Next, sizeof(Node));
  120         q->Next = t->Next;
  121         t->Next = q;        
  122         p = p->Next;
  123     }
  124     Print(temp);
  125     p = temp;
  126     Node *q = temp->Next;
  127     while(q->Next != NULL)
  128     {
  129         free(p);
  130         p = q;
  131         q = q->Next;        
  132     }
  133     free(p);
  134     free(q); 
  135 }
  136 
  137 /*按学号排序*/
  138 void sort_no(Node *Head)
  139 {
  140     Node *p = Head;
  141     Node *temp = (struct node*)malloc(sizeof (Node));
  142     Node *first = (struct node*)malloc(sizeof (Node));
  143     memcpy(first, p->Next ,sizeof (Node));
  144     temp->Next = first;
  145     first->Next = NULL;
  146     p = p->Next;
  147     while(p->Next != NULL)
  148     {
  149         Node *t = temp;
  150         while(t->Next!=NULL && atoi(p->Next->st.no)>atoi(t->Next->st.no))
  151             t = t->Next;
  152         Node *q = (struct node*)malloc(sizeof (Node));
  153         memcpy(q, p->Next, sizeof(Node));
  154         q->Next = t->Next;
  155         t->Next = q;
  156         
  157         p = p->Next;
  158     }
  159     Print(temp);
  160     p = temp;
  161     Node *q = temp->Next;
  162     while(q->Next != NULL)
  163     {
  164         free(p);
  165         p = q;
  166         q = q->Next;        
  167     }
  168     free(p);
  169     free(q); 
  170 }
  171 
  172 /*void sort_sum1(Node *Head)
  173 {
  174     Node *temp;
  175     Node *p = Head;
  176     temp->Next2 = p->Next;
  177 
  178 
  179     p = p->Next;
  180     p->Next2 = NULL;
  181     while(p->Next != NULL)
  182     {
  183         Node *t = temp;
  184         while(t->Next2!=NULL && p->Next->st.sum<t->Next2->st.sum)
  185             t = t->Next2;
  186         p->Next->Next2 = t->Next2;
  187         t->Next2 = p->Next;    
  188         
  189         p = p->Next;    
  190     }
  191     Print(temp);
  192     
  193 }*/
  194 
  195 /*打印学生信息*/
  196 void Print(Node *Head)
  197 {
  198     Node* p = Head;
  199     score_remind();
  200     while(p->Next != NULL)
  201     {
  202         printf("\t%s", p->Next->st.no);
  203         printf("\t%s", p->Next->st.name);
  204         printf("\t%.1lf", p->Next->st.couse[0]);
  205         printf("\t%.1lf", p->Next->st.couse[1]);
  206         printf("\t%.1lf", p->Next->st.couse[2]);
  207         printf("\t%.1lf", p->Next->st.couse[3]);
  208         printf("\t%.1lf\n", p->Next->st.sum);
  209         p = p->Next;
  210     }    
  211     p = NULL;
  212 }
  213 
  214 /*查询学生*/
  215 int Find(Node *Head)
  216 {
  217     Node* p = Head;
  218     printf("请输入要查找学生的学号:\n");
  219     char no[10];
  220     scanf("%s", no);
  221     while(p->Next != NULL)
  222     {
  223         if(!strcmp(no, p->Next->st.no))
  224         {
  225             score_remind();
  226             printf("\t%s", p->Next->st.no);
  227             printf("\t%s", p->Next->st.name);
  228             printf("\t%.1lf", p->Next->st.couse[0]);
  229             printf("\t%.1lf", p->Next->st.couse[1]);
  230             printf("\t%.1lf", p->Next->st.couse[2]);
  231             printf("\t%.1lf", p->Next->st.couse[3]);
  232             printf("\t%.1lf\n", p->Next->st.sum);    
  233             p = NULL;
  234             return 1;        
  235         }        
  236         p = p->Next;
  237     }
  238     printf("查找失败,不存在次学号\n");
  239     p = NULL;
  240     return 0;
  241 }
  242 
  243 /*删除学生*/
  244 int Del(Node *Head)
  245 {
  246     Node* p = Head;
  247     printf("请输入要删除学生的学号:\n");
  248     char no[10];
  249     scanf("%s", no);
  250     while(p->Next != NULL)
  251     {    
  252         if(!strcmp(no, p->Next->st.no))
  253         {
  254             Node *q = p->Next;
  255             p->Next = p->Next->Next;
  256             printf("%s  %s删除成功\n",q->st.no, q->st.name);
  257             free(q);
  258             q = NULL;        
  259             return 1; 
  260         }
  261         p = p->Next;
  262     }
  263     printf("不存在此学号\n");
  264 }
  265 
  266 /*主要提示信息*/
  267 void main_remid()
  268 {
  269     printf("\t\t\t学生成绩类\n");
  270     printf("\t\t1.查询所有学生的成绩信息\n");
  271     printf("\t\t2.增加学生\n");
  272     printf("\t\t3.查找学生\n");
  273     printf("\t\t4.删除学生\n");
  274     printf("\t\t5.查看总分排名\n");
  275     printf("\t\t6.查看此提示\n");
  276     printf("\t\tq.退出系统\n\n"); 
  277 }
  278 
  279 /*成绩提示信息*/
  280 void score_remind()
  281 {
  282     printf("\t\t\t 学生成绩信息\n");
  283     printf("\t学号\t姓名\t数学\t英语\t语文\tC语言\t总成绩\n");
  284 }
  三、C++版本的详细设计
    1 #include<iostream>
    2 #include<string>
    3 #include<list>
    4 #include<algorithm>
    5 using namespace std;
    6 
    7 class student{
    8 private:
    9     string No;
   10     string Name;
   11     double Math;
   12     double Eng;
   13     double Chn;
   14     double Cpro;
   15     double Sum;
   16 public:
   17         student(string no, string name, double math, double eng,
   18                 double chn, double cpro){
   19                 No = no;
   20                 Name = name;
   21                 Math = math;
   22                 Eng = eng;
   23                 Chn = chn;
   24                 Cpro = cpro;
   25                 Sum = math + eng + chn + cpro;
   26                 }
   27 
   28         friend ostream& operator <<(ostream& out, student& S)
   29         {
   30             out << "\t" << S.No << "\t" << S.Name << "\t" << S.Math << "\t"
   31              << S.Eng << "\t" << S.Chn << "\t" << S.Cpro << "\t" << S.Sum;
   32             return out;
   33         }
   34         const string getno()
   35         {
   36            return No;
   37         }
   38         const string getname()
   39         {
   40             return Name;
   41         }
   42         const double getsum()
   43         {
   44             return Sum;
   45         }
   46          ~student(){
   47         
   48          }
   49 };
   50 
   51 void main_remid(); /*输出主要提示信息 */
   52 void score_remind(); /*输出成绩提示信息*/
   53 int add(list<student> &lst); /*增加学生*/
   54 int find(list<student> &lst); /*查询学生信息*/
   55 int del(list<student> &lst); /*删除学生*/
   56 void sort_sum(list<student> &lst); /*按总成绩降序打印学生成绩*/
   57 void sort_no(list<student> &lst); /*按学号升序打印学生成绩*/
   58 bool cmp_no(student& st1, student& st2); /*用于sort的比较函数*/
   59 
   60 int main()
   61 {
   62     list<student> lst; /*使用list容器存储学生信息*/
   63     char op = ' ';
   64     main_remid();
   65     while(op != 'q')
   66     {
   67         cin >> op;
   68         switch(op)
   69         {
   70             case '1':              
   71                 sort_no(lst);
   72                 break;
   73             case '2':
   74                 add(lst);
   75                 break;
   76             case '3':
   77                 find(lst);
   78                 break;
   79             case '4':
   80                 del(lst);
   81                 break;
   82             case '5':
   83                 sort_sum(lst);
   84                 break;
   85             case '6':
   86                 main_remid();
   87                 break;
   88             default:
   89                 cout << "输入指令未知,请重新输入" << endl;
   90                 break;
   91         }
   92         if(op != 'q')
   93             cout << " 请继续选择您想要的操作:" << endl;
   94     }
   95     return 0;
   96 }
   97 
   98 /*增加学生*/
   99 int add(list<student> &lst)
  100 {
  101     string No;
  102     string Name;
  103     double Math;
  104     double Eng;
  105     double Chn;
  106     double Cpro;  
  107     cout << " 请输入要增加学生的学号:" << endl;
  108      cin >> No;
  109      for(list<student>::iterator it=lst.begin(); it!=lst.end(); it++)
  110          if(No == it->getno()){
  111              cout << "添加失败,此学号已存在,请重新操作" << endl;
  112              return 0;
  113          }                                       
  114      cout << " 请输入要增加学生的姓名" << endl;
  115      cin >> Name;
  116      cout << " 请输入要增加学生的数学成绩:" << endl;
  117      cin >> Math;
  118      cout << " 请输入要增加学生的英语成绩:" << endl;
  119      cin >> Eng;
  120      cout << " 请输入要增加学生的语文成绩:" << endl;
  121      cin >> Chn;
  122      cout << " 请输入要增加学生的C语言成绩:" << endl;
  123      cin >> Cpro;
  124      student *st = new student(No, Name, Math, Eng, Chn, Cpro);
  125      lst.push_back(*st);    
  126 }
  127 
  128 /*查询学生信息*/
  129 int find(list<student> &lst)
  130 {
  131     cout << "请输入要查询学生的学号:" << endl;
  132     string no;
  133     cin >> no;
  134     for(list<student>::iterator it=lst.begin(); it!=lst.end(); it++)
  135     {
  136         if(no == it->getno())
  137         {
  138             score_remind();
  139             cout << *it << endl;
  140             return 0;
  141         }
  142     }
  143         cout << "不存在此学号,请重新选择操作" << endl;
  144 }
  145 
  146 /*删除学生*/
  147 int del(list<student> &lst)
  148 {
  149     cout << " 请输入要删除学生的学号:" << endl;
  150     string no;
  151     string name;
  152     cin >> no;
  153     for(list<student>::iterator it=lst.begin(); it!=lst.end(); it++)
  154         if(no == it->getno())
  155         {
  156             no = it->getno();
  157             name = it->getname();
  158             lst.erase(it);
  159             cout << "学生" << no << " " << name << "删除成功" << endl;
  160             return 0;
  161         }
  162     cout << " 删除失败,不存在此学号" << endl;
  163 }
  164 
  165 /*按学号升序打印学生成绩*/
  166 void sort_no(list<student> &lst)
  167 {
  168     list<student> temp;
  169     temp.push_front(*lst.begin());
  170     for(list<student>::iterator it=++lst.begin(); it!=lst.end(); it++)
  171     {
  172         list<student>::iterator jt = temp.begin();
  173         while(jt!=temp.end() && strcmp(it->getno().c_str(), jt->getno().c_str()))
  174             jt++;
  175         temp.insert(jt, *it);
  176     } 
  177     score_remind();
  178     for(list<student>::iterator it=temp.begin(); it!=temp.end(); it++)
  179     {
  180         cout << *it << endl;    
  181     }
  182 }
  189 
  190 /*按成绩升序打印学生成绩*/
  191 void sort_sum(list<student> &lst)
  192 {
  193     list<student> temp;
  194     temp.push_front(*lst.begin());
  195     for(list<student>::iterator it=++lst.begin(); it!=lst.end(); it++)
  196     {
  197         list<student>::iterator jt = temp.begin();
  198         while(jt!=temp.end() && it->getsum()<jt->getsum())
  199             jt++;
  200         temp.insert(jt, *it);
  201     } 
  202     score_remind();
  203     for(list<student>::iterator it=temp.begin(); it!=temp.end(); it++)
  204     {
  205         cout << *it << endl;    
  206     }
  207 }
  208 
  209 /*输出主要提示信息 */
  210 void main_remid()
  211 {
  212     cout << "\t\t\t学生成绩类" << endl << endl;
  213     cout << "\t\t1.查询所有学生的成绩信息" << endl;
  214     cout << "\t\t2.增加学生" << endl;
  215     cout << "\t\t3.查找学生" << endl;
  216     cout << "\t\t4.删除学生" << endl;
  217     cout << "\t\t5.查看总分排名" << endl;
  218     cout << "\t\t6.查看提示" << endl;
  219     cout << "\t\tq.退出系统" << endl << endl;
  220 }
  221 
  222 /*输出成绩提示信息*/
  223 void score_remind()
  224 {
  225     cout << "\t\t\t 学生成绩信息" << endl << endl;
  226     cout << "\t学号\t" << "姓名\t" << "数学\t" << "英语\t"
  227     << "语文\t" << "C语言\t" << "总成绩" << endl;
  228 }
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号