• 函数使用初体验


    打印每个程序程序头,包括作者和联系方式等信息,一种使用函数一种不使用函数

    1.用函数结构体程序如下

    #include <stdio.h> 
    #include <string.h>
    #define NAME "JISHABAO"
    #define Email "123456745678"
    #define Date "2016-09-19"
    #define WID 40
    void show_name(char ch, int num);
    void show_no(const char * );
    int main()
    {
    	show_name('*',WID);
    	show_no(NAME);
    	show_no(Email);
    	show_no(Date);
    	show_name('*',WID);
    }
    void show_name(char ch, int num)   //打印*函数
    {
    	int count;
    	for(count=1;count<=num;count++)
    		putchar(ch);
    	printf("
    ");
    }
    void show_no(const char * p )		//打印信息函数
    {
    	int j;
    	for(j=0;j<((WID-strlen(p))/2);j++)	//信息居中设置
    		printf(" ");
    	printf("%s
    ",p);
    }
    
    2.不使用函数
    #include <stdio.h> 
    #include <string.h>
    #define NAME "JISHABAO"
    #define Email "123456745678"
    #define Date "2016-09-19"
    #define WID 40
    void show_name(char ch, int num);
    void show_no(const char * );
    int main()
    {
    	int i;
    	show_name('*',WID);
    	for(i=0;i<((WID-sizeof(NAME))/2);i++)
    		printf(" ");
    	printf("%s
    ",NAME);
    
    	for(i=0;i<((WID-sizeof(Email))/2);i++)
    		printf(" ");
    	printf("%s
    ",Email);
    
    	for(i=0;i<((WID-sizeof(Date))/2);i++)
    		printf(" ");
    	printf("%s
    ",Date);
    	show_name('*',WID);
    	printf("
    ");
    }
    void show_name(char ch, int num)<span style="white-space:pre">	</span>//打印*函数
    {
    	int count;
    	for(count=1;count<=num;count++)
    		putchar(ch);
    	printf("
    ");
    }

    对比一下使用函数与不使用函数的区别可以发现,用函数可以使程序更加精炼,看起来更符合逻辑结构。

    打印结果如下

    ****************************************
                   JISHABAO
                 123456745678
                  2016-09-19
    ****************************************
    



    一个行者的旅途
  • 相关阅读:
    C# 枚举帮助类EnumHelper(获取描述、名称和数值)
    C# RSA非对称加密、解密及格式转换
    C# RSA加密解密及RSA签名和验证
    SQL Server 帐号权限管理及编程应用(图解)
    C# LINQ之IEqualityComparer<>接口应用
    SQL Server 表变量和临时表的区别
    SQL Server 图解备份(完全备份、差异备份、增量备份)和还原
    OpenLDAP服务器的搭建
    openstack外接ceph
    openstack
  • 原文地址:https://www.cnblogs.com/xinzghewanfu/p/5904255.html
Copyright © 2020-2023  润新知