C实例代码附有解释.doc

上传人:自*** 文档编号:126236141 上传时间:2020-03-23 格式:DOC 页数:32 大小:84KB
返回 下载 相关 举报
C实例代码附有解释.doc_第1页
第1页 / 共32页
C实例代码附有解释.doc_第2页
第2页 / 共32页
C实例代码附有解释.doc_第3页
第3页 / 共32页
C实例代码附有解释.doc_第4页
第4页 / 共32页
C实例代码附有解释.doc_第5页
第5页 / 共32页
点击查看更多>>
资源描述

《C实例代码附有解释.doc》由会员分享,可在线阅读,更多相关《C实例代码附有解释.doc(32页珍藏版)》请在金锄头文库上搜索。

1、 第一题 #include class Stack /定义堆栈类 struct Node int content; Node *next; *top;public: Stack() top = NULL; / 构造函数的定义 bool push(int i); / 压栈成员函数的声明 bool pop(int& i); / 弹栈成员函数的声明;bool Stack:push(int i) / 压栈成员函数的定义 Node *p=new Node; if (p = NULL) cout content = i; p-next = top; top = p; return true;bool St

2、ack:pop(int& i) / 弹栈成员函数的定义if (top = NULL) cout next; i = p-content; delete p; return true;void main()Stack st1,st2; / 定义对象st1和st2 int x;for(int i=1;i=5;i+)st1.push(i); / 压栈成员函数的调用st2.push(i); / 压栈成员函数的调用 coutstack1:endl;for(i=1;i=3;i+)st1.pop(x); / 弹栈成员函数的调用coutxendl;st1.push(20);for(i=1;i=4;i+)if(

3、st1.pop(x)coutxendl;elsebreak;coutstack2:endl;while(st2.pop(x)coutxendl;第二题#include / 使用名空间std,则必须去掉.h扩展名using namespace std; void main() char name10; int age; coutname; coutage; coutname is nameendl; coutage is ageendl;第三题#includeusing namespace std;void main() struct studentint no;float math;int n

4、;cinn;student wang; / C+中变量的定义语句可以出现在程序的任意位置;可以使用结构体名定义变量wang.no=n;cinwang.math;coutwang.no wang.mathendl;第四题#include using namespace std;void main()int *p;p=new int; / 分配内存空间*p=5;cout*p;delete p; / 释放内存空间第五题#include using namespace std;void main() int n; / 定义数组元素的个数 int *p; coutn; if(p=new intn)=0)

5、 cout cant allocate more memory,terminating. endl;exit(1); / 分配内存空间 for( int i=0;in;i+) pi= i *2; coutNow output the array : endl; for( i=0;in;i+) coutpi ; coutendl; delete p; / 释放内存空间第六题#include void main()int num=50;int &ref=num;ref+=10;coutnum=numendl;cout ref=refendl;num+=40;cout num=numendl;cou

6、tref=refendl;第七题#include void main()int num;int & ref=num;num=5; coutnum=numendl;coutref=refendl;cout&num=&numendl;cout &ref=&refendl;第八题#include using namespace std;void swap(int &x,int &y);void main() int x=5, y=6; cout before swap, x: x ,y: y endl; swap(x,y); coutafter swap,x:x ,y:y;void swap(int

7、 &rx,int &ry) / 引用作为形参 int temp=rx; rx=ry; ry=temp;第九题#include using namespace std;int array64=60,80,90,75, 75,85,65,77, 80,88,90,98, 89,100,78,81, 62,68,69,75, 85,85,77,91;int& level(int grade, int size, int& tA, int& tB);void main() int typeA=0,typeB=0; int student=6; int gradesize=4; for(int i=0;

8、 istudent; i+) / 对所有的学生数据进行处理 level(arrayi, gradesize, typeA, typeB)+; / 函数调用作为左值 cout number of type A is typeA endl; cout number of type B is typeB endl;int& level(int grade, int size,int& tA, int& tB) int sum=0; for(int i=0; i=80) return tA; / A类学生 else return tB; / B类学生第十题#include using namespac

9、e std;float& fn2(float r) float temp; temp=r*r*3.14; return temp; / 返回了局部变量void main() fn2(5.0)=12.4; / 返回的是局部作用域内的变量,函数调用作为左值使用。此种情况应尽量避免。第十题#include #include / 使用字符串string类型的程序应包含头文件using namespace std;void main()string s,t;cout请输入一个字符串:s; / 由键盘输入一行文本,并把它赋给sring类型的变量s,注意: / 使用此方式输入的字符串中不能包含空白字符,具体请参见7.1.2小节t=I like programming!;cout字符串的输出:endlsendltendl;couts.append( OK!)endl; / append为string类的成员函数第十一题#include using namespace std;inline double circumference(double radius);/ 内联函数的声明,如果此处省略inline关键字,即使在函

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号