您的位置:首页 > 编程语言 > C语言/C++

《C++编程思想》(第二版)第3章 C++中的C(笔记、习题及答案)(二)

2015-07-26 02:00 411 查看


<span style="font-size:18px;"><span style="font-size:18px;">#include <iostream>
#include <string>
using namespace std;

void stringRef(string& s)
{
s += " come blow";
}

void stringPtr(string* p)
{
p->append(" your horn");
}

int main()
{
string s = "Little Boy Blue";
stringRef(s);
stringPtr(&s);
cout << s << endl;
}</span></span>




<span style="font-size:18px;"><span style="font-size:18px;">#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
unsigned int n;
if (argc != 2 || (n = atoi(argv[1])) <= 0)
{
return 1;
}
cout << ~(n ^ 0xf0f0) << '\n';
return 0;
}</span></span>




<span style="font-size:18px;">#include <iostream>
using namespace std;

void func()
{
int i = 0;
cout<<"i = "<<++i<<endl;
}

int main()
{
for(int x = 0;x < 10;x++)
{
func();
}

return 0;
}</span>


此时输出皆为1,因为函数中定义的局部变量在函数作用域结束时消失。当再次调用这个函数时,会重新创建该变量的存储空间,其值会被重新初始化。所以去掉static会使得其变为局部变量,每次调用都重新初始化,输出都为1。



尽管在FileStatic.cpp中fs声明为extern,但是连接器不会找到它,因为在FileStatic.cpp中它被声明为static。



<span style="font-size:18px;">#include <iostream>
using namespace std;

int main()
{
double i,j;
const double EPSILON = 1e-6;
cout<<"Enter an integer:";
cin>>i;
cout<<"Enter another integer:";
cin>>j;
cout<<"i<j is"<<" "<<(i-j<EPSILON)<<endl;
cout<<"i>=j is"<<" "<<(i-j>=EPSILON)<<endl;
cout<<"i<=j is"<<" "<<(i-j<=EPSILON)<<endl;
cout<<"i==j is"<<" "<<(i==j)<<endl;
cout<<"i!=j is"<<" "<<(i!=j)<<endl;
cout<<"i&&j is"<<" "<<(i&&j)<<endl;
cout<<"i||j is"<<" "<<(i||j)<<endl;
cout<<"(i<10)&&(j<10) is"<<" "<<((i-10<EPSILON)&&(j-10<EPSILON))<<endl;

return 0;
}</span>




由于编译器不兼容,将显示运算符列举如下:

关键字含义
and&&(逻辑与)
or||(逻辑或)
not!(逻辑非)
not_eq!=(逻辑不等)
bitand&(位与)
and_eq&=(位与-赋值)
bitor|(位或)
or_eq|=(位或-赋值)
xor^(位异或)
xor_eq^=(位异或-赋值)
compl~(补)


<span style="font-size:18px;">#include <iostream>
using namespace std;

void printBinary(const unsigned char val);
unsigned char rol(unsigned char val);
unsigned char ror(unsigned char val);

#define PL(STR,EXPR) \
cout<<STR; printBinary(EXPR);rol(EXPR); cout<<endl;
#define PR(STR,EXPR) \
cout<<STR; printBinary(EXPR);ror(EXPR); cout<<endl;

int main()
{
unsigned int getval;
unsigned char a,b;
cout<<"Enter a number :"<<endl;
cin>>getval;
a = getval;
PL("a in binary:",a);
cout<<"Enter a number :"<<endl;
cin>>getval;
b = getval;
PR("b in binary:",b);
PR("a | b = ",a|b);
PR("a & b = ",a&b);
PR("a ^ b = ",a^b);
PR("~a = ",~a);
PR("~b = ",~b);
unsigned char c = 0x5A;
PR("c in binary: ",c);
a |= c;
PR("a |= c ;a =  ",a);
b &= c;
PR("b &= c ;b =  ",b);
b ^= a;
PR("b ^= a ;b =  ",b);

return 0;
}

void printBinary(const unsigned char val)
{
for(int i = 7;i >= 0;--i)
{
if(val & (1<<i))
{
cout<<"1";
}
else
{
cout<<"0";
}
}
}
unsigned char rol(unsigned char val)
{
int highbit;
if(val & 0x80)
{
highbit = 1;
}
else
{
highbit = 0;
}
val<<=1;
val |= highbit;

return val;
}

unsigned char ror(unsigned char val)
{
int lowbit;
if(val & 1)
{
lowbit = 1;
}
else
{
lowbit = 0;
}
val>>=1;
val |= (lowbit<<7);

return val;
}</span>




<span style="font-size:18px;">#include <iostream>
using namespace std;

int main()
{
int i;
cout<<"type a number and 'Enter'"<<endl;
cin>>i;
cout<<((i>5)?"It's greater than 5":((i<5)?"It's less than 5"
:"It's equal to 5"))<<endl;;
cout<<"type a number and 'Enter'"<<endl;
cin>>i;
cout<<((i<10)?((i>5)?"5<i<10":"i<=5"):"i>=10")<<endl;;

return 0;
}</span>




<span style="font-size:18px;"><span style="font-size:18px;">#include <iostream>
#include <string>
using namespace std;

typedef struct
{
string last;
string first;
int age;
} Person;

int main()
{
using namespace std;
Person p;
p.last = "Einstein";
p.first = "Albert";
p.age = 122;
cout << p.last << ',' << p.first << ',' << p.age << endl;

Person* pptr = &p;
pptr->last = "Alger";
pptr->first = "Horatio";
pptr->age = 167;
cout << pptr->last << ',' << pptr->first << ',' << pptr->age
<< endl;
}</span></span>




<span style="font-size:18px;"><span style="font-size:18px;">#include <iostream>
using namespace std;

enum color
{
BLACK,
RED,
GREEN,
BLUE,
WHITE
};

int main()
{
for (int hue = BLACK; hue <= WHITE; ++hue)
{
cout << hue << ' ';
}
cout<<endl;

return 0;
}</span></span>




<span style="font-size:18px;">#include <iostream>
using namespace std;

union Packed
{
char i;
short j;
int k;
long l;
float f;
double d;
};

int main()
{
cout<<"sizeof(Packed) = "
<<sizeof(Packed)<<endl;
Packed x;
x.i = 'c';
cout<<x.i<<endl;
x.d = 3.14159;
cout<<x.d<<endl;
cout<<x.i<<endl;

return 0;
}</span>


输出结果:


将double d删除后输出结果:


从中可得到union开辟空间大小取决于元素最大值,对其一个元素赋值,其余值会输出无用的信息。



<span style="font-size:18px;"><span style="font-size:18px;">#include <iostream>
using namespace std;

void print(char* name, int* array, int size)
{
for(int i = 0; i < size; i++)
cout << name << "[" << i << "] ("
<< (long)(&array[i]) << ") = "
<< array[i] << endl;
}

// A preprocessor macro to simplify the printing
// of all the data in main():
#define PRT(A, B, C, D) \
print(#A, A, sizeof A / sizeof *A); \
print(#B, B, sizeof B / sizeof *B); \
cout << #C " (" << (long)(&C) << ") = " \
<< C << endl; \
print(#D, D, sizeof D / sizeof *D);

int main()
{
int a[] = { 1, 2, 3 };
int b[] = { 4, 5, 6 };
char c = 'x';
int d[] = { 7, 8, 9 };
PRT(a, b, c, d);
cout << "Index off the end of a:\n";
a[3] = 47;
PRT(a, b, c, d);
cout << "Index off the end of b:\n";
b[3] = 27;
PRT(a, b, c, d);
cout << "Abuse c with pointers and casts:\n";
*((double*)&c) = 99.99;
PRT(a, b, c, d);

return 0;
}</span></span>




<span style="font-size:18px;">#include <iostream>
using namespace std;

int main()
{
int a[10];
cout<<"sizeof(int) = "<<sizeof(int)<<endl;
for(int i = 0;i < 10;i++)
{
cout<<"&a["<<i<<"]="<<(long)&a[i]<<endl;
}
char b[10];
cout<<"sizeof(char) = "<<sizeof(char)<<endl;
for(i = 0;i < 10;i++)
{
cout<<"&b["<<i<<"]="<<(long)&b[i]<<endl;
}
float c[10];
cout<<"sizeof(float) = "<<sizeof(float)<<endl;
for(i = 0;i < 10;i++)
{
cout<<"&c["<<i<<"]="<<(long)&c[i]<<endl;
}
double d[10];
cout<<"sizeof(double) = "<<sizeof(double)<<endl;
for(i = 0;i < 10;i++)
{
cout<<"&d["<<i<<"]="<<(long)&d[i]<<endl;
}

return 0;
}</span>


也可以使用模板。



<span style="font-size:18px;">#include <iostream>
using namespace std;

typedef struct
{
int i,j,k;
}ThreeDpoint;

int main()
{
ThreeDpoint p[10];
cout<<"sizeof(ThreeDpoint) = "<<sizeof(ThreeDpoint)<<endl;
for(int i = 0;i < 10;i++)
{
cout<<"&p["<<i<<"]="<<(long)&p[i]<<endl;
}

return 0;
}</span>




<span style="font-size:18px;"><span style="font-size:18px;">#include <iostream>
#include <string>
using namespace std;

int main()
{
string stringArray[] = {"one", "small", "step",
"for", "man"};
const int nStrings = sizeof(stringArray)/sizeof(stringArray[0]);
for (int i = 0; i < nStrings; ++i)
{
cout << stringArray[i] << endl;
}

return 0;
}</span></span>




<span style="font-size:18px;">#include <iostream>
#include <cstdlib>
using namespace std;

int main(int argc, char* argv[])
{
for(int  i = 1;i < argc;i++)
{
cout<<atol(argv[i])<<endl;
}

return 0;
}</span>


<span style="font-size:18px;">#include <iostream>
#include <cstdlib>
using namespace std;

int main(int argc, char* argv[])
{
for(int  i = 1;i < argc;i++)
{
cout<<atof(argv[i])<<endl;
}

return 0;
}</span>




<span style="font-size:18px;"><span style="font-size:18px;">#include <iostream>
using namespace std;

typedef union
{
char c;
short s;
int i;
long l;
float f;
double d;
long double ld;
} Primitives;

int main()
{
Primitives p[10];
Primitives* pp = p;
cout << "sizeof(Primitives) = "
<< sizeof(Primitives) << endl;
cout << "pp = " << (long)pp << endl;
pp++;
cout << "pp = " << (long)pp << endl;
cout << "sizeof long double = " << sizeof(long double) << endl;
} </span></span>




long:

<span style="font-size:18px;">#include <iostream>
using namespace std;

#define P(EX) cout<<#EX<<":"<<EX<<endl;

int main()
{
long a[10];
for(int i = 0;i < 10;i++)
{
a[i] = i;
}
long* ip = a;
P(*ip);
P(*++ip);
P(*(ip+5));
long* ip2 = ip+5;
P(*ip2);
P(*(ip2-4));
P(*--ip2);
P(ip2-ip);

return 0;
}</span>


运行结果如下:


long double:

#include <iostream>
using namespace std;

#define P(EX) cout<<#EX<<":"<<EX<<endl;

int main()
{
long double a[10];
for(int i = 0;i < 10;i++)
{
a[i] = i;
}
long double* ip = a;
P(*ip);
P(*++ip);
P(*(ip+5));
long double* ip2 = ip+5;
P(*ip2);
P(*(ip2-4));
P(*--ip2);
P(ip2-ip);

return 0;
}


运行结果如下:


发现结果相同。



方法一:

<span style="font-size:18px;"><span style="font-size:18px;">#include <iostream>
using namespace std;

void printBinary(unsigned char);

int main()
{
float x = 128.0;
unsigned char* p = reinterpret_cast<unsigned char*>(&x);
for (int i = 0; i < sizeof(float); ++i)
{
printBinary(p[i]);
}
cout << endl;

return 0;
}</span></span>


方法二:

<span style="font-size:18px;"><span style="font-size:18px;">#include <iostream>
#include <cstddef> // For size_t
using namespace std;

void printBinary(unsigned char);

void printBytes(const void *p, size_t n)
{
const unsigned char* pByte =
reinterpret_cast<const unsigned char*>(p);
for (size_t i = 0; i < n; ++i)
{
printBinary(pByte[i]);
}
cout << endl;
}

int main()
{
float x = 128.0;
printBytes(&x, sizeof x);

return 0;
}</span></span>




此处有问题,仅供参考

<span style="font-size:18px;">#include <iostream>
using namespace std;

void func(void* p, int number, int val)
{
int* s = (int *)p;
static int i = 0;
if(i<number)
{
s[i] = val;
}
}
int main()
{
int a[10];
int i;
i = static_cast<int>(a[0]);
void *ip = &i;
for(int j = 0;j < sizeof(a)/sizeof(a[0]);++j)
{
func(ip,sizeof(a)/sizeof(a[0]),a[i]);
}

return 0;
}</span>




仅供参考

<span style="font-size:18px;">#include <iostream>
using namespace std;

int main()
{
const double i[10] = {1,2,3,4,5,6,7,8,9,0};
double* j[10] = {0};
for(int m = 0;m < 10;++m)
{
j[m] = const_cast<double*>(&i[m]);
}
volatile double k[10] = {0,1,2,3,4,5,6,7,8,9};
double* u[10] = {0};
for(m = 0;m < 10;++m)
{
u[m] = const_cast<double*>(&k[m]);
}

return 0;
}</span>




仅供参考

<span style="font-size:18px;">#include <iostream>
using namespace std;

void fun(double *s, int size)
{
for(int i = 0;i < size;++i)
{
cout<<s[i]<<" ";
}
cout<<endl;
}

int main()
{
double d[10] = {0};
fun(d,sizeof(d)/sizeof(d[0]));
unsigned char* df = reinterpret_cast<unsigned char*>(&d[0]);
for(int i = 0;i < sizeof(d)/sizeof(d[0]);++i)
{
df[i] = 1;
}
fun((double*)df,sizeof(df)/sizeof(df[0]));
return 0;
}</span>




<span style="font-size:18px;"><span style="font-size:18px;">#include <iostream>
#include <climits>
using namespace std;

void ieeePrint(float x)
{
unsigned char* p = reinterpret_cast<unsigned char*>(&x);
int bitno = 0;

for (int i = sizeof(float)-1; i >= 0; --i)
{
for (int j = CHAR_BIT-1; j >= 0; --j, ++bitno)
{
cout << !!(p[i] & (1 << j));
if (bitno == 0 || bitno == 8)  // IEEE boundaries
{
cout << ' ';
}
}
}
cout << endl;
}

int main()
{
ieeePrint(2.0);
ieeePrint(6.5);
ieeePrint(-6.5);

return 0;
}</span></span>




仅供参考
CPP = mycompiler
.SUFFIXES: .exe .cpp
.cpp .exe:
$(CPP) $<
YourPets1.exe:
YourPets2.exe:



仅供参考

<span style="font-size:18px;">#include <iostream>
using namespace std;

#define P(A) cout<< #A <<": "<< (A) <<endl;

#ifdef P(A)

int main()
{
int a = 1,b = 2,c = 3;
P(a);P(b);P(c);
P(a+b);
P((c-a)/b);

return 0;
}
#endif</span>




<span style="font-size:18px;"><span style="font-size:18px;">#include <iostream>

int round(double x)
{
// round to nearest int:
return static_cast<int>(x + 0.5);
}

int main()
{
using namespace std;
int (*fp)(double) = round;
cout << fp(2.5) << endl;   // 3

return 0;
}</span></span>




float (*(*fp1)(int))(char);



FunctionTable.cpp源程序

<span style="font-size:18px;">#include <iostream>
using namespace std;

#define DF(N) void N(){\
cout<<"function" #N "called..."<<endl;}

DF(a);DF(b);DF(c);DF(d);DF(e);DF(f);DF(g);

void (*func_table[])() = {a,b,c,d,e,f,g};

int main()
{
while(1)
{
cout<<"press a key from 'a' to 'g'"
"or q to quit"<<endl;
char c,cr;
cin.get(c);
cin.get(cr);
if(c == 'q')
{
break;
}
if(c < 'a' || c > 'g')
{
continue;
}
(*func_table[c - 'a'])();
}

return 0;
}</span>


此处有问题,希望大家解答。



<span style="font-size:18px;"><span style="font-size:18px;">#include<iostream>
using namespace std;

int main()
{
int num1, num2, product, startDigit[4],
productDigit[4], count = 0, vampCount = 0,
x, y;
for(num1 = 10; num1 <= 99; num1++)
{
for(num2 = 10; num2 <= 99; num2++)
{
product = num1 * num2;
startDigit[0] = num1 / 10;
startDigit[1] = num1 % 10;
startDigit[2] = num2 / 10;
startDigit[3] = num2 % 10;
productDigit[0] = product / 1000;
productDigit[1] = (product % 1000) / 100;
productDigit[2] = product % 1000 % 100/10;
productDigit[3] = product % 1000 % 100%10;
count = 0;
for(x = 0; x < 4; x++)
{
for(y = 0; y < 4; y++)
{
if (productDigit[x] == startDigit[y])
{
count++;
productDigit[x] = -1;
startDigit[y] = -2;
if (count == 4)
{
vampCount++;
if (vampCount < 10)
{
cout << "Vampire number  "
<< vampCount << " is "
<< product << "  " << num1
<< num2 << endl;
}
else
{
cout << "Vampire number "
<< vampCount << " is "
<< product << "  " << num1
<< num2 << endl;
}
}
}
}
}
}
}
return 0;
}</span></span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: