c++经典代码大全[共125页]
![c++经典代码大全[共125页]_第1页](https://file5.zhuangpeitu.com/fileroot5/2022-9/16/6445bdff-4526-4f55-9211-a5c44bba2236/6445bdff-4526-4f55-9211-a5c44bba22361.gif)
![c++经典代码大全[共125页]_第2页](/images/s.gif)
![c++经典代码大全[共125页]_第3页](/images/s.gif)
《c++经典代码大全[共125页]》由会员分享,可在线阅读,更多相关《c++经典代码大全[共125页](65页珍藏版)》请在装配图网上搜索。
1、#include 3、 4、
cout< 5、at x=365.5; //声明全局变量
cout<<"c="< 6、="< 7、l;
float fz=fx/fy; cout<<"outer:w="< 8、y<<"="< 9、+2) do {
cout< 10、for (int i=1;i<=9;i++) {
cout<
for(int n=1;n<=100;n++) main()
s=s+n; {
cout< 11、<"s="< 12、< 13、sum="< 15、
//直接显示数组元素 //对数组按从小到大顺序排序
cout<a[j])
for (i=0;i<5;i++) {
cout<
16、="< 17、t<<"value=";
#include 18、
} }
if (a[mid] 19、];
} h=i;
} l=j;
#include 20、<"str=";
//对数组按从大到小顺序排序 cin>>str;
for (i=0;i<=3;i++) cout< 22、ip;
cout<<"a["<>a[i][j]; //指针变量 ip 指向变量 a
} a=100;
ip=&a; //使指针变量 ip 指向变量 a
//显示数组 a cout<<"a="<
23、ip2="<<(*ip2)< 24、
cout<<"*ip="<<*ip< 25、字符型数组和指针变量
for (i=0;i<2;i++) //为数组 a 赋值 char str[10];
for (j=0;j<3;j++) char *strip=str;
{
cout<<"a["<>a[i][j]; cout<<"str=";
} cin>>str; //用字符数组输入字符串
cout<<"str="< 26、(j=0;j<3;j++) cin>>strip; //用字符指针变量输入字符串
{ cout<<"str="< 27、r (j=0;j<3;j++) strip=new char(100);
{ cout<<"strip=";
cout<<"a["<>strip; // 用字符指针变量输入字符串
cout< 28、nt h[]={1001,1002,1003,1004};
{ // 声明用于存放运动员成绩的数组
//声明数组、变量和指针变量 float x[]={12.3,13.1,11.9,12.1};
int a[]={1,2,3,4,5,6}; //声明用于存放运动姓名的字符型指针数组
int *ip1,*ip2; char *p[]={"Wang hua","Zhang jian","Li wei","Hua ming"};
//i,j,it 是用做循环控制变量和临时变量
//测试指针的赋值运算 int i,j,it;
ip1=a; //ft 用做暂存变量
ip2=ip1; fl 29、oat ft;
cout<<"*ip1="<<(*ip1)< 30、
if (x[i]>=x[j]) { cin>>Wbk.title;
ft=x[i],x[i]=x[j],x[j]=ft; cout<<"Wbk.author=";
it=h[i],h[i]=h[j],h[j]=it; cin>>Wbk.author;
pt=p[i],p[i]=p[j],p[j]=pt; cout<<"Wbk.pages=";
} cin>>Wbk.pages;
cout<<"Wbk.price=";
//以下打印排序结果 cin>>Wbk.price;
for (i=0;i<=3;i++) cout<<"Wbk:"< 31、 ,"< cout< 32、*pt; temp=Wbk;
cout<<"temp:"< 33、
} main()
} {
#include 34、struct books Zbk={"VC++ ","Zhang",295,35.5};
books Wbk; //从键盘上为结构数组输入值
cout<<" num name maths physics chemistry
//对结构变量的输出 "< 35、endl; cin>>st[i].name;
cout<<"--------------------"< 36、;
//输出结构数组各元素的值 int sex;
for (i=0;i<3;i++) int age;
{ };
cout<<"st["<
37、"(*p).name="<<(*p).name< 38、]; cout<<"p->name=";
int sex; cin>>p->name;
int age; cout<<"p->sex=";
}; cin>>p->sex;
cout<<"p->age=";
//声明结构变量和结构指针变量 ,并初始化 cin>>p->age;
struct human x={"WangPing",1,30},*p=NULL; cout<<"-------------------------"< 39、ut<<"p->sex="< 40、ut<<"(*p).name="<<(*p).name< 41、对象输入数据 {
cout<<"name:"; //定义结构类型
cin>>(*p).name; struct human {
cout<<"sex:"; char name[10];
cin>>(*p).sex; int sex;
cout<<"age:"; int age;
cin>>(*p).age; };
//显示结构变量的值 //声明结构数组和结构指针变量 ,并初始化
cout<<"x.name="< 42、5},{"LiuMin",0,23}},*p=NU
cout<<"x.age="< 43、;
//用结构指针输出结构数组的元素 int day;
for (p=x;p<=&x[2];p++) };
{
cout< 44、{2002,12,25}};
struct test {
char *str; //下列是 baby 结构变量 b1 的引用。
int *ip; cout<<"b1.num="< 45、单元 cout<<"b1.birthday.day="< 46、<<"temp.num="< 47、ay.day="< 48、t sex;
x.str=new char[5]; //分配多个单元 int age;
*x.str='G'; list *next; //成员 next 为指向其自身结
*(x.str+1)='o';
构的指针
*(x.str+2)='o'; };
*(x.str+3)='d';
*(x.str+4)='\0'; //使用递归结构变量
cout<<"x.str:"< 49、endl; cout<<"name\t"< 50、 math;
//定义名为 student 的递归结构 int computer;
struct student { float sum;
char name[10]; student *forw; //forw 成员是前指针
int math; student *next; //next 成员是后指针
int computer; };
float sum;
student *next; //next 成员是指向自身的结构指 //用 student 声明 3 个结构指针变量
针 struct student *head,*tail,*temp;
};
//申请第 1 块数据 51、,并设置各结构指针的初值
//用 student 声明 3 个结构指针变量 temp=new struct student; //申请内存
struct student *head,*tail,*temp; head=temp; // 头指针
tail=head; // 尾指针
//申请第 1 块数据,并设置各结构指针的初值 head->forw=NULL;
temp=new struct student; //申请内存
head=temp; // 头指针 //循环为链表记录输入数据
tail=head; // 尾指针 cout<<"\tname Math Computer"<< 52、endl;
for (i=1;;i++) {
//循环为链表输入数据 cout<>temp->name;
for (i=1;;i++) { if (temp->name[0]!='*')
cout<>temp->name; cin>>temp->math>>temp->computer;
if (temp->name[0]!='*') temp->sum=temp->math+temp->computer;
{ temp->next=NULL;
53、cin>>temp->math>>temp->computer; tail=temp; //设置链表尾指针
temp->sum=temp->math+temp->computer; }
temp->next=NULL; else
tail=temp; //设置链表尾指针 {
} // 以下是输入结束处理
else delete temp;
{ tail->next=NULL;
// 以下是输入结束处理 break;
delete temp; }
tail->next=NULL; //为下一个学生申请内存
break; temp->next=new struct studen 54、t;
} temp->next->forw=temp; //设置前指针
//为下一个学生申请内存 temp=temp->next; //使处理指针 temp 指向新
temp->next=new struct student;
内存块
temp=temp->next; // 使处理指针 temp 指向新内存 }
块
} // 将链表数据从头到尾打印出来
cout<<"head------>tail:"< 55、) {
temp=head; cout< 56、
#include 57、< 58、
// 使用联合变量中的字符型成员 if (i%10==0) //每 10 个数换一次行
u.c='*'; cout< 59、 num;
//声明联合变量时初始化 char name[20];
utag u1={'A'}; float grade;
};
//同时引用联合变量的各成员 void main(void)
cout<<"u1.c="< 60、0,11};
main() float float_values[] = {15.1, 13.3, 22.2, 10.4, 1.5};
{ student
//定义结构类型,并为声明的结构变量赋初值 st_arr[]={101,"WangLin",92,102,"LiPing",85,103,"ZhaoMin",88};
struct s_tag {
short i; //显示 char 类型数组元素及其大小
float x; size=sizeof(str) / sizeof(char);
} sx={100,3.1416}; cout<<"Number of elements 61、 in str: ";
cout< 62、Number of elements in int_values: ";
cout<<"sx.i="< 63、n u_tag)="< 64、for (i=0;i<=2;i++) {
} cout<<"x=";
cout< 65、ut< 66、< L1: cout<<"x=";
main() cin>>x;
{ if (x==-1)
//计算 s=1+2+3...+100 goto L2; //无条件转移语句,转到 L2 语句处
int s=0,n=1; else
while(n<=100) { sum+=x;
s=s+n; goto L1; //无条件转移语句,转到 L1 语句处
n++; //定义标号 L2
} L2: cout<<"sum="<
cout<<"x="; main()
cin>>x; {
while(x!=0) { //累加键盘输入的数据
sum+=x; double x,sum=0.0;
cout<<"x="; while(1) {
cin>>x; cout<<"x=";
} cin>>x;
cout<<"sum="<
- 温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。