C语言及程序设计提高例程-9 函数的嵌套调用

贺老师教学链接  C语言及程序设计提高 本课讲解

函数的嵌套调用:函数内调用函数

#include <stdio.h>
int gcd(int n1, int n2);
int lcm(int n1, int n2);
int main()
{
    int num1,num2;
    int iGcd, iLcm;  //最大公约数和最小公倍数
    printf("please input two numbers:");
    scanf("%d%d",&num1,&num2);
    iGcd=gcd(num1, num2);
    iLcm=lcm(num1,num2);
    printf("the gcd is: %d\n",iGcd);
    printf("the lcm is: %d\n",iLcm);
    return 0;
}

int gcd(int n1, int n2)
{
    int r;
    while(n2!=0)/*利用辗除法,直到n2为0为止*/
    {
        r=n1%n2;
        n1=n2;
        n2=r;
    }
    return n1;
}

int lcm(int n1, int n2)
{
    return n1*n2*gcd(n1, n2);
};

用弦截法求解方程

/*用弦截法求方程f(x)=x^3-5x^2+16x-80=0的根。*/

#include <stdio.h>
#include <math.h>

double f(double);                  //函数声明
double xpoint(double, double);     //函数声明
double root(double, double);       //函数声明

int main( )
{
    double x1,x2,f1,f2,x;
    do
    {
        printf("input x1,x2:");
        scanf("%lf %lf", &x1, &x2);
        f1=f(x1);
        f2=f(x2);
    }
    while(f1*f2>=0);  //保证f(x1)和f(x2)异号,(x1,x2)间有根
    x=root(x1,x2);
    printf("A root is %.5f\n", x);
    return 0;
}

/*
功能:求(x1, x2)区间方程的实根
参数:两个浮点型值,表示x轴上两点,由主调函数保证f(x1)和f(x2)是异号
返回值:方程的根
*/
double root(double x1, double  x2)
{
    double x,y,y1;
    y1=f(x1);
    do
    {
        x=xpoint(x1,x2);
        y=f(x);
        if (y*y1>0)
        {
            y1=y;
            x1=x;
        }
        else
            x2=x;
    }
    while(fabs(y)>=0.00001);
    return x;
}

/*
功能:求(x1, f(x1))和(x2, f(x2))的连线与x轴的交点x
参数:两个浮点型值,表示x轴上两点
返回值:交点的x轴坐标
*/
double xpoint(double x1, double x2)
{
    double x;
    x=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
    return x;
}

/*
功能:求函数f(x)=x3-5x2+16x-80的值
参数:一个浮点型数,表示x轴上的一点
返回值:给定函数的值
*/
double f(double x)
{
    double y;
    y=x*x*x-5*x*x+16*x-80;
    return y;
}
时间: 2024-09-03 07:19:05

C语言及程序设计提高例程-9 函数的嵌套调用的相关文章

C语言及程序设计提高例程-8 函数的声明、定义和调用

贺老师教学链接  C语言及程序设计提高 本课讲解 调用的条件--被调用的函数已存在 #include <stdio.h> #include <math.h> float max(float x, float y); int main () { float a,b,c,s; scanf("%f %f", &a, &b); s=sqrt(a); printf("sqrt is %.2f\n", s); c=max(a+b, a*b

C语言及程序设计提高例程-5 函数的参数

贺老师教学链接  C语言及程序设计提高 本课讲解 定义无参函数 #include <stdio.h> void printstars( ) { printf("*******************\n"); } void printmessage(void) { printf("Hello, world.\n"); } int main() { printstars(); printmessage(); printstars(); return 0;

C语言及程序设计提高例程-4 函数的返回值

贺老师教学链接  C语言及程序设计提高 本课讲解 函数返回 #include <stdio.h> int max(int x,int y); int main() { int a, b, c; scanf(%d %d", &a, &b); c = max(a, b); printf("max=%d", c); return 0; } int max(int x, int y) { return(x>y?x:y); }

C语言及程序设计提高例程-25 指针作函数参数

贺老师教学链接  C语言及程序设计提高 本课讲解 例:冒泡排序函数的新写法 #include <stdio.h> void bubblesort(int*, int); int main( ) { int i,a[10]= {3,5,9,1,3,6,-9,-7,10,12}; bubblesort(a,10); for(i=0; i<10; i++) printf("%d ", a[i]); return 0; } void bubblesort(int *p, in

C语言及程序设计提高例程-36 多维数组作函数参数

贺老师教学链接  C语言及程序设计提高 本课讲解 用多维数组名作函数参数 #include <stdio.h> int max_value(int array[][4]); int main( ) { int a[3][4]= {{11,32,45,67},{22,44,66,88},{15,72,43,37}}; printf("max value is %d\n", max_value(a)); return 0; } int max_value(int array[]

C语言及程序设计提高例程-27 编写查找和排序函数

贺老师教学链接  C语言及程序设计提高 本课讲解 用函数实现二分查找 #include <stdio.h> int binary_search(int arr[], int n, int k); #define SIZE 10 int main( ) { int d[SIZE] = {1, 3, 9, 12, 32, 41, 45, 62, 75, 77}; int low, high,mid,key,index=-1; printf("Input a key you want to

C语言及程序设计提高例程-24 数组名作为函数参数

贺老师教学链接  C语言及程序设计提高 本课讲解 用数组元素作函数实参 #include <stdio.h> int gcd(int m,int n) { int r; while(r=m%n) { m=n; n=r; } return n; } int main() { int i; int a[8]= {26,1007,956,705,574,371,416,517}; int b[8]= {994,631,772,201,262,763,1000,781}; int c[8]; for(

C语言及程序设计提高例程-7 返回指针的函数

贺老师教学链接  C语言及程序设计提高 本课讲解 返回指针的函数 #include <stdio.h> int *max(int *x, int *y) { int *t; if(*x > *y) t = x; else t = y; return t; } int main() { int a, b, *p; scanf("%d %d", &a, &b); p = max(&a, &b); printf("max = %d

C语言及程序设计提高例程-2 模块化程序设计及C语言中的函数

贺老师教学链接  C语言及程序设计提高 本课讲解 我们写过这样的程序 #include <stdio.h> int main() { int iChioce; do { printf("* 1. 吃饭\n"); printf("* 2. 睡觉\n"); printf("* 3. 打豆豆\n"); printf("* 0. 退出\n"); printf("* 请选择(0-3):"); scanf(