zoukankan      html  css  js  c++  java
  • 【华软C语言实验报告】实验9 函数(1)

    实验9 函数(1)
    一、实验目的:
    1、理解函数的功能
    2、熟练掌握函数的定义、声明和调用方法;

    二、实验内容
    1、(基础题)根据海伦公式由三角形的三边长度a、b、c可以计算三角形面积 ,公式为:在这里插入图片描述,其中:p=(a+b+c)/2。请根据下列要求编写程序:
    (1)三角形面积的计算由函数triangle_area实现,函数原型为:double triangle_area(double a, double b, double c);当输入的a、b、c值不能构成三角形时,返回0.0;(提示:构成三角形的三边条件:任意两边之和大于第三边,任意两边之差小于第三边。)
    (2)主函数的功能是输入三角形的三条边长,再调用triangle_area()得到面积,最后输出结果;
    (3)主函数在前,triangel_area()在后面定义。

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
        double are(double a,double b,double c);
        double x,y,z,s;
        printf("请输入三角形的三边:
    ");
        scanf("%lf%lf%lf",&x,&y,&z);
        s = are(x,y,z);
        printf("三角形的面积:%lf
    ",s);
        return 0;
    }
    
    double are(double a,double b,double c)
    {
        double s,p;
        p = (a+b+c)/2;
        if(a+b>c&&a+c>b&&b+c>a)
        {
            return sqrt(p*(p-a)*(p-b)*(p-c));
        }
        else
        {
            return 0.0;
        }
        
    }
    
    

    2、(基础题)定义一个将十进制数转换成十六进制数的函数void dec_to_hex(int n),该函数的功能是将参数中的十进制数以十六进制方式输出(编程思路参考第9讲课件例子)。在主函数输入一个十进制整数,然后调用dec2hex()输出对应的十六进制数。程序运行结果图如下:
    在这里插入图片描述

    #include <stdio.h>
    int main()
    {
    	int n,rem;
    	void dec_to_hex (int n); // dec_to_hex函数的声明
    	printf("Enter n: ");
    	scanf("%d",&n);
    	printf("
    十进制数:%d 转换为十六进制数是:",n);
    	dec_to_hex (n); // dec_to_hex函数的调用
    	printf("
    ");
    	return 0;
    }
    void dec_to_hex (int n)
    {
    	char num[20];
    	int rem,i=0;
    	do
    	{
    		rem=n%16;   //存放余数
    		n=n/16;
    		if (rem<10)
    			num[i++]='0'+rem; //10以内的数字转换成对应的字符存放在字符数组num[]中
    		else
    			num[i++]='A'+rem-10;//10以上的数字转换成对应的字符存放在字符数组num[]中
    	}while(n>0);
    	for(i=i-1;i>=0;i--)
    		printf("%c",num[i]);
    	printf("
    ");
    }
    
    

    3、(基础题)编程实现:定义一个求n的阶乘的函数double fac(int n),在主函数输入一个正整数,然后调用fac函数输出这个数的阶乘值。主函数在前,fac函数在后面定义.提示:用递归方法求n!,n!可用递归关系来表示:

    程序运行结果如下图所示:
    在这里插入图片描述
    在这里插入图片描述

    #include <stdlib.h>
    #include <stdio.h>
    int main()
    {
        double fac(int n);
        int is_int(char c[],int n);  
    	int num;
        char a[10];
    	printf("请输入一个正整数:");
    	input:scanf("%s",a);  
    	if(is_int(a,10))goto input;
    	num=atoi(a);
        fac(num);
        printf("%d! =%.0lf
    ",num,fac(num));
        return 0;
    }
    
    double fac(int n)
    {
        if(n>=0&&n<=1)
        {
            return 1;
        }
        else
        {
            return n*fac(n-1);
        }
        
    }
    
    int is_int(char c[],int n)//判断用户输入的是否是正整数
    {
        int i = 0,flag=0;
        while (c[i] != '') {
            if (c[i] == '-') {
                printf("不能是负数
    请重新输入一个正整数:");
                flag=1;
    	    break;
            }
            else if (c[i] == '.') {
                printf("不能是小数
    请重新输入一个正整数:");
                flag=1;
    			break;
            }
            else if ((c[i] < '0') || (c[i] > '9')) {
                printf("不是数字
    请重新输入一个正整数:");
                flag=1;
    			break;
            }
            i++;
        }
        return flag;
    }
    
    

    4、(提高题)编写程序,当用户从键盘输入公元年、月、日三个整数值( 年份的范围为2001–2099),能够计算该日期是这一年份的第几天,例如:
    在这里插入图片描述
    当输入的数据有错误时,也能输出提示信息,并结束程序,例如:
    在这里插入图片描述
    (提示:本程序主要考查多个函数的相互调用,除主函数外,可以定义如下三个函数:

    int is_leap_year(int year) //判断是否为闰年,如果是返回1,否则返回0
    int check_pass(int year, int month, int day) //检查输入的数据是否有错误,通过返回1,否则返回0,为简单起见,只对2月份的天数做检查
    int count_days(int year,int month, int day) //统计该日期是当年中的第几天,先累加之前各月份天数(区分闰年与平年),再加上本月份的day)
    

    提示:闰年2月份是29天,非闰年2月份是28天。

    #include <stdio.h>
    
    int main()
    {
        int is_leap_year(int year);//3个函数的声明
        int check_pass(int year, int month, int day);
        int count_days(int year,int month, int day);
        int year,month,day,days;
        label: printf("请输入年(2001--2099)、月、日: ");
        scanf("%d%d%d",&year,&month,&day);
        if(check_pass(year,month,day))  //判断输入的日期是否有误
           days=count_days(year,month,day);
        else
        {
            printf("输入的天数有误,程序结束!
    ");
            goto label;
        }
        printf("%d-%d-%d是当年的第%d天
    ",year,month,day,days);
        return 0;
    }
     //判断是否为闰年,如果是返回1,否则返回0
    int is_leap_year(int year)
    {
        if(year%4 == 0 && (year%100 != 0 || year%400==0))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    
     //检查输入的数据是否有错误,通过返回1,否则返回0
    //输入数据有错的情况:闰年2月份的天数大于29,平年2月份的天数大于等于29
    
    int check_pass(int year, int month, int day)
    {
       if(month==2)
        {
            if(((is_leap_year(year))&&(day>29))||((!is_leap_year(year))&&(day>=29)))
                return 0;
            else return 1;
        }
        else return 1;
    
    }
    //统计该日期是当年中的第几天,先累加之前各月份天数(区分闰年与平年),再加上本月份的day
    
    int count_days(int year,int month, int day)
    {
        int month_days[]={0,31,28,31,30,31,30,31,31,30,31,30};
        int days=0,i;
        if(is_leap_year(year))
        {
            month_days[2]=29;
        }
        for(i=1;i<month;i++)
        {
            days += month_days[i];
        }
        days += day;
        return days;
    }
    
    
    一键三连呀!
  • 相关阅读:
    【笔记】Excel技巧—004 多用途的文档属性
    【原创】The solutional manual of the Verilog HDL: A Guide to Digital Design and Synthesis (2nd)—ch07I
    【笔记】Excel技巧—003 实用的Excel选项设置
    【笔记】Excel技巧—005 使用工作区文件
    【原创】The solutional manual of the Verilog HDL: A Guide to Digital Design and Synthesis (2nd)—ch07II
    【原创】The solutional manual of the Verilog HDL: A Guide to Digital Design and Synthesis (2nd)ch05
    【原创】The solutional manual of the Verilog HDL: A Guide to Digital Design and Synthesis (2nd)ch12
    【笔记】DE2 硬件和处理器范例2 External SRAM interface (DE2)(digital logic)(verilog hdl)
    【笔记】Excel技巧—006 选取单元格区域
    【原创】The solutional manual of the Verilog HDL: A Guide to Digital Design and Synthesis (2nd)ch06
  • 原文地址:https://www.cnblogs.com/jee-cai/p/14095204.html
Copyright © 2011-2022 走看看