zoukankan      html  css  js  c++  java
  • 进制

    1、数的进制

    默认进制:
    默认状态下,数据按十进制输入输出。如果要求按八进制或十六进制输入输出,在cin或cout中必须指明相应的数据形式,oct为八进制,hex为十六进制,dec为十进制。

    复制代码

     1 int i, j, k, l; 2 cout<<”Input i(oct), j(hex), k(hex), l(dec):”<<endl; 3 cin>>oct>>i; //输入为八进制数 4 cin>>hex>>j; //输入为十六进制数 5 cin>>k; //输入仍为十六进制数 6 cin>>dec>>l; //输入为十进制数 7 cout<<”hex:”<<”i=”<<hex<<i<<endl; 8 cout<<”dec:”<<”j=”<<dec<<j<<′	′<<”k=”<<k<<endl; 9 cout<<”oct:”<<”l=”<<oct<<l;10 cout<<dec<<endl; //恢复十进制输出状态11 12 【执行结果】:13 (1)输出提示:Input i(oct), j(hex), k(hex), l(dec):14 (2)此时从键盘输入: 032 0x3f 0xa0 17 <CR>15 (3)输出结果为:16 17 hex:i=1a18 dec:j=63 k=16019 oct:l=21

    复制代码

    几点说明:

    使用不带.h的头文件<iostream>时,必须在cin中指明数制,否则从键盘输入时,不认八进制和十六进制数开头的0和0x标志。指明后可省略0和0x标志。

    进制控制只适用于整型变量,不适用于实型和字符型变量。

    输入数据的格式、个数和类型必须与cin中的变量一一对应,否则不仅使输入数据错误,而且影响后面其他数据的正确输入。

    在cin或cout中指明数制后,该数制将一直有效,直到重新指明使用其他数制。

    2、数据间隔

    常用设置方法:输出空格符或回车换行符。

    指定数据输出宽度:用C++提供的函数setw()指定输出数据项的宽度。setw()括号中通常给出一个正整数值,用于限定紧跟其后的一个数据项的输出宽度。如:setw(8)表示紧跟其后的数据项的输出占8个字符宽度。

    复制代码

     1 int i=2, j=3; 2 float x=2.6, y=1.8; 3 cout<<setw(6)<<i<<setw(10)<<j<<endl; 4 cout<<setw(10)<<i*j<<endl; 5 cout<<setw(8)<<x<<setw(8)<<y<<endl; 6  7 则输出结果为: 8 2 3 9 610 2.6 1.8

    复制代码

    setw()只能限定紧随其后的一个数据项,输出后即回到默认输出方式。
    使用setw()必须在程序开头再增加一句: #include<iomanip>

  • 相关阅读:
    how to pass a Javabean to server In Model2 architecture.
    What is the Web Appliation Archive, abbreviation is "WAR"
    Understaning Javascript OO
    Genetic Fraud
    poj 3211 Washing Clothes
    poj 2385 Apple Catching
    Magic Star
    关于memset的用法几点
    c++ 函数
    zoj 2972 Hurdles of 110m
  • 原文地址:https://www.cnblogs.com/gujianhan/p/3702040.html
Copyright © 2011-2022 走看看