某公司C语言笔试题
用C或者C++写一套通用函数。要求实现以下功能。
在一段连续内存(不大于10MB)中存储数据,要求FIFO。
至少有两个函数:
存入数据,将存入的数据放在缓冲区数据的尾部。第一参数为将要存入的数据首地址,第二参数为数据宽度。
取出数据,取出缓冲区最后的数据。第一参数为将要存入数据的指针,第二参数为数据宽度。
尽量提高效率。
[解决办法]
队列
queue.h
#ifndef _mytest_queue
#define _mytest_queue
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct _mytest_QueueData
{
int num; // 存储在节点中的数据长度
char stu[100];
}mytest_QueueData;
typedef struct _mytest_Node
{
mytest_QueueData *data;
struct _mytest_Node *next; // 队列中的下一个节点地址
}mytest_NODE;
typedef struct _mytest_Queue
{
mytest_NODE *head; // 队列的头部
mytest_NODE *end; // 队列的尾部
int count; // 队列长度
}mytest_QUEUE;
void mytest_Init(mytest_QUEUE *queue )
{
if( NULL == queue )
return ;
queue->head = NULL;
queue->end = NULL;
queue->count = 0;
}
void mytest_Push(mytest_QUEUE *queue, mytest_QueueData *queue_data)
{
if( NULL == queue || NULL == queue_data )
return;
mytest_NODE *new_node = (mytest_NODE *)malloc(sizeof(mytest_NODE) );
if( NULL == new_node )
return;
new_node->data=(mytest_QueueData *)malloc(sizeof(mytest_QueueData));
if( NULL == new_node->data )
return;
memcpy(new_node->data,queue_data,sizeof(mytest_QueueData));
new_node->next = NULL;
if( queue->head == NULL )
{
queue->head = new_node;
queue->end = new_node;
}
else
{
queue->end->next = new_node;
queue->end = new_node;
}
queue->count ++;
}
mytest_QueueData *mytest_Front(mytest_QUEUE *queue )
{
if( NULL == queue )
return NULL;
if( NULL == queue->head )
return NULL;
mytest_QueueData *queuedata_tmp = queue->head->data;
return queuedata_tmp;
}
void mytest_Pop( mytest_QUEUE *queue )
{
if( NULL == queue )
return;
if( NULL == queue->head )
return;
mytest_NODE *node_tmp = queue->head;
mytest_QueueData *queuedata_tmp=node_tmp->data;
queue->head = node_tmp->next;
if( NULL == queue->head )
queue->end = NULL;
queue->count --;
free(queuedata_tmp);
free(node_tmp);
queuedata_tmp=NULL;
node_tmp=NULL;
}
void mytest_FreeQueue(mytest_QUEUE *queue )
{
if( queue )
return;
mytest_NODE *tmp_node1 = queue->head;
while(tmp_node1)
{
mytest_NODE *tmp_node2 = tmp_node1;
free(tmp_node1->data);
free(tmp_node1);
tmp_node1->data = NULL;
tmp_node1 = NULL;
tmp_node1 = tmp_node2->next;
}
}
#endif
test.c
#include <stdio.h>
#include "include/queue.h"
mytest_QUEUE qe;
void test()
{
mytest_QueueData qd;
int i;
for(i=0;i<1000000;i++)
{
sprintf(qd.stu,"%d:fffzcvz",i);
qd.num=i;
mytest_Push(&qe,&qd);
}
while(qe.count>0)
{
printf("%s\n",mytest_Front(&qe)->stu);
mytest_Pop(&qe);
}
printf("end1 \n");
}
int main()
{
mytest_Init(&qe);
int n=0;
while(n++<1000)
test();
printf("fff");
return 0;
}
[解决办法]
题目要求是在连续内存上操作,用链表不能满足要求吧。
[解决办法]
so easy.
记录一个尾指针,复制出去复制进来都用memcpy,整体代码不会超过50行的。
[解决办法]
[解决办法]
很明显是个字节流环形队列, 需要注意队列满的判定方法: 剩余内存小于 < 阈值, 则认为队列满.
[解决办法]
要求FIFO 是不是写错了.按照后面的加入,取出数据完全是栈。
[解决办法]
至少有两个函数:
存入数据,将存入的数据放在缓冲区数据的尾部。
取出数据,取出缓冲区最后的数据.
[解决办法]
//循环向a函数每次发送200个字节长度(这个是固定的)的buffer,//a函数中需要将循环传进来的buffer,组成240字节(也是固定的)的新buffer进行处理,//在处理的时候每次从新buffer中取两个字节打印#include <stdio.h>#include <stdlib.h>#include <string.h>#include <windows.h>#include <process.h>#include <io.h>//Log{#define MAXLOGSIZE 10000000#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))#include <time.h>#include <sys/timeb.h>#include <stdarg.h>char logfilename1[]="MyLog1.log";char logfilename2[]="MyLog2.log";char logstr[16000];char datestr[16];char timestr[16];char mss[4];CRITICAL_SECTION cs_log;FILE *flog;void Lock(CRITICAL_SECTION *l) { EnterCriticalSection(l);}void Unlock(CRITICAL_SECTION *l) { LeaveCriticalSection(l);}void LogV(const char *pszFmt,va_list argp) { struct tm *now; struct timeb tb; if (NULL==pszFmt||0==pszFmt[0]) return; if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0; ftime(&tb); now=localtime(&tb.time); sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday); sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec ); sprintf(mss,"%03d",tb.millitm); printf("%s %s.%s %s",datestr,timestr,mss,logstr); flog=fopen(logfilename1,"a"); if (NULL!=flog) { fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr); if (ftell(flog)>MAXLOGSIZE) { fclose(flog); if (rename(logfilename1,logfilename2)) { remove(logfilename2); rename(logfilename1,logfilename2); } flog=fopen(logfilename1,"a"); if (NULL==flog) return; } fclose(flog); }}void Log(const char *pszFmt,...) { va_list argp; Lock(&cs_log); va_start(argp,pszFmt); LogV(pszFmt,argp); va_end(argp); Unlock(&cs_log);}//Log}#define ASIZE 200#define BSIZE 240#define CSIZE 2char Abuf[ASIZE];char Cbuf[CSIZE];CRITICAL_SECTION cs_HEX ;CRITICAL_SECTION cs_BBB ;struct FIFO_BUFFER { int head; int tail; int size; char data[BSIZE];} BBB;int No_Loop=0;void HexDump(int cn,char *buf,int len) { int i,j,k; char binstr[80]; Lock(&cs_HEX); for (i=0;i<len;i++) { if (0==(i%16)) { sprintf(binstr,"%03d %04x -",cn,i); sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]); } else if (15==(i%16)) { sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]); sprintf(binstr,"%s ",binstr); for (j=i-15;j<=i;j++) { sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.'); } Log("%s\n",binstr); } else { sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]); } } if (0!=(i%16)) { k=16-(i%16); for (j=0;j<k;j++) { sprintf(binstr,"%s ",binstr); } sprintf(binstr,"%s ",binstr); k=16-k; for (j=i-k;j<i;j++) { sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.'); } Log("%s\n",binstr); } Unlock(&cs_HEX);}int GetFromRBuf(int cn,CRITICAL_SECTION *cs,FIFO_BUFFER *fbuf,char *buf,int len) { int lent,len1,len2; lent=0; Lock(cs); if (fbuf->size>=len) { lent=len; if (fbuf->head+lent>BSIZE) { len1=BSIZE-fbuf->head; memcpy(buf ,fbuf->data+fbuf->head,len1); len2=lent-len1; memcpy(buf+len1,fbuf->data ,len2); fbuf->head=len2; } else { memcpy(buf ,fbuf->data+fbuf->head,lent); fbuf->head+=lent; } fbuf->size-=lent; } Unlock(cs); return lent;}void thdB(void *pcn) { char *recv_buf; int recv_nbytes; int cn; int wc; int pb; cn=(int)pcn; Log("%03d thdB thread begin...\n",cn); while (1) { Sleep(10); recv_buf=(char *)Cbuf; recv_nbytes=CSIZE; wc=0; while (1) { pb=GetFromRBuf(cn,&cs_BBB,&BBB,recv_buf,recv_nbytes); if (pb) { Log("%03d recv %d bytes\n",cn,pb); HexDump(cn,recv_buf,pb); Sleep(1); } else { Sleep(1000); } if (No_Loop) break;// wc++; if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc); } if (No_Loop) break;// }}int PutToRBuf(int cn,CRITICAL_SECTION *cs,FIFO_BUFFER *fbuf,char *buf,int len) { int lent,len1,len2; Lock(cs); lent=len; if (fbuf->size+lent>BSIZE) { lent=BSIZE-fbuf->size; } if (fbuf->tail+lent>BSIZE) { len1=BSIZE-fbuf->tail; memcpy(fbuf->data+fbuf->tail,buf ,len1); len2=lent-len1; memcpy(fbuf->data ,buf+len1,len2); fbuf->tail=len2; } else { memcpy(fbuf->data+fbuf->tail,buf ,lent); fbuf->tail+=lent; } fbuf->size+=lent; Unlock(cs); return lent;}void thdA(void *pcn) { char *send_buf; int send_nbytes; int cn; int wc; int a; int pa; cn=(int)pcn; Log("%03d thdA thread begin...\n",cn); a=0; while (1) { Sleep(100); memset(Abuf,a,ASIZE); a=(a+1)%256; if (16==a) {No_Loop=1;break;}//去掉这句可以让程序一直循环直到按Ctrl+C或Ctrl+Break或当前目录下存在文件No_Loop send_buf=(char *)Abuf; send_nbytes=ASIZE; Log("%03d sending %d bytes\n",cn,send_nbytes); HexDump(cn,send_buf,send_nbytes); wc=0; while (1) { pa=PutToRBuf(cn,&cs_BBB,&BBB,send_buf,send_nbytes); Log("%03d sent %d bytes\n",cn,pa); HexDump(cn,send_buf,pa); send_buf+=pa; send_nbytes-=pa; if (send_nbytes<=0) break;// Sleep(1000); if (No_Loop) break;// wc++; if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc); } if (No_Loop) break;// }}int main() { InitializeCriticalSection(&cs_log ); Log("Start===========================================================\n"); InitializeCriticalSection(&cs_HEX ); InitializeCriticalSection(&cs_BBB ); BBB.head=0; BBB.tail=0; BBB.size=0; _beginthread((void(__cdecl *)(void *))thdA,0,(void *)1); _beginthread((void(__cdecl *)(void *))thdB,0,(void *)2); if (!access("No_Loop",0)) { remove("No_Loop"); if (!access("No_Loop",0)) { No_Loop=1; } } while (1) { Sleep(1000); if (No_Loop) break;// if (!access("No_Loop",0)) { No_Loop=1; } } Sleep(3000); DeleteCriticalSection(&cs_BBB ); DeleteCriticalSection(&cs_HEX ); Log("End=============================================================\n"); DeleteCriticalSection(&cs_log ); return 0;}
[解决办法]