C++编程思想 第三章习题
来源:互联网 发布:淘宝怎么截图返现 编辑:程序博客网 时间:2024/05/17 21:38
3-1
myhead.h
C++ Code
2
3
4
5
6
7
8
9
#ifndef MY_HEAD_H
#define MY_HEAD_H
#include <iostream>
using namespace std;;
int add(int a,int b);
int sub(int a,int b);
void inital(void);
char str(char a,char b);
#endif
#define MY_HEAD_H
#include <iostream>
using namespace std;;
int add(int a,int b);
int sub(int a,int b);
void inital(void);
char str(char a,char b);
#endif
myhead.cpp
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "myhead.h"
int add(int a,int b)
{
cout<<"add"<<endl;
return 0;
}
int sub(int a,int b)
{
cout<<"sub"<<endl;
return 0;
}
void inital(void)
{
cout<<"inital"<<endl;
}
char str(char a,char b)
{
cout<<"str"<<endl;
return 'a';
}
int add(int a,int b)
{
cout<<"add"<<endl;
return 0;
}
int sub(int a,int b)
{
cout<<"sub"<<endl;
return 0;
}
void inital(void)
{
cout<<"inital"<<endl;
}
char str(char a,char b)
{
cout<<"str"<<endl;
return 'a';
}
main.cpp
C++ Code
2
3
4
5
6
7
8
9
10
11
12
#include "myhead.h"
#include <iostream>
using namespace std ;
void main(void)
{
int a =1,b=3;
add(a,b);
inital();
str('a','b');
sub(a,b);
system("pause");
}
#include <iostream>
using namespace std ;
void main(void)
{
int a =1,b=3;
add(a,b);
inital();
str('a','b');
sub(a,b);
system("pause");
}
3-2 素数
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std ;
void main(void)
{
int i,j;
for(i =2 ;i<=1000;i++)
{
for(j=2;j<=i/2;j++)
{
if (i%j==0)
{
break;
}
}
if (j>i/2)
{
cout<<i<<" ";
}
}
system("pause");
}
using namespace std ;
void main(void)
{
int i,j;
for(i =2 ;i<=1000;i++)
{
for(j=2;j<=i/2;j++)
{
if (i%j==0)
{
break;
}
}
if (j>i/2)
{
cout<<i<<" ";
}
}
system("pause");
}
3-3
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
using namespace std ;
void main(void)
{
string str;
int status;
while (cin>>str)
{
if (str=="q")
{
break;
}
if (str=="love")
{
status = 0;
cout<<status<<endl;
}
if (str == "no")
{
status = 1;
}
if (str == "yes")
{
status = 2;
}
switch(status)
{
case 0:cout<<status<<endl;break;
case 1:cout<<status<<endl;break;
case 2:cout<<status<<endl;break;
default:cout<<"status is error!";
}
}
system("pause");
}
#include <string>
using namespace std ;
void main(void)
{
string str;
int status;
while (cin>>str)
{
if (str=="q")
{
break;
}
if (str=="love")
{
status = 0;
cout<<status<<endl;
}
if (str == "no")
{
status = 1;
}
if (str == "yes")
{
status = 2;
}
switch(status)
{
case 0:cout<<status<<endl;break;
case 1:cout<<status<<endl;break;
case 2:cout<<status<<endl;break;
default:cout<<"status is error!";
}
}
system("pause");
}
3-4
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
using namespace std ;
void main(void)
{
char c;
while (true)
{
cout<<"MAIN MENU"<<endl;
cout<<"1: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 chose 'a'"<<endl;
continue;
}
case 'b':
{
cout<<"you chose 'b'";
continue;
}
default:
{
cout<<"you didn't choose a or b!"
<<endl;
continue;
}
}
case 'r':
{
cout<<"RIGHT MENU:"<<endl;
cout<<"select c or d"<<endl;
cin >>c;
switch(c)
{
case 'c':
cout<<"you choose 'c'"<<endl;
continue;
case 'd':
cout<<"you chose 'd'"<<endl;
continue;
default:
cout<<"you did't choose c or d!"
<<endl;
continue;
}
}
}
}
cout<<"you must type l or r or q!";
}
cout<<"quiting MENU..."<<endl;
system("pause");
}
#include <string>
using namespace std ;
void main(void)
{
char c;
while (true)
{
cout<<"MAIN MENU"<<endl;
cout<<"1: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 chose 'a'"<<endl;
continue;
}
case 'b':
{
cout<<"you chose 'b'";
continue;
}
default:
{
cout<<"you didn't choose a or b!"
<<endl;
continue;
}
}
case 'r':
{
cout<<"RIGHT MENU:"<<endl;
cout<<"select c or d"<<endl;
cin >>c;
switch(c)
{
case 'c':
cout<<"you choose 'c'"<<endl;
continue;
case 'd':
cout<<"you chose 'd'"<<endl;
continue;
default:
cout<<"you did't choose c or d!"
<<endl;
continue;
}
}
}
}
cout<<"you must type l or r or q!";
}
cout<<"quiting MENU..."<<endl;
system("pause");
}
3-7
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std ;
void fun1(string* str)//指针改变外部变量
{
*str ="string* fun(string* str)";
}
void fun2(string& str)//引用改变外部变量
{
str= "string* fun(string& str)";
}
string mystr("I know about you's ...");
void main(void)
{
cout<<mystr<<endl;
fun1(&mystr);//指针实参
cout<<mystr<<endl;
fun2(mystr);//引用实参 函数调用时候,明显引用的形式更好。
cout<<mystr;
system("pause");
}
#include <string>
using namespace std ;
void fun1(string* str)//指针改变外部变量
{
*str ="string* fun(string* str)";
}
void fun2(string& str)//引用改变外部变量
{
str= "string* fun(string& str)";
}
string mystr("I know about you's ...");
void main(void)
{
cout<<mystr<<endl;
fun1(&mystr);//指针实参
cout<<mystr<<endl;
fun2(mystr);//引用实参 函数调用时候,明显引用的形式更好。
cout<<mystr;
system("pause");
}
3-9
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "myhead.h"
#include <iostream>
#include <string>
using namespace std ;
void fun1(void)
{
int i = 0;
cout<<"i = "<<++i<<endl;
}
void main(void)
{
for (int j = 0;j<10;j++)
{
fun1();
}
system("pause");
}
#include <iostream>
#include <string>
using namespace std ;
void fun1(void)
{
int i = 0;
cout<<"i = "<<++i<<endl;
}
void main(void)
{
for (int j = 0;j<10;j++)
{
fun1();
}
system("pause");
}
在fun1函数中 变量i修饰符写上static
为什么会出现这种情况呢,这个就是static 的妙用 ,在static定义的变量只初始化一次,而且生命周期是整个程序的生命周期,所以每次调用fun1 static相当于一个全局的计数器。但是局部变量 i 每次进入fun1都会初始化为0 ,++后变成1,输出1.
3-10
main.cpp
C++ Code
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
using namespace std ;
static int age;
void main(void)
{
age = 10;
system("pause");
}
#include <string>
using namespace std ;
static int age;
void main(void)
{
age = 10;
system("pause");
}
externfile.cpp
C++ Code
2
3
4
5
extern int age ;
void fun()
{
age =100;
}
void fun()
{
age =100;
}
编译错误 ,1 unresolved externals
说明extern 可以访问外部变量但是不能访问外部静态变量,静态变量的访问只能是本文件内部访问。
3-14
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int max(int a,int b)
{
int temp;
temp = a>b? a : b;
return temp;
}
void main()
{
cout<<max(1,3);
system("pause");
}
using namespace std;
int max(int a,int b)
{
int temp;
temp = a>b? a : b;
return temp;
}
void main()
{
cout<<max(1,3);
system("pause");
}
3-15
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
using namespace std;
typedef struct person
{
string name;
string sex;
int age;
}Person;
typedef Person* LinkPerson;
void main()
{
Person jack;
LinkPerson linkperson= &jack;
linkperson->name ="damon";
linkperson->sex = "men";
linkperson->age =16;
cout<<linkperson->name<<endl;
cout<<linkperson->sex<<endl;
cout<<linkperson->age<<endl;
system("pause");
}
#include <string>
using namespace std;
typedef struct person
{
string name;
string sex;
int age;
}Person;
typedef Person* LinkPerson;
void main()
{
Person jack;
LinkPerson linkperson= &jack;
linkperson->name ="damon";
linkperson->sex = "men";
linkperson->age =16;
cout<<linkperson->name<<endl;
cout<<linkperson->sex<<endl;
cout<<linkperson->age<<endl;
system("pause");
}
3-16
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
using namespace std;
void main()
{
enum color {red= 10,blue = 20,yello= 30,pink = 40,green= 50,orange= 60};
for (int i =0;i<70;i++)
{
if (i==red)
{
cout<<red<<endl;
}
else if(i==blue)
{
cout<<blue<<endl;
}
else if(i==yello)
{
cout<<yello<<endl;
}
else if(i==pink)
{
cout<<pink<<endl;
}
else if(i==green)
{
cout<<green<<endl;
}
else if(i==orange)
{
cout<<orange<<endl;
}
}
system("pause");
}
#include <string>
using namespace std;
void main()
{
enum color {red= 10,blue = 20,yello= 30,pink = 40,green= 50,orange= 60};
for (int i =0;i<70;i++)
{
if (i==red)
{
cout<<red<<endl;
}
else if(i==blue)
{
cout<<blue<<endl;
}
else if(i==yello)
{
cout<<yello<<endl;
}
else if(i==pink)
{
cout<<pink<<endl;
}
else if(i==green)
{
cout<<green<<endl;
}
else if(i==orange)
{
cout<<orange<<endl;
}
}
system("pause");
}
3-17
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;
void main()
{
union style
{
char a;
short f;
int b;
long g;
float c;
double d;
}Style;
system("pause");
}
#include <string>
using namespace std;
void main()
{
union style
{
char a;
short f;
int b;
long g;
float c;
double d;
}Style;
system("pause");
}
联合体内所有元素共享一块内存,所以他的大小有占内存最大的类型决定。
32位系统double占8个字节, short 占2个字节,int占4个字节,float占四个字节,char占1个字节,long占4个字节。
2-20
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <cstdio>
using namespace std ;
const int SIZE = 5;
typedef struct myNode
{
int i;
int j;
int k;
}MyNode;
int main(void)
{
MyNode num[SIZE]={0};
MyNode* pMyNode[SIZE]={0};
for (int i =0;i<SIZE;i++)
{
pMyNode[i] = &num[i];
num[i].i = i;
num[i].j = 2*i;
num[i].k = 3*i;
}
for (int j = 0;j<SIZE;j++)
{
cout<<pMyNode[j]->i;
cout<<pMyNode[j]->j;
cout<<pMyNode[j]->k;
cout<<endl<<j<<endl;
}
system("pause");
}
#include <string>
#include <cstdio>
using namespace std ;
const int SIZE = 5;
typedef struct myNode
{
int i;
int j;
int k;
}MyNode;
int main(void)
{
MyNode num[SIZE]={0};
MyNode* pMyNode[SIZE]={0};
for (int i =0;i<SIZE;i++)
{
pMyNode[i] = &num[i];
num[i].i = i;
num[i].j = 2*i;
num[i].k = 3*i;
}
for (int j = 0;j<SIZE;j++)
{
cout<<pMyNode[j]->i;
cout<<pMyNode[j]->j;
cout<<pMyNode[j]->k;
cout<<endl<<j<<endl;
}
system("pause");
}
3-21
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <cstdio>
using namespace std ;
int main(void)
{
string str("I love my mm");
for (int i =0;i<str.size();i++)
{
cout<<str[i];
if (str[i]==' ')
{
cout<<endl;
}
}
system("pause");
}
#include <string>
#include <cstdio>
using namespace std ;
int main(void)
{
string str("I love my mm");
for (int i =0;i<str.size();i++)
{
cout<<str[i];
if (str[i]==' ')
{
cout<<endl;
}
}
system("pause");
}
- C++编程思想 第三章习题
- JAVA编程思想第四版—第三章—习题与答案
- 《C++编程思想》第三章 隐藏实现 (原书代码+习题+解答)
- 编程珠玑第三章习题
- C++Primer第三章习题
- C++ 编程思想 第二章习题
- 《C++编程思想》第二章习题
- Java编程思想第7章习题
- Java 编程思想 习题
- 《OpenGL编程基础》第三章习题
- 编程珠玑第三章课后习题
- 《Python 核心编程》 第三章习题 3.10
- 编程珠玑第三章习题答案
- 编程珠玑之第三章习题1
- 编程珠玑之第三章习题2
- 编程珠玑之第三章习题3
- 编程珠玑之第三章习题4
- 编程珠玑之第三章习题5
- varnish 503 的配置(需改进)
- 自控力 笔记4 容忍罪恶:为何善行之后会有恶行
- CF 154 div2 B(dp)
- hdu 2191(多重背包)
- BTrace使用简介
- C++编程思想 第三章习题
- ACM心得——(写的很好,留着警示自己)
- 使用实时文件夹显示联系人信息
- btrace一些你不知道的事(源码入手)
- 空间后方交会
- Varnish Guru Meditation on timeout
- XLWB大数据实习生面试问题总结
- 网络监测以及判断
- 新人求罩