zoukankan      html  css  js  c++  java
  • 06函数

     函数 是具有特定功能的代码段,也就是一连串语句组合在一起,实现了某一功能,我们为它们起了一个名字来替代这些语句

    void hello(void) //无参数 无返回值

    {

        printf("Hello,Lanou");

    }

    void peopleCount(void)//无参数 有返回值

    {

       return 3;

    }

    int max(int a ,int b) //有参数 无返回值

    {

       printf("number is %d",x);

    }

    C语⾔言不允许函数嵌套定义,但是允许函数嵌套调⽤用。! 注意避免嵌套定义。 

    //    1、(**)一维浮点型数组的冒泡排序函数

       float array1[]={1.3, 5.7, 2.5, 5.6, 6.6};

        array(array1, 5);

    //    2、(***)编写实现 strlen 同功能的函数 stringLength。

        char array[]="iphone1";

        int length = 0;

       length = stringLength(array);

        printf("length:%d ",length);

    //    3、(***)编写实现 strcpy 同功能的函数 stringCopy。

       char string1[10]="iphone2";

       char string2[10]="";

        stringCopy(string1, string2);

    //    4、(***)编写实现 strcat 同功能的函数 stringCatch。

        stringCatch(array, string1, 8);

    //    5、(***)编写实现 strcmp 同功能的函数 stringCompare。

        char a[10]="mac";

        char b[10]="macd";

       int num= stringCompare(a,b);

        printf("%d ",num);

     1     
     2 //    1. (**)定义一个结构体变量(包括年、月、日),计算该日在本年中为第
     3 //    几天?(注意考虑闰年问题),要求写一个函数 days,实现上面的计算。 由主函数将年月日传递给 days 函数,计算后将日子传递回主函数输出。
     4    Year myYear ={0};
     5     days(myYear);
     6 
     7 /定义结构体变量包括:年 月 日
     8 typedef struct{
     9     int year;
    10     int mouth;
    11     int date;
    12 } Year;
    13 
    14 void days(Year year);
    16 void days(Year year)
    17 {
    18     printf("请输入年月日:");
    19     scanf("%d %d %d", &year.year,&year.mouth ,&year.date);
    20     int sumday = 0;
    21     int mouth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31,30,31};
    22     if (!(year.mouth % 400) || !((year.mouth % 4) && year.mouth %100)) {
    23         mouth[1] = 29; //判断闰年
    24     }
    25     for (int i = 0; i < year.mouth - 1; i++) {
    26         sumday +=mouth[i]; //计算前几个月的天数
    27     }
    28     sumday +=year.date; //总天数,即前几个月的天数加上本月天数
    29     printf("%d年%d月%d日是%d年的第%d天",year.year, year.mouth,year.date,year.year,sumday);
    30 }

      

     1 /    2. (***)某班有 5 个学生,三门课。分别编写 3 个函数实现以下要求:
     2 //    (1) 求各门课的平均分;
     3 //    (2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;
     4 //    (3) 找出三门课平均成绩在 85-90 分的学生,并输出其学号和姓名
     5     Student stu[]={
     6         {"zhangsan", 111, 50, 56, 80, 90},
     7         {"lisi", 112, 89, 88,84,0},
     8         {"panny",122, 78, 35, 65,90},
     9         {"wang",123, 56, 76, 34, 32},
    10         {"wanghong", 124, 43, 23, 53, 64}
    11     };
    12     int count = sizeof(stu)/sizeof(stu[0]);
    13     averageScore(stu, count);
    14     printfFailStudent(stu, count);
    15     printfWellStudent(stu, count);
    16 
    17 // (1) 求各门课的平均分;
    18 void averageScore(Student stu[],int count);
    19 //找出两门不及格学生并输出他们的学号和不及格科目的成绩
    20 void printfFailStudent(Student stu[],int count);
    21 //实现找出平均分在[85,90]中的学生并输出其姓名及学号
    22 
    23 //求平均分
    24 void averageScore(Student stu[],int count)
    25 {
    26     for (int i = 0; i < count ; i++) {
    27         stu[i].average = (stu[i].math+stu[i].chinese+stu[i].English)/3;
    28         printf("%s的平均分是:%.2f
    ",stu[i].name,stu[i].average);
    29     }
    30 }
    31 //找出两门不及格学生并输出他们的学号和不及格科目的成绩
    32 void printfFailStudent(Student stu[],int count)
    33 {
    34     for (int i = 0; i < count; i++) {
    35         int flag = 0;
    36         stu[i].math < 60 ? flag++ : flag;//如果成绩小于60,flag 自增,即不及格科数加一
    37         stu[i].chinese < 60 ? flag++ :flag;
    38         stu[i].English < 60 ? flag++ :flag;
    39         if (flag >= 2) {
    40             printf("
    %s 有%d 门不及格分别是: ",stu[i].name,flag);
    41        
    42         if (stu[i].math < 60) {
    43             printf("math = %d",stu[i].math);
    44         }
    45         if (stu[i].chinese < 60) {
    46             printf("chinese = %d",stu[i].chinese);
    47         }
    48         if (stu[i].English < 60) {
    49             printf("English =%d ",stu[i].English);
    50         }
    51     }
    52   }
    53 }
    54 //实现找出平均分在[85,90]中的学生并输出其姓名及学号
    55  void printfWellStudent(Student stu[],int count)
    56 {
    57     for (int i = 0; i < 3; i++) {
    58         if (stu[i].average >=85 && stu[i].average <= 90) {
    59             printf("
    %s的平均分为%.2f,他的学号是:%d",stu[i].name, stu[i].average,stu[i].number);
    60         }
    61     }
    62 }

     

     

     

     

     

  • 相关阅读:
    SqlServer 查看被锁的表和解除被锁的表
    Windows Server 2012 R2 或 2016 无法安装 .Net 3.5.1
    请求文件下载URL过长处理
    T4语法
    windows下 安装 rabbitMQ 及操作常用命令
    ubuntu系统启动qtceator时提示:Qt5.5.1/Tools/QtCreator/lib/qtcreator/plugins/libHelp.so: 无法加载库
    升级到VS2013常见问题
    Windowns 无法启动 Office Software Protection Platform 服务,系统找不到指定的文件
    SVN clean失败解决方法
    使用PostSharp在.NET平台上实现AOP
  • 原文地址:https://www.cnblogs.com/panny/p/4075144.html
Copyright © 2011-2022 走看看