Ⅰ c语言计数器
#include <stdlib.h>
#include <math.h>
#include <graphics.h>
#include <stdio.h>
#include <process.h>
#define EXCAPE 27
#define ENTER 13
main(){
int press,i,x,y,x1,y1,ch_z=0;
int dian=0;
char ch='0'; /*input + - * / */
char emp[80],sum[80],*e,*s;
double yuan=0.000000000000;
void init(void);
void clear_z(char *u);
double strtoflt(char *p);
int getkey();
int gd=DETECT, gm;
initgraph(&gd, &gm, "");
e=emp;
s=sum;
init();
x = (getmaxx() / 2) - 120;
y = (getmaxy() / 2) - 150;
x1 = (getmaxx() / 2) + 120;
y1 = (getmaxy() / 2) + 150;
while(1){
press = getkey();
switch(press){
case EXCAPE:
exit(0);
case 47:
bar (x + 10, y + 80 + 10, x + 60 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
if (strtoflt(emp)==0.0){
ch='0';
ch_z=0;
dian=0;
emp[0]='\0';
sum[0]='\0';
e=emp;
s=sum;
outtextxy(x+30,y+40,"error!!!!!");
break;
}
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
}
ch='/';
ch_z=0;
emp[0]='\0';
e=emp;
dian=0;
break;
case 42:
bar (x + 60 + 10, y + 80 + 10, x + 60 * 2 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else
outtextxy(x+30,y+40,sum);
}
ch='*';
ch_z=0;
dian=0;
break;
case 45:
bar (x + 60 * 2 + 10, y + 80 + 10, x + 60 * 3 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else
outtextxy(x+30,y+40,sum);
}
ch='-';
ch_z=0;
dian=0;
break;
case 43:
bar (x + 60 * 3 + 10, y + 80 + 10, x + 60 * 4 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else
outtextxy(x+30,y+40,sum);
}
ch='+';
ch_z=0;
dian=0;
break;
case 49:
bar (x + 10, y + 80 + 53 + 10, x + 60 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (ch_z==0){
*e='1';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 50:
bar (x + 60 + 10, y + 80 + 53 + 10, x + 60 * 2 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (ch_z==0){
*e='2';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 51:
bar (x + 60 * 2 + 10, y + 80 + 53 + 10, x + 60 * 3 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (ch_z==0){
*e='3';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case ENTER:
bar (x + 60 * 3 + 10, y + 80 + 53 + 10, x + 60 * 4 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else{
outtextxy(x+30,y+40,sum);
}
}
ch='0';
ch_z=1;
dian=0;
break;
case 52:
bar (x + 10, y + 80 + 53 * 2 + 10, x + 60 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0){
*e='4';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 53:
bar (x + 60 + 10, y + 80 + 53 * 2 + 10, x + 60 * 2 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0){
*e='5';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 54:
bar (x + 60 * 2 +10, y + 80 + 53 * 2 + 10, x + 60 * 3 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0){
*e='6';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 46:
bar (x + 60 * 3 + 10, y + 80 + 53 * 2 + 10, x + 60 * 4 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (dian==0){
if (ch_z==0){
*e='.';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
}
else{
if (ch_z==0)
outtextxy(x+30,y+40,emp);
else
outtextxy(x+30,y+40,sum);
}
dian=1;
break;
case 55:
bar (x + 10, y + 80 + 53 * 3 + 10, x + 60 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='7';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 56:
bar (x + 60 + 10, y + 80 + 53 * 3 + 10, x + 60 * 2 -10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='8';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 57:
bar (x + 60 * 2 + 10, y + 80 + 53 * 3 + 10, x + 60 * 3 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='9';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 48:
bar (x + 60 * 3 + 10, y + 80 + 53 * 3 + 10, x + 60 * 4 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='0';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 32:
emp[0]='\0';
sum[0]='\0';
e=emp;
s=sum;
ch='0';
ch_z=0;
dian=0;
init();
break;
case 8:
delay(8000);
for(i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (i==0)
break;
if (i!=79&&i!=0){
i--;
emp[i]='\0';
e=&emp[i];
}
init();
outtextxy(x+30,y+40,emp);
break;
}
}
}
/*---------------------------------------------------------------------*/
void init(void){
int x, y, x1, y1, i, j;
char emp;
x = (getmaxx() / 2) - 120;
y = (getmaxy() / 2) - 150;
x1 = (getmaxx() / 2) + 120;
y1 = (getmaxy() / 2) + 150;
cleardevice();
setbkcolor(3);
setfillstyle(1, 15);
setcolor(15);
settextstyle(1,0,1);
rectangle (x, y, x1, y1);
rectangle (x - 7, y - 7, x1 + 7, y1 + 7);
rectangle (x + 10, y + 10, x1 - 10, y + 80 - 10);
line (x, y + 80, x1, y + 80);
y = y + 80;
for (j = 1; j <= 4; j++){
x = (getmaxx() / 2) - 120;
for (i = 1; i <= 4; i++){
/* bar (x + 10, y + 10, x + 60 - 10, y + 60 - 10);*/
rectangle(x + 10, y + 10, x + 60 - 10, y + 60 - 10);
if (j == 1){
if (i == 1)
outtextxy(x + 20, y + 20, "/");
if (i == 2)
outtextxy(x + 25, y + 20, "*");
if (i == 3)
outtextxy(x + 27, y + 20, "-");
if (i == 4)
outtextxy(x + 25, y + 20, "+");
}
if (j == 2){
if (i == 1)
outtextxy(x + 25, y + 20, "1");
if (i == 2)
outtextxy(x + 25, y + 20, "2");
if (i == 3)
outtextxy(x + 25, y + 20, "3");
if (i == 4)
outtextxy(x + 25, y + 20, "=");
}
if (j == 3){
if (i == 1)
outtextxy(x + 25, y + 20, "4");
if (i == 2)
outtextxy(x + 25, y + 20, "5");
if (i == 3)
outtextxy(x + 25, y + 20, "6");
if (i == 4)
outtextxy(x + 25, y + 20, ".");
}
if (j == 4){
if (i == 1)
outtextxy(x + 25, y + 20, "7");
if (i == 2)
outtextxy(x + 25, y + 20, "8");
if (i == 3)
outtextxy(x + 25, y + 20, "9");
if (i == 4)
outtextxy(x + 25, y + 20, "0");
}
x = x + 60;
}
y = y + 53;
}
}
/*---------------------------------------------------------------------*/
int getkey(){
char lowbyte;
int press;
while(bioskey(1)==0);
press = bioskey(0);
press = press&0xff? press&0xff: press>>8;
return(press);
}
double strtoflt(char *p)
{
double rtl=0.000000000000;
double pnt=0.000000000000;
double t = 10;
int ispoint = 0;
while (*p!='\0'||*p!='.'){
if(*p<'0'||*p>'9')
break;
rtl*=10;
rtl+=*p-'0';
p++;
}
if (*p=='.'){
ispoint=1;
p++;
}
while(ispoint&&*p!='\0'){
pnt+=(double)(*p-'0')/t;
t*=10;
p++;
}
rtl+=pnt;
return (rtl);
}
/*-----------------------------------------------------------------------*/
void clear_z(char u[]){
int i;
for(i=strlen(u)-1;i>=0;i--){
if (u[i]!='0')
break;
}
if (u[i]=='.'){
u[i]='\0';
}
else{
i++;
u[i]='\0';
}
}
Ⅱ 51单片机按键计数器C语言编程
#include<reg51.h>
#defineucharunsignedchar;
uchardistab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};//0到f
ucharnumber,dat,dis[4];
voidt0isr()interrupt1
{
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
number++;
number%=3;
switch(number)
P1=0x20<<number;
P0=distab[dis[number]];
}
voidint0isr()interrupt0
{
dat++;
dat%=1000;
dis[0]=dat%10;
dis[1]=dat%100/10;
dis[2]=dat/100;
}
main()
{
TMOD=0x01;
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
TR1=1;
ET1=1;
EX0=1;
IT0=1;
EA=1;
while(1);
}
Ⅲ c语言:编写程序,从键盘输入80个字符,统计其中数字字符的个数,并输出统计结果
可以参考下面的代码:#include <stdio.h>intmain(){inta,b,c,ch;a=b=c=0;//计数器初始化为0.while((ch=getchar())!='\n')//循环读取字符,到换行结束。{if(ch>='0' && ch<='9')//数字a++;else if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))//字母b++;else//其它c++;}printf("%d%d%d\n",a,b,c);//输出结果。return0;}(3)c语言计数器代码扩展阅读:printf()函数函数printf()函数是格式化输出函数, 一般用于向标准输出设备按规定格式输出信息。在编写程序时经常会用到此函数。函数的原型为:int printf(const char *format, ...);函数返回值为整型。若成功则返回输出的字符数,输出出错则返回负值,printf()函数的调用格式为:printf("<格式化字符串>", <参量表>);while语句的一般表达式为:while(表达式){循环体}。参考资料来源:网络-printf()参考资料来源:网络-while (循环语句及英文单词)
Ⅳ c语言编写计算器程序
C语言编写计算器
我们可以用printf和scanf函数输出结果和获取用户的输入。需要<stdio.h>头文件。scanf函数在读取数据的时候不需要再一行上输入每个数据,只要数据和数据之间留出空白就可以了。先声明两个变量number1和number2,operation变量用来存储运算符。用scanf函数获取这两个数字和运算符。分别用%lf %c %lf
Ⅳ 基于51单片机的简易计数器设计c程序
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar code ledtab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//0-9
uchar scanled;
uchar disdat[4];
uint ss,time;
sbit led=P1^0;
void dischg()
{
disdat[3]=ss%10;
disdat[2]=(ss/10)%10;
disdat[1]=(ss/100)%10;
disdat[0]=(ss/1000)%10;
}
void t0isr() interrupt 1
{
ss++;
dischg();
}
void t1isr() interrupt 3//显示
{
TH1=(65536-5000)/256;
TL1=(65536-5000)%256;
P2=1<<scanled;
P0=~ledtab[disdat[scanled]];
scanled++;
scanled%=4;
time++;
if(time>100){time=0;led=~led;}
}
main()
{
TMOD=0x16;
TH0=0xff;
TL0=0xff;
TH1=(65536-5000)/256;
TL1=(65536-5000)%256;
TR1=1;
TR0=1;
ET0=1;
ET1=1;
EA=1;
led=0;
scanled=0;
ss=0;
dischg();
while(1);
}
Ⅵ 介绍计数器 /定时器 程序 的编写步骤 (C语言的)
假设你用的晶振为12m,用p1.0口输出周期为2ms的方波。使用定时器工作方式1.
至于计数初值的计算,授之以鱼不如授之以渔!
在定时器模式下,计数器的计数脉冲来自于晶振脉冲的12分频信号,即对机器周期进行计数。若选择12m晶振,则定时器的计数频率为1mhz。假设定时时间为t,机器周期为t1,即12/晶振频率。x为定时器初值。则
x=2^n-t/t1。方式0,n=13,方式1时,n=16,方式2和方式3,n=8
自己算去吧!
#include
void
inittimer0(void)//
{
tmod
=
0x01;
th0
=
0x0fc;
//计数器初值
tl0
=
0x18;
ea
=
1;
et0
=
1;
tr0
=
1;//开启定时器t0
}
void
main(void)
{
inittimer0();
}
void
timer0interrupt(void)
interrupt
1
{
th0
=
0x0fc;//重新赋初值
tl0
=
0x18;
p1.0=~p1.0;
//输出方波
}
Ⅶ C语言设计一个加减计数器,通过两个按键来控制。就是按一个键就加一,按另外一个就减一。求大神帮忙设计
如果是windows上程序。单词按键判断ASCII码,然后变量值++,--就可以了。
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#defineKEYA61//按键盘'+'键(非小键盘)
#defineKEYM45//按键盘'-'键(非小键盘)
intmain()
{
intkey,num=0;
while(1)
{
system("cls");
printf("当前值:%d ",num);
key=getch();
switch(key)
{
caseKEYA:num++;break;
caseKEYM:num--;break;
}
}
return0;
}
如果你是要其他平台,比如单片机上运行,只要对应按钮电平对应防抖代码中对变量++,--就可以了。我之前给别人写个一个单片机的简单程序,里面就有按钮+-的,你可以参考。
以前回答记录
Ⅷ 用C语言写两个计数器的小程序。
很多人回家过年了,所以回答少,程序已通过
#include<stdio.h>
/*第一个程序:*/
main()
{
int i=1, m=0;
printf("请输入一个数:\n");
while(i!=0){ //while 循环开始当i等于0时,程序中止;
scanf("%d",&i); //从键盘获取数值,如果i=0,则退出并输出m的值
//printf("\n");
if (i==1) m++; //如果i=1,则m+1;
}
printf("输入 '1' 的次数:%d\n",m);
}
//第二个程序 在第一个基础上稍修改即可
main()
{
int i=1, n,m=0;
printf("请输入N:\n");
scanf("%d",&n);
printf("请输入一个数:\n");
while(i!=0){
scanf("%d",&i); //从键盘获取数值,如果i=0,则退出并输出m的值
if (i>n) m++; //如果i>n,则m+1;
}
printf("大于N次数:%d\n",m);
}
Ⅸ 计算器c语言代码
#include<dos.h>/*DOS接口函数*/
#include<math.h>/*数学函数的定义*/
#include<conio.h>/*屏幕操作函数*/
#include<stdio.h>/*I/O函数*/
#include<stdlib.h>/*库函数*/
#include<stdarg.h>/*变量长度参数表*/
#include<graphics.h>/*图形函数*/
#include<string.h>/*字符串函数*/
#include<ctype.h>/*字符操作函数*/
#defineUP0x48/*光标上移键*/
#defineDOWN0x50/*光标下移键*/
#defineLEFT0x4b/*光标左移键*/
#defineRIGHT0x4d/*光标右移键*/
#defineENTER0x0d/*回车键*/
void*rar;/*全局变量,保存光标图象*/
structpalettetypepalette;/*使用调色板信息*/
intGraphDriver;/*图形设备驱动*/
intGraphMode;/*图形模式值*/
intErrorCode;/*错误代码*/
intMaxColors;/*可用颜色的最大数值*/
intMaxX,MaxY;/*屏幕的最大分辨率*/
doubleAspectRatio;/*屏幕的像素比*/
voiddrawboder(void);/*画边框函数*/
voidinitialize(void);/*初始化函数*/
voidcomputer(void);/*计算器计算函数*/
voidchangetextstyle(intfont,intdirection,intcharsize);/*改变文本样式函数*/
voidmwindow(char*header);/*窗口函数*/
intspecialkey(void);/*获取特殊键函数*/
intarrow();/*设置箭头光标函数*/
/*主函数*/
intmain()
{
initialize();/*设置系统进入图形模式*/
computer();/*运行计算器*/
closegraph();/*系统关闭图形模式返回文本模式*/
return(0);/*结束程序*/
}
/*设置系统进入图形模式*/
voidinitialize(void)
{
intxasp,yasp;/*用于读x和y方向纵横比*/
GraphDriver=DETECT;/*自动检测显示器*/
initgraph(&GraphDriver,&GraphMode,"");
/*初始化图形系统*/
ErrorCode=graphresult();/*读初始化结果*/
if(ErrorCode!=grOk)/*如果初始化时出现错误*/
{
printf("GraphicsSystemError:%s\n",
grapherrormsg(ErrorCode));/*显示错误代码*/
exit(1);/*退出*/
}
getpalette(&palette);/*读面板信息*/
MaxColors=getmaxcolor()+1;/*读取颜色的最大值*/
MaxX=getmaxx();/*读屏幕尺寸*/
MaxY=getmaxy();/*读屏幕尺寸*/
getaspectratio(&xasp,&yasp);/*拷贝纵横比到变量中*/
AspectRatio=(double)xasp/(double)yasp;/*计算纵横比值*/
}
/*计算器函数*/
voidcomputer(void)
{
structviewporttypevp;/*定义视口类型变量*/
intcolor,height,width;
intx,y,x0,y0,i,j,v,m,n,act,flag=1;
floatnum1=0,num2=0,result;/*操作数和计算结果变量*/
charcnum[5],str2[20]={""},c,temp[20]={""};
charstr1[]="1230.456+-789*/Qc=^%";/*定义字符串在按钮图形上显示的符号*/
mwindow("Calculator");/*显示主窗口*/
color=7;/*设置灰颜色值*/
getviewsettings(&vp);/*读取当前窗口的大小*/
width=(vp.right+1)/10;/*设置按钮宽度*/
height=(vp.bottom-10)/10;/*设置按钮高度*/
x=width/2;/*设置x的坐标值*/
y=height/2;/*设置y的坐标值*/
setfillstyle(SOLID_FILL,color+3);
bar(x+width*2,y,x+7*width,y+height);
/*画一个二维矩形条显示运算数和结果*/
setcolor(color+3);/*设置淡绿颜色边框线*/
rectangle(x+width*2,y,x+7*width,y+height);
/*画一个矩形边框线*/
setcolor(RED);/*设置颜色为红色*/
outtextxy(x+3*width,y+height/2,"0.");/*输出字符串"0."*/
x=2*width-width/2;/*设置x的坐标值*/
y=2*height+height/2;/*设置y的坐标值*/
for(j=0;j<4;++j)/*画按钮*/
{
for(i=0;i<5;++i)
{
setfillstyle(SOLID_FILL,color);
setcolor(RED);
bar(x,y,x+width,y+height);/*画一个矩形条*/
rectangle(x,y,x+width,y+height);
sprintf(str2,"%c",str1[j*5+i]);
/*将字符保存到str2中*/
outtextxy(x+(width/2),y+height/2,str2);
x=x+width+(width/2);/*移动列坐标*/
}
y+=(height/2)*3;/*移动行坐标*/
x=2*width-width/2;/*复位列坐标*/
}
x0=2*width;
y0=3*height;
x=x0;
y=y0;
gotoxy(x,y);/*移动光标到x,y位置*/
arrow();/*显示光标*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,"");/*设置str2为空串*/
while((v=specialkey())!=45)/*当压下Alt+x键结束程序,否则执行下面的循环*/
{
while((v=specialkey())!=ENTER)/*当压下键不是回车时*/
{
putimage(x,y,rar,XOR_PUT);/*显示光标图象*/
if(v==RIGHT)/*右移箭头时新位置计算*/
if(x>=x0+6*width)
/*如果右移,移到尾,则移动到最左边字符位置*/
{
x=x0;
m=0;
}
else
{
x=x+width+width/2;
m++;
}/*否则,右移到下一个字符位置*/
if(v==LEFT)/*左移箭头时新位置计算*/
if(x<=x0)
{
x=x0+6*width;
m=4;
}/*如果移到头,再左移,则移动到最右边字符位置*/
else
{
x=x-width-width/2;
m--;
}/*否则,左移到前一个字符位置*/
if(v==UP)/*上移箭头时新位置计算*/
if(y<=y0)
{
y=y0+4*height+height/2;
n=3;
}/*如果移到头,再上移,则移动到最下边字符位置*/
else
{
y=y-height-height/2;
n--;
}/*否则,移到上边一个字符位置*/
if(v==DOWN)/*下移箭头时新位置计算*/
if(y>=7*height)
{
y=y0;
n=0;
}/*如果移到尾,再下移,则移动到最上边字符位置*/
else
{
y=y+height+height/2;
n++;
}/*否则,移到下边一个字符位置*/
putimage(x,y,rar,XOR_PUT);/*在新的位置显示光标箭头*/
}
c=str1[n*5+m];/*将字符保存到变量c中*/
if(isdigit(c)||c=='.')/*判断是否是数字或小数点*/
{
if(flag==-1)/*如果标志为-1,表明为负数*/
{
strcpy(str2,"-");/*将负号连接到字符串中*/
flag=1;
}/*将标志值恢复为1*/
sprintf(temp,"%c",c);/*将字符保存到字符串变量temp中*/
strcat(str2,temp);/*将temp中的字符串连接到str2中*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,str2);/*显示字符串*/
}
if(c=='+')
{
num1=atof(str2);/*将第一个操作数转换为浮点数*/
strcpy(str2,"");/*将str2清空*/
act=1;/*做计算加法标志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0.");/*显示字符串*/
}
if(c=='-')
{
if(strcmp(str2,"")==0)/*如果str2为空,说明是负号,而不是减号*/
flag=-1;/*设置负数标志*/
else
{
num1=atof(str2);/*将第二个操作数转换为浮点数*/
strcpy(str2,"");/*将str2清空*/
act=2;/*做计算减法标志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);/*画矩形*/
outtextxy(5*width,height,"0.");/*显示字符串*/
}
}
if(c=='*')
{
num1=atof(str2);/*将第二个操作数转换为浮点数*/
strcpy(str2,"");/*将str2清空*/
act=3;/*做计算乘法标志值*/
setfillstyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0.");/*显示字符串*/
}
if(c=='/')
{
num1=atof(str2);/*将第二个操作数转换为浮点数*/
strcpy(str2,"");/*将str2清空*/
act=4;/*做计算除法标志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0.");/*显示字符串*/
}
if(c=='^')
{
num1=atof(str2);/*将第二个操作数转换为浮点数*/
strcpy(str2,"");/*将str2清空*/
act=5;/*做计算乘方标志值*/
setfillstyle(SOLID_FILL,color+3);/*设置用淡绿色实体填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2);/*画矩形*/
outtextxy(5*width,height,"0.");/*显示字符串*/
}
if(c=='%')
{
num1=atof(str2);/*将第二个操作数转换为浮点数*/
strcpy(str2,"");/*将str2清空*/
act=6;/*做计算模运算乘方标志值*/
setfillstyle(SOLID_FILL,color+3);/*设置用淡绿色实体填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2);/*画矩形*/
outtextxy(5*width,height,"0.");/*显示字符串*/
}
if(c=='=')
{
num2=atof(str2);/*将第二个操作数转换为浮点数*/
switch(act)/*根据运算符号计算*/
{
case1:result=num1+num2;break;/*做加法*/
case2:result=num1-num2;break;/*做减法*/
case3:result=num1*num2;break;/*做乘法*/
case4:result=num1/num2;break;/*做除法*/
case5:result=pow(num1,num2);break;/*做x的y次方*/
case6:result=fmod(num1,num2);break;/*做模运算*/
}
setfillstyle(SOLID_FILL,color+3);/*设置用淡绿色实体填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2);/*覆盖结果区*/
sprintf(temp,"%f",result);/*将结果保存到temp中*/
outtextxy(5*width,height,temp);/*显示结果*/
}
if(c=='c')
{
num1=0;/*将两个操作数复位0,符号标志为1*/
num2=0;
flag=1;
strcpy(str2,"");/*将str2清空*/
setfillstyle(SOLID_FILL,color+3);/*设置用淡绿色实体填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2);/*覆盖结果区*/
outtextxy(5*width,height,"0.");/*显示字符串*/
}
if(c=='Q')exit(0);/*如果选择了q回车,结束计算程序*/
}
putimage(x,y,rar,XOR_PUT);/*在退出之前消去光标箭头*/
return;/*返回*/
}
/*窗口函数*/
voidmwindow(char*header)
{
intheight;
cleardevice();/*清除图形屏幕*/
setcolor(MaxColors-1);/*设置当前颜色为白色*/
setviewport(20,20,MaxX/2,MaxY/2,1);/*设置视口大小*/
height=textheight("H");/*读取基本文本大小*/
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);/*设置文本样式*/
settextjustify(CENTER_TEXT,TOP_TEXT);/*设置字符排列方式*/
outtextxy(MaxX/4,2,header);/*输出标题*/
setviewport(20,20+height+4,MaxX/2+4,MaxY/2+20,1);/*设置视口大小*/
drawboder();/*画边框*/
}
voiddrawboder(void)/*画边框*/
{
structviewporttypevp;/*定义视口类型变量*/
setcolor(MaxColors-1);/*设置当前颜色为白色*/
setlinestyle(SOLID_LINE,0,NORM_WIDTH);/*设置画线方式*/
getviewsettings(&vp);/*将当前视口信息装入vp所指的结构中*/
rectangle(0,0,vp.right-vp.left,vp.bottom-vp.top);/*画矩形边框*/
}
/*设计鼠标图形函数*/
intarrow()
{
intsize;
intraw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4};/*定义多边形坐标*/
setfillstyle(SOLID_FILL,2);/*设置填充模式*/
fillpoly(8,raw);/*画出一光标箭头*/
size=imagesize(4,4,16,16);/*测试图象大小*/
rar=malloc(size);/*分配内存区域*/
getimage(4,4,16,16,rar);/*存放光标箭头图象*/
putimage(4,4,rar,XOR_PUT);/*消去光标箭头图象*/
return0;
}
/*按键函数*/
intspecialkey(void)
{
intkey;
while(bioskey(1)==0);/*等待键盘输入*/
key=bioskey(0);/*键盘输入*/
key=key&0xff?key&0xff:key>>8;/*只取特殊键的扫描值,其余为0*/
return(key);/*返回键值*/
}
Ⅹ 5位数码管累计数器,高位 0 不显示,c语言
下面是关键部分,主要是利用if语句的嵌套,并非完整代码,因为做5位计数器电路原理图有多种方式,连接不同代码也不一样。
#define uchar unsigned char
//共阴接法数码管0~9的数字编码,最后一个是黑屏
uchar code DSY_CODE[ ] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
//计数值分解后的各待显示数位
uchar Display_Buffer[ ] = {0,0,0,0,0};
int Count = 0;
//--------------------------------------
//在数码管上显示计数值的函数
//--------------------------------------
void Show_Count_ON_DSY( )
{
Display_Buffer[4] = Count/10000; //获取第五位数(最高位)
Display_Buffer[3] = Count%10000/1000; //获取第四位数
Display_Buffer[2] = Count%1000/100; //获取第三位数
Display_Buffer[1] = Count%100/10; //第二位
Display_Buffer[0] = Count%10; //最低位
if(Display_Buffer[4] == 0) //高位为0时不显示
{
Display_Buffer[4] = 0x0a; //取DSY_CODE[ ]数组的第10个元素即0x00
if(Display_Buffer[3] == 0) //同理,利用if嵌套,从高位到低位一位一位去判断
{
Display_Buffer[3] = 0x0a;
if(Display_Buffer[2] == 0)
{
Display_Buffer[2] = 0x0a;
if(Display_Buffer[1] == 0)
{
Display_Buffer[1] = 0x0a;
}
}
}
}
//芯片引脚分配得自己定义,5位计数应该要外接一个I/O芯片,我这里例如把低3位的数码管单独接到cpu的P0,P1,P2
P0 = DSY_CODE[Display_Buffer[0]];
P1 = DSY_CODE[Display_Buffer[1]];
P2 = DSY_CODE[Display_Buffer[2]];
}
//然后在main函数里可以这样调用
void main( )
{
……
while(1)
{
Show_Count_ON_DSY( ); //持续刷新数码管显示
}
}
代码有什么问题的可以继续问~~