zoukankan      html  css  js  c++  java
  • C程序设计语言习题(35)

    编写函数itob(n,s,b),将整数n转换为以b为底的数,并将转换结果以字符的形式保存到字符串s中。e.g.itob(n,s,16)把整数n格式化为十六进制整数保存在s中。

     1 #include<stdio.h>
     2 #include<ctype.h>
     3 #include<string.h>
     4 
     5 void swap(char *a, char *b)
     6 {
     7     int t;
     8     t = *a;
     9     *a = *b;
    10     *b = t;
    11 }
    12 
    13 void reverse(char *s)  //倒置串s
    14 {
    15     int c, i, j;
    16     i = 0;
    17     j = strlen(s) - 1;
    18     while(i < j) {
    19         swap(&s[i], &s[j]);
    20         i++;
    21         j--;
    22     }
    23 }
    24 
    25 void itob(int n, char *s, int b)
    26 {
    27     int i, j, sign;
    28     i = 0;
    29     if((sign = n) < 0)
    30          n = -n;
    31     do {
    32         j = n % b;
    33         s[i++] = (j <= 9) ? j + '0' : j - 10 + 'A';
    34     } while((n /= b) > 0);
    35     if(sign < 0)  s[i++] = '-';
    36     s[i] = '\0';
    37     reverse(s);
    38 }
    39 
    40 int main()
    41 {
    42     char a[1000];
    43     int n, b;
    44     n = 1232323;
    45     b = 16;
    46     itob(n,a,b);
    47     printf("%s\n",a);
    48     return 0;
    49 }
  • 相关阅读:
    XSS初探
    简单的HTTP服务实现
    WinForm“假死”问题汇总
    Access 数据库的数据类型
    C#应用调试C++ dll的方法
    Visual Studio远程调试
    【汇总】C#编程技巧
    SQL Server常见问题及解决方法
    java订单生成工具类
    JAVA-学习路线
  • 原文地址:https://www.cnblogs.com/cpoint/p/3367505.html
Copyright © 2011-2022 走看看