第一范文网 - 专业文章范例文档资料分享平台

C++经典代码大全

来源:用户分享 时间:2025/7/12 3:20:01 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

. float x; } sx={100,3.1416}; //定义联合类型,并为声明的联合变量赋初值 union u_tag { short i; float x; } ux={1000}; //输出结构类型和结构变量的有关信息 cout<<\s_tag)=\ student st_arr[]={101,\8}; //显示char类型数组元素及其大小 size=sizeof(str) / sizeof(char); cout<<\ cout< main() { //自定义类型 typedef int ARRAY_INT[50]; int i; ARRAY_INT a; //用自定义类型声明数组变量a //以下为数组a赋值,并打印 for (i=0;i<50;i++) { if (i==0) //每10个数换一次行 cout< //定义结构类型 struct student { int num; char name[20]; float grade; }; void main(void) { //声明数组 int i,size; char str[]=\ int int_values[] = {51, 23, 2, 44, 45,0,11}; float float_values[] = {15.1, 13.3, 22.2, 10.4, 1.5}; Word 资料

cout< //add()函数的定义,其有返回值 double add(double x,double y) { double z; z=x+y; cout< //定义f()函数 f(int x,int y) //f()的参数以值方式传递 { ++x; --y; cout<<\} main() { int a,b; //设置实际参数的值 a=b=10; } #include //定义符号函数sgn(),其返回值为int类型 int sgn(double x) { if (x>0) return(1); //返回出口1 if (x<0) return(-1); //返回出口2 return(0); //返回出口3 } //main()函数定义 main() { double x; int i; for (i=0;i<=2;i++) { cout<<\ cin>>x; cout<<\ } } #include //函数原型语句可以在这里 //定义main()函数 main() { //max()函数原型声明语句 float max(float,float); //变量声明语句 float a,b,Max; //输入参数并计算 cout<<\ cin>>a; cout<<\ cin>>b; Max=max(a,b); //调用max()函数 cout<<\} //定义max()函数 float max(float x,float y) //max()返回值类型为浮点型 { float z; z=(x>y)?x:y; return(z); } Word 资料

//以变量为参数调用f()函数 f(a,b); //验证实际参数的值 cout<<\ //以表达式参数形式调用f()函数 f(2*a,a+b); } #include //定义公共结构类型 struct student { int num; char name[10]; float maths; float physics; float chemistry; double total; }; //定义结构输入函数 input_Rec(struct student *p) //参数为student类型的结构指针变量 { cin>>p->num; cin>>p->name; cin>>p->maths; cin>>p->physics; cin>>p->chemistry; } //定义结构数据交换函数 swap_Rec(struct student *p1,struct student *p2) { struct student x; //交换两个记录的数据 x=*p1; *p1=*p2; *p2=x; } //输出结构的值 put_Rec(struct student *p) { cout<num<<'\\t'; . cout<name<<'\\t'; cout<maths<<'\\t'; cout<physics<<'\\t'; cout<chemistry<<'\\t'; cout<total<total=p1->maths+p1->physics+p1->chemistry; } //对3个学生的数据排序 for (i=0;i<=2;i++) for (j=i+1;j<=2;j++) if (a[i].total //定义结构 struct student { char name[10]; float grade; }; //交换student类型的数据 void swap(student &x,student &y) //swap的参数为引用传递方式 { student temp; temp=x; x=y; y=temp; } //返回student类型的引用,求优者 student& max(student &x,student &y) //swap的参数为引用传递方式 { Word 资料

return (x.grade>y.grade?x:y); } //显示student类型的数据 void show(student &x) //show的参数为引用传递方式 { cout< //参数带有默认值的函数 disp(int x=1,int y=1,int z=1) { cout<<\参数1: \ cout<<\参数2: \ cout<<\参数3: \ cout<<\} //main()函数中测试参数带有默认值的函数disp() void main() { disp(); disp(10); disp(10,20); disp(10,20,30); int a=1,b=2,c=3; disp(a,b,c); } #include //计算字符串长度的函数 int str_len(const char *string) { //char *temp=string; 编译报错! //*string='x'; 编译报错! int i=0; while (*(string+i)!=NULL) . i++; return i; } //main()函数中测试str_len() void main() { char a[]=\ cout< //max()为内联函数 test.\} #include void disp(void); //这个函数声明语句不能少 //定义main()函数的参数和返回值类型是void类型 void main(void) { //调用void类型函数 disp(); } //以下定义disp()函数 void disp(void) { cout<<\} #include //函数原型语句 int abs(int x); long abs(long x); float abs(float x); //main()函数的定义 void main(void) { //声明变量 int i1=32767,i2=-32767; long l1=456789,l2=-456789; float x1=1.1234,x2=-1.1234; //直接在cout输出中调用函数 cout<

inline int max(int x,int y) //注意inline关键字 { return x>y?x:y; } //定义main()函数 main() { int a=3,b=5,c; c=max(a,b); cout<<\ cout<<\} #include main() { //函数原型声明 int fact(int x); int n,sn; //依次从键盘上输入3个正整型数据计算它们的阶乘 for (int i=1;i<=3;i++) { cout<>n; sn=fact(n); cout< //带参数的main()函数 int main(int argc,char *argv[]) { int i; for(i=0;i //用函数原型声明要使用的函数 . void show_array1(int*,int); void show_array2(int a[],int); void sort(int*,int); main() { //声明数组并初始化 int a[]={2,4,6,1,3,5}; int b[3][3]={{2,4,6},{1,3,5},{0,1,2}}; //显示数组的值 cout<<\ show_array1(a,6); show_array1(&b[0][0],3*3); //用sort1排序并显示 cout<<\and show_array1(int*,int): \ sort(a,6); show_array1(a,6); sort(&b[0][0],3*3); show_array1(&b[0][0],9); //显示数组的值 cout<<\ show_array2(a,6); show_array2(&b[0][0],3*3); } //显示数组,用指针当参数 void show_array1(int *p,int size) { for(int i=0;i //定义结构 struct student { char name[10]; float grade; }; Word 资料

//更改student数据的grade成员,参数形式为引用 void change(student &x,float grade) { x.grade=grade; } //更改student数据的grade成员,参数形式为指针 void change1(student *p,float grade) { p->grade=grade; } //更改student类型的数据,普通参数形式 void change2(student x,float grade) { x.grade=grade; } //显示student类型的数据,参数形式为引用 void show(student &x) { cout< //定义函数计算数组的和和平均值 void calculate(int a[],int size,int& sum,float& average) { sum=0; for (int i=0;i

搜索更多关于: C++经典代码大全 的文档
C++经典代码大全.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c21pyq9df1n6tzp834d3b207lq1bb5x01ej5_4.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top