#include<stdio.h> #define TREEMAX 100 typedef struct BT { char data; BT *lchild; BT *rchild; }BT; BT *CreateTree(); void Preorder(BT *T); void Postorder(BT *T); void Inorder(BT *T); void Leafnum(BT *T); void Nodenum(BT *T); int TreeDepth(BT *T); int count=0; void main() { BT *T=NULL; char ch1,ch2,a; ch1='y'; while(ch1=='y'||ch1=='y') { printf("\n"); printf("\n\t\t 二叉树子系统"); printf("\n\t\t*****************************************"); printf("\n\t\t 1---------建二叉树 "); printf("\n\t\t 2---------先序遍历 "); printf("\n\t\t 3---------中序遍历 "); printf("\n\t\t 4---------后序遍历 "); printf("\n\t\t 5---------求叶子数 "); printf("\n\t\t 6---------求结点数 "); printf("\n\t\t 7---------求树深度 "); printf("\n\t\t 0---------返 回 "); printf("\n\t\t*****************************************"); printf("\n\t\t 请选择菜单号 (0--7)"); scanf("%c",&ch2); getchar(); printf("\n"); switch(ch2) { case'1': printf("\n\t\t请按先序序列输入二叉树的结点:\n"); printf("\n\t\t说明:输入结点(‘0’代表后继结点为空)后按回车。\n"); printf("\n\t\t请输入根结点:"); T=CreateTree(); printf("\n\t\t二叉树成功建立!\n");break; case'2': printf("\n\t\t该二叉树的先序遍历序列为:"); Preorder(T);break; case'3': printf("\n\t\t该二叉树的中序遍历序列为:"); Inorder(T);break; case'4': printf("\n\t\t该二叉树的后序遍历序列为:"); Postorder(T);break; case'5': count=0;Leafnum(T); printf("\n\t\t该二叉树有%d个叶子。\n",count);break; case'6': count=0;Nodenum(T); printf("\n\t\t该二叉树总共有%d个结点。\n",count);break; case'7': printf("\n\t\t该树的深度为:%d",TreeDepth(T)); break; case'0': ch1='n';break; default: printf("\n\t\t***请注意:输入有误!***"); } if(ch2!='0') { printf("\n\n\t\t按【Enter】键继续,按任意键返回主菜单!\n"); a=getchar(); if(a!='\xA') { getchar(); ch1='n'; } } } } BT *CreateTree() { BT *t; char x; scanf("%c",&x); getchar(); if(x=='0') t=NULL; else { t=new BT; t->data=x; printf("\n\t\t请输入%c结点的左子结点:",t->data); t->lchild=CreateTree(); printf("\n\t\t请输入%c结点的右子结点:",t->data); t->rchild=CreateTree(); } return t; } void Preorder(BT *T) { if(T) { printf("%3c",T->data); Preorder(T->lchild); Preorder(T->rchild); } } void Inorder(BT *T) { if(T) { Inorder(T->lchild); printf("%3c",T->data); Inorder(T->rchild); } } void Postorder(BT *T) { if(T) { Postorder(T->lchild); Postorder(T->rchild); printf("%3c",T->data); } } void Leafnum(BT *T) { if(T) { if(T->lchild==NULL&&T->rchild==NULL) count++; Leafnum(T->lchild); Leafnum(T->rchild); } } void Nodenum(BT *T) { if(T) { count++; Nodenum(T->lchild); Nodenum(T->rchild); } } int TreeDepth(BT *T) { int ldep,rdep; if(T==NULL) return 0; else { ldep=TreeDepth(T->lchild); rdep=TreeDepth(T->rchild); if(ldep>rdep) return ldep+1; else return rdep+1; } }
上传时间: 2020-06-11
上传用户:ccccy
#include <stdio.h> #include <stdlib.h> #define SMAX 100 typedef struct SPNode { int i,j,v; }SPNode; struct sparmatrix { int rows,cols,terms; SPNode data [SMAX]; }; sparmatrix CreateSparmatrix() { sparmatrix A; printf("\n\t\t请输入稀疏矩阵的行数,列数和非零元素个数(用逗号隔开):"); scanf("%d,%d,%d",&A.cols,&A.terms); for(int n=0;n<=A.terms-1;n++) { printf("\n\t\t输入非零元素值(格式:行号,列号,值):"); scanf("%d,%d,%d",&A.data[n].i,&A.data[n].j,&A.data[n].v); } return A; } void ShowSparmatrix(sparmatrix A) { int k; printf("\n\t\t"); for(int x=0;x<=A.rows-1;x++) { for(int y=0;y<=A.cols-1;y++) { k=0; for(int n=0;n<=A.terms-1;n++) { if((A.data[n].i-1==x)&&(A.data[n].j-1==y)) { printf("%8d",A.data[n].v); k=1; } } if(k==0) printf("%8d",k); } printf("\n\t\t"); } } void sumsparmatrix(sparmatrix A) { SPNode *p; p=(SPNode*)malloc(sizeof(SPNode)); p->v=0; int k; k=0; printf("\n\t\t"); for(int x=0;x<=A.rows-1;x++) { for(int y=0;y<=A.cols-1;y++) { for(int n=0;n<=A.terms;n++) { if((A.data[n].i==x)&&(A.data[n].j==y)&&(x==y)) { p->v=p->v+A.data[n].v; k=1; } } } printf("\n\t\t"); } if(k==1) printf("\n\t\t对角线元素的和::%d\n",p->v); else printf("\n\t\t对角线元素的和为::0"); } int main() { int ch=1,choice; struct sparmatrix A; A.terms=0; while(ch) { printf("\n"); printf("\n\t\t 稀疏矩阵的三元组系统 "); printf("\n\t\t*********************************"); printf("\n\t\t 1------------创建 "); printf("\n\t\t 2------------显示 "); printf("\n\t\t 3------------求对角线元素和"); printf("\n\t\t 4------------返回 "); printf("\n\t\t*********************************"); printf("\n\t\t请选择菜单号(0-3):"); scanf("%d",&choice); switch(choice) { case 1: A=CreateSparmatrix(); break; case 2: ShowSparmatrix(A); break; case 3: SumSparmatrix(A); break; default: system("cls"); printf("\n\t\t输入错误!请重新输入!\n"); break; } if (choice==1||choice==2||choice==3) { printf("\n\t\t"); system("pause"); system("cls"); } else system("cls"); } }
上传时间: 2020-06-11
上传用户:ccccy
FPGA片内FIFO读写测试Verilog逻辑源码Quartus工程文件+文档说明,使用 FPGA 内部的 FIFO 以及程序对该 FIFO 的数据读写操作。FPGA型号Cyclone4E系列中的EP4CE6F17C8,Quartus版本17.1。timescale 1ns / 1ps//////////////////////////////////////////////////////////////////////////////////module fifo_test( input clk, //50MHz时钟 input rst_n //复位信号,低电平有效 );//-----------------------------------------------------------localparam W_IDLE = 1;localparam W_FIFO = 2; localparam R_IDLE = 1;localparam R_FIFO = 2; reg[2:0] write_state;reg[2:0] next_write_state;reg[2:0] read_state;reg[2:0] next_read_state;reg[15:0] w_data; //FIFO写数据wire wr_en; //FIFO写使能wire rd_en; //FIFO读使能wire[15:0] r_data; //FIFO读数据wire full; //FIFO满信号 wire empty; //FIFO空信号 wire[8:0] rd_data_count; wire[8:0] wr_data_count; ///产生FIFO写入的数据always@(posedge clk or negedge rst_n)begin if(rst_n == 1'b0) write_state <= W_IDLE; else write_state <= next_write_state;endalways@(*)begin case(write_state) W_IDLE: if(empty == 1'b1) //FIFO空, 开始写FIFO next_write_state <= W_FIFO; else next_write_state <= W_IDLE; W_FIFO: if(full == 1'b1) //FIFO满 next_write_state <= W_IDLE; else next_write_state <= W_FIFO; default: next_write_state <= W_IDLE; endcaseendassign wr_en = (next_write_state == W_FIFO) ? 1'b1 : 1'b0; always@(posedge clk or negedge rst_n)begin if(rst_n == 1'b0) w_data <= 16'd0; else if (wr_en == 1'b1) w_data <= w_data + 1'b1; else w_data <= 16'd0; end///产生FIFO读的数据always@(posedge clk or negedge rst_n)begin if(rst_n == 1'b0) read_state <= R_IDLE; else read_state <= next_read_state;endalways@(*)begin case(read_state) R_IDLE: if(full == 1'b1) //FIFO满, 开始读FIFO next_read_state <= R_FIFO; else next_read_state <= R_IDLE; R_FIFO: if(empty == 1'b1)
上传时间: 2021-12-19
上传用户:20125101110
STM32F103开发板 DHT11温湿度DS18B20 气体MQ-2光敏声控雨滴传感器实验程序**--------------------------------------------------------------------------------------------------------** Created by: FiYu** Created date: 2015-12-12** Version: 1.0** Descriptions: DHT11温湿度传感器实验 **--------------------------------------------------------------------------------------------------------** Modified by: FiYu** Modified date: ** Version: ** Descriptions: ** Rechecked by: **********************************************************************************************************/#include "stm32f10x.h"#include "delay.h"#include "dht11.h"#include "usart.h"DHT11_Data_TypeDef DHT11_Data;/************************************************************************************** * 描 述 : GPIO/USART1初始化配置 * 入 参 : 无 * 返回值 : 无 **************************************************************************************/void GPIO_Configuration(void){ GPIO_InitTypeDef GPIO_InitStructure; /* Enable the GPIO_LED Clock */ RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO , ENABLE); GPIO_DeInit(GPIOB); //将外设GPIOA寄存器重设为缺省值 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_DeInit(GPIOA); //将外设GPIOA寄存器重设为缺省值 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOB , GPIO_Pin_9); //初始状态,熄灭指示灯LED1}/************************************************************************************** * 描 述 : 串口显示实时温湿度 * 入 参 : 无 * 返回值 : 无 **************************************************************************************/void DHT11_SCAN(void){ if( Read_DHT11(&DHT11_Data)==SUCCESS) { printf("\r\n读取DHT11成功!\r\n\r\n湿度为%d.%d %RH ,温度为 %d.%d℃ \r\n",\ DHT11_Data.humi_int,DHT11_Data.humi_deci,DHT11_Data.temp_int,DHT11_Data.temp_deci); //printf("\r\n 湿度:%d,温度:%d \r\n" ,DHT11_Data.humi_int,DHT11_Data.temp_int); } else { printf("Read DHT11 ERROR!\r\n"); }}/************************************************************************************** * 描 述 : MAIN函数 * 入 参 : 无 * 返回值 : 无 **************************************************************************************/int main(void){ SystemInit(); //设置系统时钟72MHZ GPIO_Configuration(); USART1_Init(); //初始化配置TIM DHT11_GPIO_Config(); // 初始化温湿度传感器PB1引脚初始时为推挽输出 GPIO_ResetBits(GPIOB , GPIO_Pin_9); delay_ms(500); while(1) { GPIO_SetBits(GPIOB , GPIO_Pin_9); DHT11_SCAN(); //实时显示温湿度 delay_ms(1500); } }
上传时间: 2022-05-03
上传用户:得之我幸78
基于STC89C51单片机的智能电热水器的控制器的设计,要达到的控制要求有:(1)用LCD1602液晶显示水温、设置上下限和定时时间,(2)水温检测显示范围为00~99℃,精度为±1℃。(3)温度预设范围为0~99℃,当检测温度低于预设温度时,开始加热;检测温度高于预设温度时,停止加热。(4)设置4个程序按键。分别问设置按键、加键、减键、确定。(5)可以红外遥控,通过红外一体接收探头接收遥控器信号,执行与主板按键同等功能。(6)有水位检测功能,无水自动上水,无水不加热。//外部中断解码程序_外部中断0void intersvr1(void) interrupt 2 using 1{ TR0=1; Tc=TH0*256+TL0;//提取中断时间间隔时长 TH0=0; TL0=0; //定时中断重新置零 if((Tc>Imin)&&(Tc<Imax)) { m=0; f=1; return; } //找到启始码 if(f==1) { if(Tc>Inum1&&Tc<Inum3) { Im[m/8]=Im[m/8]>>1|0x80; m++; } if(Tc>Inum2&&Tc<Inum1) { Im[m/8]=Im[m/8]>>1; m++; //取码 } if(m==32) { m=0; f=0; if(Im[2]==~Im[3]) { IrOK=1; TR0=0; } else IrOK=0; //取码完成后判断读码是否正确 } //准备读下一码 }}
上传时间: 2022-05-14
上传用户:
PID-小车类-手机遥控十分mimi蓝牙小车V2资料全部开源/**************************************************************************函数功能:增量PI控制器入口参数:编码器测量值,目标速度返回 值:电机PWM根据增量式离散PID公式 pwm+=Kp[e(k)-e(k-1)]+Ki*e(k)+Kd[e(k)-2e(k-1)+e(k-2)]e(k)代表本次偏差 e(k-1)代表上一次的偏差 以此类推 pwm代表增量输出在我们的速度控制闭环系统里面,只使用PI控制pwm+=Kp[e(k)-e(k-1)]+Ki*e(k)**************************************************************************/int Speed_Incremental_PI (int Encoder,int Target){ static int Bias,Pwm,Last_bias; Bias=Encoder-Target; //计算偏差 Pwm+=Speed_Kp*(Bias-Last_bias)+Speed_Ki*Bias; //增量式PI控制器if(Pwm>500)Pwm=500;else if(Pwm<-500)Pwm=-500; Last_bias=Bias; //保存上一次偏差 return Pwm; //增量输出}
上传时间: 2022-06-01
上传用户:20125101110
应广单片机 MINI-C编程指南.Mini-c总结文档一、缺点:(一)函数不能带参数.解决方法:通过A或全局变量进行参数传递;(二)不能使用for循环解决方法:用while循环代替for循环.(三)数组和指针功能弱化解决方法:① 使用rom查表② 使用ram查表(四)不支持全局变量定义同时进行初始化解决方法: 在初始化的时候记得对全局变量进行初始化. (五)Bit变量只支持写入0,或1,不支持直接bit变量取反解决方法 :if(uBitFlag){ uBitFlag=0;}Else{ uBitFlag=1; } 二、优点:(一)函数不带参数,可以节省堆栈空间(二)支持ROM查表(三).delay时间非常准确,无需手工计算(四)支持bit变量,节省ram空间,支持字,字节拆分.(五)烧录支持滚动码写入.(六)端口配置可以使用脚本(七) 代码自动生成
标签: 单片机
上传时间: 2022-06-17
上传用户:
基于单片机的密码锁设计,内含工程文件和源码文件以及电路图#include "config.h" void main(){ LCD_Initial(); //lcd初始化 init_sys(); //硬件系统初始化 setpsw(); //密码设置 while(1) { time=3; //限定尝试3次 while(1) { inputpsw(); //输入密码 checkpsw(); //密码验证 checksuper(); //验证是否是超级密码 if(error_flag==0) {right();break;} else if(time>1) error(); //允许2次尝试错误 else {lock();break;} //3次错误,系统锁定 } }}
上传时间: 2022-07-17
上传用户:默默
ADI实验室电路时经过构建和测试可以确保功能和性能的电路设计 借助ADI公司了众多应用专业技术,解决了多种常见的模拟、RF/IF和混合信号设计挑战。 所以我觉得这个还是非常值得我们学习的,特此分享一下
上传时间: 2013-06-20
上传用户:manlian
In this paper, we discuss efficient coding and design styles using verilog. This can beimmensely helpful for any digital designer initiating designs. Here, we address different problems rangingfrom RTL-Gate Level simulation mismatch to race conditions in writing behavioral models. All theseproblems are accompanied by an example to have a better idea, and these can be taken care off if thesecoding guidelines are followed. Discussion of all the techniques is beyond the scope of this paper, however,here we try to cover a few of them.
标签: Efficient Verilog Digital Coding
上传时间: 2013-11-22
上传用户:han_zh