📄 c01.cpp
字号:
void myfunction();
int x=5,y=7;
void san()
{
cout<<"x from main:"<<x<<"\n";
cout<<"y from main:"<<y<<"\n\n";
myfunction();
cout<<"back from myfunction!\n\n";
cout<<"x from main:"<<x<<"\n";
cout<<"y from main:"<<y<<"\n";
}
void myfunction()
{
int y=10;
cout<<"x from main:"<<x<<"\n";
cout<<"y from main:"<<y<<"\n\n";
}
class cat
{
public:
cat(int xx=0,int yy=0,int zz=0) {X=xx;Y=yy;Z=zz;howmanycatsc++;}
cat(cat&c);
~cat(){howmanycatsc--;}
int gethowmanycX() {return X;}
int gethowmanycY() {return Y;}
int gethowmanycZ() {return Z;}
static void gethowmanyc() {cout<<"get how many cats:"<<howmanycatsc<<endl;}
private:
int X,Y,Z;
static int howmanycatsc;
};
cat::cat(cat&c)
{
X=c.X;
Y=c.Y;
Z=c.Z;
howmanycatsc++;
}
int cat::howmanycatsc=0;
void diqi()
{
cat A(1);
cout<<"cat A:"<<A.gethowmanycX()<<endl;
A.gethowmanyc();
cat B(A);
cout<<"cat B:"<<B.gethowmanycX()<<endl;
cat::gethowmanyc();
cat C(B);
cout<<"cat C:"<<C.gethowmanycX()<<endl;
cat::gethowmanyc();
}
void fn2()
{
int n=5;
}
int n;
void shiyi()
{
int n=10;
fn2();
cout<<"n="<<n<<endl;
}
void fn1()
{
static int n=10;
n++;
cout<<"n="<<n<<endl;
}
void shier()
{
int i;
for(i=0;i<10;i++)
fn1();
}
class boat
{
public:
boat(int yy=0) {Y=yy;}
int boatweight() {return Y;}
friend class car;
friend int totalweight(boat&m,car&n);
private:
int Y;
};
class car
{
public:
car(int xx=0) {X=xx;}
int carweight() {return X;}
friend int totalweight(boat&m,car&n);
private:
int X;
};
int totalweight(boat&b1,car&c1)
{
return int(b1.Y+c1.X);
}
void fourteen()
{
int bb,cc;
cout<<"please input the weight of the boat:"<<endl;
cin>>bb;
boat myb1(bb);
cout<<"please input the weight of the car:"<<endl;
cin>>cc;
car myc1(cc);
cout<<"the totalweight is:"<<totalweight(myb1,myc1)<<endl;
}
void diwuzhang()
{
int c;
cout<<"======请问,您想看哪道题:======="<<endl;
cout<<"==========第3题=================="<<endl;
cout<<"==========第7题=================="<<endl;
cout<<"==========第11题================="<<endl;
cout<<"==========第12题================="<<endl;
cout<<"==========第14题================="<<endl;
cout<<"==========0(返回主菜单)========"<<endl;
cin>>c;
switch(c)
{
case 3: san(); diwuzhang(); break;
case 7: diqi(); diwuzhang(); break;
case 11: shiyi(); diwuzhang(); break;
case 12: shier(); diwuzhang(); break;
case 14: fourteen(); diwuzhang(); break;
case 0: break;
}
}
void four()
{
int onearray[]={1,2,3,4,5,6,7,8};
int i;
cout<<"the data of onearray[] is:"<<endl;
for(i=0;i<sizeof(onearray)/sizeof(int);i++)
cout<<onearray[i]<<endl;
cout<<"there are"<<" "<<sizeof(onearray)/sizeof(int)<<" "<<"numbers."<<endl;
}
void wu()
{
int a[5][3]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int i,j;
cout<<"the data of a[5][3] is:"<<endl;
for(i=0;i<5;i++)
for(j=0;j<3;j++)
cout<<a[i][j]<<endl;
}
void seventeen()
{
int *p;
int i;
p=&i;
i=9;
cout<<"the value at p:"<<*p<<endl;
}
int fn3();
void eighteen()
{
int a=fn3();
cout<<"the value of a is:"<<a<<endl;
}
int fn3()
{
int *p=new int(5);
return *p;
}
class simplecircle
{
public:
void setradius(double r=0);
void getarea();
private:
double *radius,s;
};
void simplecircle::setradius(double r)
{
radius=&r;
s=3.14159*r*r;
}
void simplecircle::getarea()
{
cout<<"圆的面积是:"<<s<<endl;
}
void twenty()
{
simplecircle mycircle;
double r1;
cout<<"请输入圆的半径:"<<endl;
cin>>r1;
mycircle.setradius(r1);
mycircle.getarea();
}
int count(char *str)
{
int i,n=0;
for(i=0;i<str[i];i++)
{
if((str[i]>='a'&&str[i]<='z')||
(str[i]>='A'&&str[i]<='Z'))
n++;
}
return n;
}
void twentyfirst()
{
int h;
char A[50];
cout<<"请输入一条英文句子:"<<endl;
gets(A);
h=count(A);
cout<<"字母的个数是:"<<h<<endl;
}
void twentytwo()
{
int a,b,i,j;
a=0,b=0,i=0,j=0;
char s[100],t[100];
char s1,t1;
cout<<"请输入字符串s,以#结尾"<<endl;
do
{ cin>>s1;
s[i]=s1;
i++;
}while(s1!='#');
cout<<"请输入字符串t,以#结尾"<<endl;
do
{ cin>>t1;
t[j]=t1;
j++;
}while(t1!='#');
while((t[b]!='#')&&(s[a]!='#'))
{
if(s[a]==t[b]) { a++; b++;}
else { b=0; a=a-b+1; }
}
if(t[b]=='#')
cout<<"字符串t在字符串s中首次出现的位置是:"<<a-b+1<<endl;
else
cout<<"字符串t不存在!"<<endl;
}
void reverse(char *s,char *t)
{
char ch;
if(s<t)
{
ch=*s;*s=*t;*t=ch;
reverse(++s,--t);
}
}
void reverse(char *s)
{
reverse(s,s+strlen(s)-1);
}
void twentythree()
{
char str[20];
cout<<"请输入一个字符串:";
cin>>str;
cout<<"这个字符串是:"<<str<<endl;
reverse(str);
cout<<"转置字符串是:"<<str<<endl;
}
void diershiliu()
{
int i,j;
int a[3][3];
cout<<"input 9 numbers:"<<endl;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
cout<<"the a[3][3] is:"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{cout<<a[i][j]<<" ";}
cout<<endl;
}
cout<<"the reverse of a[3][3] is:"<<endl;
for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
{cout<<a[i][j]<<" ";}
cout<<endl;
}
}
void twentyseven()
{
int i,j,m,n;
int a[10][10];
cout<<"请问,你想输入几行:"<<endl;
cin>>m;
cout<<"请问,你想输入几列:"<<endl;
cin>>n;
cout<<"现在,请输入"<<" "<<m*n<<" "<<"个元素:"<<endl;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<"这个矩阵是:"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{cout<<a[i][j]<<" ";}
cout<<endl;
}
cout<<"转置矩阵是:"<<endl;
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{cout<<a[i][j]<<" ";}
cout<<endl;
}
}
void diliuzhang()
{
int c;
cout<<"====请问,您想看哪道题:======="<<endl;
cout<<"========第4题=================="<<endl;
cout<<"========第5题=================="<<endl;
cout<<"========第17题================="<<endl;
cout<<"========第18题================="<<endl;
cout<<"========第20题================="<<endl;
cout<<"========第21题================="<<endl;
cout<<"========第22题================="<<endl;
cout<<"========第23题================="<<endl;
cout<<"========第26题================="<<endl;
cout<<"========第27题================="<<endl;
cout<<"========0(返回主菜单)========"<<endl;
cin>>c;
switch(c)
{
case 4: four(); diliuzhang(); break;
case 5: wu(); diliuzhang(); break;
case 17: seventeen(); diliuzhang(); break;
case 18: eighteen(); diliuzhang(); break;
case 20: twenty(); diliuzhang(); break;
case 21: twentyfirst(); diliuzhang(); break;
case 22: twentytwo(); diliuzhang(); break;
case 23: twentythree(); diliuzhang(); break;
case 26: diershiliu(); diliuzhang(); break;
case 27: twentyseven(); diliuzhang(); break;
case 0: break;
}
}
void main()
{
int c;
do
{
cout<<"======请问,您想看哪章的题:======"<<endl;
cout<<"==========第2章=================="<<endl;
cout<<"==========第3章=================="<<endl;
cout<<"==========第4章=================="<<endl;
cout<<"==========第5章=================="<<endl;
cout<<"==========第6章=================="<<endl;
cout<<"==========0(退出)================"<<endl;
cin>>c;
switch(c)
{
case 2: dierzhang(); break;
case 3: disanzhang(); break;
case 4: disizhang(); break;
case 5: diwuzhang(); break;
case 6: diliuzhang(); break;
}
}while(c!=0);
cout<<"谢谢使用。"<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -