收藏 分销(赏)

c--经典代码大全.doc

上传人:仙人****88 文档编号:9494434 上传时间:2025-03-28 格式:DOC 页数:63 大小:725.54KB 下载积分:10 金币
下载 相关 举报
c--经典代码大全.doc_第1页
第1页 / 共63页
c--经典代码大全.doc_第2页
第2页 / 共63页


点击查看更多>>
资源描述
#include <iostream.h> //包含iostream.h头文件 main() { //声明变量,并初始化 int a=010,b=10,c=0X10; //以十进制形式显示数据 cout<<"DEC:"; cout<<" a="<<a; cout<<" b="<<b; cout<<" c="<<c<<endl; //以八进制形式显示数据 cout<<"OCT:"; cout<<oct; //指定八进制输出 cout<<" a="<<a; cout<<" b="<<b; cout<<" c="<<c<<endl; //以十六进制形式显示数据 cout<<"HEX:"; cout<<hex; //指定十六进制输出 cout<<" a="<<a; cout<<" b="<<b; cout<<" c="<<c<<endl; //八、十和十六进制数混合运算并输出 cout<<"a+b+c="; cout<<dec; //恢复十进制输出 cout<<a+b+c<<endl; //测试八、十和十六进制输入 cout<<"DEC:a="; cin>>a; cout<<"OCT:b="; cin>>b; cout<<"HEX:a="; cin>>c; cout<<"DEC:"<<dec<<endl; //指定十进制输出 cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; cout<<"c="<<c<<endl; } cin>>dx; cout<<"dy="; cin>>dy; cout<<dx<<"+"<<dy<<"="<<dx+dy<<endl; cout<<dx<<"-"<<dy<<"="<<dx-dy<<endl; cout<<dx<<"*"<<dy<<"="<<dx*dy<<endl; cout<<dx<<"/"<<dy<<"="<<dx/dy<<endl<<endl; //cout<<fx<<"%"<<fy<<"="<<fx%fy<<endl; Error! //测试float和double类型数据的有效位 fx=10.0;fy=6.0; float fz=fx/fy; dx=10.0;dy=6.0; double dz=dx/dy; cout<<"fz="; cout<<setprecision(20)<<fx<<"/"<<fy<<"="<<fz<<endl; cout<<"dz="; cout<<setprecision(20)<<dx<<"/"<<dy<<"="<<dz<<endl<<endl;; //float型溢出 float x=3.5e14; cout<<"x="<<x<<endl; cout<<"x*x="<<x*x<<endl; cout<<"x*x*x="<<x*x*x<<endl; } #include <iostream.h> main() { //x,y 为操作数,c为运算符 int x,y,z; char c1; cin>>x>>c1>>y; //c1 //多路选择语句选择不同表达式计算语句 switch(c1) { case '+':cout<<x<<"+"<<y<<"="<<x+y<<endl; break; case '-':cout<<x<<"-"<<y<<"="<<x-y<<endl; break; case '*':cout<<x<<"*"<<y<<"="<<x*y<<endl; break; case '/':cout<<x<<"/"<<y<<"="<<x/y<<endl; break; case '%':cout<<x<<"%"<<y<<"="<<x%y<<endl; break; default :cout<<"Wrong !"<<endl; //当不符合上述情况时执行本子句 } } #include<iostream.h> float x=365.5; //声明全局变量 main() { int x=1,y=2; double w=x+y; { double x=1.414,y=1.732,z=3.14; cout<<"inner:x="<<x<<endl; cout<<"inner:y="<<y<<endl; cout<<"inner:z="<<z<<endl; cout<<"outer:w="<<w<<endl; cout<<"::x="<<::x<<endl; //访问重名的全局变量 } cout<<"outer:x="<<x<<endl; cout<<"outer:y="<<y<<endl; cout<<"outer:w="<<w<<endl; //cout<<"inner:z="<<z<<endl;无效 cout<<"::x="<<::x<<endl; //访问重名的全局变量 } #include<iostream.h> main() { //显示1,2,3...10 for(int i=1;i<=10;i++) cout<<i<<" "; cout<<endl; //显示10,9,8...1 for(int j=10;j>=1;j--) cout<<j<<" "; cout<<endl; //显示1,3,5...9 for(int k=1;k<=10;k=k+2) cout<<k<<" "; cout<<endl; //显示ABC...Z for(char c='A';c<='Z';c++) cout<<c; cout<<endl; //显示0,0.1,0.2...1.0 for(float x=0;x<=1.0;x=x+0.1) cout<<x<<" "; cout<<endl; //显示0,0.1,0.2...1.0 for(float x1=0;x1<=1.0+0.1/2;x1=x1+0.1) cout<<x1<<" "; cout<<endl; //计算s=1+2+3...+100 int s=0; for(int n=1;n<=100;n++) s=s+n; cout<<"s="<<s<<endl; } #include<iostream.h> main() { //计算s=1+2+3...+100 int s=0,n=1; while(n<=100) { s=s+n; n++; } cout<<"s="<<s<<endl; //累加键盘输入的数据 double x,sum=0.0; cout<<"x="; cin>>x; while(x!=0) { sum+=x; cout<<"x="; cin>>x; } cout<<"sum="<<sum<<endl; } #include<iostream.h> main() { //计算s=1+2+3...+100 int s=0,n=0; do { n++; s+=n; }while(n<100); cout<<"s="<<s<<endl; //累加键盘输入的数据 double x,sum=0.0; do { cout<<"x="; cin>>x; sum+=x; } while(x!=0); cout<<"sum="<<sum<<endl; } #include<iostream.h> main() { //计算和打印打印乘法九九表 for (int i=1;i<=9;i++) { cout<<i; for (int j=1;j<=9;j++) cout<<'\t'<<i<<"*"<<j<<"="<<i*j; cout<<endl; } } #include<iostream.h> main() { int x,sum=0; //定义标号L1 L1: cout<<"x="; cin>>x; if (x==-1) goto L2; //无条件转移语句,转到L2语句处 else sum+=x; goto L1; //无条件转移语句,转到L1语句处 //定义标号L2 L2: cout<<"sum="<<sum<<endl; } #include<iostream.h> main() { //累加键盘输入的数据 double x,sum=0.0; while(1) { cout<<"x="; cin>>x; if (x<=0) break; sum+=x; } cout<<"sum="<<sum<<endl; } #include<iostream.h> main() { int i; for (i=1;i<=20;i++) { if (i%3==0) //能被 3 整除的整数,返回进行下次循环 continue; cout<<i<<" "; } cout<<endl; } #include<iostream.h> main() { //声明数组和变量 int a[5],i,sum; double avg; //从键盘上循环为数组赋值 for (i=0;i<5;i++) { cout<<"a["<<i<<"]="; cin>>a[i]; } //直接显示数组元素 cout<<a[0]<<a[1]<<a[2]<<a[3]<<a[4]<<endl; //利用for循环显示数组各元素的值 for (i=0;i<5;i++) cout<<a[i]<<" "; cout<<endl; //计算数组元素之和,并显示计算结果 sum=a[0]+a[1]+a[2]+a[3]+a[4]; cout<<"sum="<<sum<<endl; //利用循环计算数组的累加和 for (sum=0,i=0;i<5;i++) sum+=a[i]; //显示累加和及平均值 cout<<"sum="<<sum<<endl; avg=sum/5.0; cout<<"avg="<<avg<<endl; } #include<iostream.h> main() { int i,max,index,a[5]; //从键盘上为数组赋值 for (i=0;i<=4;i++) { cout<<"a["<<i<<"]="; cin>>a[i]; } // 利用循环遍历数组,找出最大值的元素及其下标 max=a[0]; for (i=0;i<=4;i++) { if (max<a[i]) { max=a[i]; index=i; } } cout<<"\nMax="<<max<<" index="<<index; } #include<iostream.h> #define size 5 main() { //声明变量 int i,j; float t,a[size]; //从键盘上为数组赋值 for (i=0;i<size;i++) { cout<<"a["<<i<<"]="; cin>>a[i]; } //对数组按从小到大顺序排序 for (i=0;i<size-1;i++) for (j=i+1;j<size;j++) if (a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } //显示排序结果 for (i=0;i<size;i++) cout<<a[i]<<" "; cout<<endl; //输入要查找的数据 int value; int found; //找到为1,否则为0 int low,high,mid; for (i=1;i<=3;i++) { cout<<"value="; cin>>value; //二分法查找数组a found=0; low=0; high=size-1; while(low<=high) { mid=(high+low)/2; if (a[mid]==value) { found=1; break; } if (a[mid]<value) low=mid+1; else high=mid-1; } if (found) cout<<"The valu found at:a["<<mid<<"]="<<a[mid]<<endl; else cout<<"The "<<value<<" is not found!"<<endl; } } #include<iostream.h> main() { //声明变量 int i,j; float t,a[5]; //从键盘上为数组赋值 for (i=0;i<=4;i++) { cout<<"a["<<i<<"]="; cin>>a[i]; } //对数组按从大到小顺序排序 for (i=0;i<=3;i++) for (j=i+1;j<=4;j++) if (a[i]<=a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } //显示排序结果 for (i=0;i<=4;i++) cout<<a[i]<<" "; } #include<iostream.h> main() { //声明二维数组及变量 int a[2][3],i,j; //从键盘上为数组a赋值 for (i=0;i<2;i++) for (j=0;j<3;j++) { cout<<"a["<<i<<"]["<<j<<"]="; cin>>a[i][j]; } //显示数组a for (i=0;i<2;i++) { for (j=0;j<3;j++) { cout<<a[i][j]<<" "; } cout<<endl; } //找出该数组的最大元素及其下标 int h,l,Max=a[0][0]; for (i=0;i<2;i++) { for (j=0;j<3;j++) { if (Max<a[i][j]) { Max=a[i][j]; h=i; l=j; } } } cout<<"Max:"<<"a["<<h<<"]["<<l<<"]="<<a[h][l]<<endl; } #include<iostream.h> main() { //声明字符数组和变量 char str[6]; int i; //从键盘上输入字符串 cout<<"str="; cin>>str; cout<<str<<endl; //按数组和下标变量两种方式显示字符数组 cout<<str<<endl; for (i=0;i<6;i++) cout<<str[i]; cout<<endl; //字符串反向输出 for (i=5;i>=0;i--) cout<<str[i]; cout<<endl; //将字符数组变成大写字母后输出 for (i=0;i<=5;i++) str[i]-=32; //小写字母转换成大写字母 cout<<str<<endl; //显示字符串 } #include<iostream.h> main() { //声明变量和指针变量 int a,b,c,*ip; //指针变量ip指向变量a a=100; ip=&a; //使指针变量 ip 指向变量a cout<<"a="<<a<<endl; cout<<"*ip="<<*ip<<endl; cout<<"ip="<<ip<<endl; //指针变量ip指向变量b ip=&b; //使指针变量 ip 指向变量b b=200; cout<<"b="<<b<<endl; cout<<"*ip="<<*ip<<endl; cout<<"ip="<<ip<<endl; //指针变量ip指向变量c ip=&c; //使指针变量 ip 指向变量b *ip=a+b; cout<<"c="<<c<<endl; cout<<"*ip="<<*ip<<endl; cout<<"ip="<<ip<<endl; } #include<iostream.h> main() { //声明数组、变量和指针变量 int a[2][3],i,j; int* ip; //从键盘上为数组a赋值 for (i=0;i<2;i++) //为数组a赋值 for (j=0;j<3;j++) { cout<<"a["<<i<<"]["<<j<<"]="; cin>>a[i][j]; } //利用下标变量显示数组a for (i=0;i<2;i++) { for (j=0;j<3;j++) { cout<<a[i][j]<<" "; } cout<<endl; } //利用指针变量显示数组a ip=&a[0][0]; for (i=0;i<2;i++) { for (j=0;j<3;j++) { cout<<"a["<<i<<"]["<<j<<"]="; cout<<ip<<" "; cout<<*ip<<endl; ip++; } } } #include<iostream.h> main() { //声明数组、变量和指针变量 int a[]={1,2,3,4,5,6}; int *ip1,*ip2; //测试指针的赋值运算 ip1=a; ip2=ip1; cout<<"*ip1="<<(*ip1)<<endl; cout<<"*ip2="<<(*ip2)<<endl; //测试指针的自增自减运算和组合运算 ip1++; ip2+=4; cout<<"*ip1="<<(*ip1)<<endl; cout<<"*ip2="<<(*ip2)<<endl; //测试指针变量之间的关系运算 int n=ip2>ip1; cout<<"ip2>ip1="<<n<<endl; cout<<"ip2!=NULL="<<(ip2!=NULL)<<endl; //指针变量之间的减法 n=ip2-ip1; cout<<"ip2-ip1="<<n<<endl; } #include<iostream.h> main() { //声明字符型数组和指针变量 char str[10]; char *strip=str; //输入输出 cout<<"str="; cin>>str; //用字符数组输入字符串 cout<<"str="<<str<<endl; cout<<"strip="<<strip<<endl; cout<<"strip="; cin>>strip; //用字符指针变量输入字符串 cout<<"str="<<str<<endl; cout<<"strip="<<strip<<endl; //利用指针变量改变其指向字符串的内容 *(strip+2)='l'; cout<<"str="<<str<<endl; cout<<"strip="<<strip<<endl; //动态为字符型指针变量分配内存 strip=new char(100); cout<<"strip="; cin>>strip; //用字符指针变量输入字符串 cout<<"str="<<str<<endl; cout<<"strip="<<strip<<endl; } #include<iostream.h> main() { // 声明用于存放运动员号码的数组 int h[]={1001,1002,1003,1004}; // 声明用于存放运动员成绩的数组 float x[]={12.3,13.1,11.9,12.1}; //声明用于存放运动姓名的字符型指针数组 char *p[]={"Wang hua","Zhang jian","Li wei","Hua ming"}; //i,j,it是用做循环控制变量和临时变量 int i,j,it; //ft 用做暂存变量 float ft; //pt为字符型指针变量用做暂存指针变量 char *pt; //用选择法对数组x进行排序,并相应调整数组h和p中的数据 for (i=0;i<=3;i++) for (j=i+1;j<=3;j++) if (x[i]>=x[j]) { ft=x[i],x[i]=x[j],x[j]=ft; it=h[i],h[i]=h[j],h[j]=it; pt=p[i],p[i]=p[j],p[j]=pt; } //以下打印排序结果 for (i=0;i<=3;i++) cout<<h[i]<<" ,"<<p[i]<<" ,"<<x[i]<<endl; } #include<iostream.h> main() { //声明指针数组 char *colors[]={"Red","Blue","Yellow","Green"}; //指向指针的指针变量 char **pt; //通过指向指针的变量访问其指向的内容 pt=colors; for (int i=0;i<=3;i++) { cout<<"pt="<<pt<<endl; cout<<"*pt="<<*pt<<endl; cout<<"**pt="<<**pt<<endl; pt++; } } #include<iostream.h> main() { //定义结构类型 struct books { char title[20]; char author[15]; int pages; float price; } ; //声明结构变量 struct books Zbk={"VC++ ","Zhang",295,35.5}; books Wbk; //对结构变量的输出 cout<<"Zbk:"<<endl; cout<<Zbk.title <<endl; cout<<Zbk.author<<endl; cout<<Zbk.pages<<endl; cout<<Zbk.price<<endl; cout<<"--------------------"<<endl; //对结构成员的运算 Zbk.pages+=10; Zbk.price+=0.5; cout<<"Zbk.pages="<<Zbk.pages<<endl; cout<<"Zbk.price="<<Zbk.price<<endl; cout<<"--------------------"<<endl; //对结构变量的输入输出 cout<<"Wbk.title ="; cin>>Wbk.title; cout<<"Wbk.author="; cin>>Wbk.author; cout<<"Wbk.pages="; cin>>Wbk.pages; cout<<"Wbk.price="; cin>>Wbk.price; cout<<"Wbk:"<<endl; cout<<Wbk.title <<endl; cout<<Wbk.author<<endl; cout<<Wbk.pages<<endl; cout<<Wbk.price<<endl; cout<<"--------------------"<<endl; //结构变量之间的相互赋值 books temp; temp=Wbk; cout<<"temp:"<<endl; cout<<temp.title<<endl; cout<<temp.author<<endl; cout<<temp.pages<<endl; cout<<temp.price<<endl; } #include<iostream.h> main() { int i; //定义结构类型 struct student { int num; char name[10]; float maths; float physics; float chemistry; double total; }; //声明结构数组st student st[3]; //从键盘上为结构数组输入值 cout<<" num name maths physics chemistry "<<endl; for (i=0;i<3;i++) { cout<<i+1<<" "; cin>>st[i].num; cin>>st[i].name; cin>>st[i].maths; cin>>st[i].physics; cin>>st[i].chemistry; } //计算每个学生的总成绩 for (i=0;i<3;i++) st[i].total=st[i].maths+st[i].physics+st[i].chemistry; //输出结构数组各元素的值 for (i=0;i<3;i++) { cout<<"st["<<i<<"]: "; cout<<st[i].num<<'\t'; cout<<st[i].name<<'\t'; cout<<st[i].maths<<'\t'; cout<<st[i].physics<<'\t'; cout<<st[i].chemistry<<'\t'; cout<<st[i].total<<endl; } } #include<iostream.h> main() { //定义结构类型 struct human { char name[10]; int sex; int age; }; //声明结构变量和结构指针变量,并初始化 struct human x={"WangPing",1,30},*p=NULL; //结构指针变量指向对象 p=&x; //显示结构变量的值 cout<<"x.name="<<x.name<<endl; cout<<"x.sex="<<x.sex<<endl; cout<<"x.age="<<x.age<<endl; //利用结构指针显示结构对象中的数据 cout<<"(*p).name="<<(*p).name<<endl; cout<<"(*p).sex="<<(*p).sex<<endl;
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服