C语言下各种小开发的实用小tips

本文介绍了C语言中获取当前时间的方法,包括C和C++的实现,以及如何对Excel文件进行简单的读写操作。此外,还详细讲解了CRC16校验的实现过程,并提供了计算两点间距离的函数,以及字符串分割和查找的函数示例。
摘要由CSDN通过智能技术生成

C语言实用小tips


一、获取时间

时间存储的结构体是由以下组成

struct tm {
  int tm_sec;   // 秒
  int tm_min;   // 分
  int tm_hour;  // 小时
  int tm_mday;  // 日
  int tm_mon;   // 月 
  int tm_year;  // 年     自 1900 年起的年数
  int tm_wday;  // 一周中的第几天 
  int tm_yday;  // 一年中的第几天 
  int tm_isdst; // 夏令时
};

c语言

#include<time.h>
int main()
{
    time_t timep;
    struct tm *p;
    time (&timep);
    p=gmtime(&timep);
    printf("/*秒*/                          %d\n",p->tm_sec);
    printf("/*分*/                          %d\n",p->tm_min); 
    printf("/*时*/                          %d\n",8+p->tm_hour);
    printf("/*日*/                          %d\n",p->tm_mday);
    printf("/*月*/                          %d\n",1+p->tm_mon);
    printf("/*年*/                          %d\n",1900+p->tm_year);
    printf("/*统计日*/                      %d\n",p->tm_yday); 
    printf("/*统计周*/                      %d\n",p->tm_wday);  
}

C++部分

#include <iostream>
#include <ctime> 
using namespace std; 
int main( )
{
   // 基于当前系统的当前日期/时间
   time_t now = time(0); 
   // 把 now 转换为字符串形式
   char* dt = ctime(&now); 
   cout << "本地日期和时间:" << dt << endl; 
   // 把 now 转换为 tm 结构
   tm *gmtm = gmtime(&now);
   dt = asctime(gmtm);
   cout << "UTC 日期和时间:"<< dt << endl;
}
 time_t time(time_t *time);
 该函数返回系统的当前日历时间,自 197011 日以来经过的秒数。如果系统没有时间,则返回 -1char *ctime(const time_t *time);
该返回一个表示当地时间的字符串指针,字符串形式 day month year hours:minutes:seconds year\n\0struct tm *localtime(const time_t *time);
该函数返回一个指向表示本地时间的 tm 结构的指针。
 clock_t clock(void);
该函数返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。如果时间不可用,则返回 -1char * asctime ( const struct tm * time );
该函数返回一个指向字符串的指针,字符串包含了 time 所指向结构中存储的信息,返回形式为:day month date hours:minutes:seconds year\n\0struct tm *gmtime(const time_t *time);
该函数返回一个指向 time 的指针,time 为 tm 结构,用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。
 time_t mktime(struct tm *time);
该函数返回日历时间,相当于 time 所指向结构中存储的时间。
 double difftime ( time_t time2, time_t time1 );
该函数返回 time1 和 time2 之间相差的秒数。
 size_t strftime();
该函数可用于格式化日期和时间为指定的格式。

二、对excel操作

##打开

void main()
{
    FILE *fp = NULL ;
    fp = fopen("D:\\work\\111\\aa.xls","w") ; 
    fclose(fp);
}

void writeExcel_int(FILE *fp_row,int tab_num,int enter_num,int data)
{

    FILE * fp=fp_row;

    fseek(fp, 0, SEEK_SET	);   /*fp指针从当前位置向后移动*/
    for(int i=0;i<enter_num;i++)
    {
        fprintf(fp,"\n");
    }
    for(int i=0;i<tab_num;i++)
    {
        fprintf(fp,"\t");
    }

    fprintf(fp,"%d",data);

}


void writeExcel_float(FILE *fp_row,int tab_num,int enter_num,float data)
{
    FILE *fp=fp_row;

    fseek(fp, 0, SEEK_SET	);   /*fp指针从当前位置向后移动*/
    for(int i=0;i<enter_num;i++)
    {
        fprintf(fp,"\n");
    }
    for(int i=0;i<tab_num;i++)
    {
        fprintf(fp,"\t");
    }

    fprintf(fp,"%f",data);

}

三、CRC 16校验


const unsigned char auchCRCHi[] = /* CRC锟斤拷位锟街节憋拷*/
{
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
	0x00, 0xC1, 0x81, 0x40,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68, 0x78,
	0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4,
	0x74, 0x75, 0xB5, 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50,
	0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C,
	0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,
	0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 0x44,
	0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40
} ;

/******************************************************************************
												    CRC校验
*******************************************************************************/
unsigned int CRC_Calculate(unsigned char *pdata,unsigned char num)
{
  unsigned char uchCRCHi = 0xFF ;
	unsigned char  uchCRCLo = 0xFF ;
	unsigned char uIndex ;
	while(num --)
	{
		uIndex = uchCRCHi^*pdata++ ;
		uchCRCHi = uchCRCLo^auchCRCHi[uIndex];
		uchCRCLo = auchCRCLo[uIndex];
	}
	return (uchCRCHi << 8 | uchCRCLo) ;
}

验证
int main(){
    char buff[20]={0x01,0x10,0x00,0x28,0x00,0x01,0x02,0x00,0x03 };
    unsigned int crc;
    crc=CRC_Calculate(buff,9);
	printf("low %x hight %x  ",crc/256,crc%256);
}


四、判断两点距离

int distance_cacu(int x1,int y1,int x2,int y2 )
{
    return sqrt(pow((y2-y1),2)+pow((x2-x1),2));
}

五、字符串分割

void test01()
{    //1.使用strtok()实现分割
	char str[] = "hello,world hello";
	char* str1 = strtok(str, " ,");
	printf("%s\n", str1);
	while (str1 != NULL)
	{
         str1 = strtok(NULL, " ,");
		 printf("%s\n", str1);
	}
}

六、字符串查找

1、字符查找

strchr() 用于查找字符串中的一个字符,并返回该字符在字符串中第一次出现的位置。

char *strchr(const char *str, int c);
【参数】str 为要查找的字符串,c 为要查找的字符。
【返回】其返回值是地址的位置,其位置可以是return - str,没有则NULL

strrchr() 用于查找字符串中的一个字符,并返回该字符在字符串中最后一次出现的位置。

char *strrchr(const char *str, int c);
【参数】str 为要查找的字符串,c 为要查找的字符。
【返回】其返回值是地址的位置,其位置可以是return - str,没有则NULL

strstr () 用于查找字符串中的一个字符串,并返回该字符在字符串中第一次出现的位置。

char * strstr ( const char * str1, const char * str1);
【参数】str1为要查找的字符串,str1为要查找的字符。
【返回】其返回值是地址的位置,其位置是当前起始位置,没有则NULL

strpbrk() 用途跟strstr 一样

	char *strpbrk(const char *str1, const char *str2)

index() 用途跟strchr一样
rindex() 用途跟strrchr一样

	char *index(const char *s, int c);
	char *rindex(const char *s, int c);

demo代码

#include <stdio.h>
#include <string.h>

int main ()
{
   const char str[] = "www.tonghidenddddddkcm.com";
   const char ch = '.';
   char *ret;

   ret = strchr(str, ch);

   printf("now location ptr in |%ld| start  location ptr in |%ld|  sub  location ptr in   |%ld|\n", ret, str,ret-str);

   printf("reconize ptr char is |%c|  now location ptr char |%s|\n", ch, ret);

   printf("\n----------------11111111111111---------------------\n");

   ret = strrchr(str, ch);

   printf("now location ptr in |%ld| start  location ptr in |%ld|  sub  location ptr in   |%ld|\n", ret, str,ret-str);

   printf(" now location ptr char |%s|\n",  ret);


   printf("\n----------------22222222222222---------------------\n");


   char *retccc = strstr(str, "ddd");

   if (*retccc != NULL)
      printf(" now location ptr char |%s|\n",  retccc);
   else
      printf("没找到!");

   printf("\n----------------3333333333333333---------------------\n");


   retccc = index(str, 'd');

   if (*retccc != NULL)
      printf(" now location ptr char |%s|\n",  retccc);
   else
      printf("没找到!");
   return(0);
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值