Ⅰ 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( ); //持續刷新數碼管顯示
}
}
代碼有什麼問題的可以繼續問~~