单片机c语言实例程序300篇(3)
题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。 2.程序源代码: age(n) int n; { int c;
if(n==1) c=10; else c=age(n-1)+2; return(c); }
main()
{ printf("%d",age(5)); }
============================================================== 【程序29】
题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。 1. 程序分析:学会分解出每一位数,如下解释: 2.程序源代码: main( ) {
long a,b,c,d,e,x; scanf("%ld",&x);
a=x/10000;/*分解出万位*/
b=x%10000/1000;/*分解出千位*/ c=x%1000/100;/*分解出百位*/ d=x%100/10;/*分解出十位*/ e=x%10;/*分解出个位*/
if (a!=0) printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a); else if (b!=0) printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b); else if (c!=0) printf(" there are 3,%ld %ld %ld\n",e,d,c); else if (d!=0) printf("there are 2, %ld %ld\n",e,d);
单片机 C语言
else if (e!=0) printf(" there are 1,%ld\n",e); }
============================================================== 【程序30】
题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。 1.程序分析:同29例 2.程序源代码: main( ) {
long ge,shi,qian,wan,x; scanf("%ld",&x); wan=x/10000;
qian=x%10000/1000; shi=x%100/10; ge=x%10;
if (ge==wan&&shi==qian)/*个位等于万位并且十位等于千位*/ printf("this number is a huiwen\n"); else
printf("this number is not a huiwen\n"); }
【程序31】
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。
1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。 2.程序源代码: #include <stdio.h> void main() {
char letter;
printf("please input the first letter of someday\n");
while ((letter=getch())!='Y')/*当所按字母为Y时才结束*/ { switch (letter)
{case 'S':printf("please input second letter\n"); if((letter=getch())=='a') printf("saturday\n");
else if ((letter=getch())=='u') printf("sunday\n"); else printf("data error\n"); break;
case 'F':printf("friday\n");break; case 'M':printf("monday\n");break;
case 'T':printf("please input second letter\n"); if((letter=getch())=='u') printf("tuesday\n");
else if ((letter=getch())=='h') printf("thursday\n");
单片机 C语言
else printf("data error\n"); break;
case 'W':printf("wednesday\n");break; default: printf("data error\n"); } } }
============================================================== 【程序32】
题目:Press any key to change color, do you want to try it. Please hurry up! 1.程序分析: 2.程序源代码: #include <conio.h> void main(void) {
int color;
for (color = 0; color < 8; color++) {
textbackground(color);/*设置文本的背景颜色*/ cprintf("This is color %d\r\n", color); cprintf("Press any key to continue\r\n"); getch();/*输入字符看不见*/ } }
============================================================== 【程序33】
题目:学习gotoxy()与clrscr()函数 1.程序分析: 2.程序源代码: #include <conio.h> void main(void) {
clrscr();/*清屏函数*/ textbackground(2);
gotoxy(1, 5);/*定位函数*/
cprintf("Output at row 5 column 1\n"); textbackground(3); gotoxy(20, 10);
cprintf("Output at row 10 column 20\n"); }
============================================================== 【程序34】
题目:练习函数调用 1. 程序分析: 2.程序源代码: #include <stdio.h>
单片机 C语言
void hello_world(void) {
printf("Hello, world!\n"); }
void three_hellos(void) {
int counter;
for (counter = 1; counter <= 3; counter++) hello_world();/*调用此函数*/ }
void main(void) {
three_hellos();/*调用此函数*/ }
============================================================== 【程序35】
题目:文本颜色设置 1.程序分析: 2.程序源代码: #include <conio.h> void main(void) {
int color;
for (color = 1; color < 16; color++) {
textcolor(color);/*设置文本颜色*/ cprintf("This is color %d\r\n", color); }
textcolor(128 + 15);
cprintf("This is blinking\r\n"); }
============================================================== 【程序36】
题目:求100之内的素数 1.程序分析: 2.程序源代码: #include <stdio.h> #include "math.h" #define N 101 main() {
int i,j,line,a[N];
for(i=2;i<N;i++) a[i]=i; for(i=2;i<sqrt(N);i++) for(j=i+1;j<N;j++) {
单片机 C语言
if(a[i]!=0&&a[j]!=0) if(a[j]%a[i]==0) a[j]=0;} printf("\n");
for(i=2,line=0;i<N;i++) {
if(a[i]!=0)
{printf("%5d",a[i]); line++;} if(line==10) {printf("\n"); line=0;} } }
============================================================== 【程序37】
题目:对10个数进行排序
1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,下次类推,即用第二个元素与后8个进行比较,并进行交换。 2.程序源代码: #define N 10 main()
{int i,j,min,tem,a[N]; /*input data*/
printf("please input ten num:\n"); for(i=0;i<N;i++) {
printf("a[%d]=",i); scanf("%d",&a[i]);} printf("\n"); for(i=0;i<N;i++) printf("%5d",a[i]); printf(&qu …… 此处隐藏:3439字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [小学教育]四年级综合实践活动课《衣物的洗涤》教
- [小学教育]2014半年工作总结怎么写
- [小学教育]20世纪外国文学专题综合试题及答案
- [小学教育]TS_1循环使用催化丙烯环氧化反应研究
- [小学教育]最实用的考勤签到表(上下班签到表)
- [小学教育]气候与生态建筑——以新疆民居为例
- [小学教育]二人以上股东有限责任公司章程参考样本
- [小学教育]2014届第一轮复习资料4.1,3美好生活的
- [小学教育]土方开挖、降水方案
- [小学教育]手绘儿童绘本《秋天的图画》(蜡笔)
- [小学教育]2002级硕士研究生卫生统计学考试试题
- [小学教育]环保装备重点发展目录
- [小学教育]金蝶K3合并报表培训教材
- [小学教育]岩浆岩试题及参考答案
- [小学教育]知之深爱之切学习心得
- [小学教育]第十二章 蛋白质的生物合成
- [小学教育]Chapter 2-3 Solid structure and basi
- [小学教育]市政道路雨季专项施工方案
- [小学教育]中国海洋大学2012-2013学年第二学期天
- [小学教育]教育心理学第3章-学习迁移
- 浅谈深化国企改革中加强党管企业
- 2006年中国病理生理学会学术活动安排
- 设计投标工作大纲
- 基于ARP的网络攻击与防御
- 2016届湖北省七市(州)教科研协作体高三
- Google_学术搜索及其检索技巧
- 2019-2020学年七年级地理下册6.3美洲教
- 城市道路可研报告
- 【名师指津】2012高考英语 写作基础技
- 6级知识点培训北京师范大学《幼儿智趣
- 注册会计师会计知识点:金融资产
- 新安装 500 kV 变压器介损分析与判断
- PS2模拟器PCSX2设置及使用教程.
- 医院药事管理与药剂科管理组织机构
- {PPT背景素材}丹巴的醉人美景,免费,一
- NAS网络存储应用解决方案
- 青海省西宁市六年级上学期数学期末考试
- 测量管理体系手册依据ISO10012:2003
- 洞子小学培养骨干教师工作计划
- 浅谈《牛津初中英语》的教材特点及教学




