#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define FOSC 11059200ul
#define BAUD 9600
#define T1_H 256-(FOSC/12/32/BAUD)
#define T1_L 256-(FOSC/12/32/BAUD)
#define T0_H 65536-(FOSC*20/(12*1000))/256 #define T0_L 65536-(FOSC*20/(12*1000))%256
uchar cx;
uchar cd;
uchar fs;
uchar ab;
//定义TLC549串行总线操作端口
sbit CLK=P1^0;
sbit DAT=P1^1;
sbit CS=P1^2;
sbit com1=P1^3;
sbit com2=P1^4;
sbit com3=P1^5;
sbit com4=P1^6;
uint i;
uchar bdata ADCdata;
sbit ADbit=ADCdata^0;
void initsys();
uchar TLC549ADC(void)
{
uchar i;
CLK=0;
DAT=1;
CS=0;
for(i=0;i<8;i++)
{
CLK=1;
_nop_();
_nop_();
ADCdata<<=1;
ADbit=DAT;
CLK=0;
_nop_();
}
return (ADCdata);
}
uchar code zixing[]={0xc0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; uint qian,bai,shi,ge;
void delay();
void main()
{
uchar i;
float AD_DATA; //定义A/D转换数据变量
int ad_data;
TLC549ADC(); //启动一次A/D转换
for(i=0xff;i>0;i--) //延时
{
_nop_();
}
AD_DATA=(TLC549ADC()); //读取当前电压值A/D转换数据
initsys();
while(1)
{
if(cd==1)
{
cd=0;
AD_DATA=TLC549ADC();
}
if(ab==1)
{
ab=0;
SBUF=AD_DATA;
while(TI==0);
TI=0;
}
ad_data=AD_DATA*5/255*100;
ge=ad_data%10;
shi=ad_data%100/10;
bai=ad_data%1000/100;
qian=ad_data/1000;
for(i=0;i<4;i++)
{
switch(i)
{
case 0: P3=zixing[qian]; com1=0,com2=1,com3=1,com4=1;delay();break;
case 1: P3=zixing[bai]&0x7f;com1=1,com2=0,com3=1,com4=1;delay();break;
case 2: P3=zixing[shi]; com1=1,com2=1,com3=0,com4=1;delay();break;
case 3: P3=zixing[ge]; com1=1,com2=1,com3=1,com4=0;delay();break;
}
}
}
}
void timer0() interrupt 1
{
TH0=T0_H;
TL0=T0_L;
cx++;
fs++; //来一次中断加1
if(cx==5)
{
cd=1;
cx=0; //来20次为一次
}
if(fs==50)
{
ab=1;
fs=0;
}
}
void initsys()
{
SCON=0x50;
TH1=T1_H;
TL1=T1_L;
TH0=T0_H;
TL0=T0_L;
TMOD=0x01;
TMOD=(TMOD&0x0f)|0x20;
EA=1;
ET0=1;
ES=0; //关闭串行口
TR1=1;
TR0=1;
}
void delay()
{
for(i=0;i<=100;i++)
_nop_();
}
c 语言程序设计教程第二版课后习题 答 案 篇一: c 语言程序设计教程_李含光_ 郑关胜_ 清华大学 出版社习题答案习题答案[完美打印版]】1.单项选择题 (1)a (2)c(3)d (4)c (5)b 2.填空题(1)函数 (2)主函数(main )(3)printf(),scanf()第 2 章习题参考答案 1.单项选择题 1-5 cbccc 6-10 cdcdc 11-13 dbb 2 .填空题 (1)1(2)26 (3)6 , 4 , 2 (4)10 , 6 (5)3.000000 (6)双精度(double )(7)9 (8)字母,数字,下划线(9)13.700000 (10)11(11)((m/10 )%10 )*100+ (m/100 )*10+m%10 (12)0 (13)10 ,9 ,11(15) (x0y0)||(x0z0)||(y0||z0)(16 )double (17)x==0 (18)sqrt(fabs(a-b))/(3*(a+b))(19 )sqrt((x*x+y*y)/ (a+b)) 第 3 章习题参考答案 1.单项选择题 1-5 cccdd 6-10 bcdbc11-15 bcbbb16 a 2 .填空题 (1)用;表示结束(2){ } (3 )y=x0?1:x==0?0:-1 (4)y%4==0y%100!=0||y%400==0 (5)上面未配对(6)default 标号(7)while ,do while ,for (8)do while (9)本次(10)本层3.阅读程序,指出结果 (1)yes (2)*(3)abother (4)28 70 (5)2,0(6)8(7)36 (8)1(9)3,1,-1,3,1,-1(10)a=12 ,y=12 (11) i=6 ,k=4 (12)1,-2 4 .程序填空 (1)x:y ,u :z (2)m=n ,m !=0 ,m=m/10 (3)teps , t*n/(2*n+1) , printf( n”“,%2*lsf )(4)m%5==0 ,
基于 5 1 单片机的电子钟 C 语言程序 #include
【例4.1】从键盘输入一个大写英文字母,将其转换为小写字母后,再显示到屏幕上。程序如下: 1 2 3 4 5 6 7 8 9 10#include
1.6 编写一个程序,输入a、b、c 三个值,输出其中最大值。 课后习题答案完整版 第一章 1.5 请参照本章例题,编写一个C 程序,输出以下信息: ************************** Very Good! ************************** 解:mian() {int a,b,c,max; printf( “请输入三个数a,b,c:\n ” ); scanf( “%d,%d,%”d ,&a,&b,&c); C语言程序设计第三版谭浩强 解: mian() {printf( ”); “************************** printf( “”X “ n” ); printf( “Very Good!” \ n”); printf( “”X “ n” ); printf( “************************** ); max=a; if(max
{char #include
1.要求在屏幕上输出下一行信息。 This is a c program. 程序: #include
#include
#include
算法:将当前时间显示到屏幕,当时间发生变化时,清屏,显示新的时间(当有键盘操作时退出程序)。 显示时间格式:小时:分钟:秒 /* DEV C++ Win XP*/ #include
int print1(Point p) { int i=0; for(;i<13;i++) { gotoxy(p.x+1, p.y+i); printf("%5s%c"," ",2); } return 0; } int print2(Point p) { int i=0; for(;i<13;i++) { gotoxy(p.x+1, p.y+i); if(i==0||i==6||i==12) printf("%c%c%c%c%c%c",2,2,2,2,2,2); else if(i>0&&i<6) printf("%5s%c"," ",2); else printf("%c",2); } return 0; } int print3(Point p) { int i=0; for(;i<13;i++) { gotoxy(p.x+1, p.y+i); if(i==0||i==6||i==12) printf("%c%c%c%c%c%c",2,2,2,2,2,2); else printf("%5s%c"," ",2); } return 0; } int print4(Point p) {
C 语言程序设计(第三版)习题库 1、设圆半径r=,圆柱高h=3,求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积。用scanf 输入数据,输出计算结果,输出时要求文字说明,取小数点后两位数字。请编程序。 #include<> main(){ floatr,h,C1,Sa,Sb,Va,Vb; scanf(__”%f ”__,&r); scanf(”%d ”,__&h _);; C1=2**r; Sa=*r*r; Sb=4*Sa; Va=4**r*r*r/3; Vb=Sa*h; printf(___”Cl=%.2fSa=%.2fSb=%.2fVa=%.2fVb=%.2f ”,Cl,Sa,Sb,Va,Vb ); } 2、输入一个华氏温度,要求输出摄氏温度。公式为c=5(F-32)/9 输出要求有文字说明,取位2小数。 #include<> main(){ floatF,c; scanf("%f",&F); ____c=5*(F-32)/9______; printf("c=%.2f",c); } 3、有一函数:?? ???≥-<≤-<=10113101121x x x x x x y 写一程序,输入x 值,输出y 值。 #include<> main(){ intx,y; printf("输入x :"); scanf("%d",&x); if(x<1){/*x<1*/ y=x; printf("x=%3d,y=x=%d\n",x,y);
}elseif(____x<10_______){/*1≤x-10*/ _____y=2*x-1_______; printf("x=%3d,y=2*x-1=%d\n",x,y); }else{/*x≥10*/ y=3*x-11; printf("x=%3d,y=3*x-11=%d\n",x#include"" main() { intx,y; scanf("%d",&x); if(x<1) {y=x;} elseif(x>=1&&x<10) {y=2*x-1;} else {y=3*x-11;} printf("%d",y); }#include"" main() { intx,y; scanf("%d",&x); if(x<1) {y=x;} elseif(x>=1&&x<10) {y=2*x-1;} else {y=3*x-11;} printf("%d\n",y); }#include"" main() { intx,y; scanf("%d",&x); if(x<1) {y=x;} elseif(x>=1&&x<10) {y=2*x-1;} else {y=3*x-11;} printf("%d",y); }scanf("%d",&x);
实践教学报告2011-2012 学年第二学期 项目名称基于51单片机的数字钟设计所属课程电子产品开发设计与实践教程 班级电子10A1 姓名赵祥琨 学号1006013145 指导教师张亚 实训时间2012 . 6
电子钟 摘要 本系统以MCS-51单片机为核心,以8位8段数码管显示时间,可以通过键盘设定时间和闹钟时间,并具备整点报时功能。 系统特色:1)使用定时器产生节拍,程序运行采用轮循机制,分时复用CPU 资源,增强系统可靠性。 2)采用采用三个按键设置时钟时间和闹钟时间,使用一个按键关闭闹钟,电路结构简单,使用方便。 关键词:电子钟;C语言;MCS-51;电子 ELECTRONIC CLOCK Abstract: The system takes the MCS-51 single chip as the core,display time on 8X7seg,Through the keyboard to set the time and alarm time, and with the whole point timekeeping function. System characteristics:1)Use timer produces beats, running the round-robin mechanism, time division multiplexing CPU resources, improve the reliability of the system. 2)Using three key clock set time and alarm time, the use of a button to turn off the alarm, the circuit structure is simple, easy to use. Keywords:…electronic ;C language;electronics
51单片机驱动1602液晶显示器c程序 /*程序效果:单片机控制液晶显示器1602 显示字母数字,用户自行更改io 程序原创安全:51hei*/#includereg52.h //头文件#define uchar unsigned char //宏定义#define uint unsigned intuchar code table[]=“51HEI XING XING “ ; //显示的字母uchar code table1[]=“51HEI MCUXUE YUAU” ;sbit lcdrs=P1; //寄存器选择引脚sbit lcdwr=P1;//读写引脚sbit lcde=P1 ; //片选引脚void delay(uchar x) //延时子函数{uchar i,j;for(i=x;i0;i--) for(j=110;j0;j--);}void write_com(uchar com) //写指令子函数{ //根据1602 液晶 显示器协议编写P2=com; lcdrs=0;lcdwr=0;delay(5);lcde=0; delay(5);lcde=1; }void write_dat(uchar dat) //写数据子函数{P2=dat; lcdrs=1;lcdwr=0;delay(5);lcde=0;delay(5);lcde=1;}void init() //初始化子函数{write_com(0x01); //清屏write_com(0x3f); //功能设置write_com(0x0d); //显示控制write_com(0x06); //输入方式设置}void main(){ uchar i; //定义局部变量init();write_com(0x80+0x02+0x10); //指针的位置for(i=0;i15;i++) //显示{ write_dat(table[i]); delay(50); // 延时,用于调节速度不匹配}write_com(0x80+0x40+0x10); for(i=0;i16;i++){ write_dat(table1[i]); delay(50);} for(i=0;i16;i++){ write_com(0x18); delay(50);} while(1);}tips:感谢大家的阅读,本文由我司收集整编。仅供参阅!
#include <> void print_star(void) { printf("*****************\n"); } void print_welcome(void) { printf("C language,welcome!\n"); } void main() { print_star(); print_welcome(); print_star(); getchar(); } 演示2 #include "" int sum(int i,int j) { return(i + j); } void main() { int n1,n2; printf("input 2 numbers:\n"); scanf("%d%d",&n1,&n2); printf("the sum = %d\n",sum(n1,n2)); getchar(); } 演示3 #include "" int maxnum(int,int,int); main() { int a,b,c; printf("Please enter 3 numbers:\n"); scanf("%d,%d,%d",&a,&b,&c); printf("Maxnum is %d\n",maxnum(a,b,c)); return 0;
} int maxnum(int x,int y,int z) { int max=x; if(y>max) max = y; if(z>max) max = z; return max; } 演示4 #include <> int s1(int n) { int j,s; s=0; for(j=1;j<=n;j++) s=s+j; return s; } int sum(int n) { int i,s=0; for(i=1;i<=n;i++) s=s+s1(i); return s; } void main() { int n; printf("n:"); scanf("%d",&n); printf("s=%d\n",sum(n)); } 演示5 #include <>
数字时钟89C52 单片机C语言程序 STC89C52| /************** 【数字时钟】****************/ /****【功能】1、时间显示2、秒表3、闹钟4、日期显示都可以设置****/ #include
教材习题答案 第一章 习题 一、单项选择题 1. C 2. B 3. B 4. C 5. D 6. A 7. C 8. A 二、填空题 1. 判断条件 2. 面向过程编程 3. 结构化 4. 程序 5. 面向对象的程序设计语言 6. 基本功能操作、控制结构 7. 有穷性 8. 直到型循环结构 9. 算法 10.可读性 11.模块化 12.对问题的分解和模块的划分
习题 一、单项选择题 1. B 2. D 3. C 4. B 5. A 6. A 7. B 8.C 二、填空题 1. 主 2. C编译系统 3. 函数、函数 4. 输入输出 5. 头 6. .OBJ 7. 库函数 8. 文本 第三章 习题 一、单项选择题 1. D 2. B 3. A
5. C 6. D 7. D 8. B 9. B 10.C 11.A 12.D 13.C 14.C 15.C 16.A 17.C 18.C 19.C 20.D 21.A 22.D 23.D 24.D,A 25.D 26.A 27.B 二、填空题 1. 补码 2. 308 10 - ±
~)308 10 ,15 —6 2. 308 10 - ± (~)308 10 ,15
6 3. 逻辑 4. 单目,自右向左 5. 函数调用 6. a 或 b ( 题目有错 , 小括号后面的 c<=98 改成( c>=97&&c<=98 )就可以得到所给的答案了) 7.
8. 65 , 89 第四章 习题 一、单项选择题 1. D 2. C 3. D 4. A 5. D 6. B 7. A 8. C 9. B 10.B 二、填空题 1. 一
C语言程序设计习题参考答案 习题1 一、判断题 1.在计算机中,小数点和正负号都有专用部件来保存和表示。 2.二进制是由0和1两个数字组成的进制方式。 3.二进制数的逻辑运算是按位进行的,位与位之间没有进位和借位的关系。 4.在整数的二进制表示方法中,0的原码、反码都有两种形式。 5.有符号数有三种表示法:原码、反码和补码。 6.常用字符的ASCII码值从小到大的排列规律是:空格、阿拉伯数字、大写英文字母、小写英文字母。 解:1.F 2.T 3.T 4.T 5.T 6.T 二、单选题 1.在计算机中,最适合进行数值加减运算的数值编码是。 A. 原码 B. 反码 C. 补码 D. 移码 2.已知英文小写字母m的ASCII码为十进制数109,则英文小写字母y的ASCII码为十进制数。 A. 112 B. 120 C. 121 D. 122 3.关于ASCII码,在计算机中的表示方法准确地描述是。 A. 使用8位二进制数,最右边一位为1 B. 使用8位二进制数,最左边一位为1 C. 使用8位二进制数,最右边一位为0 D. 使用8位二进制数,最左边一位为0 4.设在机器字长4位,X=0111B,Y=1011B,则下列逻辑运算中,正确的是___________。 A. X∧Y=1000 B. X∨Y=1111 C. X⊕Y=0011 D. ˉY=1000 5.下列叙述中正确的是()。 A.高级语言就是机器语言 B.汇编语言程序、高级语言程序都是计算机程序,但只有机器语言程序才是计算机可以直接识别并执行的程序 C.C语言因为具有汇编语言的一些特性,所以是汇编语言的一种 D.C源程序经过编译、连接,若正确,执行后就能得到正确的运行结果6.用C语言编写的源程序经过编译后,若没有产生编译错误,则系统将()。 A.生成可执行文件B.生成目标文件 C.输出运行结果D.自动保存源文件 7.下列叙述中不正确的是()。 A.main函数在C程序中必须有且只有一个 B. C程序的执行从main函数开始,所以main函数必须放在程序最前面 C. 函数可以带参数,也可以不带参数。 D. 每个函数执行时,按函数体中语句的先后次序,依次执行每条语句 解:1.C 2.C 3.D 4.B 5.B 6.B 7.B 三、填空题
演示1 #include
return 0; } int maxnum(int x,int y,int z) { int max=x; if(y>max) max = y; if(z>max) max = z; return max; } 演示4 #include
/*电子钟显示时分秒*/ #include
#include
} p=0; } } } } void delay(uchar n) { uchar i,j; for(i=0;i C语言程序设计第三版谭浩强 课后习题答案完整版 第一章 1.5请参照本章例题,编写一个C程序,输出以下信息:************************** V ery Good! ************************** 解: mian() {printf(“**************************”); printf(“\n”); printf(“V ery Good!\n”); printf(“\n”); printf(“**************************”); } 1.6 编写一个程序,输入a、b、c三个值,输出其中最大值。解: mian() {int a,b,c,max; printf(“请输入三个数a,b,c:\n”); scanf(“%d,%d,%d”,&a,&b,&c); max=a; if(max main() { char c1=?C?,c2=?h?,c3=?i?,c4=?n?,c5=?a?; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; printf("密码是%c%c%c%c%c\n",c1,c2,c3,c4,c5); } 运行结果: 密码是Glmre 3.9求下面算术表达式的值。 (1)x+a%3*(int)(x+y)%2/4 设x=2.5,a=7,y=4.7 (2)(float)(a+b)/2+(int)x%(int)y 设a=2,b=3,x=3.5,y=2.5 (1)2.5 (2)3.5 3.10写出程序运行的结果。 main() {int i,j,m,n; i=8; j=10; m=++i; n=j++; printf(“%d,%d,%d,%d”,i,j,m,n); } 解: 9,11,9,10 3.12 写出下面表达式运算后a的值,设原来a=12。设a和n都已定义为整型变量。 (1)a+=a (2)a-=2 (3)a*=2+3 (4)a/=a+a (5)a%=(n%=2),n的值等于5 (6)a+=a-=a*=a 解: (1) 24 (2) 10 (3) 60 (4) 0 (5) 0 (6) 0 第四章 4.4若a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51274,n=128765,c1=’a’,c2=’b’。想得到以下输出格式和结果,请写出程序(包括定义变量类型和设计输出)。 a=_3_ _b=_4_ _c=_5 x=1.200000,y=2.400000,z=-3.600000 x+y=_3.600_ _y+z=-1.20_ _z+x=-2.40 c1=ˊaˊ_or_97(ASCII) /**返回的long型的最大值是startLongValue+count-1(产生一个随机数) * param needCount * param count * param startLongValue * return */ public static ListC语言程序设计第三版谭浩强课后习题答案完整版
一些比较简c语言程序源代码