#include <stdio.h>
int fun(int a[], int n)
{
int i,j=0
for(i=1i<ni++)
{
if(a[i]!=a[j])
{
j++
a[j]=a[i]
}
}
return j+1
}
main()
{ int a[10],i,n=10
for (i=0i<10i++)
scanf("%d",&a[i])
printf("The original data :\n")
for(i=0i<ni++)printf("%3d",a[i])
n=fun(a,n)
printf("\n\nThe data after deleted :\n")
for(i=0i<ni++)printf("%3d",a[i])printf("\n\n")
}
2,解:源程序如下:
# define M sizeof (unsigned int)*8/*函数TRANS将无符号整数N转换成D(2,8,16)进制吵蠢表示的字符串S*/
int trans( unsigned n, int d, char s[])
{
static char digits[]="0123456789ABCDEF"/*16进制数字字符*/
char buf[M+1]
int j,i=M
if (d<2||d>16)
{
s[0]='\0'/*不合理的进制,置S为空字符串*/
return 0/*不合理的进制,函数返回0*/
}
buf[i]='\0'
do
{
buf[--i]=digits[n%d]/*得出最低位,将对应字符存入对应数组中*/
n/=d
}while (n)
for (j=0(s[j]=buf[i])!='\0'j++,i++)/*将工作数组中的字符串复制到S*/
s[j]=buf[i]/*其中控制条件可写成S[J]=BUF[i]*/
return j
}
main()
{
unsigned int num=0
int scale[]=
char str[33]
int i
clrscr()
puts("please input a number to num:")
scanf("%d",&num)
printf("the number you input is %d,\n the translation results are:\n",num)
for (i=0i<sizeof(scale)/sizeof(scale[0])i++)
{
if (trans(num,scale[i],str))
printf("%5d=%s(%d)\n",num,str,scale[i])
else
printf("%5d=>(%d)error!\n",num,str,scale[i])
}
printf("\n press any key to quit\n")
getch()
}
3,解:源程序如下:
(注意:本源码中,函数GetWeekDay的返回值仅仅表示输入参数是否“正确/有效”(例如前三个参数使用2001,2,29就是无效参数),能否得到预期的星期值;真正的星期值则是通过输出变量返回给主调函数的。)
////////升局陪///////////////腊键/////////////////////
#include <time.h>
#include <stdio.h>
int GetWeekDay(int nYear,int nMon,int nDay, int *npWeekDay)
int main()
{
int nMyWeekDay
int nResult
nResult = GetWeekDay(2007,1,10,&nMyWeekDay)
return 0
}
//输入日期,返回星期的C语言函数
int GetWeekDay(int nYear,int nMon,int nDay, int *npWeekDay)
{
int nRetVal = 0
time_t tMyDateTime
struct tm tmMyDate,*tmpMyDate
if((nYear<1900||nYear>=3000)
|| (nMon<1 || nMon>12)
|| (nDay<1||nDay>31) )
{
nRetVal = 1
*npWeekDay = -1
return nRetVal
}
//memset(tmMyDate,0,sizeof(struct tm))
tmMyDate.tm_year = nYear-1900
tmMyDate.tm_mon = nMon-1
tmMyDate.tm_mday = nDay
tmMyDate.tm_hour =16
tmMyDate.tm_min =30
tmMyDate.tm_sec =30
tMyDateTime = mktime(&tmMyDate)
if(tMyDateTime<0)
{
nRetVal = 1
*npWeekDay = -1
return nRetVal
}
tmpMyDate = localtime(&tMyDateTime)
*npWeekDay = tmpMyDate->tm_wday
return nRetVal
}
#include<reg51.h>#define u16 unsigned int
#define u8 unsigned char
code u16 sj_tab[]={800,400,200,100}//每个档位对应的时间
u8 setsj=0//档档族位
u16 jsflag
#define LED P2
code u8 led_tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,}//LED亮的方式
u8 ledflag=0
/****************************************/
void init() //初始化函数
{ TMOD=0x01
TH0=(65536-1000)/256
TL0=(65536-1000)%256
EA=1
ET0=1
}
/*********按键扫描******/
sbit k=P3^2
u8 key(){
static u8 ms
if(k==0){//检测到0
if(ms<10)ms++
if(ms==5)return 0//连续5次扫描都为行饥弊0,
}
else ms=0
return 1
}
/***********************************/
u8 count=0 //计数
void main()//主函数
{
init()//系统初始化
jsflag=sj_tab[setsj]//初始加载时间
TR0=1 //定时器开始计时
while(1){
if(key()==0){ //按键按下
if(++setsj>肢悄=4)setsj=0//档位+1,加到最大后归0
}
LED=led_tab[ledflag]
}
}
/****************************/
void timer0() interrupt 1//1ms定时器
{
TH0=(65536-1000)/256//重载初值
TL0=(65536-1000)%256
if(jsflag>0)jsflag--//1ms减1
if(jsflag==0){//减到0
jsflag=sj_tab[setsj]//重载时间
if(++ledflag>=8)ledflag=0
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)