zoukankan      html  css  js  c++  java
  • C++ 第一次上机作业

    今天完成了C++第一次上机作业,感觉比较简单。

    题目:

    1. 求2个数或3个正整数中的最大数,用带有默认参数的函数实现。
    2. 对3个变量按由小到大顺序排序,要求使用变量的引用。
    3. 编写一个程序,用同一个函数名对几个数据进行从小到大排序,数据类型可以是整型、浮点型。用重载函数实现。
    4. 对第4题改用函数模板实现,并与第4题程序进行对比分析。

    第一题代码:

     1 #include<iostream>
     2 using namespace std;
     3 
     4 void max(int a=0,int b=0,int c=0)
     5 {
     6     a= a>b ? a : b;
     7     a= a>c ? a : c;
     8     cout<<a<<endl;
     9 }
    10 
    11 int main()
    12 {
    13     max(1,2);
    14     max(1,2,3);
    15     return 0;
    16 }

    第二题代码:

    #include<iostream>
    using namespace std;
    
    void sort(int &a,int &b,int &c)
    {
        int tmp;
        if(a>b) {tmp=a;a=b;b=tmp;}
        if(b>c) {tmp=b;b=c;c=tmp;}
        if(a>b) {tmp=a;a=b;b=tmp;}
    }
    
    int main()
    {
        int a1=3,a2=2,a3=1;
        sort(a1,a2,a3);
        cout<<a1<<" "<<a2<<" "<<a3<<endl;
    }

    第三题代码:

     1 #include<iostream>
     2 using namespace std;
     3 
     4 void sort(int* a){                     //冒泡排序
     5     int t;
     6     for(int j=4;j>0;j--)
     7         for(int i=0;i<=j-1;i++)
     8             if(a[i]>a[i+1])
     9             {t=a[i];a[i]=a[i+1];a[i+1]=t;}
    10     for(int i=0;i<5;i++)
    11         cout<<a[i]<<" ";
    12 }
    13 
    14 void sort(float* a){
    15     float t;
    16     for(int j=4;j>0;j--)
    17         for(int i=0;i<=j-1;i++)
    18             if(a[i]>a[i+1])
    19             {t=a[i];a[i]=a[i+1];a[i+1]=t;}
    20     for(int i=0;i<5;i++)
    21         cout<<a[i]<<" ";
    22 }
    23 
    24 int main()
    25 {
    26     int a[5]={5,4,2,1,3};
    27     float b[5]={5.1,4.1,2.1,1.1,3.1};
    28     sort(a);
    29     sort(b);
    30     return 0;
    31 }
    32     

    第四题代码:

     1 #include<iostream>
     2 using namespace std;
     3 template<typename T>
     4 int Partition(T* cp,int low,int high)
     5 {
     6     T tmp=cp[low];
     7     T pivotkey=cp[low];
     8     while(low<high)
     9     {
    10         while(low<high&&cp[high]>=pivotkey) --high;
    11         cp[low]=cp[high];
    12         while(low<high&&cp[low]<=pivotkey) ++low;
    13         cp[high]=cp[low];
    14     }
    15     cp[low]=tmp;
    16     return low;
    17 }
    18 template<typename T>
    19 void Qsort(T* cp,int low,int high)       //快速排序
    20 {
    21     if(low<high)
    22     {
    23         int pivotloc=Partition(cp,low,high);
    24         Qsort(cp,low,pivotloc-1);
    25         Qsort(cp,pivotloc+1,high);
    26     }
    27 }
    28 int main()
    29 {
    30     int a[5]={5,4,2,1,3};
    31     float b[5]={5.1,4.1,2.1,1.1,3.1};
    32     Qsort(a,0,4);
    33     Qsort(b,0,4);
    34     for(int i=0;i<5;i++)
    35     {
    36         cout<<a[i]<<" "<<b[i]<<endl;
    37     }
    38     return 0;
    39 }

    ******

  • 相关阅读:
    python笔记第十一天 模块补充
    python笔记第十天 模块
    python笔记第九天 装饰器
    python笔记第八天 迭代器与生成器
    python笔记第七天 文件操作
    python笔记第六天 函数和函数的内置方法
    C语言----指针形参(指向指针的指针形参)
    NEON使用方法
    ARM NEON指令集总结
    三维变换矩阵左乘和右乘分析
  • 原文地址:https://www.cnblogs.com/sage-blog/p/4029626.html
Copyright © 2011-2022 走看看