zoukankan      html  css  js  c++  java
  • 结构~函数~输入输出

    /*结构作为函数参数
    int numberofdays(struct date d)
        整个结构可以作为参数传入函数
        这时候在函数内部新建一个结构变量,并
        复制调用者的结构的值
        也可以返回一个结构
        这与数组完全不同
    */
    /*
    //要求对方输入今天的日期,你输出明天的日期
    #include<stdio.h>
    #include<stdbool.h>  //声明调用布尔类型

    struct date
    {
        int month;
        int day;
        int year;
    };         //建立一个结构关于month day year

    bool isleap(struct date d);    //声明创建一个布尔类型的函数
    int numberofdays(struct date d); //声明建立以函数

    int main(int argc,char const *argv[])
    {
        struct date today,tomorrow;  //声明结构的名称
        
        printf("Enter today's date(mm dd yyyy):");
        scanf("%i %i %i",&today.month,&today.day,&today.year);
        
        if(today.day!=numberofdays(today))  //判断这一天是否处于闰年的二月
        {
            tomorrow.day=today.day+1;
            tomorrow.month=today.month;
            tomorrow.year=today.year;
        }else if(today.month == 12)
        {
            tomorrow.day=1;
            tomorrow.month=1;
            tomorrow.year=(int)today.year+1;
        }else
        {
            tomorrow.day=1;
            tomorrow.month=today.month+1;
            tomorrow.year=today.year;
        }
        
        printf("Tomorrrow's date is %i-%i-%i ",
                tomorrow.year,tomorrow.month,tomorrow.day);
        return 0;        
    }

    int numberofdays(struct date d)   //
    {
        int days;
        
        const int dayspermonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
        
        if(d.month==2&&isleap(d))
            days = 29;
        else
            days=dayspermonth[d.month-1];
            
        return days;
    }

    bool isleap(struct date d) //用布尔类型判断这一年是否是闰年
    {
        bool leap =false;
        
        if(d.year%4==0&&d.year%100!=0||d.year%400==0)
        {
            leap =true;
        }
        return leap;
    }

    */

    /*输入结构  
        没有直接的方式可以一次scanf一个结构
        如果我们打算写一个函数来读入结构
        
        但是读入的结构如何送回来呢?
            记住C在调用函数时是传值的
    */
    /*
    #include<stdio.h>

    struct point
    {
        int x;
        int y;
    };

    void getstruct(struct point);
    void output(struct point);
    void main()
    {
        struct point y={0,0};
        getstruct(y);
        output(y);
    }

    void getstruct(struct point p)
    {
        scanf("%d",&p.x);
        scanf("%d",&p.y);
        printf("%d,%d ",p.x,p.y);
    }

    void output(struct point p)
    {
        printf("%d,%d ",p.x,p.y);
    }

    //>>5 9
      5,9
      0,0
    */

    /*      之前的方案,吧一个结构传入函数,然后在函数
        中操作,但是没有返回回去
          问题在于传入函数的是在外面那个结构的克隆体
          而不是指针
                  传入结构和传入数组是不同那个的
        在这个输入函数中,完全可以创建一个临时
        变量,然后把这个结构返回给调用者
    */
    /*
    #include<stdio.h>

    struct point
    {
        int x;
        int y;
    } ;

    struct point getstruct(void); //声明两个函数
    //void output(struct point);

    int main(int argc,char const *argv[])
    {
        struct point y={0,0};
        y=getstruct();
        output(y);
    }

    struct point getstruct(void)
    {
        struct point p;
        scanf("%d",&p.x);
        scanf("%d",&p.y);
        printf("%d,%d ",p.x,p.y);
    }

    void output(struct point p)
    {
        printf("%d,%d ",p.x,p.y);
    }

    */

    /*    结构指针作为参数
        K&R说过
        “If a large structure is to be passed to a
        function ,it is generally more efficient to
        pass a pointer then to copy the whole structure”
    */
    /*//指向结构的指针
    struct date
    {
        int month;
        int day;
        int year;
    } myday;

    struct date *p=&myday;

    (*p).month=12;
    p->month =12~p所指的month(一个新的运算符)     ->表示指针所指的结构变量中成员
    */
    #include<stdio.h>
    struct point
    {
        int x;
        int y;
    };

    struct point *getstruct(struct point*);
    void output(struct point);
    void print(const struct point *p);

    int main(int argc,char const *argv[])
    {
        struct point y={0,0};
        getstruct(&y);
        output(y);
        output(*getstruct(&y));
        print(getstruct(&y));
    }

    struct point* getstruct(struct point *p)
    {
        scanf("%d",&p->x);
        scanf("%d",&p->y);
        printf("%d,%d ",p->x,p->y);
        return p;
    }

    void output(struct point p)
    {
        printf("%d,%d ",p.x,p.y);
    }

    void print(const struct point *p)
    {
        printf("%d,%d ",p->x,p->y);
    }

  • 相关阅读:
    泛泰A870(高通APQ8064t 600 cpu) Mokee4.4.2(Android4.4) 图赏
    cocos2d-x 游戏优化方案
    IOS中用UIStoryBoard类初始化/跳转控制器
    PHPthinking邀请您一起赚Money
    天凉好个秋(连载九)
    图像处理之Canny边缘检測
    SAE+wordpress邮箱问题,WP MAIL STMP插件配置但无效解决的方法
    Codeforces Beta Round #25 (Div. 2)--A. IQ test
    eclipse4.2+安装modelgoon插件,该插件支持在eclipse直接依据java文件生产类图
    使用Volley缓存图片时,缓存无效的原因。
  • 原文地址:https://www.cnblogs.com/lijianmin6/p/10354284.html
Copyright © 2011-2022 走看看