这学期c++习题电子科大版.docx

上传人:b****7 文档编号:10619080 上传时间:2023-02-21 格式:DOCX 页数:25 大小:21.10KB
下载 相关 举报
这学期c++习题电子科大版.docx_第1页
第1页 / 共25页
这学期c++习题电子科大版.docx_第2页
第2页 / 共25页
这学期c++习题电子科大版.docx_第3页
第3页 / 共25页
这学期c++习题电子科大版.docx_第4页
第4页 / 共25页
这学期c++习题电子科大版.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

这学期c++习题电子科大版.docx

《这学期c++习题电子科大版.docx》由会员分享,可在线阅读,更多相关《这学期c++习题电子科大版.docx(25页珍藏版)》请在冰豆网上搜索。

这学期c++习题电子科大版.docx

这学期c++习题电子科大版

这学期c++习题,全是自己写的,绝对正确!

适用于西安电子科大的教材

P35T

(1)完数

#include

#include

intmain()

{

     inti,j;

     for(i=2;i<=999;i++)

          {

                inttemp=0;

                for(j=1;j

                {   if(i%j==0)

                     temp+=j;             

                }

               if(i==temp)

                cout<

          }   

}

P35T

(2)九九乘法表

#include

#include

intmain()

{

     inti,j,k,m;

     cout<<"*"<<"\t";

     for(m=1;m<=9;m++)

     {cout<

     cout<<"\n";

     for(m=0;m<=24;m++)

     {cout<<"___";}

     cout<<"\n";

     for(i=1;i<=9;i++)//控制行

     {

          cout<

          for(j=1;j<=9;j++)//控制列

          {k=j*i;

cout<

          cout<<"\n";

     }

P36T(3)判断成绩

#include

#include

#include

boolIsNum(char*s);

voidmain()

{

     intN;

     chars[100];  

     do//判断是否为数字,是,IsNum(S)返回true

     {

          cout<<"Pleaseinputascore!

"<

          cin>>s;

     }while(!

IsNum(s));

     N=atoi(s);//把字符串转为整型数

     if(N>=100)

                     cout<<'A'<

     else

                {

                     switch(N/10)

                     {

                           case6:

cout<<'D'<

                           case7:

cout<<'C'<

                           case8:

cout<<'B'<

                           case9:

cout<<'A'<

                           default:

cout<<'E'<

                     }

                }

}

boolIsNum(char*s)

{

   for(inti=0;i

   {

       if(s[i]int('9'))

       returnfalse;

   }

   returntrue;

}

P64T

(1) 递归求10以内加法                

#include

intadd(intk)

{

     intnum;

     if(k==0)

    num=0;

     else

     num=k+add(k-1);

     returnnum; 

}

voidmain()

{

     inti;

     i=10;

     i=add(i);

     cout<

}

P64T

(2)递归求Fibonacci级数

#include

intjs(intn)

{   

     if(n==1||n==2)

          return1;

     else 

       returnjs(n-1)+js(n-2);

}

intmain()

{

     intn;

     cout<<"Pleaseinputthenum:

";

     cin>>n;

     int*a=newint[n];

     for(inti=1;i<=n;i++

{

          a[i-1]=js(i);

        cout<

     }

     return0;

}

P64T(3)绝对值函数模板

#include

template//利用函数模板求绝对值

doubleabsolute(Xj)

{

     

     if(j>=0)

      returnj;

     else

      return-1*j;

}

voidmain()

{

     doublei;

     cout<<"Pleaseinputafigure!

";

     cin>>i;

     i=absolute(i);

     cout<

}

P64T(4)非类实现 Rectangle的面积和周长

#include

#include

voidcomputeTriangle(float&a,float&p,floatx,floaty,floatz)

{

     floatm;

     a=x+y+z;

     m=0.5*(x+y+z);

     p=sqrt(m*(m-x)*(m-y)*(m-z));//海伦公式

}

voidmain()

{

     floati,j,k,s,c;

          c=0,s=0;

     cout<<"Pleaseenterthelengthofthethreesidesofthetriangle"<

     cin>>i>>j>>k;

     computeTriangle(c,s,i,j,k);

     cout<

}

P64T(7)小于1000平方和

#include

voidmain()

{

     inti,d;

     d=0;

     for(i=1;d<1000;i++)

     d+=(i*i);

     cout<

}

P120T

(1)类实现rectangle

#include

classrectangle

{

     private:

          floatx1,y1,x2,y2;

     public:

          rectangle(floatxx1,floatyy1,floatxx2,floatyy2);

          floatarea();

};

rectangle:

:

rectangle(floatxx1,floatyy1,floatxx2,floatyy2)

{x1=xx1;y1=yy1;x2=xx2;y2=yy2;}

floatrectangle:

:

area()

{

return(x1-x2)*(y1-y2);

}

voidmain()

{

     floatx1,y1,x2,y2;

     floatresult;

     cout<<"Pleaseenterthecoordinatevaluesoftherectangleontheupperleftcorner!

(forexample:

00)"<

          cin>>x1>>y1;

     cout<<"Pleaseenterthecoordinatevaluesoftherectangleonthelowerrightcorner!

(forexample:

11)"<

          cin>>x2>>y2;

     rectanglePo(x1,y1,x2,y2);

     

     result=Po.area();

     cout<

}

 

P120T

(2)complex类的构造和拷贝构造函数

#include

classcomplex

{

     private:

                floatreal;

                floatimaginary;

     public:

                complex(floata=0,floatb=0)

                {real=a;imaginary=b;}

                complex(complex&temp);

                floatPrintReal()

                {returnreal;}

                floatPrintimaginary()

                {returnimaginary;}

 

};

complex:

:

complex(complex&temp)

{

     real=temp.real;

     imaginary=temp.imaginary;

     cout<<"拷贝构造函数被调用!

";

}

voidf(complextemp)

{

     cout<

}

complexr(floata,floatb)

{

     complexq(a,b);

     returnq;

}

voidmain()

{

     floata,b;

     cout<<"Pleaseentertherealpartofthecomplex!

"<

     cin>>a;

     cout<<"Pleaseentertheimaginarypartofthecomplex!

"<

     cin>>b;

     complexq(a,b);

     complexm(q);

     cout<

     f(m);

     m=r(a,b);

     cout<

}

P120 类组合求复数加法

#include

classcomplex

{

     private:

                floatreal;

                floatimaginary;

     public:

                complex(floata,floatb)

                {

                     real=a;

                     imaginary=b;

                

                }

 

                floatget_real()

                {returnreal;}

                floatget_imaginary()

                {returnimaginary;}

 

};

classplus

{

     private:

                floatplus_real,plus_imaginary;

                complexa,b;

     public:

          

          plus(complexxa,complexxb):

a(xa),b(xb)

          {

                plus_real=a.get_real()+b.get_real();

                plus_imaginary=a.get_imaginary()+b.get_imaginary();

          }

          floatget_r()

          {

                returnplus_real;

          }

          floatget_i()

          {

                returnplus_imaginary;

          }

};

voidmain()

{   

     floatm,n,o,p;

     cout<<"Pleaseentertherealvalueofthecomplex"<

     cin>>m;

     cout<<"Pleaseentertheimaginaryvalueofthecomplex"<

     cin>>n;

     complexsa(m,n);

     cout<<"Pleaseentertherealvalueofthecomplex"<

     cin>>o;

     cout<<"Pleaseentertheimaginaryvalueofthecomplex"<

     cin>>p;

     complexsb(o,p);

     plussc(sa,sb);

     cout<<"Theresultis"<

     cout<

}

P120 类模板求最大最小值

#include

template

classcompare

{

     private:

                Ta,b;

     public:

                compare(Tx,Ty)

                {a=x;b=y;}

                voidprint()

                {

                     if(a>b)

                     cout<<"Themaximumis "<

                     elseif(a==b)

                           cout<

                           else

                           cout<<"Themaximumis "<

                }

};

voidmain()

{

     intx,y;

     floatm,n;

     cout<<"PleaseentertwoIntegers!

"<

     cin>>x>>y;

     comparep(x,y);

     p.print();

     cout<<"Pleaseentertwodecimals!

"<

     cin>>m>>n;

     compareq(m,n);

     q.print();

}

P154T

(1)静态数据成员cat

#include

classCat

{

     private:

                staticintHowManyCats;

     public:

           Cat(){HowManyCats++;}

           Cat(Cat&c){HowManyCats++;}

          staticvoid GetHowMany()

           {cout<

 

};

intCat:

:

HowManyCats=0;

voidmain()

{   

     Cata;

     a.GetHowMany();

     Catb(a);

     b.GetHowMany();

     Catc;

     c.GetHowMany();

}

P154T

(2) 友元实现学生成绩

#include

#include

classstudent

{

     private:

          charname[20];

            

                floatEnglish_score;

                floatComputerScience_score;

                floatsum_score;

     public:

           student(charc[20],floatE_score,floatCS_score)

           {

                 

                 strcpy(name,c);

               English_score=E_score;    

                  ComputerScience_score=CS_score;

           }

           

 

          floatsum()

          {

                 sum_score=English_score+ComputerScience_score;

                 returnsum_score;

          }

          friendvoidDescendingOrder(studentp1,studentp2);     

};

voidDescendingOrder(studentp1,studentp2) 

{

                if(p1.sum()>p2.sum())

                cout<

                else

                cout<

}

voidmain()

{

     charc1[20],c2[20];

     floatE_score1,CS_score1,E_score2,CS_score2;

     cout<<"Pleaseenterastudent'sname:

";

     cin>>c1;

     cout<<"Pleaseenterthestudent'sEnglishscore:

"<

     cin>>E_score1;

     cout<<"Pleaseenterthestudent's ComputerSciencescore!

"<

     cin>>CS_score1;

     studentm(c1,E_score1,CS_score1);

     cout<<"Pleaseenteranotherstudent'sname!

"<

     cin>>c2;

     cout<<"Pleaseenterthestudent'sEnglishscore!

"<

     cin>>E_score2;

     cout<<"Pleaseenterthestudent's ComputerSciencescore!

"<

     cin>>CS_score2;

     studentn(c2,E_score2,CS_score2);

     DescendingOrder(m,n);

}

P193T(6)指针数组比较字符串

#include

#include

voidsort_country(char*c[],intn)

{

     inti,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1