#include <cstring>
#include <iostream>
using namespace std;
int main()
{
const char *s = "hhhhh";
cout << "the length of the s is " << strlen(s) << endl;
char str[10] = "ddddd";
cout << "hello" << endl;
cout << s << str << endl;
cout << &str[0] << endl;
cout << (float *)s << endl;
cout << (void *)str << endl;
cout << (int *)"hello" << endl;
cout << "-------------------------------------------\n";
cout.put('d');
cout.put('\n');
cout.put('d').put('b').put('\n');
cout.put(65);
cout.put(65.9);
cout.put('\n');
cout << "-------------------------------------------\n";
const char *state1 = "Florida";
const char *state2 = "Kansas";
const char *state3 = "Euphoria";
int len = strlen(state2);
cout << "Increasing loop index:\n";
int i;
for (i = 1; i <= len; i++)
{
cout.write(state2, i);
cout << endl;
}
cout << "Decreasing loop index:\n";
for (i = len; i > 0; i--)
cout.write(state2, i) << endl;
cout << "Exceeding string length:\n";
cout.write(state2, len + 5).write("\n", 1).write(state2, len + 4) << endl;
long val = 1094795585;
cout.write((char *)&val, sizeof(long)).write("\n", 1);
cout << "-------------------------------------------\n";
cout << "Hello, good-looking! " << flush;
cout << "Wait just a moment, please." << endl;
flush(cout);
cout << flush;
return 0;
}
输出
the length of the s is 5
hello
hhhhhddddd
ddddd
0x406045
0x61feee
0x406063
-------------------------------------------
d
db
AA
-------------------------------------------
Increasing loop index:
K
Ka
Kan
Kans
Kansa
Kansas
Decreasing loop index:
Kansas
Kansa
Kans
Kan
Ka
K
Exceeding string length:
Kansas Euph
Kansas Eup
AAAA
-------------------------------------------
Hello, good-looking! Wait just a moment, please.
#include <iostream>
using namespace std;
int main()
{
int n = 10;
cout << "n\n";
cout << n << " (decimal)\n";
cout << hex << n << " (hexadecimal)\n";
cout << oct << n << " (octal)\n";
dec(cout);
cout << n << " (decimal)\n";
cout << "-------------------------------------------\n";
int w = cout.width(2);
cout.fill('*');
cout << "default field width = " << w << ":\n";
cout.width(5);
cout << "N"
<< ":\n";
for (long i = 1; i <= 100; i *= 10)
{
cout.width(5);
cout << i << ":\n";
}
cout << "-------------------------------------------\n";
float price1 = 20.40;
float price2 = 1.9 + 8.0 / 9.0;
cout << "\"Furry Friends\" is $" << price1 << "!\n";
cout << "\"Fiery Fiends\" is $" << price2 << "!\n";
cout.precision(2);
cout << "\"Furry Friends\" is $" << price1 << "!\n";
cout << "\"Fiery Fiends\" is $" << price2 << "!\n";
cout.precision(6);
cout.setf(ios_base::showpoint);
cout << "\"Furry Friends\" is $" << price1 << "!\n";
cout << "\"Fiery Fiends\" is $" << price2 << "!\n";
cout.precision(2);
cout << "\"Furry Friends\" is $" << price1 << "!\n";
cout << "\"Fiery Fiends\" is $" << price2 << "!\n";
cout << "-------------------------------------------\n";
int temperature = 63;
cout << "Today's water temperature: ";
cout.setf(ios_base::showpos);
cout << temperature << endl;
cout << "For our programming friends, that's\n";
cout << std::hex << temperature << endl;
cout.setf(ios_base::uppercase);
cout.setf(ios_base::showbase);
cout << "or\n";
cout << temperature << endl;
cout << "How " << true << "! oops -- How ";
cout.setf(ios_base::boolalpha);
cout << true << "!\n";
cin.get();
return 0;
}
输出
n
10 (decimal)
a (hexadecimal)
12 (octal)
10 (decimal)
-------------------------------------------
default field width = 0:
****N:
****1:
***10:
**100:
-------------------------------------------
"Furry Friends" is $20.4!
"Fiery Fiends" is $2.78889!
"Furry Friends" is $20!
"Fiery Fiends" is $2.8!
"Furry Friends" is $20.4000!
"Fiery Fiends" is $2.78889!
"Furry Friends" is $20.!
"Fiery Fiends" is $2.8!
-------------------------------------------
Today's water temperature: +63
For our programming friends, that's
3f
or
0X3F
How 0X1! oops -- How true!
附录
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)