C++经典练习程序代码

上传人:woxinch****an2018 文档编号:38630881 上传时间:2018-05-05 格式:DOC 页数:59 大小:264.50KB
返回 下载 相关 举报
C++经典练习程序代码_第1页
第1页 / 共59页
C++经典练习程序代码_第2页
第2页 / 共59页
C++经典练习程序代码_第3页
第3页 / 共59页
C++经典练习程序代码_第4页
第4页 / 共59页
C++经典练习程序代码_第5页
第5页 / 共59页
点击查看更多>>
资源描述

《C++经典练习程序代码》由会员分享,可在线阅读,更多相关《C++经典练习程序代码(59页珍藏版)》请在金锄头文库上搜索。

1、#include main() /用 SIZEOF 计算常量长度 cout main()/测试表达式类型转换 int n=100,m; double x=3.791,y; cout main() int score; coutscore;if(score100) cout=90) cout=80) cout=70) cout=60) cout main() int x,y,z; char c1; cinxc1y; switch(c1) case +:coutmain() / for(int i=1;i=1;j-) coutmain() int s=0,n=0; do n+; s+=n; whi

2、le (nx; sum+=x; while (x!=0);coutmain() /打印乘法九九表 for(int i=1;i using namespace std; int main() int a23,i,j;/从键盘输入数组元素 for(i=0;iaij; /show array data for(i=0;i using namespace std; #define n 8 int main() /指针与引用的测试 int a,b,c,*ip; / a=100; ip= cout /#include /using namespace std; int main() / int i; st

3、ruct student int num; char name20; float maths; float physics; float chemistry; double total; ;student st3;coutsti.num; cinsti.name; cinsti.maths; cinsti.physics; cinsti.chemistry;for(i=0;i /#include /using namespace std; int main() / struct human char name10; int sex; int age; struct human x=“WangP

4、ing“,1,30,*p= coutname=“; cinp-name; coutsex=“; cinp-sex; coutage=“; cinp-age; coutname=“namesex=“sexage=“age main() struct date int year; int month; int day; ; struct baby int num;float weight; date birthday; ;baby b1=10001,10,2002,12,25; cout main() int i; struct student char name10; int math; int

5、 computer;float sum; student *next;struct student *head,*tail,*temp; temp=new struct student;/申请内存 head=temp; tail=head; couttemp-name; if(temp-name0!=*) cintemp-mathtemp-computer; temp-sum=temp-math+temp-computer; temp-next=NULL; tail=temp; else delete temp; tail-next=NULL; break; temp-next=new str

6、uct student; temp=temp-next;/ coutnamemathcomputersumnext; -=- /双链表前后篇历算法。 #include main() int i; struct student char name10; int math; int computer;float sum; student *forw; student *next; ;struct student *head,*tail,*temp;temp=new struct student; head=temp; tail=temp; head-forw=NULL;couttemp-name;

7、 if(temp-name0!=*) cintemp-mathtemp-computer; temp-sum=temp-math+temp-computer; temp-next=NULL; tail=temp; else delete temp; tail-next=NULL; break; temp-next=new struct student; temp-next-forw=temp; temp=temp-next; coutnamemathcomputersumnext;couthead:“namemathcomputersumforw; -=- /UNION 联合应用, 联合内各元

8、素,点用同一内存空间, #include main() int i; union utag char c; int k; float x; ; utag u;u.c=*; cout struct student int num; char name20; float grade; ;void main() int i,size; char str=“This a string.“; int int_values=51,23,2,44,45,0,11; float float_values=5.1,13.3,22.2,10.4,1.5; studentst_arr=101,“WangLin“,9

9、2,102,“LiPing“,85,103,“ZhaoMin“,88;size=sizeof(str)/sizeof(char); cout double add(double x,double y) double z; z=x+y; cout int sgn(double x) if(x0)return (1); if(xx; cout main() float max(float,float); float a,b,Max; couta; coutb; Max=max(a,b); couty)?x:y; return (z); -=-#include struct student char

10、 name10; float grade; ;void swap(student temp=x; x=y; y=temp; student void show(student /error /*string=x; /error int i=0; while(*(string+i)!=NULL) i+; return i; void main() char a=“ABCDE“; cout int abs(int x); long abs(long x); float abs(float x);/ void main(void) int i1=32767,i2=-32767; long l1=45

11、678,l2=-45678; float x1=1.1234,x2=-1.1234;cout main() int fact(int x); int n,sn; for(int i=1;in; sn=fact(n); cout int main(int argc,char *argv) int i; for(i=0;istruct student char name10; float grade; ;void change(student void change1(student *p,float grade) /指针传递,引用作用相同,都是传地址, 影响实参 p-grade=grade; v

12、oid change2(student x,float grade)/值传递,不影响实参 x.grade=grade; void show(student for(int i=0;i int get_result(int a,int b,int(*sub)(int,int) int r; r=sub(a,b); return r; int max(int a,int b) coutb)?a:b); /计算最小值 int min(int a,int b) coutab; cout extern int a=1,2,3; extern float p=3.14;show() extern int

13、y; int i; cout count1() static int i=0; return (+i); count2() int i=0; return (+i); main() int i;/调用 COUNT1()12 次 cout #include #include #include main() /从文件读写字符串 FILE *fp1; char str80;cout #include #include #include void main(void) /从文件读写字符 char ch; FILE *fp1;/以写方式打开文件 D.DAT fp1=fopen(“d.dat“,“w“);

14、 if(fp1=NULL) coutch; while(ch!=*) fputc(ch,fp1);/把字符写到 fp1 指向的流文件中 cinch; fclose(fp1);if(fp1=fopen(“d.dat“,“r“)=NULL) cout #include #include #include #include void main(void) int i; char ch; FILE *fp1; /以写方式打开文件 if(fp1=fopen(“d.dat“,“w“)=NULL) coutch; while(ch!=*) fputc(ch,fp1); cinch; cout #include #include #include #define MAX 5/show array void show_array(double x,int size) for(int i=0;i #include #include #include #define MAX 5 struct student in

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

当前位置:首页 > 中学教育 > 高中教育

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