I also want to write a function like printf (a convincing pen question)

Source: Internet
Author: User

Remembering the question that I was convinced last time, we were able to implement a function similar to the printf function, that is, the number of variable parameters. At that time, we had nothing to do with it. I just searched the internet for some gains. I wrote a program instance. If you are interested, you can see:

 

# Include <iostream>
# Include <conio. h>
# Include <stdio. h>
# Include <stdarg. h> // defines macros such as av_list, av_start, and av_arg.
/*************************************** ****************************
Function:
Implement a function with variable number of parameters. This function is similar to printf,
But in terms of format processing, it is not as rich as printf
If no exception occurs, true is returned. Otherwise, false is returned.
The format string is valid as follows:
1. "% zyk %", output: % zyk %
2. "% dzyk % fzyk % s", output :( INT) zyk (float) zyk (string)
3. "zyk", output: zyk
Illegal conditions are as follows:
1. "% zyk %" error: % Z format does not exist, and % must be followed by a format character
**************************************** ***************************/
Bool zykprintf (const char * format ,...)
{
// Define a pointer (actually char *) that can be used to point to a parameter *),
Va_list argptr;

// Pass the format address of the first parameter of the function to argptr.
Va_start (argptr, format );

Const int size = strlen (Format) + 1;
Char * TMP = new char [size];
Memset (TMP, 0, size );

While (* Format! = 0)
{
Int I;
For (I = 0; I <size & * Format! = '%' & * Format! = 0; I ++)
{
TMP [I] = * Format ++;
}
TMP [I] = 0; // 0 value protection at the end of a valid string

Printf ("% s", TMP );

If (* format = 0)
Return true;

Switch (* ++ format)
{
// Read the next parameter of the specified type and print it
Case 'D': {printf ("% d", va_arg (argptr, INT); break ;}
Case's ': {printf ("% s", va_arg (argptr, char *); break ;}
Case 'C': {printf ("% C", va_arg (argptr, char); break ;}
Case 'F': {printf ("% F", va_arg (argptr, float); break ;}

// Process %
Case '%': {printf ("%"); break ;}

// Format Error
Default: {printf ("error ocurr! Please check the format! "); Return false ;}
}

++ Format;

}

Delete [] TMP;
Return true;
}

Int main (INT argc, char * argv [])
{

Zykprintf ("% zyk"); // Error
Zykprintf ("zyk %"); // Error

Zykprintf ("% zyk %"); // output: % zyk %
Zykprintf ("/nzyk is a pretty boy! His age is % d and % s ", 5," I love zyk ^_^! ");

Getch ();
Return 0;
}

There are better methods. Please post them.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.