第49课函数初探:
—神奇的“黑盒”
·函数:
·是完成特定任务的独立程序代码
·语法规则定义了函数的结构和使用方式
·为什么要使用函数
·可以省去编写重复代码的苦闷
·可以让程序更加模块化,提高代码可读性
·方便后期修改、完善
·函数的种类
·内置函数:
由C语言系统提供
需要在程序前包含定义函数的头文件
·自定义函数
不带参数
带参数
第50课 内置函数:
头文件:ctype.h
isupper:大写字母
islower:小写字母
isalpha:是否为字母
isdigit:是否为数字
注意:‘9’ != 9 ,一个是数字,一个是ascll码。
//程序示例***************************************************************
#include <stdio.h>
#include <ctype.h>
int main()
{
//常用内置函数
//isupper(''),判断是不是大写字母,零表示假,非零表示真
//islower(''),判断是不是小写字母,零表示假,非零表示真
printf("%d\n",isupper('a'));//大写字母
printf("%d\n",islower('a'));//小写字母
printf("%d\n",isalpha(97));//是不是字母,这里是,是a的ascll码
printf("%d\n",isdigit('a'));//数字,'9'是数字,9是ascall码,50是数字2的ascall码
//转换大小写
printf("大写%c",toupper('a'));
/*
// 打印所有ascll码的字符
int i;
for(i = 0; i < 127; i++)
{
printf("%c\n",i);
}
*/
return 0;
}
//程序示例***************************************************************
//运行结果********************************************
0
2
2
0
大写A
Process returned 0 (0x0) execution time : 0.294 s
Press any key to continue.
//运行结果********************************************
·小项目:将用户输入的小写数字转换成中文大写
//程序示例***************************************************************
#include <stdio.h>
#include <ctype.h>
int main()
{
int money, count = 0;
int moneys[6];//默认支持六位数字
int i = 0;
char unit[10][4] = {"零","壹","贰","弎","肆","伍","陆","柒","捌","玖"};//一个汉字占四个字节
printf("请输入金额:");
scanf("%d", &money);
//判断用户输入了几位数字,将每一位数字取出来
while(money != 0)
{
moneys[i] = money % 10; //这个操作就是得到最后一位,也就是取余
money /= 10; //去掉最后一位
i++;
count++;
}
printf("用户输入的数字一共有%d位!", count);
printf("大写数字:\n");
for(i = 0; i < count; i++)
{
printf("%s",unit[moneys[count - i-1]]);//注意逆序打印出来。
}
return 0;
}
//程序示例***************************************************************
//运行结果********************************************
请输入金额:12345
用户输入的数字一共有5位!大写数字:
壹贰弎肆伍
Process returned 0 (0x0) execution time : 5.855 s
Press any key to continue.
//运行结果********************************************