watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

<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>

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

<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。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

虽然在FileStatic.cpp中fs声明为extern,可是连接器不会找到它,由于在FileStatic.cpp中它被声明为static。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

<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>

因为编译器不兼容,将显示运算符列举例如以下:

keyword 含义
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>

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

<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>

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

<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;
}

执行结果例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

发现结果同样。

方法一:

<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>

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

此处有问题,仅供參考

<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>

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

仅供參考

<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>

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

<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>

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

  1. C++学习书籍推荐《C++编程思想第二版第二卷》下载

    百度云及其他网盘下载地址:点我 编辑推荐 “经典原版书库”是响应教育部提出的使用原版国外教材的号召,为国内高校的计算机教学度身订造的.<C++编程思想>(英文版第2版)是书库中的一本,在广 ...

  2. C++学习书籍推荐《C++编程思想第二版第一卷》下载

    百度云及其他网盘下载地址:点我 编辑推荐 “经典原版书库”是响应教育部提出的使用原版国外教材的号召,为国内高校的计算机教学度身订造的.<C++编程思想>(英文版第2版)是书库中的一本,在广 ...

  3. 《Python核心编程》第二版第五章答案

    本人python新手,答案自己做的,如果有问题,欢迎大家评论和讨论! 更新会在本随笔中直接更新. 5-1.整型.讲讲Python普通整型和长整型的区别. Python的标准整形类型是最通用的数字类型. ...

  4. 《Python核心编程》第二版第三章答案

    本人python新手,答案自己做的,如果有问题,欢迎大家评论和讨论! 更新会在本随笔中直接更新. 我在Windows使用python版本是2.7.0 3–10. 异常.使用类似readTextFile ...

  5. Learning ROS for Robotics Programming - Second Edition(《ROS机器人编程学习-第二版》)

    Learning ROS for Robotics Programming - Second Edition <ROS机器人编程学习-第二版> ----Your one-stop guid ...

  6. c++学习书籍推荐《C++编程思想第二卷》下载

    百度云及其他网盘下载地址:点我 编辑推荐 <C++编程思想>(第2卷)是惟一一本如此清晰地阐述如何重新思考以面向对象方法构造程序的书籍.<C++编程思想>(第2卷)介绍实用的编 ...

  7. 【转】apue《UNIX环境高级编程第三版》第一章答案详解

    原文网址:http://blog.csdn.net/hubbybob1/article/details/40859835 大家好,从这周开始学习apue<UNIX环境高级编程第三版>,在此 ...

  8. CSAPP深入理解计算机系统(第二版)第三章家庭作业答案

    <深入理解计算机系统(第二版)>CSAPP 第三章 家庭作业 这一章介绍了AT&T的汇编指令 比较重要 本人完成了<深入理解计算机系统(第二版)>(以下简称CSAPP) ...

  9. 【转】编程思想之多线程与多进程(3)——Java中的多线程

    <编程思想之多线程与多进程(1)——以操作系统的角度述说线程与进程>一文详细讲述了线程.进程的关系及在操作系统中的表现,这是多线程学习必须了解的基础.本文将接着讲一下Java中多线程程序的 ...

  10. 《Java并发编程的艺术》 第9章 Java中的线程池

    第9章 Java中的线程池 在开发过程中,合理地使用线程池能带来3个好处: 降低资源消耗.通过重复利用已创建的线程 降低线程创建和销毁造成的消耗. 提高响应速度.当任务到达时,任务可以不需要等到线程创 ...

随机推荐

  1. python基础之数据类型之数字、字符串、列表

    数据类型及内置方法 一.数字类型 整数型(int) 1.用途:年龄,号码等 2.定义:age = 10   age = int(10) x = int(’11’)   int只能转换纯数字的字符串 3 ...

  2. Java并发(四):happens-before

    happens-before 一个操作执行的结果需要对另一个操作可见,那么这两个操作之间必须存在happens-before关系 happen-before原则是JMM中非常重要的原则,它是判断数据是 ...

  3. [典型漏洞分享]YS的防暴力破解设计存在缺陷

    YS使用的防暴力破解机制存在缺陷,该缺陷可被用于暴力破解其它用户密码[高] 问题描述: YS在用户登录页面设置了验证码机制,当用户输入密码错误次数达到3次时,再次登录需要验证码以防止攻击者进行暴力破解 ...

  4. [EF]使用EF简单增删改查

    目录 认识EF 添加数据 删除数据 修改数据 查询数据 总结 认识EF ADO.NET Entity Framework 是微软以ADO.NET为基础所发展出来的对象关系对伊(O/R Mapping) ...

  5. pcap报文格式

    pcap报文格式 pcap报文整体格式 pcap 报文头格式 pcap报文格式,黄色部分为报文头 pcapng报文格式 PCAPNG: PCAP Next Generation Dump File F ...

  6. 对oracle实例的内存(SGA和PGA)进行调整,优化数据库性

    一.名词解释 (1)SGA:SystemGlobal Area是OracleInstance的基本组成部分,在实例启动时分配;系统全局域SGA主要由三部分构成:共享池.数据缓冲区.日志缓冲区. (2) ...

  7. java POI实现Excel单元格数据换行

    当我们通过POI设置了表格的列宽的时候,如果文字过长,希望文字能够自己折行显示. 截取代码如下: Workbook wb = new XSSFWorkbook(); //or new HSSFWork ...

  8. postgres--vacuum

    vacuum的功能 回收空间 数据库总是不断地在执行删除,更新等操作.良好的空间管理非常重要,能够对性能带来大幅提高. postgresql中执行delete操作后,表中的记录只是被标示为删除状态,并 ...

  9. Jpeglib读取jpg文件 【转】

    http://blog.csdn.net/blues1021/article/details/45424695 整理自 : http://hi.baidu.com/lewutian/item/e8ee ...

  10. jquery触发/失去焦点事件

    触发焦点: $("Element").focus() 触发每一个匹配元素获得焦点事件. $("Element").focus(function) 事件会在获得焦 ...