zoukankan      html  css  js  c++  java
  • C++ 排序、查找的应用

     1 // order.cpp : 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include "string.h"
     6 #include <iostream>
     7 
     8 #define length 26
     9 using namespace std;
    10 /*********选择排序*********/
    11 void order(char *a,int n)
    12 {
    13    int i = 0,j = 0;
    14    char temp;
    15    for(i = 0; i < n; i++)
    16        for(j = i+1; j < n; j++)
    17        {
    18            if(a[i] > a[j])
    19            {
    20                temp = a[i];
    21                a[i] = a[j];
    22                a[j] = temp; 
    23            }
    24 
    25        }
    26 }
    27 /**********查找函数*************/
    28 void seek(char *a,int n ,char c)
    29 {
    30     int i = 0,flag = 0;
    31     for(i = 0; i < n; i++)
    32     {
    33         if(a[i] == c)
    34         { 
    35             flag = 1;
    36         }
    37     }
    38     if(flag == 1)
    39     {
    40         cout << "找到所要找的字母"<< endl;
    41     }
    42     else 
    43         cout << "没有找到所要找的字母" << endl;
    44 }
    45 
    46 
    47 int main()
    48 {
    49     char a[length];
    50     char find;
    51     int b;
    52     cout << "请输入"<< length <<"个字母" <<endl;
    53     for(int i = 0;i < length; i++)
    54     {
    55         cin >> a[i];
    56     }
    57         
    58 
    59     for(int j = 0;j < length; j++)
    60     {
    61          cout << a[j]<<"  ";
    62     }
    63          cout << endl;
    64    
    65         order(a,length);
    66         cout <<"输出有序数组:"<<endl;
    67     for(int j = 0;j < length; j++)
    68         cout << a[j]<<"  ";
    69         cout << endl;
    70     
    71     cout << "请输入需要查找的字母" <<endl;
    72         cin >> find;
    73         cout << endl;
    74 
    75     seek(a,length,find);
    76     cin >> b;
    77     return 0;
    78 }

    实验名称:排序、查找的应用

    实验目的:学会如何应用排序算法和查找算法实现排序、查找。

    实验要求:先从键盘上输入26个字母生成无序数组,对数组进行排序,再从键盘输入一个字符进行查找。

    实验步骤及内容

    1、从键盘输入26个字母并输出。

     cout << "请输入"<< length <<"个字母" <<endl;

        for(int i = 0;i < length; i++)

        {

            cin >> a[i];

        }

           

     

        for(int j = 0;j < length; j++)

        {

             cout << a[j]<<"  ";

        }

         cout << endl;

    2、  对字符数组进行排序。这里用了选择排序来对数组进行排序。

     void order(char *a,int n)

    {

       int i = 0,j = 0;

       char temp;

       for(i = 0; i < n; i++)

           for(j = i+1; j < n; j++)

           {

               if(a[i] > a[j])

               {

                   temp = a[i];

                   a[i] = a[j];

                   a[j] = temp;

               }

     

           }

    }

    3、  查找函数,若找到则提示已经找到,否则提示没有找到。

      void seek(char *a,int n ,char c)

    {

        int i = 0,flag = 0;

        for(i = 0; i < n; i++)

        {

            if(a[i] == c)

            {

                flag = 1;

            }

        }

        if(flag == 1)

        {

            cout << "找到所要找的字母"<< endl;

        }

        else

            cout << "没有找到所要找的字母" << endl;

    }

    实验总结:

    在弄选择排序的时候,把j = i + 1,写成了j = i,然后老是错,最后终于找到错误的根源。

  • 相关阅读:
    D-Bus,kdbus和Binder
    Android init system replaced with systemd & binder replaced by kdbus
    在 Android 上 chroot 一个 ArchLinux
    An experiment in porting the Android init system to GNU/Linux
    Android init
    Linux和RISC-V基金会宣合作,打造开源CPU!
    How does systemd use /etc/init.d scripts?
    SysV, Upstart and systemd init script coexistence
    Purism Shows Off Latest GNOME Mobile Shell Mockups For The Librem 5
    One Widget to Adapt Them All and to The Librem 5 Port Them
  • 原文地址:https://www.cnblogs.com/zhuxuekui/p/3474768.html
Copyright © 2011-2022 走看看