手写简化版printf函数

手写简化版printf函数

2019.02.01更新:经同学提醒,myprintf函数应有返回值为输出的字符数。

 

期末的大作业,手写一个myprintf函数,支持如下一些操作。

 

也就是  % -(负号控制左右对齐) 数(控制字段宽). 数(控制精度) ?(字符,控制类型) 

 

我实现的话就是按上面的实现的,说一下这个简化版存在的问题(简化的地方):

1)%d %i %o %x %X %u %c 这些都默认后面输入的是int,所以long long其实没有用。

2)支持最大精度<=30,如果有需要请更改PRECISION

 

myprintf默认四舍五入(rounding_off函数实现)

 

如果发现错误,请指出,谢谢大家啦!

 

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<math.h>
  5 #include<stdarg.h>
  6 
  7 #define N 110
  8 #define eps 1e-16
  9 #define INF 1e9
 10 #define PRECISION 30
 11 
 12 int maxx(int x,int y){
     return x>y ? x:y;}
 13 void swapp(char *x,char *y){
     char t;t=*x;*x=*y;*y=t;}
 14 int find_int_type(int int_type,char ch);
 15 void change_int_type_to_char(int x,int int_type,char *s,int *sl,int *minus);
 16 void print_int_regardless_type(char *s,int *sl,int minus,int Justify_type,int min_width,int precision);
 17 void print_int(int x,int Justify_type,int min_width,int precision,int int_type);
 18 void print_string(char *s,int Justify_type,int min_width,int precision);
 19 int is_g_or_G(char double_type){
     return (double_type == 'g' || double_type=='G');}
 20 void rounding_off(char *s,int *sl,int *ml,int precision);
 21 void print_double_e_or_E(char *s,int sl,int exponent,int minus,int Justify_type,int min_width,int precision,char double_type);
 22 void print_double_f(char *s,int sl,int ml,int minus,int Justify_type,int min_width,int precision);
 23 void print_double(double x,int Justify_type,int min_width,int precision,char double_type);
 24 int myprintf(const char *format,...);
 25 
 26 
 27 int main()
 28 {
 29     // freopen("a.out","w",stdout);
 30     int a=123;
 31     double b=9.9734567;
 32     int count=123456789;
 33     myprintf("%\n");
 34     myprintf("%15e%15e\n",-0.0000123,123.123);printf("%15e%15e\n",-0.0000123,123.123);
 35     myprintf("%f\n",0.0000123);printf("%f\n",0.0000123);
 36     myprintf("%.1E\n",b);printf("%.1E\n",b);
 37     myprintf("%e\n",0.0000123);printf("%e\n",0.0000123);
 38     myprintf("%15.8g\n",0.000012346789);printf("%15.8g\n",0.0000123456789);
 39     myprintf("%5d%10d\n",a,a);printf("%5d%10d\n",a,a);
 40     myprintf("%10.5d%10.5d\n",a,a);printf("%10.5d%10.5d\n",a,a);
 41     myprintf("%15.10hd%15.10ld\n",count,count);printf("%15.10hd%15.10ld\n",count,count);
 42     myprintf("%5o\n",a);printf("%5o\n",a);
 43     myprintf("%f\n",b);printf("%f\n",b);
 44     myprintf("%15f%20.10f\n",-b,-b);printf("%15f%20.10f\n",-b,-b);
 45     myprintf("%15g\n",0.0000123);printf("%15g\n",0.0000123);
 46     myprintf("%15e%15E%15g%15g\n",-b,b,b,0.0000123);printf("%15e%15E%15g%15g\n",-b,b,b,0.0000123);
 47     myprintf("%15g\n",0.0000123);printf("%15g\n",0.0000123);
 48     myprintf("%-15.10x%10.10X\n",count);printf("%-15.10x%10.10X\n",count);
 49     myprintf("%10.5s\n%c\n&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值