我想学习C++编程,想要多读别人的代码,请问去那里可以下程序,或者有这样的书吗
看看这几个,或许对你有帮助:
#include<iostream.h>
void main()
{ int a[20][20],n,i,j,sum1=0,sum2=0;
cout<<"请确定二阶矩阵的大小:"<<"n=";
cin>>n;
if(n>20)
{cout<<"矩阵过大,请重新定义"<<"n=";cin>>n;}
cout<<"请输入数据:"<<endl;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<endl;
for(i=0;i<n;i++)
{sum1=sum1+a[i][i];sum2=sum2+a[n-1-i][n-1-i];}
cout<<"正对角线之和="<<sum1<<endl<<"副对角线之和="<<sum2<<endl;
}
#include<iostream.h>
void main()
{
char a[40],b[40],c[80];
int i,j;
cout<<"请输入第一个小于40个字符的字符串:";
cin>>a;
cout<<"请输入第二个小于40个字符的字符串:";
cin>>b;
for(i=0;a[i]!='\0';i++)
c[i]=a[i];
for(j=0;b[j]!='\0';j++)
c[i+j]=b[j];
c[i+j]='\0';
cout<<"两字符串连接后为:"<<c<<endl;
}
#include<iostream.h>
void main()
{void max_min(float *x,int n);
float a[100],*pt;
int i,n;
cout<<"Please define the size of the arry:n=";cin>>n;
if(n>100)
{cout<<"error!n<=100\nPlease redefine n=";cin>>n;}
cout<<"Enter data:";
for(i=0;i<n;i++)
cin>>a[i];
pt=a;
max_min(pt,n);
}
void max_min(float *x,int n)
{float *p,min,max,i=0,j,k=0,f;
min=*x;
max=*x;
for(p=x;p<x+n;p++,i++)
if(*p<=min)
{min=*p;j=i;}
cout<<endl<<"最小数为:"<<min<<endl<<"其下标为:"<<j;
for(p=x;p<x+n;p++,k++)
if(*p>=max)
{max=*p;f=k;}
cout<<endl<<"最大数为:"<<max<<endl<<"其下标为:"<<f<<endl;
}
#include <iostream.h>
const float PI = 3.14159;
class Circle
{
private:
float radius;
public:
Circle(float r); //构造函数
float Circumference(); //圆周长
float Area(); //圆面积
};
Circle::Circle(float r)
{radius=r;}
// 计算圆的周长
float Circle::Circumference()
{return 2 * PI * radius;}
// 计算圆的面积
float Circle::Area()
{return PI * radius * radius;}
void main()
{
float radius,ference,area;
cout<<"Enter the radius of the circle: ";
cin>>radius;
Circle c(radius);
Circle s(radius);
ference=c.Circumference();
area=s.Area();
cout<<"The Circumference of the circle is:"<<ference<<endl;
cout<<"The Area of the circle is:"<<area<<endl;
}
//定义一个学生类,使用构造函数和析构函数实现对数据的输入、输出
#include<iostream.h>
#include<string.h>
int i=0;
int n;
class student
{public:
student(int s_id,char s_name[20],int s_age);
~student();
private:
char name[20];
int id;
int age;
};
student::student(int s_id,char s_name[20],int s_age)
{
id=s_id;
strcpy(name,s_name);/*特别注意*/
age=s_age;
}
student::~student(void)
{cout<<"The student's information: "<<"id="<<id
<<" name="<<name<<" age="<<age<<endl<<"NEXT:"<<endl;
}
void main()
{
int n,s_id,s_age;
char s_name[20];
cout<<"Please define the number of the students:N=";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the student's id:";
cin>>s_id;
cout<<"Enter the student's name:";
cin>>s_name;
cout<<"Enter the student's age:";
cin>>s_age;
student s(s_id,s_name,s_age);/*构造函数调用之后,隐含析构函数调用*/
}
cout<<"TASK OVER !"<<endl;
}
//定义一个学生类,使用构造函数和析构函数实现对数据的输入、输出
#include<iostream.h>
#include<string.h>
int i=0;
int n;
class student
{public:
student(int s_id,char s_name[20],int s_age);
~student();
private:
char name[20];
int id;
int age;
};
student::student(int s_id,char s_name[20],int s_age)
{
id=s_id;
strcpy(name,s_name);/*特别注意*/
age=s_age;
}
student::~student(void)
{cout<<"The student's information: "<<"id="<<id
<<" name="<<name<<" age="<<age<<endl<<"NEXT:"<<endl;
}
void main()
{
int n,s_id,s_age;
char s_name[20];
cout<<"Please define the number of the students:N=";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the student's id:";
cin>>s_id;
cout<<"Enter the student's name:";
cin>>s_name;
cout<<"Enter the student's age:";
cin>>s_age;
student s(s_id,s_name,s_age);/*构造函数调用之后,隐含析构函数调用*/
}
cout<<"TASK OVER !"<<endl;
}
//定义一个学生类,使用构造函数和析构函数实现对数据的输入、输出
#include<iostream.h>
#include<string.h>
int i=0;
int n;
class student
{public:
student(int s_id,char s_name[20],int s_age);
~student();
private:
char name[20];
int id;
int age;
};
student::student(int s_id,char s_name[20],int s_age)
{
id=s_id;
strcpy(name,s_name);/*特别注意*/
age=s_age;
}
student::~student(void)
{cout<<"The student's information: "<<"id="<<id
<<" name="<<name<<" age="<<age<<endl<<"NEXT:"<<endl;
}
void main()
{
int n,s_id,s_age;
char s_name[20];
cout<<"Please define the number of the students:N=";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the student's id:";
cin>>s_id;
cout<<"Enter the student's name:";
cin>>s_name;
cout<<"Enter the student's age:";
cin>>s_age;
student s(s_id,s_name,s_age);/*构造函数调用之后,隐含析构函数调用*/
}
cout<<"TASK OVER !"<<endl;
}
//编写一将编号、姓名的输入和显示设计成一个类person,并作为student和teacher的基类,
#include<iostream.h>
int i=0;
int n;
class person
{public:
void getid_name();
void putid_name();
private:
char name[20];
int id;
};
class student:public person
{public:
void getscore_classes();
void putscore_classes();
private:
int score;
int classes;
};
class teacher:public person
{public:
void getpost_depart();
void putpost_depart();
private:
char post[20];
char depart[20];
};
void person::getid_name()
{cout<<"id:";cin>>id;
cout<<"name:";cin>>name;
}
void person::putid_name()
{cout<<"id="<<id<<" name="<<name<<" ";}
void student::getscore_classes()
{cout<<"score:";cin>>score;
cout<<"classes:";cin>>classes;
}
void student::putscore_classes()
{cout<<"score="<<score<<" classes="<<classes<<endl;}
void teacher::getpost_depart()
{cout<<"post:";cin>>post;
cout<<"depart:";cin>>depart;
}
void teacher::putpost_depart()
{cout<<"post="<<post<<" depart="<<depart<<endl;}
void main()
{ char flag;
student s;
teacher t;
cout<<"Please define the number of the persons:N=";
cin>>n;
cout<<"The type of people :t=teacher,s=student?";
cin>>flag;
if(flag=='s')
for(i=0;i<n;i++)
{cout<<"NO."<<i+1<<endl;
s.getid_name();
s.getscore_classes();
cout<<"The student's information: ";
s.putid_name();
s.putscore_classes();
}
else if(flag=='t')
for(i=0;i<n;i++)
{cout<<"NO."<<i+1<<endl;
t.getid_name();
t.getpost_depart();
cout<<"The teacher's information: ";
t.putid_name();
t.putpost_depart();
}
else
cout<<"ERROR!"<<endl;
}