美文网首页
6. 模块化的使用函数1 Modularity Using Fu

6. 模块化的使用函数1 Modularity Using Fu

作者: 53df91bc24df | 来源:发表于2016-08-03 09:03 被阅读35次

6.1 函数的声明和参数的声明

自建函数的调用

#include <stdio.h>

//自建一个比较两个数字大小的函数
void findMax(float x, float y) /*函数首部行*/ //---------------------声明一个函数
{
    float maxNum;
    
    if (x > y){
        maxNum = x;
        printf("The maximum of the two numbers entered is %f\n", maxNum);
    }
    else if (x == y)
        printf("The tow numbers just entered are equal.\n");
    else{
        maxNum =y;
        printf("The maximum of the two numbers entered is %f\n", maxNum);
    }
    
    //没有return 0!
}

int main()
{
    void findMax(float, float);
    float firstNum, secondNum;/*函数原型*/ //---------------------在main函数中引入自建函数
    
    printf("Enter 1st number: ");
    scanf("%f", &firstNum);
    printf("Enter 2nd number: ");
    scanf("%f", &secondNum);
    
    findMax(firstNum, secondNum);/*自建函数的调用*/ //---------------------自建函数在这里被调用
    
    return 0;
}

6.2 返回一个数值

函数自建时 return expression
比如

return maxNum;//返回两个中较大的那一个
return ((5.0/9.0) * (Temp -32.0));//返回温度转换数值

6.3 案例

计算年龄标准
年龄=年数+月数/12.0
正常身高=-0.25(年龄-6)平方+3.5(年龄-6)+45
差额百分比=100(实际身高-正常身高)/正常身高

输入:
年数
月数
实际身高

输出:
年龄
正常身高
差额百分比

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

int main()
{
    float norm(float);       /* here are the function prototypes */
    float pcdif(float, float);
    void showit(float, float );
    
    int years, months;
    float height, normht;
    float age, perdif;
    
    /* this is the input section */
    printf("\nHow old (in years) is this child? ");
    scanf("%d", &years);
    printf("How many months since the child's birthday? ");
    scanf("%d", &months);
    age = years + months/12.0;  /* convert to total years */
    printf("Enter the child's height (in inches): ");
    scanf("%f", &height);
    
    normht = norm(age);
    
    perdif  = pcdif(height, normht);
    
    showit(normht, perdif);
    
    return 0;
}

float norm(float age)
{
#define MINAGE 6.0
    float agedif, avght;
    
    agedif = age - MINAGE;
    avght = -0.25*pow(agedif,2) + 3.5*agedif + 45.0;
    return (avght);
}

float pcdif(float actual, float base)
{
    return (actual - base)/base * 100.0;
}

//显示函数单独写
void showit(float normht, float perdif)
{
    printf("\nThe average height in inches is: %5.2f\n", normht);
    printf("The actual height deviates from the norm by: %6.2f%c\n", perdif, '%');
}```

###标准库函数
**数学库函数** 

include <math.h>


double fabs(double); //返回它的双精度型参数的绝对值
double ceil(double); //返回一个>=它的参数数值的最小整数浮点型数值
double floor(double); //返回一个<=它的参数数值的最大整数浮点型数值
double fmod(double); //返回第一个参数被第二个参数整除的余数
double exp(double); //返回e自乘到它的双精度参数幂的数值
double log(double); //返回参数的自然对谁,基数e
double log10(double); //返回参数的常用对数,基数10
//都是double
//sqrt 平方根
//pow 幂
//sin 正弦
//cos 余弦
//tan 正切
//asin 正弦角度
//acos 余弦角度
//atan 正切角度
//atan2(double, double) 正切角度, 正切=第二个参数/第一个参数
//sinh 参数的双曲正弦
//cosh 参数的双曲余弦
//tanh 参数的双曲正切


include <stdlib.h>

int abs(int); //返回它的整型参数的绝对值


**rand()**生成 *0 到 RAND_MAX* 的随机数
**srand()**为rand()提供一个开始的“种子值”

include <stdio.h>

include <stdlib.h>

include <time.h>

define TOTALNUMBERS 10

int main()
{
float randValue;
int i;

srand(time(NULL));  
//这里生成第一个“种子”值
//srand函数的参数是一个time函数,这个time函数的参数是NULL
//NULL使time()函数以秒为单位读取计算机内部时钟的时间
//这个srand()函数使用time(NULL)初始化rand()的随机数发生器函数randomize()
//一旦rand()函数被初始化,下面for循环重复调用这个函数10次,每次调用产生一个新的随机数

for (i = 1; i <= TOTALNUMBERS; i++)
{
    randValue = rand();
    printf("%6.0f\n", randValue);
}

return 0;

}


**比例缩放**
生成1 - 100之间的随机数:1+(int)rand%100
生成a - b之间的随机数:a+(int)(rand%(b-a+1))

**硬币抛掷模拟**

include <stdio.h>

include <stdlib.h>

include <time.h>

int flip(int); //prototype for flip function
void percentages(int, int) ; //prototype for percentage function

int main()
{
int numTosses = 1000;//总共抛掷1000次
int heads;//面朝上

heads = flip(numTosses);//函数调用
percentages(numTosses, heads);//函数调用

return 0;

}

// this method tosses the coin numTimes
// and returns the number of heads
int flip(int numTimes)
{
int randValue;
int heads = 0;
int i;

srand(time(NULL));//rand函数初始化

//算法:产生1-100之间的随机数,如果这个数>50,面朝上+1
for (i = 1; i <= numTimes; i++)
{
    randValue = 1 + (int)rand() % 100;
    if (randValue > 50)
        heads++;
}

return (heads);

}

// this method calculates and displays
// the percentages of heads and tails
void percentages(int numTosses, int heads)
{
int tails;
float perheads, pertails;

if (numTosses == 0)
    printf("There were no tosses, so no percentages can be calculated.\n");
else
{
    tails = numTosses - heads;
    printf("Number of coin tosses: %d\n", numTosses);
    printf("   Heads: %d   Tails: %d\n", heads, tails);
    perheads = (float)heads/numTosses * 100.0;
    pertails = (float)(numTosses - heads)/numTosses * 100.0;
    printf("Heads came up %6.2f percent of the time.\n", perheads);
    printf("Tails came up %6.2f percent of the time.\n", pertails);
} 

}


**字符处理函数**
ctype.h

程序:输入字符,并判断字母还是数字?当输入大写或者小写x时,程序退出

include <stdio.h>

include <ctype.h>

int main()
{
char inChar;

do
{
    printf("\nPush any key (type an x to stop) ");
    inChar = getchar(); /* get the next character typed */
    //getchar()用于返回在终端输入的下一个字符

    inChar = tolower(inChar); /* convert to lowercase */
    //如果参数是大些字母则返回它的小写字母,否则返回未改变的参数值
    getchar(); /* get and ignore the ENTER key */
    //幻影,忽视enter的影响
    
    if ( isalpha(inChar) ) /* a nonzero value is true in C */
    //isalpha()如果参数是一个字母,返回一个非0数,否则返回0
        printf("\nThe character entered is a letter.\n");
    else if ( isdigit(inChar) )
    //isdigit()如果参数是一个数字(0-9),返回一个非0,否则0
        printf("\nThe character entered is a digit.\n");
    
} while (inChar != 'x');

}


**转换函数**
int atoi(string) 转ASCII字符串为整数
int itoa(int) 转整数为一个ASCII

include <stdio.h>

include <stdlib.h> /* required for string conversion functions */

int main()
{
int num;
double dnum;

num = atoi("1234");  /* convert a string to an integer */

printf("The string \"1234\" as an integer number is: %d \n", num);
printf("This number divided by 3 is: %d \n\n", num / 3);

dnum = atof("1234.96"); /* convert a string to a double */

printf("The string \"1234.96\" as a double is: %f \n", dnum);
printf("This number divided by 3 is: %f \n", dnum / 3);

return 0;

}



##语法
**函数调用**
-*main()外(上面)*
1) void 函数名(数据类型, 数据类型) ***函数原型***

-*main()里*
2) 函数名(参数, 参数) ***函数调用***

-*main()外(下面)*
3) void 函数名(数据类型 参数, 数据类型 参数) ***函数首部行***

*void 属于 “返回数据类型”,还可以是别的int, float, double
*数据类型 参数 叫 “参数数据类型列表”,定义函数被调用时必须提供的数据***个数***、***次序***、***类型***

相关文章

网友评论

      本文标题:6. 模块化的使用函数1 Modularity Using Fu

      本文链接:https://www.haomeiwen.com/subject/uraxsttx.html