C++编程思想 第三章习题
2013-07-18 23:21
239 查看
3-1
myhead.h
C++ Code
myhead.cpp
C++ Code
main.cpp
C++ Code
3-2 素数
C++ Code
3-3
C++ Code
3-4
C++ Code
3-7
C++ Code
3-9
C++ Code
在fun1函数中 变量i修饰符写上static
为什么会出现这种情况呢,这个就是static 的妙用 ,在static定义的变量只初始化一次,而且生命周期是整个程序的生命周期,所以每次调用fun1 static相当于一个全局的计数器。但是局部变量 i 每次进入fun1都会初始化为0 ,++后变成1,输出1.
3-10
main.cpp
C++ Code
externfile.cpp
C++ Code
编译错误 ,1 unresolved externals
说明extern 可以访问外部变量但是不能访问外部静态变量,静态变量的访问只能是本文件内部访问。
3-14
C++ Code
3-15
C++ Code
3-16
C++ Code
3-17
C++ Code
联合体内所有元素共享一块内存,所以他的大小有占内存最大的类型决定。
32位系统double占8个字节, short 占2个字节,int占4个字节,float占四个字节,char占1个字节,long占4个字节。
2-20
C++ Code
3-21
C++ Code
myhead.h
C++ Code
1 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 |
C++ Code
1 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'; } |
main.cpp
C++ Code
1 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"); } |
3-2 素数
C++ Code
1 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"); } |
C++ Code
1 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"); } |
C++ Code
1 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"); } |
C++ Code
1 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"); } |
C++ Code
1 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"); } |
在fun1函数中 变量i修饰符写上static
为什么会出现这种情况呢,这个就是static 的妙用 ,在static定义的变量只初始化一次,而且生命周期是整个程序的生命周期,所以每次调用fun1 static相当于一个全局的计数器。但是局部变量 i 每次进入fun1都会初始化为0 ,++后变成1,输出1.
3-10
main.cpp
C++ Code
1 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"); } |
C++ Code
1 2 3 4 5 | extern int age ; void fun() { age =100; } |
说明extern 可以访问外部变量但是不能访问外部静态变量,静态变量的访问只能是本文件内部访问。
3-14
C++ Code
1 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"); } |
C++ Code
1 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"); } |
C++ Code
1 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"); } |
C++ Code
1 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"); } |
32位系统double占8个字节, short 占2个字节,int占4个字节,float占四个字节,char占1个字节,long占4个字节。
2-20
C++ Code
1 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"); } |
C++ Code
1 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"); } |
相关文章推荐
- C++编程思想 第四章数据抽象习题
- C++编程思想 第二章习题2_2
- C++编程思想 第五章隐藏实现 习题
- 基于结构;基于对象;面向对象;基于接口四种C++编程思想的实例分析
- Java第三章习题3-4(for循环输出俄文字母表)
- 编程珠玑:第三章 数据决定程序结构 习题解答
- 数据库原理 西安电子科技大学(第三版) 付婷婷 第三章 课后习题答案
- C++第三章习题
- 编程珠玑之第三章习题3
- 《APUE》第三章笔记(4)及习题3-2
- C++编程思想(卷二):防御性编程
- java2 实用教程 第三版 第三章 习题 3
- 【转】《APUE》第三章笔记(4)及习题3-2
- 【c++编程思想】第十章 名字控制
- java编程思想第四版习题_8.13
- 算法导论课后习题解析 第三章
- 谭浩强 《C程序设计》 第三版 第三章习题答案 (修正)
- 《C Primer Plus 》第六版 习题 第三章
- orange's习题——第三章【中断和异常】
- 鸟哥的linux私房菜基础学习篇课后习题与答案 第三章