C和指针 学习(二)第七章习题第五题、第六题
作者:互联网
5、实现一个简化的printf函数 (假设已经有了print_integer和print_float)
注意:
1、满足printf的四种形式:%f %s %d %c
2、满足printf中可以输入任意参数的性质
3、熟练掌握指针技巧
#include <stdio.h> #include <stdarg.h> #include <stdlib.h>
void print_integer(int integer){ printf("%d", integer); }
void print_float(float f){ printf("%f", f); }
void easy_printf(char *format, ...){ va_list arg; va_start(arg,format); char *sptr = format; char *str; float flt; while (*sptr != '\0'){ if (*sptr != '%'){ putchar(*sptr++); continue; } if (*sptr == '%' && *(sptr+1) == '%'){ putchar(*sptr++); *sptr++; continue; } switch(*(sptr+1)){ case 'd': print_integer((va_arg(arg, int))); sptr += 2; break; case 'f': print_float((va_arg(arg, double))); sptr += 2; break; case 's': str = va_arg(arg, char *); while(*str != '\0'){ putchar(*str++); } sptr += 2; break; case 'c': putchar(va_arg(arg, int)); sptr += 2; break; default: sptr += 2; break; } } va_end(arg); }
int main(){ char *name = "Peter"; int age = 26; double pi = 3.14; char mark = 'Y'; mini_printf("My name is %s. I am %d years old. The value of pi is %f. The answer is %c.", name, age, pi, mark); }
6、编写如下函数:
void written_amount(unsigned int amount, char* buffer);
它把amount表示的值转换为单词形式。
注意:
1、将数字根据英文分组,每3位一组;
2、将20以下的数字单独排出;
3、注意0;
#include <stdlib.h> #include <string.h> #include <stdio.h>void div_num_3(unsigned int num_array[], unsigned int num){ unsigned int div_num; int loc = 1; while(num){ div_num = num % 1000; num = num / 1000; num_array[loc] = div_num; loc++; } num_array[0] = loc - 1; }
int num_to_word(unsigned int num, char* buffer){ if (num == 0){ return 0; } static char* basic_digits[] = {"", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN","SEVENTEEN", "EIGHTEEN" "NINETEEN"}; static char* ten_digits[] = {"", "", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY"}; if (num /100){ strcat(buffer, basic_digits[num/100]); strcat(buffer, " "); strcat(buffer, "HUNDRED"); strcat(buffer, " "); } num = num % 100; if (num < 20){ strcat(buffer, basic_digits[num]); if (num != 0){ strcat(buffer, " "); } } else{ int ten_num = num / 10; num = num % 10; strcat(buffer, ten_digits[ten_num]); strcat(buffer, " "); strcat(buffer, basic_digits[num]); if (num != 0){ strcat(buffer, " "); } } }
void written_amount(unsigned int amount, char* buffer){ if(amount == 0){ strcat(buffer, "zero"); return; } static char* basic_element[] = {"", "THOUSAND", "MILLION", "BILLION"}; unsigned int div_num[100]; div_num_3(div_num, amount); int len = div_num[0]; while (len){ num_to_word(div_num[len], buffer); strcat(buffer, basic_element[len - 1]); strcat(buffer, " "); len--; }
}
int main(){ unsigned int T = 0x12; unsigned int num_array[100]; char buffer[] = ""; div_num_3(num_array, T); printf("共有%d组数字\n", num_array[0]); for (int i = 1; i <= num_array[0]; i++){ printf("第%d组数为:%d\n", i, num_array[i]); } written_amount(T, buffer); printf("The num is: %s\n", buffer); return 0; }
标签:int,char,buffer,num,第七章,arg,习题,sptr,指针 来源: https://www.cnblogs.com/ELFTE/p/14784899.html