本文目录一览:

  • 1、c语言计数器
  • 2、如何用C语言编写这个计数程序
  • 3、介绍计数器 /定时器 程序 的编写步骤 (C语言的)
  • 4、用C语言写两个计数器的小程序。
  • 5、用c语言 (c++) 编写计算器程序
  • 6、51单片机按键计数器C语言编程

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!=79i!=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 = press0xff? press0xff: press8;

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′;

}

}

如何用C语言编写这个计数程序?

按照题目要求编写的C语言计数程序如下

#includestdio.h

int main(){

int i,start,end,step;

printf(“从哪个数字开始计数:”);

scanf(“%d”,start);

printf(“在哪个数字停止计数:”);

scanf(“%d”,end);

printf(“每次增加的数字:”);

scanf(“%d”,step);

for(i=start;i=end;i=i+step){

printf(“%d\n”,i);

}

return 0;

}

用c语言编写计数器程序,c语言中的计数器(如何用C语言编写计数器)-风君雪科技博客

介绍计数器 /定时器 程序 的编写步骤 (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语言写两个计数器的小程序。

很多人回家过年了,所以回答少,程序已通过

#includestdio.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 (in) m++; //如果in,则m+1;

}

printf(“大于N次数:%d\n”,m);

}

用c语言 (c++) 编写计算器程序

我们平时进行数学运算都是用计算器完成的,那么如何用C语言编写一个计算器呢?下面我给大家分享一下。

工具/材料

Dev C++

01

首先我们需要在Dev C++软件中创建一个C语言项目,项目类型选择控制台程序,如下图所示

02

接下来我们在项目下面新建C语言文件,如下图所示

03

然后我们在C文件中写入计算器逻辑代码,主要是让用户输入计算方式,然后程序自动计算,如下图所示

04

接下来我们点击运行菜单,选择下拉菜单中的运行选项,如下图所示

05

最后在弹出的界面中我们输入要计算的公式,程序就会自动计算,如下图所示

51单片机按键计数器C语言编程

#includereg51.h

#define uchar unsigned char;

uchar distab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};  //0到f

uchar number,dat,dis[4];

void t0isr() interrupt 1

{

TH0=(65536-5000)/256;

TL0=(65536-5000)%256;

number++;

number%=3;

switch(number)

P1=0x20number;

P0=distab[dis[number]];

}

void int0isr() interrupt 0

{

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);

}