一.总结本章的内容:
1.如果声明指针是void* ,它意味着任何类型的地址都可以间接引用那个指针(而如果声明int*,则只能对int型变量的地址间接引用那个指针)。一旦我们间接引用一个void*,就会丢失关于类型的信息。这意味着在使用前,必须转换为正确的类型。
2.Static变量使得局部变量的值在程序的整个生命期里仍然存在,其优点是在函数范围之外它是不可用的,所以不可能被轻易改变。
3.内部链接意味着只对正在编译的文件创建空间,外部连接意味着所有被编译过的文件创建一片单独的存储空间。
4.显示转换语法:
(1).static_cast:用于“良性”和“适度良性”转换,包括不用强制转换(例如自动类型转换);
(2).const_cast:对“const”和/或“volatile”进行转换;
(3).reinterpret_cast:转换为完全不同的意思。为了安全使用它,关键必须转换回原来的类型。转换成的类型一般只能用于位操作,否则就是为了其他隐秘的目的。这是所有转换中最危险的。
(4).dynamic_cast:用于类型安全的向下转换。
5.asm关键字:是一种转义机制,允许在C++程序中写汇编代码。在汇编程序代码中经常可以引用C++的变量,这意味着可以方便的和C++代码通信,且限定汇编代码只能用于必要的高效调整,或使用特殊的处理器指令。
6.想给程序传递命令行参数时,C和C++的函数main()有特殊的参数表,其形式如:int main(int argc, char* argv[]);
argv[0]是程序本身的路径和名字。
7.复杂变量和函数定义:
(1).void *(*(*fp1)(int))[10];
*fp1是一个指向函数的指针,该函数接受一个整型参数并返回一个指向含有10个void指针数组的指针。
(2).float (*(*fp2)(int,int,float))(int);
*fp2是一个指向函数的指针,该函数接收三个参数(int、int和float)且返回一个指向函数的指针,该函数接受一个整型参数并返回一个float。
(3).typedef double (*(*(fp3)())[10])();
fp3是一个指向函数的指针,该函数无参数,且返回一个含有10个指向函数指针数组的指针,这些函数不接受参数且返回double值。
(4).int (*(*f4())[10])();
f4是一个返回指针的函数,该指针指向含有10个函数指针的数组,这些函数返回整型值。
二.和习题有关的函数:
Ifthen.cpp
#include <iostream>
using namespace std;
int main()
{
int i;
cout<<"type a number and 'Enter'"<<endl;
cin>>i;
if(i>5)
{
cout<<"It's greater than 5"<<endl;
}
else
{
if(i<5)
{
cout<<"It's less than 5"<<endl;
}
else
{
cout<<"It's equal to 5"<<endl;
}
}
cout<<"type a number and 'Enter'"<<endl;
cin>>i;
if(i<10)
{
if(i>5)
{
cout<<"5<i<10"<<endl;
}
else
{
cout<<"i<=5"<<endl;
}
}
else
{
cout<<"i>=10"<<endl;
}
return 0;
}Menu.cpp
#include <iostream>
using namespace std;
int main()
{
char c;
while(true)
{
cout<<"MAIN MENU:"<<endl;
cout<<"l:left,r:right,q:quit->";
cin>>c;
if(c == 'q')
{
break;
}
if(c == 'l')
{
cout<<"LEFT MENU:"<<endl;
cout<<"select a or b:";
cin>>c;
if(c == 'a')
{
cout<<"you choose 'a'"<<endl;
continue;
}
if(c == 'b')
{
cout<<"you choose 'b'"<<endl;
continue;
}
else
{
cout<<"you don't choose a or b"<<endl;
continue;
}
}
if(c == 'r')
{
cout<<"RIGHT MENU:"<<endl;
cout<<"select c or d:";
cin>>c;
if(c == 'c')
{
cout<<"you choose 'c'"<<endl;
continue;
}
if(c == 'd')
{
cout<<"you choose 'd'"<<endl;
continue;
}
else
{
cout<<"you don't choose c or d"<<endl;
continue;
}
}
cout<<"you must type l or r or q!"<<endl;
}
cout<<"quitting menu..."<<endl;
return 0;
}YourPets1.cpp
#include <iostream>
using namespace std;
int dog,cat,bird,fish;
void f(int pet)
{
cout<<"pet id number:"<<pet<<endl;
}
int main()
{
int i,j,k;
}YourPet2.cpp
#include <iostream>
using namespace std;
int dog,cat,bird,fish;
void f(int pet)
{
cout<<"pet id number:"<<pet<<endl;
}
int main()
{
int i,j,k;
cout<<"f():"<<(long)&f<<endl;
cout<<"dog:"<<(long)&dog<<endl;
cout<<"cat:"<<(long)&cat<<endl;
cout<<"bird:"<<(long)&bird<<endl;
cout<<"fish:"<<(long)&fish<<endl;
cout<<"i:"<<(long)&i<<endl;
cout<<"j:"<<(long)&j<<endl;
cout<<"k:"<<(long)&k<<endl;
return 0;
} Static.cpp
#include <iostream>
using namespace std;
void func()
{
static int i = 0;
cout<<"i = "<<++i<<endl;
}
int main()
{
for(int x = 0;x < 10;x++)
{
func();
}
return 0;
}FileStatic.cpp
#include <iostream>
using namespace std;
static int fs;
int main()
{
fs = 1;
}FileStatic2.cpp
#include <iostream>
using namespace std;
extern int fs;
int main()
{
fs = 100;
}Boolean.cpp
#include <iostream>
using namespace std;
int main()
{
int i,j;
cout<<"Enter an integer:";
cin>>i;
cout<<"Enter another integer:";
cin>>j;
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!=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)&&(j<10))<<endl;
return 0;
}Bitwise.cpp
#include <iostream>
using namespace std;
#define PR(STR,EXPR) cout<<STR; printBinary(EXPR); cout<<endl;
int main()
{
unsigned int getval;
unsigned char a,b;
cout<<"Enter a number between 0 and 255:"<<endl;
cin>>getval;
a = getval;
PR("a in binary:",a);
cout<<"Enter a number between 0 and 255:"<<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;
}#include <iostream>
using namespace std;
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;
}Union.cpp
#include <iostream>
using namespace std;
union Packed
{
char i;
short j;
int k;
long l;
float f;
double d;
};
int main()
{
cout<<"aizeof(Packed) = "
<<sizeof(Packed)<<endl;
Packed x;
x.i = 'c';
cout<<x.i<<endl;
x.d = 3.14159;
cout<<x.d<<endl;
return 0;
}StructArray.cpp
#include <iostream>
using namespace std;
typedef struct
{
int i,j,k;
}ThreeDpoint;
int main()
{
ThreeDpoint p[10];
for(int i = 0;i < 10;i++)
{
p[i].i = i+1;
p[i].j = i+2;
p[i].k = i+3;
}
return 0;
}ArrayAddresses.cpp
#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;
}
return 0;
}ArgsToInts.cpp
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[])
{
for(int i = 1;i < argc;i++)
{
cout<<atoi(argv[i])<<endl;
}
}PointerIncrement2.cpp
#include <iostream>
using namespace std;
typedef struct
{
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;
return 0;
}PointerArithmetic.cpp
#include <iostream>
using namespace std;
#define P(EX) cout<<#EX<<":"<<EX<<endl;
int main()
{
int a[10];
for(int i = 0;i < 10;i++)
{
a[i] = i;
}
int* ip = a;
P(*ip);
P(*++ip);
P(*(ip+5));
int* ip2 = ip+5;
P(*ip2);
P(*(ip2-4));
P(*--ip2);
P(ip2-ip);
return 0;
}三.习题及答案
//: S03:Prototypes.h
// Declares various functions
void f(int);
int g(float);
float h(char);
char k(void); // same as char k()
///:~
//: S03:Prototypes.cpp {O}
// Implements functions declared in Prototypes.h
#include <iostream>
using namespace std;
void f(int i) {
cout << "f(" << i << ") returning void\n";
}
int g(float x) {
cout << "g(" << x << ") returning int\n";
return 0;
}
float h(char c) {
cout << "h(" << c << ") returning float\n";
return 1.5;
}
char k(void) { // same as char k()
cout << "k() returning char\n";
return 'a';
}#include "Prototypes.h"
int main() {
f(1);
g(1.5);
h('c');
k();
}
方法一:
#include <iostream>
#include <cmath> // for sqrt()
using namespace std;
int main() {
const int MAX = 100;
// Print 2 as a prime:
cout << "2 ";
for (int i = 3; i <= MAX; i += 2)
{
float val = i; // Produce float value
int mid = static_cast<int>(sqrt(val));
int j;
for (j = 3; j <= mid; j += 2)
{
if (i % j == 0)
{
break;
}
}
if (j > mid)
{
cout << i << ' ';
}
}
cout << endl;
return 0;
}#include <iostream>
#include <cmath> // for sqrt()
using namespace std;
#define MAX 100
int main()
{
cout<<"2";
for (int i = 3; i < MAX+1; i+=2)
{
for (int j = 3; j <= sqrt(i); j += 2)
{
if (i % j == 0)
{
break;
}
}
if (j > sqrt(i))
{
cout << i << ' ';
}
}
cout << endl;
return 0;
}
#include <iostream>
#include <string>
#include <cstdio>
int main() {
using namespace std;
string word;
for (;;) {
int code;
cin >> word;
if (word == "exit" | word == "return")
break;
// Map words:
if (word == "a" || word == "an" || word == "the")
code = 0;
else if (word == "after" || word == "before" ||
word == "beside" || word == "by" ||
word == "for" || word == "from" ||
word == "in" || word == "into" ||
word == "of" || word == "to")
code = 1;
else if (word == "if" || word == "else")
code = 2;
else if (word == "who" || word == "what" ||
word == "when" || word == "where" ||
word == "why")
code = 3;
else
code = 4;
// Print code description:
switch (code) {
case 0:
puts("article");
break;
case 1:
puts("preposition");
break;
case 2:
puts("conditional");
break;
case 3:
puts ("interrogative");
break;
default:
puts("unmapped word");
break;
}
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
char c;
while(true)
{
cout<<"MAIN MENU:"<<endl;
cout<<"l:left,r:right,q:quit->";
cin>>c;
switch(c)
{
case 'q':
{
break;
}
case 'l':
{
cout<<"LEFT MENU:"<<endl;
cout<<"select a or b:";
cin>>c;
switch(c)
{
case 'a':
{
cout<<"you choose 'a'"<<endl;
continue;
}
case 'b':
{
cout<<"you choose 'b'"<<endl;
continue;
}
default:
{
cout<<"you don't choose a or b"<<endl;
continue;
}
}
}
case 'r':
{
cout<<"RIGHT MENU:"<<endl;
cout<<"select c or d:";
cin>>c;
switch(c)
{
case 'c':
{
cout<<"you choose 'c'"<<endl;
continue;
}
case'd':
{
cout<<"you choose 'd'"<<endl;
continue;
}
default:
{
cout<<"you don't choose c or d"<<endl;
continue;
}
}
}
}
cout<<"you must type l or r or q!"<<endl;
}
cout<<"quitting menu..."<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int X=1,Y=2,Z=3;
cout<<"A = "<<X+Y-2/2+Z<<endl;
cout<<"A = "<<X+(Y-2)/(2+Z)<<endl;
return 0;
}
#include <iostream>
using namespace std;
int dog;
char cat;
float bird;
double fish;
void f(int pet)
{
cout<<"pet id number:"<<pet<<endl;
}
int main()
{
int i,j,k;
cout<<"f():"<<(long)&f<<endl;
cout<<"dog:"<<(long)&dog<<endl;
cout<<"cat:"<<(long)&cat<<endl;
cout<<"bird:"<<(long)&bird<<endl;
cout<<"fish:"<<(long)&fish<<endl;
cout<<"i:"<<(long)&i<<endl;
cout<<"j:"<<(long)&j<<endl;
cout<<"k:"<<(long)&k<<endl;
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
《C++编程思想》(第二版)第2章 C++中的C(笔记、习题及答案)(一)
原文地址:http://blog.csdn.net/qaz3171210/article/details/47048073