C++程序设计语言练习4.4

来源:互联网 发布:grav cms 编辑:程序博客网 时间:2024/06/06 01:45

不假定任何字符具有一般性的值,我们可能得到更具有可移植性的代码。

#include <iostream>#include <ctype.h>#include <iomanip>using std::cout;using std::hex;int main(){  for (char ch = 'a'; ch <= 'z'; ch++) {    cout<<"the char is "<<ch<<" and the value is "<<(int)ch<<'\n';  }  for (char num = '0'; num <= '9'; num++) {    cout<<"the char is "<<num<<" and the value is "<<(int)num<<'\n';  }  for ( unsigned char pch = 0; pch !=255; pch++) {    if (isprint(pch)) {      cout<< "the char can be printed is: "<<pch<<" and the hex is: 0x"<<hex<<(unsigned int)pch<<"\n";    }  }}

运行结果:

[chaos@localhost cpp]$ g++ -o 4.4 ./4.4.cpp[chaos@localhost cpp]$ ./4.4the char is a and the value is 97the char is b and the value is 98the char is c and the value is 99the char is d and the value is 100the char is e and the value is 101the char is f and the value is 102the char is g and the value is 103the char is h and the value is 104the char is i and the value is 105the char is j and the value is 106the char is k and the value is 107the char is l and the value is 108the char is m and the value is 109the char is n and the value is 110the char is o and the value is 111the char is p and the value is 112the char is q and the value is 113the char is r and the value is 114the char is s and the value is 115the char is t and the value is 116the char is u and the value is 117the char is v and the value is 118the char is w and the value is 119the char is x and the value is 120the char is y and the value is 121the char is z and the value is 122the char is 0 and the value is 48the char is 1 and the value is 49the char is 2 and the value is 50the char is 3 and the value is 51the char is 4 and the value is 52the char is 5 and the value is 53the char is 6 and the value is 54the char is 7 and the value is 55the char is 8 and the value is 56the char is 9 and the value is 57the char can be printed is:   and the hex is: 0x20the char can be printed is: ! and the hex is: 0x21the char can be printed is: " and the hex is: 0x22the char can be printed is: # and the hex is: 0x23the char can be printed is: $ and the hex is: 0x24the char can be printed is: % and the hex is: 0x25the char can be printed is: & and the hex is: 0x26the char can be printed is: ' and the hex is: 0x27the char can be printed is: ( and the hex is: 0x28the char can be printed is: ) and the hex is: 0x29the char can be printed is: * and the hex is: 0x2athe char can be printed is: + and the hex is: 0x2bthe char can be printed is: , and the hex is: 0x2cthe char can be printed is: - and the hex is: 0x2dthe char can be printed is: . and the hex is: 0x2ethe char can be printed is: / and the hex is: 0x2fthe char can be printed is: 0 and the hex is: 0x30the char can be printed is: 1 and the hex is: 0x31the char can be printed is: 2 and the hex is: 0x32the char can be printed is: 3 and the hex is: 0x33the char can be printed is: 4 and the hex is: 0x34the char can be printed is: 5 and the hex is: 0x35the char can be printed is: 6 and the hex is: 0x36the char can be printed is: 7 and the hex is: 0x37the char can be printed is: 8 and the hex is: 0x38the char can be printed is: 9 and the hex is: 0x39the char can be printed is: : and the hex is: 0x3athe char can be printed is: ; and the hex is: 0x3bthe char can be printed is: < and the hex is: 0x3cthe char can be printed is: = and the hex is: 0x3dthe char can be printed is: > and the hex is: 0x3ethe char can be printed is: ? and the hex is: 0x3fthe char can be printed is: @ and the hex is: 0x40the char can be printed is: A and the hex is: 0x41the char can be printed is: B and the hex is: 0x42the char can be printed is: C and the hex is: 0x43the char can be printed is: D and the hex is: 0x44the char can be printed is: E and the hex is: 0x45the char can be printed is: F and the hex is: 0x46the char can be printed is: G and the hex is: 0x47the char can be printed is: H and the hex is: 0x48the char can be printed is: I and the hex is: 0x49the char can be printed is: J and the hex is: 0x4athe char can be printed is: K and the hex is: 0x4bthe char can be printed is: L and the hex is: 0x4cthe char can be printed is: M and the hex is: 0x4dthe char can be printed is: N and the hex is: 0x4ethe char can be printed is: O and the hex is: 0x4fthe char can be printed is: P and the hex is: 0x50the char can be printed is: Q and the hex is: 0x51the char can be printed is: R and the hex is: 0x52the char can be printed is: S and the hex is: 0x53the char can be printed is: T and the hex is: 0x54the char can be printed is: U and the hex is: 0x55the char can be printed is: V and the hex is: 0x56the char can be printed is: W and the hex is: 0x57the char can be printed is: X and the hex is: 0x58the char can be printed is: Y and the hex is: 0x59the char can be printed is: Z and the hex is: 0x5athe char can be printed is: [ and the hex is: 0x5bthe char can be printed is: \ and the hex is: 0x5cthe char can be printed is: ] and the hex is: 0x5dthe char can be printed is: ^ and the hex is: 0x5ethe char can be printed is: _ and the hex is: 0x5fthe char can be printed is: ` and the hex is: 0x60the char can be printed is: a and the hex is: 0x61the char can be printed is: b and the hex is: 0x62the char can be printed is: c and the hex is: 0x63the char can be printed is: d and the hex is: 0x64the char can be printed is: e and the hex is: 0x65the char can be printed is: f and the hex is: 0x66the char can be printed is: g and the hex is: 0x67the char can be printed is: h and the hex is: 0x68the char can be printed is: i and the hex is: 0x69the char can be printed is: j and the hex is: 0x6athe char can be printed is: k and the hex is: 0x6bthe char can be printed is: l and the hex is: 0x6cthe char can be printed is: m and the hex is: 0x6dthe char can be printed is: n and the hex is: 0x6ethe char can be printed is: o and the hex is: 0x6fthe char can be printed is: p and the hex is: 0x70the char can be printed is: q and the hex is: 0x71the char can be printed is: r and the hex is: 0x72the char can be printed is: s and the hex is: 0x73the char can be printed is: t and the hex is: 0x74the char can be printed is: u and the hex is: 0x75the char can be printed is: v and the hex is: 0x76the char can be printed is: w and the hex is: 0x77the char can be printed is: x and the hex is: 0x78the char can be printed is: y and the hex is: 0x79the char can be printed is: z and the hex is: 0x7athe char can be printed is: { and the hex is: 0x7bthe char can be printed is: | and the hex is: 0x7cthe char can be printed is: } and the hex is: 0x7dthe char can be printed is: ~ and the hex is: 0x7e


0 0