| |

VerySource

 Forgot password?
 Register
Search
Author: xiaowen08

Is this interview question difficult? C expert please come in!

[Copy link]

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-7-8 21:45:01
| Show all posts
#ifndef _COUNT_
int __cdecl sprintf_s (
        char *string,
        size_t sizeInBytes,
        const char *format,
        ...
        )
{
        va_list arglist;
        va_start(arglist, format);
        return _vsprintf_s_l(string, sizeInBytes, format, NULL, arglist);
}

int __cdecl _sprintf_s_l (
        char *string,
        size_t sizeInBytes,
        const char *format,
        _locale_t plocinfo,
        ...
        )
{
        va_list arglist;
        va_start(arglist, plocinfo);
        return _vsprintf_s_l(string, sizeInBytes, format, plocinfo, arglist);
}

int __cdecl _snprintf_s (
        char *string,
        size_t sizeInBytes,
        size_t count,
        const char *format,
        ...
        )
{
        va_list arglist;
        va_start(arglist, format);
        return _vsnprintf_s_l(string, sizeInBytes, count, format, NULL, arglist);
}

int __cdecl _snprintf_s_l (
        char *string,
        size_t sizeInBytes,
        size_t count,
        const char *format,
        _locale_t plocinfo,
        ...
        )
{
        va_list arglist;
        va_start(arglist, plocinfo);
        return _vsnprintf_s_l(string, sizeInBytes, count, format, plocinfo, arglist);
}

int __cdecl _sprintf_p (
        char *string,
        size_t count,
        const char *format,
        ...
        )
{
        va_list arglist;
        va_start(arglist, format);
        return _vsprintf_p_l(string, count, format, NULL, arglist);
}

int __cdecl _sprintf_p_l (
        char *string,
        size_t count,
        const char *format,
        _locale_t plocinfo,
        ...
        )
{
        va_list arglist;
        va_start(arglist, plocinfo);
        return _vsprintf_p_l(string, count, format, plocinfo, arglist);
}

#endif /* _COUNT_ */

/***
* _scprintf()-counts the number of character needed to print the formatted
* data
*
*Purpose:
* Counts the number of characters in the fotmatted data.
*
*Entry:
* char *format-format string to control data format/number
* of arguments followed by list of arguments, number and type
* controlled by format string
*
*Exit:
* returns number of characters needed to print formatted data.
*
*Exceptions:
*
************************************************** *****************************/


#ifndef _COUNT_
int __cdecl _scprintf (
        const char *format,
        ...
        )
{
        va_list arglist;

        va_start(arglist, format);

        return _vscprintf(format, arglist);
}

int __cdecl _scprintf_p (
        const char *format,
        ...
        )
{
        va_list arglist;

        va_start(arglist, format);

        return _vscprintf_p(format, arglist);

}

int __cdecl _scprintf_l (
        const char *format,
        _locale_t plocinfo,
        ...
        )
{
       va_list arglist;
       va_start(arglist, plocinfo);

       return _vscprintf_l(format, plocinfo, arglist);
}

int __cdecl _scprintf_p_l (
        const char *format,
        _locale_t plocinfo,
        ...
        )
{
        va_list arglist;

        va_start(arglist, plocinfo);

        return _vscprintf_p_l(format, plocinfo, arglist);

}
#endif /* _COUNT_ */
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-10 19:00:01
| Show all posts
sprintf(char* target, const char *first,...)
{
  char FormatType[Max_Lenth]; //Can be made into a linked list, which is used to store the format to parse the symbol after the% sign
  int countType = 0;

  countType = Fun_AnalyseFormat(target, FormatType); //Parse out the format in the string
  
  va_list format;
  va_start(format, first);

  char *str;
  int interger;
  ...;

  for(int i = 0; i <countType; i++)
  {
    switch(FormatType[i])
    {
      case's': str = va_arg(format,char *);
                Fun_Replace(target, substr, pos);//Replace the source string
                break;
      case'..':
               ....;
    }
  }
  va_end(format);

}
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-10 22:30:01
| Show all posts
Ha ha, think again, the above is not well written, you can start directly from a parameter. Directly.
sprintf(char* target,...)
{
  va_list format;
  va_start(target);
  va_arg(format,char *); //Directly filter out the source

  for(...)
{
   switch(...)
   {
   }
}
va_end(format);
}
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-7-11 07:30:02
| Show all posts
Have not tried it on the Intel CPU, huh, huh

From linux kernel:

linux-2.6.19.1\arch\powerpc\boot\stdio.h

#ifndef _PPC_BOOT_STDIO_H_
#define _PPC_BOOT_STDIO_H_

#include <stdarg.h>

#define ENOMEM 12 /* Out of Memory */
#define EINVAL 22 /* Invalid argument */
#define ENOSPC 28 /* No space left on device */

extern int printf(const char *fmt, ...);

#define fprintf(fmt, args...) printf(args)

extern int sprintf(char *buf, const char *fmt, ...);

extern int vsprintf(char *buf, const char *fmt, va_list args);

#endif /* _PPC_BOOT_STDIO_H_ */


linux-2.6.19.1\arch\powerpc\boot\stdio.c

/*
* Copyright (C) Paul Mackerras 1997.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*/
#include <stdarg.h>
#include <stddef.h>
#include "string.h"
#include "stdio.h"
#include "ops.h"

size_t strnlen(const char * s, size_t count)
{
const char *sc;

for (sc = s; count--&&*sc != '\0'; ++sc)
/* nothing */;
return sc-s;
}

extern unsigned int __div64_32(unsigned long long *dividend,
unsigned int divisor);

/* The unnecessary pointer compare is there
* to check for type safety (n must be 64bit)
*/
# define do_div(n,base) ({\
unsigned int __base = (base);\
unsigned int __rem;\
(void)(((typeof((n)) *)0) == ((unsigned long long *)0));\
if (((n) >> 32) == 0) {\
__rem = (unsigned int)(n)% __base;\
(n) = (unsigned int)(n) / __base;\
} else\
__rem = __div64_32(&(n), __base);\
__rem;\
})

static int skip_atoi(const char **s)
{
int i, c;

for (i = 0; '0' <= (c = **s)&&c <= '9'; ++*s)
i = i*10 + c-'0';
return i;
}

#define ZEROPAD 1 /* pad with zero */
#define SIGN 2 /* unsigned/signed long */
#define PLUS 4 /* show plus */
#define SPACE 8 /* space if plus */
#define LEFT 16 /* left justified */
#define SPECIAL 32 /* 0x */
#define LARGE 64 /* use'ABCDEF' instead of'abcdef' */

static char * number(char * str, unsigned long long num, int base, int size, int precision, int type)
{
char c,sign,tmp[66];
const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
int i;

if (type&LARGE)
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (type&LEFT)
type&= ~ZEROPAD;
if (base <2 || base> 36)
return 0;
c = (type&ZEROPAD)? '0': '';
sign = 0;
if (type&SIGN) {
if ((signed long long)num <0) {
sign ='-';
num =-(signed long long)num;
size--;
} else if (type&PLUS) {
sign ='+';
size--;
} else if (type&SPACE) {
sign = '';
size--;
}
}
if (type&SPECIAL) {
if (base == 16)
size -= 2;
else if (base == 8)
size--;
}
i = 0;
if (num == 0)
tmp[i++]='0';
else while (num != 0) {
tmp[i++] = digits[do_div(num, base)];
}
if (i> precision)
precision = i;
size -= precision;
if (!(type&(ZEROPAD+LEFT)))
while(size-->0)
*str++ = '';
if (sign)
*str++ = sign;
if (type&SPECIAL) {
if (base==8)
*str++ = '0';
else if (base==16) {
*str++ = '0';
*str++ = digits[33];
}
}
if (!(type&LEFT))
while (size--> 0)
*str++ = c;
while (i <precision--)
*str++ = '0';
while (i--> 0)
*str++ = tmp[i];
while (size--> 0)
*str++ = '';
return str;
}

int vsprintf(char *buf, const char *fmt, va_list args)
{
int len;
unsigned long long num;
int i, base;
char * str;
const char *s;

int flags; /* flags to number() */

int field_width; /* width of output field */
int precision; /* min. # of digits for integers; max
number of chars for from string */
int qualifier; /*'h','l', or'L' for integer fields */
/*'z' support added 23/7/1999 S.H. */
/*'z' changed to'Z' --davidm 1/25/99 */

The
for (str=buf; *fmt; ++fmt) {
if (*fmt !='%') {
*str++ = *fmt;
continue;
}
The
/* process flags */
flags = 0;
repeat:
++fmt; /* this also skips first'%' */
switch (*fmt) {
case'-': flags |= LEFT; goto repeat;
case'+': flags |= PLUS; goto repeat;
case '': flags |= SPACE; goto repeat;
case'#': flags |= SPECIAL; goto repeat;
case '0': flags |= ZEROPAD; goto repeat;
}
The
/* get field width */
field_width = -1;
if ('0' <= *fmt&&*fmt <= '9')
field_width = skip_atoi(&fmt);
else if (*fmt =='*') {
++fmt;
/* it's the next argument */
field_width = va_arg(args, int);
if (field_width <0) {
field_width = -field_width;
flags |= LEFT;
}
}

/* get the precision */
precision = -1;
if (*fmt =='.') {
++fmt;
if ('0' <= *fmt&&*fmt <= '9')
precision = skip_atoi(&fmt);
else if (*fmt =='*') {
++fmt;
/* it's the next argument */
precision = va_arg(args, int);
}
if (precision <0)
precision = 0;
}

/* get the conversion qualifier */
qualifier = -1;
if (*fmt =='h' || *fmt =='l' || *fmt =='L' || *fmt =='Z') {
qualifier = *fmt;
++fmt;
}

/* default base */
base = 10;

switch (*fmt) {
case'c':
if (!(flags&LEFT))
while (--field_width> 0)
*str++ = '';
*str++ = (unsigned char) va_arg(args, int);
while (--field_width> 0)
*str++ = '';
continue;

case's':
s = va_arg(args, char *);
if (!s)
s = "<NULL>";

len = strnlen(s, precision);

if (!(flags&LEFT))
while (len <field_width--)
*str++ = '';
for (i = 0; i <len; ++i)
*str++ = *s++;
while (len <field_width--)
*str++ = '';
continue;

case'p':
if (field_width == -1) {
field_width = 2*sizeof(void *);
flags |= ZEROPAD;
}
str = number(str,
(unsigned long) va_arg(args, void *), 16,
field_width, precision, flags);
continue;


case'n':
if (qualifier =='l') {
long * ip = va_arg(args, long *);
*ip = (str-buf);
} else if (qualifier =='Z') {
size_t * ip = va_arg(args, size_t *);
*ip = (str-buf);
} else {
int * ip = va_arg(args, int *);
*ip = (str-buf);
}
continue;

case'%':
*str++ ='%';
continue;

/* integer number formats-set up the flags and "break" */
case'o':
base = 8;
break;

case'X':
flags |= LARGE;
case'x':
base = 16;
break;

case'd':
case'i':
flags |= SIGN;
case'u':
break;

default:
*str++ ='%';
if (*fmt)
*str++ = *fmt;
else
--fmt;
continue;
}
if (qualifier =='l') {
num = va_arg(args, unsigned long);
if (flags&SIGN)
num = (signed long) num;
} else if (qualifier =='Z') {
num = va_arg(args, size_t);
} else if (qualifier =='h') {
num = (unsigned short) va_arg(args, int);
if (flags&SIGN)
num = (signed short) num;
} else {
num = va_arg(args, unsigned int);
if (flags&SIGN)
num = (signed int) num;
}
str = number(str, num, base, field_width, precision, flags);
}
*str = '\0';
return str-buf;
}

int sprintf(char * buf, const char *fmt, ...)
{
va_list args;
int i;

va_start(args, fmt);
i=vsprintf(buf,fmt,args);
va_end(args);
return i;
}

static char sprint_buf[1024];

int
printf(const char *fmt, ...)
{
va_list args;
int n;

va_start(args, fmt);
n = vsprintf(sprint_buf, fmt, args);
va_end(args);
console_ops.write(sprint_buf, n);
return n;
}
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-7-14 22:15:01
| Show all posts
A little bit. Uncertain parameters are not a problem, the problem is how to parse the string and format it.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-17 02:30:01
| Show all posts
Looks very complicated
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-23 20:00:01
| Show all posts
A little bit. Indefinite parameters are not a problem, the problem is how to parse the string and format it.
  
-------------------------------
There are N ways, let’s talk about the simplest one:
Use the CString class to deal with, this class has Find (pSubStr, iStart) method and Replace method.
There are also strings in stl, and there are many methods in stl.
Of course, it is very simple to write such a search and replace algorithm. There are many examples in the data structure tutorial.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list