C/C++ 各进制赋值、int/char转换、sscanf/sprintf、位操作运算

一、各进制赋值

1.十六进制赋值

  int i=0x12AD;

  int i=0X12AD;

  int i=0x12Ad;

  int i=0X12Ad;

  //以上都是十六进制,表示十进制173;

2.八进制赋值

  int num=017;

  //数字前面加0就是8进制,017表示十进制15,若写018则编译出错。(error C2041: 非法的数字“8”(用于基“8”))

二、int/char/string转换

1.char*/char[]转int:atoi()

  char* a="154";    int i=atoi(a);   //成功

  char a[]="154";    int i=atoi(a);   //成功

  char* a="abc";     int i=atoi(a);   //i=0,也没出错

2.int转char[](转char*老崩溃)

  int n=154;

  char p[10];

  itoa(n,p,16);  //非STL,vs里能用,一些在线oj不能用

  //十六是进制,可以自己指定。结果:p=“9a”;

3.char*/char[]转string

  char*/char[] p="dsdsdsd";

  string str=p;//直接赋值就行

4.string转char*

  string p="ewqewq";

  const char* o=p.c_str();//必须得是const

5.string/int互转

  通过char即可;

三、sscanf/sprintf(c语言)

  scanf:把键盘输入的数据给指定变量;

  printf:打印指定变量至屏幕;

  sscanf:把某const char*的数据给指定char*变量;

  sprintf:把数据输入到指定char*。

1.sscanf():int sscanf(const char *buffer, const char *format, [argument ]...);

  e.g.char buf[512] ;
    sscanf("123456 ", "%s", buf);
    printf("%s\n", buf);
    结果为:123456

  e.g.sscanf("123456 ", "%4s", buf);
    printf("%s\n", buf);
    结果为:1234

2.sprintf():int sprintf(char *buffer, const char *format, [argument] … );

  格式控制符的类型要和后面argument的类型一样。

  sprintf(s, "%d", 123);  //把整数123打印成一个字符串保存在s中,

  sprintf(s, "%8x", 4567);  //小写16进制,宽度占8个位置,右对齐

  sprintf(buf, "fdsafsdf");

  char a = 'a';    sprintf(buf, "The ASCII code of a is %d.", a);

四、位操作运算

  位操作只能用于整形数据,对实类型进行位操作会被编译器报错。

1.&(与操作)

  int i=100;(二进制1100100)

  则i&1=0;//只是最后一位和1进行与运算,所以可以判断奇偶性;

2.^(异或,两者不同则为1)

  void Swap(int &a, int &b)  
  {  
      if (a != b)  
      {  
          a ^= b;  
          b ^= a;  
          a ^= b;  
      }  
  }  //交换两个数不用第3个变量

3.移位

  e.g.int n=98;

      n=n>>1;//n=49;

  <<各二进位全部左移若干位,高位丢弃,低位补0

  >>各二进位全部右移若干位,对无符号数,高位补0,有符号数,各编译器处理方法不一样,有的补符号位(算术右移),有的补0(逻辑右移)

转载于:https://www.cnblogs.com/chakyu/p/7412683.html

### C++中截取浮点数小数点后指定位数的方法 在C++中,可以通过多种方法实现对浮点数小数点后指定位数的截取。以下是几种常见的实现方式: #### 方法一:使用`std::stringstream`和`std::setprecision` 通过`std::stringstream`结合`std::setprecision`可以精确控制浮点数的小数位数。这种方法利用字符串流将浮点数格式化为指定精度后再转换回浮点数。 ```cpp #include <iostream> #include <iomanip> #include <sstream> double Round(double src, int bits) { std::stringstream ss; ss << std::fixed << std::setprecision(bits) << src; // 设置固定小数位数 ss >> src; // 将格式化后的字符串重新赋值给src return src; } int main() { double val = 3.141592653589793; double num = Round(val, 5); // 截取小数点后5位 printf("num: %.5f\n", num); // 使用printf输出查看效果 return 0; } ``` 此代码示例展示了如何通过`std::stringstream`来截取浮点数的小数点后指定位数[^1]。 #### 方法二:使用`std::cout`和`std::setprecision` 直接使用`std::cout`配合`std::setprecision`可以控制浮点数的输出精度,但需要注意的是,这种方式仅影响输出格式,并不会改变原始浮点数值。 ```cpp #include <iostream> #include <iomanip> int main() { double pi = 3.1415926; std::cout << std::setprecision(3) << pi << std::endl; // 有效位数为3位 std::cout << std::fixed << std::setprecision(3) << pi << std::endl; // 保留小数点后3位 return 0; } ``` 上述代码中,`std::fixed`确保输出固定的小数位数,而`std::setprecision`指定具体的小数位数[^2]。 #### 方法三:使用`sprintf`和`sscanf` 通过`sprintf`将浮点数格式化为字符串,再用`sscanf`将其解析回浮点数,从而实现小数位数的截取。 ```cpp #include <iostream> #include <cstdio> int main() { double d = 1.23456789; char str[20]; sprintf(str, "%.2f", d); // 格式化为两位小数 sscanf(str, "%lf", &d); // 解析回浮点数 std::cout << "Result: " << d << std::endl; return 0; } ``` 此方法适用于需要将浮点数临时转换为字符串进行处理的情况[^4]。 #### 方法四:数学运算法 通过数学运算手动截取小数位数。该方法基于乘法和除法操作,将浮点数放大到整数范围后截断多余部分。 ```cpp #include <cmath> #include <iostream> double Truncate(double value, int precision) { double factor = std::pow(10.0, precision); return std::trunc(value * factor) / factor; } int main() { double num = 3.141592653589793; double result = Truncate(num, 4); // 截取小数点后4位 std::cout << "Truncated Value: " << result << std::endl; return 0; } ``` 这种方法不依赖于I/O流或字符串操作,适合需要高效处理的场景[^3]。 ### 注意事项 - 如果需要对浮点数进行四舍五入,可以使用`std::round`代替`std::trunc`。 - 在高精度计算中,浮点数的精度损失可能会影响结果,需谨慎处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值