c语言计数器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言计数器相关的知识,希望对你有一定的参考价值。
求一个选举统计的c语言程序代码......刚开始界面是“请输入选票上的号码:”。输入号码后回车,然后不断有“请输入选票上的的号码:”。输入“0”回车后自动统计每个号码得票次数。谢谢!
#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';
参考技术A C语言书上有这个例子。
就是谭浩强老先生那本,第二版。 参考技术B //---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
typedef struct TX /*得票数*/
int id;
int count;
struct TX *next;
X;
typedef struct /*共计投票数*/
int total;
X *h;
XH;
X *input(X *a,int id)/*投票*/
if (a==NULL)
a=malloc(sizeof(X));
a->id=id;
a->count =1;
a->next =NULL;
else if (a->id!=id)
a->next=input(a->next ,id);
else if (a->id==id)
a->count++;
return a;
void Free(X *h)/*释放资源*/
if (h->next !=NULL)
Free(h->next );
free(h);
int main(void)
int id=1,assert;
XH vote;
vote.total=0;
vote.h=NULL;
printf("请输入选票上的号码:");
scanf("%d",&id);
while (id)
if (assert)
vote.total ++;
vote.h=input(vote.h,id);
printf("请输入选票上的号码:");
fflush(stdin);
assert=scanf("%d",&id);
fflush(stdin);
printf("\n\nTotal:%d\n",vote.total );
while (vote.h!=NULL)
printf("%d:%d\n",vote.h->id,vote.h->count);
vote.h=vote.h->next ;
if (vote.h!=NULL) Free(vote.h);
return 0;
//---------------------------------------------------------------------------本回答被提问者采纳
c52开发版计数器c语言,C语言的简单计数器
现在在读写文件时不用fscanf和fprintf,使用fgetc和fputc该怎么该啊、
#include "stdio.h"
#include "stdlib.h"
#define FilePath "counter.dat"
int readCounter(char* path);
void writeCounter(int count , char* path);
void main()
{
int count=0;
count=readCounter(FilePath)+1;
printf("该程序已运行:%d\\n",count);
printf("-----------------------------------\\n");
printf("下面将运行次数写入计数器文件(counter.dat)....\\n");
writeCounter(count,FilePath);
printf("写入文件结束!\\n");
printf("程序运行结束!\\n");
printf("-----------------------------------\\n");
system("pause");//程序暂停
}
int readCounter(char* path)
{
FILE* fp;
int count;
if((fp=fopen(path,"r"))==NULL)
{
printf("没有计数器文件:count.dat,保存运行次数时将新建该文件!\\n");
return 0;
}
fscanf(fp,"%d",&count);
fclose(fp);
return count;
}
void writeCounter(int count , char* path)
{
FILE *fp;
if((fp=fopen(path,"w"))==NULL)
{
printf("无法创建计数器文件!\\n");
return ;
}
fprintf(fp,"%d",count);
fclose(fp);
}
以上是关于c语言计数器的主要内容,如果未能解决你的问题,请参考以下文章
分别用汇编语言和C语言设计: 采用中断方式,利用定时器进行0-9秒表计数器的设计,并用数码
用C语言的科学计数法形式写出下面这些数。1300,123.45,0.00426