❶ 单片机c语言关于定时计数器程序运行时序问题
这个和你设置的定时器的工作模式有关系,你要在定时时间到后,依然想要TH0=0xfc; TL0=0x18。这两个寄存器值存在的话,你就要设置定时器的工作模式为:自动重装初值
❷ 单片机用c语言编一个程序,要求用内部定时/计数器对30个脉冲进行计数,并同时对这三十个脉冲进行计时。
看了一下你的要求,单片机里有个GATE位的设置如果你懂得用,编这个程序就不难了。下面是我个人按照你要求刚编的程序:
/***********************
功能:内部定时/计数器记录脉冲个数和这些脉冲通过后所用的时间。
硬件:单片机用AT89c51,12M晶振。
原理:当门控位GATE为“1”时,TR0/1=1,INT0/1才能启动定时器。
利用这个特性可以测量外部输入脉冲的宽度。
分析:设外部脉冲由(P3.2)输入,T0工作于定时方式1(16位计数),
GATE设为1.测试时,应在INT0低电平时,设置TR0为1(避免检测
到正在进行的脉冲);当INT0为高电平时,就启动计数,INT0再
次变低时,停止计数。此计数值与机器周期的乘积即为被测正脉
冲的宽度。因晶振为12MHz,机器周期为1us.
************************/
#include <reg51.h>
//sbit INT0=P3^2;//INT0在头文件里已定义为P3^2口
#define uint unsigned int
uint pulseWidth[30],num;
void init()
{
num=0;
TMOD=0x09;//选择定时器并设T0为方式1
TH0=0x00; //设计数初会为最大值
TL0=0x00;
}
main()
{
init();
while(1)
{
if(num<30) //记录30个脉冲
{
while(INT0);//等待INT0变低
TR0=1; //应在INT0低电平时,设置TR0为1
while(!INT0);//等待INT0变高,启动定时器开始计数
while(INT0); //等待INT0变低,停止定时器开始计数
TR0=0;
pulseWidth[num]=(uint)TL0;
pulseWidth[num]|=(uint)(TH0<<8);
//因机器周期为1us,故pulseWidth数组里存的数目加上单位us,
//即为所求的脉冲宽度
num++;
}
}
}
❸ 51C语言的计数器怎么设置的,最好有解释,
void dis_cnter(void) 显示计数器值子程序开始
{uchar i; 定义字符型局部变量i
P3_7=1;P3_6=1; 消隐有效,允许显示
for(i=0;i8;i++) 循环8次
根据8个单元的计数值查表发送显示
P3_7=0; 关闭消隐,正常显示
} 显示计数器值子程序结束
void cnter(void) 计数转换子程序开始
{uchar i; 定义字符型局部变量i
for(i=0;i7;i++) 循环8次
}若计数器某位值大于9,则该位回0,
而高位被进位加1
------------------------------------
if(cnt[7]9) 若计数器最高位值大于9,则计数器全清0
} 计数转换子程序结束
void pt_cnt(void) 输出控制子程序开始
{
if((cnt[0]==pre_cnt[0])&&(cnt[1]==pre_cnt[1])&&
(cnt[2]==pre_cnt[2])&&(cnt[3]==pre_cnt[3])&&
(cnt[4]==pre_cnt[4])&&(cnt[5]==pre_cnt[5])&&
(cnt[6]==pre_cnt[6])&&(cnt[7]==pre_cnt[7])) 若计数值等于预置值
out_flag=1; 输出标志置1
else out_flag=0; 否则输出标志置0
=================================================
if(out_flag)drv=0; 若输出标志为1,输出端(P3.4)置0,驱动光耦U2
else drv=1; 否则输出端(P3.4)置1
} 输出控制子程序结束
void main(void) 主程序开始
{ IE=0x84; 开放CPU中断,开放外中断1的中断
IT1=1; 置外中断1为边沿触发
while(1) 无限循环
{P3_6=0;P3_7=1; 禁止发送给显示器,消隐有效
SBUF=0x00;while(!TI);TI=0; 向IC2发送0x00,使IC2输出为全0
if((P3&0x24)!=0x24)keydown_flag=1; 检测P3.2、P3.5是否为高电平。
若不为高电平,说明有键按下,键被按下标志置1
else keydown_flag=0; 否则,键被按下标志置0
-------------------------------------------------------------------------------
SBUF=0xfe;while(!TI);TI=0; 向IC2发送0xfe
if(!P3_2)set_flag=!set_flag; 若P3.2为0,说明S9键按下,标志set_flag取反
==================================================
if(!set_flag)
若标志set_flag为0,调用计数、显示计数、输出控制的子程序
else
否则标志set_flag为1,调用扫描S1~S8键、显示预置值的子程序
=====================================================
if(keydown_flag)delay(300); 因有键按下,调用延时300mS子程序,
以免调整键值的速度过快
}
} 主程序结束
void extern_int1(void) interrupt 2 using 1 外中断1服务子程序
{
cnt[0]++; 计数器的最低位加1
}
❹ 脉搏计数器的程序(用C语言编写程序)
#include <reg51.h>
unsigned char i,j,t,m,DelayTime,DispBuf[3];
unsigned int n,mb;
unsigned char code
BitTab[3]={0xf7,0xef,0xdf};//位驱动码
unsigned char code
DispTab[10]={0x81,0xcf,0x92,0x86,0xcc,0xa4,0xa0,0x8f,0x80,0x84};//字形码
sbit P3_0=P3^0;
void delay(DelayTime);
main() //主程序
{
TMOD=0x01; //定时器T0工作于方式1
TH0=0xec;
TL0=0x78; //T0定时时间为5ms
IE=0X83; //开中断
IT0=1; //外部中断0为边沿触发方式
TR0=1; //开定时器T0
for(;;) //脉搏指示灯控制
{
if(P3_0==0)
{
delay(200);
P3_0=1;
}
}
}
external0() interrupt 0//外部中断服务程序
{
P3_0=0; //点亮指示灯
if(n==0)
mb=0;
else
mb=12000/n; //计算每分钟脉搏数
DispBuf[2]=mb%10;//取个位数
mb=mb/10;
DispBuf[1]=mb%10;//取十位数
DispBuf[0]=mb/10;//取百位数
n=0;
}
Timer0() interrupt 1//定时中断服务程序
{
TH0=0xec;
TL0=0x78;
t=BitTab[j];//取位值
P3=P3|0x38;//P3.3-P3.5送1
P3=P3&t; //P3.3-P3.5输出取出的位值
t=DispBuf[j];//取出待显示的数
t=DispTab[t];//取字形码
P1=t; //字型码由P3输出显示
j++; //j作为数码管的计数器,取值0-2,显示程序通过它确认显示哪个数码管
if(j==3)
j=0;
n++;
if(n==2000)//10秒钟测不到心率,n复位
n=0;
}
void delay(DelayTime)//延时子程序
{
for(;DelayTime>0;DelayTime--)
{
for(i=0;i<250;i++)
;
}
}
❺ 单片机计数器编程如何计数脉冲,用C语言
每50ms来了多少脉冲,那定时器就不能50ms中断一次,尽量快的中断会比较好,2个变量计数,一个计算50ms,一个累加脉冲
❻ 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';
}
}
❼ 用c语言做一个简单的计算器。要求能做四则运算就够了,越简单越好。刚学了半年的C语言
#include <stdio.h>
#include <malloc.h>
#include<string.h>
struct int_char
{
short biao;/*标志是否运算数和运算符,0运算数1运算符*/
long su;/*存储运算数*/
char yunsuanhu;/*存储运算符*/
struct int_char *next;
};
int main(void)
{
int fujianche(char *pstring);
struct int_char *jieshuanshi(char *string);
void yunsuan(struct int_char *suansi);
char sr[100]={0};
struct int_char *p;
int jcsr,off=1;
printf("***The positive integer four mixed operation***\n");
do
{
printf("Please enter the formula:");
gets(sr);
jcsr=fujianche(sr);/*检测输入是否合法*/
if(jcsr==0)break;
if(jcsr==2)
{
printf("除数不能为0\n");
continue;
}
if(jcsr==1)
{
printf("非法的算式\n");
continue;
}
}
while(1);
p=jieshuanshi(sr);/*将字符串转换成链表*/
if(p==0)
{
printf("未输入算式");/*如果链表为空就表示在没有输入任何字符的情况下按下了回车*/
return(0);
}
yunsuan(p); /*运算并输出解题步骤*/
return(0);
}
int fujianche(char *pstring)/*字符串输入合法性检测函数*/
{
char suozuanfan(char fu); /*用于确定当前字符是否数字*/
int i,kuohao1=0,kuohao2=0;/*kuohao1,huohao2,用于检测括号的对称性*/
for(i=0;pstring[i]!='\0';i++)
{
/*算式的开头只能出现数字,'(' 结尾只能出现数字,')'*/
if(i==0&&pstring[i]!=suozuanfan(pstring[i])&&pstring[i]!='(')
{return(1);break;}
if(pstring[i+1]=='\0'&&pstring[i]!=suozuanfan(pstring[i])&&pstring[i]!=')')
if(i==0&&(pstring[i+1]==')'||pstring[i+2]==')'||pstring[i+3]==')'))
{return(1);break;}
if(pstring[i+1]=='\0'&&(pstring[i-1]=='('||pstring[i-2]=='('||pstring[i-3]=='('))
{return(1);break;}
/*检测运算符出现位置的合法性*/
if(i!=0&&pstring[i+1]!='\0')/*判断字符是否在字符串的第一位和倒数第二位!*/
{
if(pstring[i]=='+'||pstring[i]=='-'||pstring[i]=='*'||pstring[i]=='/')
{
if(pstring[i-1]!=')'&&pstring[i-1]!=suozuanfan(pstring[i-1]))
{return(1);break;}
if(pstring[i+1]!='('&&pstring[i+1]!=suozuanfan(pstring[i+1]))
{return(1);break;}
}
if(pstring[i]=='(')
{
if(pstring[i-1]!='('&&pstring[i-1]!='+'&&pstring[i-1]!='-'&&pstring[i-1]!='*'&&pstring[i-1]!='/')
{return(1);break;}
if(pstring[i+1]!='('&&pstring[i+1]!=suozuanfan(pstring[i+1]))
{return(1);break;}
}
if(pstring[i]==')'&&pstring[i+1]!='\0')
{
if(pstring[i-1]!=')'&&pstring[i-1]!=suozuanfan(pstring[i-1]))
{return(1);break;}
if(pstring[i+1]!=')'&&pstring[i+1]!='+'&&pstring[i+1]!='-'&&pstring[i+1]!='*'&&pstring[i+1]!='/')
{return(1);break;}
}
}
/*算式中不应出现除数字,加减乘除,()以外的其他字符*/
switch(pstring[i])
{
case '1' :break;
case '2' :break;
case '3' :break;
case '4' :break;
case '5' :break;
case '6' :break;
case '7' :break;
case '8' :break;
case '9' :break;
case '0' :break;
case '+' :break;
case '-' :break;
case '*' :break;
case '/' :{if(pstring[i+1]=='0')return(2);break;}
case '(' :kuohao1++;break;/*检测括号的对称性假设:如果'('和')'数量相同则视为括号对称这里对'('和')'进行统计在循环结束时对其对比.......*/
case ')' :kuohao2++;break;
default :return(-1);
}
}
if(kuohao1!=kuohao2)/*对比括号是否对称*/
return(1);
return(0);
}
char suozuanfan(char fu)/*检查fu是否数字*/
{
switch(fu)
{
case'1':{return('1');break;}
case'2':{return('2');break;}
case'3':{return('3');break;}
case'4':{return('4');break;}
case'5':{return('5');break;}
case'6':{return('6');break;}
case'7':{return('7');break;}
case'8':{return('8');break;}
case'9':{return('9');break;}
case'0':{return('0');break;}
default :return(-1);
}
}
struct int_char *jieshuanshi(char *string)/*这个函数将字符串转换成一个含算式的链表它用到了suozuanfan,suozuanfan1,yunsuanfutiqu三个函数,和struct int_char结构体*/
{
char suozuanfan(char fu);
int suozuanfan1(char fu);
char yunsuanfutiqu(char yunsuanfu);
long yunsuansu=0;
int i,j,x,y,jisuqi,weisuqi;/*jisuqi用于转换后的整数在yunsuansu数组中存储的位置,weisuqi用于数字转换位数的标志*/
int n=0;/*n 位链表长度,为0时表示建立第一节点,非0时表示建立的不是第一节点*/
struct int_char *head=0;
struct int_char *p1,*p2;
if(strlen(string)==0)return(0);
for(i=0,jisuqi=0;string[i]!='\0';i++,jisuqi++)/*遍历字符串*/
{
if(string[i]==suozuanfan(string[i]))/*检查串中的最高位数字*/
{
x=i;/*定位最高数字在串中的位置*/
for(j=i;string[j]==suozuanfan(string[j]);j++);/*检查个位数字的位置*/
y=j-1;/*定位个位数字在串中的位置*/
i=j-1;/*将遍历字符串的计数器定位在最后被检查的字符的位置*/
for(weisuqi=1,yunsuansu=0;y>=x;y--,weisuqi*=10)/*数字转换*/
yunsuansu+=suozuanfan1(string[y])*weisuqi;
if(n==0)/*建立表头及将运算数接入链表*/
{
p1=p2=head=(struct int_char *)malloc(sizeof(struct int_char));
n++;
p1->biao=0;
p1->su=yunsuansu;
p1->yunsuanhu=0;
p1->next=0;
}
else/*将运算数接入链表*/
{
p1=(struct int_char *)malloc(sizeof(struct int_char));
n++;
p2->next=p1;
p2=p2->next;
p1->biao=0;
p1->su=yunsuansu;
p1->yunsuanhu=0;
p1->next=0;
}
}
if(string[i]==yunsuanfutiqu(string[i]))/*检测是否运算符*/
{
if(n==0)/*建立表头及将运算符接入链表*/
{
p1=p2=head=(struct int_char *)malloc(sizeof(struct int_char));
n++;
p1->biao=1;
p1->su=0;
p1->yunsuanhu=string[i];
p1->next=0;
}
else/*将运算符接入链表*/
{
p1=(struct int_char *)malloc(sizeof(struct int_char));
n++;
p2->next=p1;
p2=p2->next;
p1->biao=1;
p1->su=0;
p1->yunsuanhu=string[i];
p1->next=0;
}
}
}
return (head);
}
char yunsuanfutiqu(char yunsuanfu)/*检测yunsuanfu是否运算符*/
{
switch(yunsuanfu)
{
case '(':{return('(');break;}
case ')':{return(')');break;}
case '+':{return('+');break;}
case '-':{return('-');break;}
case '*':{return('*');break;}
case '/':{return('/');break;}
default:return(0);
}
}
void yunsuan(struct int_char *suansi)/*运算函数有两个子函数jjcc用于进行右括号的运算但括号没只有一个运算符时消除括号*/
{ /*wkjjcc用于进行无括号的运算,jjcc,wkjjcc只进行单步运算*/
void jjcc(struct int_char *up,struct int_char *np);
void wkjjcc(struct int_char *up,struct int_char *np);
struct int_char *p1,*p2;/*p1,当前节点p2,上一个节点。*/
struct int_char *up,*nP;/*up(的位置,np)的位置*/
short off;
p1=suansi;
if(p1->next==0)/*如果连表中只有一个节点*/
{
printf("=%d\n",p1->su);/*就输出这个接点并结束*/
return;
}
for(;p1!=0;p1=p1->next)
if(p1->biao&&p1->yunsuanhu==')')/*找到')'的位置*/
{
nP=p1;
for(p2=suansi;p2!=nP;p2=p2->next)/*找到'('的位置*/
if(p2->biao&&p2->yunsuanhu=='(')
up=p2;
jjcc(up,nP);
printf("=");
for(p2=suansi;p2!=0;p2=p2->next)/*输出步骤*/
if(p2->biao)
printf("%c",p2->yunsuanhu);
else
printf("%d",p2->su);
printf("\n");
p1=suansi;
}
for(off=1;off;)
{
for(p1=suansi;p1->next;p1=p1->next);
up=suansi;
nP=p1;
if(up==nP)
off=0;
else
{
wkjjcc(up,nP);
printf("=");
for(p2=suansi;p2!=0;p2=p2->next)/*输出步骤*/
if(p2->biao)
printf("%c",p2->yunsuanhu);
else
printf("%d",p2->su);
printf("\n");
}
}
}
void jjcc(struct int_char *up,struct int_char *np)
{
struct int_char *p1,*p2,*p3;/*p1被运算数的位置p2运算符的位置p3运算数的位置*/
short off=1;
int i;
for(p2=up;p2!=np;p2=p2->next)
{
if(p2->biao&&(p2->yunsuanhu=='*'||p2->yunsuanhu=='/'))
if(p2->yunsuanhu=='*')
{
p3=p2->next;
p1->su=p1->su*p3->su;
p1->next=p3->next;
free(p2);
free(p3);
off=0;
break;
}
else
{
p3=p2->next;
p1->su=p1->su/p3->su;
p1->next=p3->next;
free(p2);
free(p3);
off=0;
break;
}
p1=p2;
}
for(p2=up;off&&p2!=np;p1=p2,p2=p2->next)
{
if(p2->biao&&(p2->yunsuanhu=='+'||p2->yunsuanhu=='-'))
if(p2->yunsuanhu=='+')
{
p3=p2->next;
p1->su=p1->su+p3->su;
p1->next=p3->next;
free(p2);
free(p3);
break;
}
else
{
p3=p2->next;
p1->su=p1->su-p3->su;
p1->next=p3->next;
free(p2);
free(p3);
break;
}
}
for(i=1,p3=up;p3!=np;i++,p3=p3->next);
if(i<5)
{
p2=up->next;
p3=p2->next;
up->biao=0;
up->yunsuanhu=0;
up->su=p2->su;
up->next=p3->next;
free(p2);
free(p3);
}
}
int suozuanfan1(char fu)
{
switch(fu)
{
case'1':{return(1);break;}
case'2':{return(2);break;}
case'3':{return(3);break;}
case'4':{return(4);break;}
case'5':{return(5);break;}
case'6':{return(6);break;}
case'7':{return(7);break;}
case'8':{return(8);break;}
case'9':{return(9);break;}
case'0':{return(0);break;}
}
}
void wkjjcc(struct int_char *up,struct int_char *np)
{
struct int_char *p1,*p2,*p3;/*p1被运算数的位置p2运算符的位置p3运算数的位置*/
short off=1;
for(p2=up;p2!=np;p2=p2->next)
{
if(p2->biao&&(p2->yunsuanhu=='*'||p2->yunsuanhu=='/'))
if(p2->yunsuanhu=='*')
{
p3=p2->next;
p1->su=p1->su*p3->su;
p1->next=p3->next;
free(p2);
free(p3);
off=0;
break;
}
else
{
p3=p2->next;
p1->su=p1->su/p3->su;
p1->next=p3->next;
free(p2);
free(p3);
off=0;
break;
}
p1=p2;
}
for(p2=up;off&&p2!=np;p1=p2,p2=p2->next)
{
if(p2->biao&&(p2->yunsuanhu=='+'||p2->yunsuanhu=='-'))
if(p2->yunsuanhu=='+')
{
p3=p2->next;
p1->su=p1->su+p3->su;
p1->next=p3->next;
free(p2);
free(p3);
break;
}
else
{
p3=p2->next;
p1->su=p1->su-p3->su;
p1->next=p3->next;
free(p2);
free(p3);
break;
}
}
}
❽ 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语言中,编写一个结构体数据的计数器的问题
你打开方式带a了?
追加?
代码贴出来
你的代码不全 我补了半天才能编译通过
这样
FILE *fp=fopen("d:\\ctest\\stu13","r");
FILE *fpn=fopen("d:\\ctest\\stu13_n.txt","w");
就这样
❿ 用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);
}