博文
将整数转化为一个字符串(2007-12-16 18:13:00)
摘要:#include<stdio.h>
#define LENGTH 10
void IntToChar(int *p,int k,int n)
{
if(k!=0)
{
*(p+k--)=n%10+48;/*在ascii中的码的顺序,相差的值为48*/
IntToChar(p,k,n/10);
}
else
{
*(p+k)=n+48;
return;
} /*如果n是一位数,则*(p+0),即a[0]等于此数,对于多位数,已除到数的最高位*/
}
void main()
{
int a[LENGTH],i,j=0,n,m; /*j用于判断输入数n是几位数*/
printf("Please input the integer wanted to change:\n");
scanf("%d",&n);
m=n; /*m仅为方便下步while循环中判断n为几位数的参数*/
while(m/10!=0) /*while循环中判断n为几位数*/
{ m=m/10;
j++;
}
IntToChar(a,j,n); /*a[LENGTH]为存储转化后字符的数组*/
printf("The changed char is:\n");
for(i=0;i<=j;i++)
printf("%c",a[i]);
}
/*说明:*/
/*由于各种c编译器对int型数据的取值范围不一样,可能会有不同的结果*/
/*Turbo C 中int的范围为不能超过32767*/ ......
关于冒泡法(2007-11-27 11:11:00)
摘要:看了文涛的blog,感触很深,觉得程序的有些细节问题可以自己通过编程来了解,也更细致地了解它的执行过程,同时也当作是一种测试,下面是一个排序的测试程序
#include <stdio.h>
void main()
{
int a[11]; /* 第0号元素不用 */
int i, j,k, cup;
printf("Input 10 numbers:\n");
for(i = 1; i < 11; i++)
scanf("%d", &a[i]);
for(i = 1; i <= 9; i++) /* 趟数 */
{
printf("i=%d\n\n",i);
for(j = 1; j <= 10 - i; j++) /* 每趟要比较数 */
{ if(a[j] > a[j+1]) /* 前面数大于后面数刚对调 */
{
cup = a[j];
 ......
vhdl实现的多循环方式彩灯(2007-11-20 15:29:00)
摘要:------the top file of cyc_led design
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity cyc_led is
port(contr : in std_logic_vector(1 downto 0);
clk : in std_logic;
light : out std_logic_vector(7 downto 0));
end entity cyc_led;
architecture cyc_light of cyc_led is
component mode1 is---mode1
port(clk1 : in std_logic;
light1 : out std_logic_vector(7 downto 0));
end component;
component mode2 is---mode2
port(clk2 : in std_logic;
light2 : out std_logic_vector(7 downto 0));
end component;
component mode3 is ---mode3
port(clk3 : in std_logic;
light3 : out std_logic_vector(7 downto 0));
end component;
component mode4 is ---mode4
port(clk4 : in std_logic;
......
VHDL的任意整数且占空比为50%分频代码(2007-11-19 17:35:00)
摘要:说明如下:
1.其中top file 为 division,其中的clk_com是比较的频率,用它来和分频后波形进行比较,便于观察,
2.any_enve为任意偶数分频文件
3.any_odd为任意奇数分频文件
4.是一个用于2进制与8进制的译码器,我用它来显示在数码管上当前到底是多少分频
5.以下代码在开发板上实验过,请大家放心使用,欢迎转载,但请注明出处,另外说明由于用的是quartus7.1编辑的,中间无法加中文注释,请大家慢慢读了;以下是代码:
------the top file of the design division
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity division is
port (input : in std_logic_vector(7 downto 0);
clk : in std_logic;
clk_out : out std_logic;
clk_com : out std_logic;
led1: out std_logic_vector(6 downto 0);
led2: out std_logic_vector(6 downto 0);
led3: out std_logic_vector(6 downto 0));
end entity division;
--------------------------------------------------
architecture freq of division is
component decoder is----decoder
port(bin : in std_logic_vector(2 downto 0);
de : out st......
数码管动态显示(2007-11-07 12:45:00)
摘要:#include <reg51.h>
typedef unsigned char byte;// 0-255
typedef unsigned char word;//0-65535
static byte arry_display[5];
byte table[10]={0x42,0xee,0x58,0x68,0xe4,0x61,0x41,0xea,0x40,0x60};//0-9
//显示函数
void display(void)
{
byte position=0xfe;
byte i,j,temp;
for(i=0;i<4;i++)//4数码管轮留导通
{
temp=arry_display[i];
temp=table[temp];
for(j=0;j<200;j++)//延时
{
P2=position;P0=temp;
}
position<<=1;
position|=0x01;//以保证循环点亮
}
//position=0xfe;
}
///将10进制转化为BCD
int cov_bcd(unsigned int n)
{
arry_display[0]=n/1000;//千位
arry_display[1]=(n/100)%10;//百位
arry_display[2]=(n/10)%10;//十位
arry_display[3]=n%10; //个位
}
void main(void)
{
unsigned int a;
a=0x00;
while(1)
{
for(;a<9999;a++)
{
cov_bcd(a);
disp......
vhdl实现的8位可逆计数器(2007-11-01 19:55:00)
摘要:能实现连续脉冲还手动脉冲计数,在实验板上实验过~!
程序很简单,相应的注释就没加了,
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity counter is
port(rst,clk,clk1: in std_logic;
con : std_logic_vector(1 downto 0);
output : out std_logic_vector(7 downto 0));
end entity counter;
architecture behave of counter is
begin
process(rst,con,clk,clk1)
variable temp1 : std_logic_vector(7 downto 0);
begin
if rst = '1' then temp1 := (others => '0');
else
case con is
when "10" =>--手动顺记数
if clk'event and (clk='1') and (clk'last_value='0') then
if temp1 < 255 then temp1 := temp1 + 1;
else temp1 := (others => '0');
......
猴子吃桃问题代码(2007-10-26 20:32:00)
摘要:#include <stdio.h>
int remain(int i)
{
int j;
if(i==10)
j=1;
else if(i<10&&i>0)
j=(2*remain(i+1))+2;
else if (i<=0)
j=remain(1);
else if(i>10)
j=0;
return(j);
}
void main()
{
int a,b;
printf("please input the day you want to kown how many peach remained:\n");
scanf("%d",&a);
if(a>10)printf("The peach is eaten out.\n");
else if(a<=0)printf("The peach is not eatean,so have %d peach.\n",remain(1));
else
{
for(b=a;b>=1;b--)
printf("The %d day reamins %d peach.\n",b,remain(b));
}
}......
第一个实验-8路开关控制8个灯(2007-10-26 20:22:00)
摘要:library ieee;
use ieee.std_logic_1164.all;
entity switch_led is
port(key : in std_logic_vector(7 downto 0);
light : out std_logic_vector(7 downto 0));
end entity switch_led;
architecture behav of switch_led is
begin
process(key)
begin
for i in 7 downto 0 loop
light(i) <= key(i);--the led is lighting when the votage is high;
end loop;
end process;
end architecture behav;......
38译码器的几种描述方法(2007-10-26 20:21:00)
摘要:library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity decoder38 is
port (Q0: out std_logic_vector(7 downto 0);
Q1: in std_logic_vector(2 downto 0);
en: in std_logic);
end entity decoder38;
-----the num.1 method to descript
architecture code1 of decoder38 is
begin
process(Q1,en)
variable temp : std_logic_vector(7 downto 0);
begin
if en = '1' then temp := "ZZZZZZZZ";---"en" is effective on low voltage;
else
case Q1 is
when "000" => temp := "00000001";
when "001" => temp := "00000010";
when "010" => temp := "00000100";
when "011" => temp := "00001000";
when "100" => temp := "00010000";
when "101" =>......
魔方阵的c语言算法(2007-09-26 16:41:00)
摘要:
魔方阵的c语言算法:
#include "stdio.h"
#define Q 100
int a[Q][Q];
FILE *fp;
void swap(int *a,int *b)/*交换函数*/
{
int t;
t=*a;
*a=*b;
*b=t;
}
void Magic2Kplus1(int t,int H,int L,int N)
{
/*
阶数N是奇数魔方阵
其中int t,表示:从t开始向魔方阵中填数
int H,int L分别表示从从魔方阵中的哪个位置开始填数
int N 表示魔方阵的阶数
下面举例子说明以3阶魔方阵为例
第1步: 中间填 1
x 1 x
x x x
x x x
第2步:数2应向1的右上角填但是没地方了就移到右下角
x 1 x
x x x
x x 2
第3步:数3应向2的右上角填但是没地方了就移到2上一行的最左边
x 1 x
3 x x
x x 2
第4步:数4应向3的右上角填但是这个地方已经有数字1了就移到3下边
x 1 x
3 x x
4 x 2
第5步:数5向4的右上角填
x 1 x
3 5 x
......