Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

[C/C++] °ÑÒ»¸öÕûÐÍÕûÊýת³É×Ö·û´®

ÎÊÌâÃèÊö£ºÐ´Ò»¸öº¯Êý£¬½ÓÊÜÒ»¸öÕûÊý£¨¼Ù¶¨ÓÃint±íʾ£©£¬Êä³ö¸ÃÕûÊýµÄ×Ö·û´®ÐÎʽ¡£²ÉÓÃÈçϺ¯ÊýÔ­ÐÍ£º
void itoa ( int val, buf* );
ÕâÀï¼Ù¶¨buf×ã¹»´óÒÔÈÝÄÉÈκÎintÀàÐÍÊýÖµµÄ×Ö·û´®¡£
Õâ¸öÎÊÌ⣬³õ¿´ºÃÏñûÓÐʲôÄѶȡ£Èç¹ûÊÇ0£¬¾ÍÖ±½ÓÊä³ö'0'£»Èç¹ûÊÇÕýÊý£¬¾Íͨ¹ýÈ¡Ä£(%)ºÍÇóÉÌ(/)ÔËËã¿ÉÒÔÖðλȡµÃ¸ÃÕûÊýµÄÊý×Ö£¬²¢°ÑËüÃǷŵ½bufÖÐÈ¥£»Èç¹ûÊǸö¸ºÊý£¬¾ÍÏÈת³ÉÕýµÄ£¬È»ºóÔÚ×îºóÊä³öʱ¼ÓÈ븺ºÅ¡£°´ÕÕÕâ¸ö˼·£¬¿ÉÒÔдÏÂÈçÏ´úÂ룺
void itoa_flawed(int val, char* buf)
{
// deal with special case
if(val == 0)
{
buf[0] = '0';
buf[1] = '\0';
return;
}
bool negative = (val < 0);
int abs_val = negative ? -val : val;
unsigned int char_cnt = 0;
while(abs_val > 0)
{
buf[char_cnt] = abs_val % 10 + '0';
char_cnt ++;
abs_val = abs_val / 10;
}
if(negative) buf[char_cnt++] = '-';
//reverse the string
unsigned int i = 0;
while(i < char_cnt / 2)
{
char temp = buf[i];
buf[i] = buf[char_cnt-i-1];
buf[char_cnt-i-1] = temp;
i++;
}
buf[char_cnt] = '\0';
}
 
Ò»Çп´ÆðÀ´¶¼Ã»ÓÐÎÊÌâ¡£µ«ÊÇ£¬Èç¹ûÎÒÃÇÕâÑùµ÷Óøú¯ÊýµÄ»°£¬²Â²Â»áÊä³öʲô£º
char buf[40];
itoa_flawed(1 << 31,buf);
cout<<buf;

ÎÒÃǵõ½µÄÊÇ"-"¡£ÕâÊÇΪʲôÄØ£¿´ó¼ÒÇë×¢ÒâµÚ11ÐдúÂë¡£µ±ÎÒÃÇÇóÒ»¸ºÊýµÄ¾ø¶Ôֵʱ£¬Èç¹û»¹ÊÇÓÃÔ­À´´óСµÄÓзûºÅÀàÐÍʱ£¬¿ÉÄÜ»á²úÉúÒç³ö£¡ÎÒÃÇÖªµÀ£¬intÀàÐÍÖ»Äܱíʾ-231
~ 231
-1
Ö®¼äµÄÊýÖµ¡£Èç¹ûÎÒÃǶÔ-231
Çó¾ø¶ÔÖµ²¢°ÑËü´æ·Åµ½intÀàÐ͵ıäÁ¿ÖУ¬»á·¢ÉúʲôÇé¿ö£¿·ÖÎöµ½ÕâÀ´ó¼Ò¶¼Ó¦¸ÃÇå³þÁË°É£¡
Õâ¸öbugÊǺÜÒþ±ÎµÄ¡£ÎÒÔÚÍøÉÏ¿´µ½µÄ´úÂë»ù±¾É϶¼ºÍÉÏÃæ¸ø³öµÄ²î²»¶à£¬Ã»Óп¼ÂÇÕâ¸ö¿ÉÄܵÄÒç³öÎÊÌâ¡£ÐÞÕýµÄ°ì·¨£¬¾ÍÊÇÓøü´óµÄÀàÐÍÀ´´æ·Å¾ø¶ÔÖµ¡£¹Û²ìµ½¾ø¶ÔÖµÊǷǸºµÄ£¬ÎÒÃÇ¿ÉÒÔÓÃÒ»¸öunsigned int
À´±íʾ¾ø¶ÔÖµ£¬ÕâÑù¾Í²»»áÓÐÎÊÌâÁË¡£
Õâ¸öÌâÄ¿¸øÎÒµÄÆôʾÊÇ£¬ËäÈ»¿´ÆðÀ´¼òµ¥µÄÌâÄ¿£¬Ò²¿ÉÄÜÓкܴóµÄÏÝÚå¡£Èç¹ûÕâÊÇÒ»µÀÃæÊÔÌâµÄ»°£¬¹À¼ÆÍêÈ«×ö¶ÔµÄ²»¶à£¬¿ÉÄÜÒ²°üÀ¨ÏÖ³¡µÄÎÒÔÚÄÚ¡£


Ïà¹ØÎĵµ£º

C/C++ std::string ÇдÊ

ÓÃÁËÈýÖÖ·½·¨...
#if 0
void StringTokenize(const std::string& strSrc, const std::string& strDelimit, std::vector<std::string>& vecSub)
{
if (strSrc.empty() || strDelimit.empty())
{
throw "tokenize: empty string\n";
......

C/C++º¯ÊýµÄ½è¿Ú¶¨Òå

1¡¢ÔÚCÎļþÖе÷ÓÃC++ÎļþÖж¨ÒåµÄÎļþ
Ö±½ÓµÄ·½·¨ÊÇÔÚC++ ÎļþµÄÍ·²¿Ìí¼ÓÈçÏ´úÂë¶Î£º
extern "C"
{
      int API(int A);
}
2¡¢C++½Ó¿ÚµÄ·½·¨
ÔÚC++Öе÷ÓÃCµÄº¯Êý£¬ÔÚCÍ·ÎļþÖмÓÈëÈçÏ´úÂ룺
#ifdef __cplusplus          // ¿ªÊ¼
exte ......

C¡£¡£¡£¡£Ô½À´Ô½ºÍгÁË°¡¡£¡£= =

ÊäÈëÒ»¸öÓ¢ÎÄÓï¾ä£¨²»¼Ç±êµã·ûºÅ£¬µ¥´ÊÖ®¼äÖ»ÓÐÒ»¸ö¿Õ¸ñ£©£¬ÔÙÄæÏòÊä³ö
ÀýÈ磺I am game boy
         boy game am I
#include<stdio.h>
#include<string.h>
int f(char,char,int,int);
int main()
{
char a[80]={0},b[80]={0},c[80]={0};
int i,n,m=0;
......

memcachde Óë c/c++ Ó¦ÓÃ

ÐÞ¸Ämakefile£¬ÔÚLIBSÀïÃæ¼ÓÉÏ-lmemcached£¬±ÈÈçÔ­À´ gcc test.c£¬ÏÖÔÚ gcc test.c -lmemcached¡£Õâ¸ö¿â¾ÍÊÇlibmemcachedÌṩµÄ¡£
È»ºóÌí¼Ó#include<libmemcached/memcached.h>£¬Õâ¸öÎļþÒ²ÊÇlibmemcachedÌṩµÄ¡£
Ö÷º¯ÊýÀïÃæÐèÒªÌí¼Ó£º
    memcached_st *memc;
    uint32_t&nbs ......

linuxÏÂcÓïÑÔ±à³Ì vi ¼ò½é

 /*±¾ÎÄÑ¡×Ô²©´´linuxʵÑéÖ¸µ¼Êé
*Áô×Å×Ô¼ºÑ§Ï°Ö®ÓÃ
*ÔÚÕâÀï±£´æ×Å£¬·ÖÏíÒ»ÏÂ*/
VI ¼ò½é
    vi ÊÇ Linux/Unix ÊÀ½çÀKΪÆÕ±éµÄ¿ÉÊÓ»¯µÄÈ«ÆÁÄ»Îı¾±à¼­Æ÷(visual edit),¼¸ºõ¿É
ÒÔ˵ÈκÎһ̨ Linux/Unix »úÆ÷¶¼»áÌṩÕâ¸öÈí¼þ¡£
    vi ÓÐÈýÖÖ״̬,¼´±à¼­·½Ê½¡¢²åÈ뷽ʽºÍÃüÁî·½Ê ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ