zoukankan      html  css  js  c++  java
  • if、for、while、do 等语句自占一行

    if、for、while、do 等语句自占一行,执行语句不得紧跟其后。不论 执行语句有多少都要加{}。这样可以防止书写失误。

     1 #include <iostream>
     2 
     3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     4 using namespace std;
     5 //用函数原型声明要使用的函数
     6 void show_array1(int*,int);
     7 void show_array2(int a[],int);
     8 void sort(int*,int);
     9 
    10 int main(int argc, char** argv) {
    11     
    12         //声明数组并初始化
    13     int a[]={2,4,6,1,3,5};
    14     int b[3][3]={{2,4,6},{1,3,5},{0,1,2}};
    15     
    16     //显示数组的值
    17     cout<<"show_array1(int*,int):"<<endl;
    18     show_array1(a,6);
    19     show_array1(&b[0][0],3*3);
    20 
    21     //用sort1排序并显示
    22     cout<<"sort(int*,int) and show_array1(int*,int): "<<endl;
    23     sort(a,6);
    24     show_array1(a,6);
    25     sort(&b[0][0],3*3);
    26     show_array1(&b[0][0],9);
    27 
    28     //显示数组的值
    29     cout<<"show_array2(int a[],int):"<<endl;
    30     show_array2(a,6);
    31     show_array2(&b[0][0],3*3);
    32     return 0;
    33 }
    34 
    35 
    36 //显示数组,用指针当参数
    37 void show_array1(int *p,int size) {
    38     for(int i=0;i<size;i++)
    39         cout<<*(p+i)<<" ";
    40     cout<<endl;
    41 }
    42 
    43 //显示数组,用数组当参数
    44 void show_array2(int a[],int size) {
    45     for(int i=0;i<size;i++)
    46         cout<<a[i]<<" ";
    47     cout<<endl;
    48 }   
    49 
    50 //对数组按从大到小顺序排序
    51 void sort(int *p,int size) {
    52     int t;
    53     for (int i=0;i<size-1;i++)
    54         for (int j=i+1;j<size;j++)
    55             if (*(p+i)<=*(p+j))
    56             {
    57                t=*(p+i);
    58                *(p+i)=*(p+j);
    59                *(p+j)=t;
    60             }
    61 }
  • 相关阅读:
    【web安全】浅谈web安全之XSS
    【css】浅谈BFC
    nodejs笔记一--模块,全局process对象;
    逼真打字机效果;
    深入理解CSS3 animation的steps
    网址链接收藏
    用hoverclock插件实现HTML5时钟
    JS多种方法实现随机颜色;
    canvas实现跟随鼠标旋转的箭头
    封装insertAfter、addClass、格式化时间
  • 原文地址:https://www.cnblogs.com/borter/p/9413077.html
Copyright © 2011-2022 走看看