LINUXSOFT.cz
Nazwa użytkownika: Hasło:     
    CZ UK PL

> Komentarze :: Poradna Sprintf

Sprintf 10.6.2007 15:31
Tomáš Eichler

Dobrý den,chtěl jsem se zeptat,jestli byste mi neporadili s jednim problémem nad kterým si lámu hlavu už týden.Mam zadani teto ulohy:

Napište v jazyce C implementaci zjednodušené funkce sprintf dle následujícího manuálu:

Deklarace a popis chování
int Sprintf(char *str, const char *format, ...);

DESCRIPTION
The function produces output according to a format as described below and writes it to the character string str.

The function writes the output under the control of a format string that specifies how subsequent arguments are converted for output.

Return value
Upon successful return, the function return the number of characters printed (not including the trailing ’\0’ used to end output to strings). If an output error is encountered, a negative value is returned.

Format of the format string
The format string is a character string, beginning and ending in its initial shift state, if any. The format string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments. Each conversion specification is introduced by the character %, and ends with a conversion specifier. In between there may be an optional length modifier.

The arguments must correspond properly (after type promotion) with the conversion specifier. By default, the arguments are used in the order given, where each conversion specifier asks for the next argument (and it is an error if insufficiently many arguments are given).

The length modifier
Here, ‘integer conversion’ stands for d, i, o, u, x, or X conversion.

hh

A following integer conversion corresponds to a signed char or unsigned char argument.

h

A following integer conversion corresponds to a short int or unsigned short int argument.

l

(ell) A following integer conversion corresponds to a long int or unsigned long int argument.

The conversion specifier
A character that specifies the type of conversion to be applied. The conversion specifiers and their meanings are:

d,i

The int argument is converted to signed decimal notation.

o,u,x,X

The unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal (x and X) notation. The letters abcdef are used for x conversions; the letters ABCDEF are used for X conversions.

c

The int argument is converted to an unsigned char, and the resulting character is written.

s

The const char* argument is expected to be a pointer to an array of character type (pointer to a string). Characters from the array are written up to (but not including) a terminating NUL character.

p

The void * pointer argument is printed in hexadecimal (as if by 0x%x or 0x%lx).

%

A ‘%’ is written. No argument is converted. The complete conversion specification is ‘%%’.

Zdrojovy kod mám takovýhle:

[code]

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

enum
{ eNone = 0x00, efShort = 0x02, efLong = 0x04,
efChar = 0x08, eInt = 0x10, eString = 0x20,
efUnsDec = 0x40, eChar = 0x80, eOctal = 0x200,
eHexa = 0x400, eBigHexa = 0x800, ePointer = 0x1000
};
int Sprintf(char *str, const char *format, ...)
{

va_list l;
char * pch = (char*)format, *p1, *p2;
char* start = str;
char temp[128];
long num;
unsigned long n;
int i, t, s, len = 0;

if(str == 0) return -1;
va_start(l, format);
for (;*pch;)
{ if(*pch == '%')
{ for(pch = pch +1;*pch >= '0' && *pch<='9'; )
len = len * 10 + *pch++ - '0';
s = eNone;
if(*pch == 'l'){ s = efLong; pch ++;}
if(*pch == 'h'){ s = efShort; pch ++;}
if(s == efShort && *pch == 'h'){ s = efChar; pch ++;}
switch(*pch)
{
case 'i':
case 'd': s |= eInt; pch ++; break;
case 's': s = eString;pch ++; break;
case 'u': s |= efUnsDec;pch ++; break;
case 'c': s = eChar; pch ++; break;
case 'o': s |= eOctal;pch++; break;
case 'x': s |= eHexa;pch++; break;
case 'X': s |= eBigHexa;pch++;break;
case 'p': s = ePointer;pch++; break;
};

if(s == eNone) *str++ = *pch++;
else if(s == eString)
{ p1 = va_arg(l,char*);
for(p2=p1;*p2; ++p2, ++str)
{ if(len && (p2-p1) >= len) break;
*str = *p2;
}
}
else if(s == ePointer)
{ *str++ ='0';*str++ = 'x';
n = (unsigned long)va_arg(l, void*);
for(i=0; n > 0;n /= 16, ++i)
{ t = n % 16;
if(t > 9) { t %= 10; temp[i] = 'a'+t;}
else temp[i] = t + '0';
}
for(;i < 2*sizeof(void *);++i) temp[i] = '0';
for(;i > 0;) *str++ = temp[--i];
}
else if(s == eChar)
{ *str++ = (unsigned char)va_arg(l, int);
}
else {
num = va_arg(l, long);
if(s & efShort) num = (s & efUnsDec)?(unsigned short)num:(short)num;
else if(s & efChar) num = (s & efUnsDec)?(unsigned char)num:(char)num;

if(s & eInt)
{ num = (int)num;

n = num < 0 ? -num : num;
if(num < 0) *str++ = '-';
for(i=0; n > 0; n /= 10, ++i)
temp[i] = n % 10 + '0';

}
else if(s & eOctal)
{ n = (num < 0) ? -num : num;
for(i=0; n > 0; n /= 8, ++i)
temp[i] = n % 8 + '0';
if(num < 0) temp[i++] = '-';
}
else if((s & eHexa)||(s & eBigHexa))
{ n = (num < 0) ? -num : num;
for(i=0; n > 0;n /= 16, ++i)
{ t = n % 16;
if(t > 9) { t %= 10; temp[i] = (s & eBigHexa)?'A'+t:'a'+t;}
else temp[i] = t + '0';
}
if(num < 0) temp[i++] = '-';
}
else

{
if(s & efUnsDec)n = num;
else{
if(num < 0)
n = -num;
else
n = num;
}
if(num < 0 && !(s & efUnsDec)) *str++ = '-';
for(i=0; n > 0; n /= 10, ++i)
temp[i] = n % 10 + '0';
}
for(;i > 0; ) *str++ = temp[--i];
}
}
else
*str++ = *pch++;
}
*str = '\0';
va_end(l);
return str - start;
}
int main(int argc, char** argv[])
{ char buf[512];
int len = Sprintf(buf, "%d-%d=%d", -1, 4, -1-4);
printf("%s\nVytisknuto %d znaku.\n", buf, len);
len = Sprintf(buf, "20%% ze 100 je %d", 100/5);
printf("%s\nVytisknuto %d znaku.\n", buf, len);
len = Sprintf(buf, "-1000 v osmickove soustave je %o", -1000);
printf("%s\nVytisknuto %d znaku.\n", buf, len);
len = Sprintf(buf, "1000 v hexa soustave je %x nebo %X.", 1000, 1000);
printf("%s\nVytisknuto %d znaku.\n", buf, len);
len = Sprintf(buf, "1000 v hexa soustave je %x nebo %X.", 1000, 1000);
printf("%s\nVytisknuto %d znaku.\n", buf, len);
len = Sprintf(buf, "Znak %c nebo %hhc.", 165, 165);
printf("%s\nVytisknuto %d znaku.\n", buf, len);
len = Sprintf(buf, "Adresa buf je %p.", buf);
printf("%s\nVytisknuto %d znaku.\n", buf, len);
len = Sprintf(buf, "1. dva znaky ze slova %s jsou %2s\n", "Pepiku", "Pepiku");
printf("%sVytisknuto %d znaku.\n", buf, len);
system ("PAUSE");
return 0;
}

[/code]
přes automat kterymu to ucitelo posilam se vypsie protokol s timto vypisem

[code]

--- gcc-4.0 prelozeno ---
--- gcc-2.95 prelozeno ---


--- protokol "./test.1.bin" ---


59: "-109725520, -109725520, 4185241776, 0-642443520, 0x-68a4750" (60: "-109725520, -109725520, 4185241776, 037135334260, 0xf975b8b0")
136: "-80, 176, -18256, 47280, -109725520, 4185241776, -109725520, 4185241776 80, 80, 18256, 18256, 109725520, 109725520, 109725520, 109725520" (136: "-80, 176, -18256, 47280, -109725520, 4185241776, -109725520, 4185241776 80, 80, 18256, 18256, 109725520, 109725520, 109725520, 109725520")
36: "0, "Chrchly Chrchly", 48, 0xbf89af28" (36: "0, "Chrchly Chrchly", 48, 0xbf89af28")

! Chyba: 1


---




--- protokol "./test.2.bin" ---


59: "-109725520, -109725520, 4185241776, 0-642443520, 0x-68a4750" (60: "-109725520, -109725520, 4185241776, 037135334260, 0xf975b8b0")
136: "-80, 176, -18256, 47280, -109725520, 4185241776, -109725520, 4185241776 80, 80, 18256, 18256, 109725520, 109725520, 109725520, 109725520" (136: "-80, 176, -18256, 47280, -109725520, 4185241776, -109725520, 4185241776 80, 80, 18256, 18256, 109725520, 109725520, 109725520, 109725520")
36: "0, "Chrchly Chrchly", 48, 0xbfc1c4f4" (36: "0, "Chrchly Chrchly", 48, 0xbfc1c4f4")

! Chyba: 1


---
[/code]


u toho vypisu mám nějaky ty čisla,pak mam taky čisla a znaky v závorkách,to co je v závorce je správný výpis jak by to mělo správně vypisovat,takže z toho poznám kde co je špatně.špatně je jen na prvnich řádkách poslední dvě číselné kombinace.
No a v tom je ten problem,chybu mám v číslech asi někde v hh, h a l.říkal mi to aspon kamarád,jelikoz ja na to neprisel.Nemohli byste mi s tím prosim Vás někdo pomoc jak to mám spravit aby to vypisovalo správně.Byl bych Vám moc vděčný.

Re: Sprintf 10.6.2007 15:59
Aleš Hakl

ja mam za to, ze primarni problem je v tom, ze %x a %o je pouze unsigned - tudiz je ponekud zvlastni, ze vam to vypisuje zaporna cisla. Videl bych problem ve zpusobu, jakym konvertujete ciselne soustavy.

Re: Sprintf 10.6.2007 16:13
Aleš Hakl

A jak tak koukam, tak ta konstrukce s ternalnim operatorem a pretypovanim uvnitr je take docela podezrela, me prijde, ze takovy kod proste nic nedela.

Re: Sprintf 11.6.2007 13:12
Tomáš Eichler

a jak si teda myslíte že bych to měl spravit,protže já už vážně nevím.

Re: Sprintf 11.6.2007 15:33
Aleš Hakl

Nemit jednu promennou num typu long, ale samostatnou pro kazdy datovy typ (nebo jeste lepe oddelit ty konverze do samostatnych funkci s tim spravnym argumentem).Jde o to, ze kdyz pretypujete long na unsigned long a nasledne implicitni konverzi zpet, tak niceho rozumneho nedosahnete.

Re: Sprintf 12.6.2007 22:11
Tomáš Eichler

Aha.A jak to mam prosim Vas vyjadrit zdorjovym kodem.

Re: Sprintf 12.6.2007 22:11
Tomáš Eichler

Aha.A jak to mam prosim Vas vyjadrit zdorjovym kodem.

Re: Sprintf 13.6.2007 18:46
Petr Zajíc

... já teda Céčko neumím, ale tak nějak nevěřím tomu, že byste na internetu nenašel příklad na přetypování proměnných. Nejste spíš líný hledat? Myslíte si, že Vám to tady bude někdo psát celé?


KOMENTARZE
Sprintf 10.6.2007 15:31 Tomáš Eichler
  L Re: Sprintf 10.6.2007 15:59 Aleš Hakl
    L Re: Sprintf 10.6.2007 16:13 Aleš Hakl
      L Re: Sprintf 11.6.2007 13:12 Tomáš Eichler
        L Re: Sprintf 11.6.2007 15:33 Aleš Hakl
          |- Re: Sprintf 12.6.2007 22:11 Tomáš Eichler
          L Re: Sprintf 12.6.2007 22:11 Tomáš Eichler
            L Re: Sprintf 13.6.2007 18:46 Petr Zajíc
Tylko zarejestrowani użytkownicy mogą dopisywać komentarze.
> Szukanie oprogramowania
1. Pacman linux
Download: 4789x
2. FreeBSD
Download: 8979x
3. PCLinuxOS-2010
Download: 8483x
4. alcolix
Download: 10792x
5. Onebase Linux
Download: 9565x
6. Novell Linux Desktop
Download: 0x
7. KateOS
Download: 6134x

1. xinetd
Download: 2324x
2. RDGS
Download: 937x
3. spkg
Download: 4539x
4. LinPacker
Download: 9787x
5. VFU File Manager
Download: 3109x
6. LeftHand Mała Księgowość
Download: 6994x
7. MISU pyFotoResize
Download: 2695x
8. Lefthand CRM
Download: 3480x
9. MetadataExtractor
Download: 0x
10. RCP100
Download: 2991x
11. Predaj softveru
Download: 0x
12. MSH Free Autoresponder
Download: 0x
©Pavel Kysilka - 2003-2024 | mailatlinuxsoft.cz | Design: www.megadesign.cz