河北工业大学C课后习题实验习题.docx

上传人:b****9 文档编号:29158620 上传时间:2023-07-20 格式:DOCX 页数:63 大小:30.41KB
下载 相关 举报
河北工业大学C课后习题实验习题.docx_第1页
第1页 / 共63页
河北工业大学C课后习题实验习题.docx_第2页
第2页 / 共63页
河北工业大学C课后习题实验习题.docx_第3页
第3页 / 共63页
河北工业大学C课后习题实验习题.docx_第4页
第4页 / 共63页
河北工业大学C课后习题实验习题.docx_第5页
第5页 / 共63页
点击查看更多>>
下载资源
资源描述

河北工业大学C课后习题实验习题.docx

《河北工业大学C课后习题实验习题.docx》由会员分享,可在线阅读,更多相关《河北工业大学C课后习题实验习题.docx(63页珍藏版)》请在冰豆网上搜索。

河北工业大学C课后习题实验习题.docx

河北工业大学C课后习题实验习题

习题1

1.1 书写一个简单的 C++程序,从而理解 C++程序的结构及特点。

该程序只由一个主函数组成,主函数的函数体中只包含一个语句:

cout<<"ThisisaC++program.";

【1.1 答】

#include

voidmain()

{

cout<<"ThisisaC++program.";

}

 

习题2

2.1      写出 C++标识符的命名规则,写出 5个合法的标识符和 5个不合法的标识符。

2.2      下列符号哪些是 C++标识符?

哪些不是,为什么?

2m  a+c  int  i*j  mm  data  n-a  m22  x  -n  a_1

【2.2 答】

mm  data  m22  x  a_1 是C++ 标识符,int是是 C++标识符也是关键字

2.3      指出下列合法的常量,并说明其类型。

     65538   1.3   2.1E47L  9uL  '9'  'a'  123u

【2.3 答】

65538   整型常量

1.3     double型常量

2.1E4   double型常量

7L     长整型常量

9uL     无符号长整型常量

'9'     字符型常量

'a'     字符型常量

123u    无符号整型常量

2.4      写出符合 C++规则的 int、 double、char 和字符串4种类型的常量各 2个。

2.5      说明字符常量和字符串常量有何区别?

2.6      下列变量的定义是否合法?

为什么?

⑴ INT  i,j,k;     ⑵  charc1,c2;    ⑶ inta,b;floatf,b;

⑷ unsignedintm   ⑸  char:

a1,a2,a3; ⑹ unsignedfloatx,y;

【2.6 答】

⑵和⑶合法。

⑴的INT要小写;⑷少分号;⑸ char后多了冒号;⑹数据类型错误

2.7      将下列数学表示式写成 C++表达式。

(1 )            (2 )

(3)              (4)    【2.7答】

(1 )(2*x*x+3*y*y)/(x-y)

(2 )(-b+sqrt(b*b-4*a*c))/(2*a)    和  (-b-sqrt(b*b-4*a*c))/(2*a)

或 (-b+sqrt(b*b-4*a*c))/2/a        和  (-b-sqrt(b*b-4*a*c))/2/a

(3 )1+(1+a/b)/(1-(a/c)

(4 )x/sqrt(fabs(x*x*x+y*y*y+z*z*z))

2.8      已有如下变量定义,求下列表达式的值。

doublex=1.2,y=8.5;

inta=3;

x+a%3*(int)(x+y)

inta=2,b=3;

doublex=3.5,y=2.5;

(double)(a+b)/2+(int)x%(int)y

intx=4,y=8;

(++x)*(--y)

inte=1,f=4,g=2;

doublem=10.5,n=4.0,k;

k=(e+f)/g+sqrt(n)*1.2/g+m

doublex=2.5,y=4.7;

inta=7;

x+a%3*(int)(x+y)%2/4

inta,b;

intx;

x=(a=2,b=5,a++,b++,a+b)

【2.8 答】

⑴  1.2

⑵ 3.5

⑶ 35

⑷ 13.7

⑸ 2.5

⑹  9

⑴  1.2

#include

voidmain()

{

    doublex=1.2,y=8.5;

    inta=3;

    cout<<(x+a%3*(int)(x+y))<

}

⑵ 3.5

#include

voidmain()

{

    inta=2,b=3;

    doublex=3.5,y=2.5;

    cout<<((double)(a+b)/2+(int)x%(int)y)<

}

⑶ 35

#include

voidmain()

{

    intx=4,y=8;

    cout<<((++x)*(--y))<

}

⑷  13.7

#include

#include

voidmain()

{

    inte=1,f=4,g=2;

    doublem=10.5,n=4.0,k;

    k=(e+f)/g+sqrt(n)*1.2/g+m;

    cout<

}

⑸ 2.5

#include

voidmain()

{

    doublex=2.5,y=4.7;

    inta=7;

    cout<<(x+a%3*(int)(x+y)%2/4)<

}

⑹  9

#include

voidmain()

{

    inta,b;

    intx;

    x=(a=2,b=5,a++,b++,a+b);

    cout<

}

习题3

3.1 编写程序求行驶的距离,输入行驶的速度和时间,输出距离。

voidmain()

{

int  s,t,d;

cin>>s>>t;

d=s*t;

cout<<"d="<

 }

3.2 编写程序求圆柱体的表面积和体积。

程序运行时输入圆柱的半径和高,输出圆柱的表面积和体积。

#include

constdoublePI=3.1415926;

voidmain()

{

     double  r,h,areaf,volume;

     cin>>r>>h;

     areaf=2.*PI*r*h+PI*r*r*2.;

     volume=PI*r*r*h;

     cout<<"area="<

   cout<<"volume="<

}

3.3 输入梯形的上边长、下边长及高度,计算梯形的面积。

#include

constdoublePI=3.1415926;

voidmain()

{

double  d1,d2,h,area;

cin>>d1>>d2>>h;

area=(d1+d2)*h/2.;

cout<<"area="<

 }

 

3.4 编程求输入的两个字符中的最小者。

#include

voidmain()

{

charc1,c2;

cin>>c1>>c2;

cout<<"c1="<

if(c1>c2)

   cout<<"min="<

else

   cout<<"min="<

}

3.5 编程求输入的三个整数中的最大值。

#include

voidmain()

{

     int  a,b,c,max;

     cin>>a>>b>>c;

     max=a;

     if(b>max)max=b;

     if(c>max)max=c;

     cout<<"max="<

}

3.6 编写程序:

输入一个整数,若为 0或正数时,显示 :

ispositive;为负数时,显示:

isnegative。

#include

voidmain()

{

     int  a;

     cin>>a;

      if(a>=0)

             cout<

     else

        cout<

}

3.7 编程求一元二次方程的实根:

当有实根时,输出两个实根;没有实根时,显示提示信息。

实验3  p48 例3.8

3.8 某超市为促进销售,采用了购物打折扣的优惠方法。

设每位顾客一次购物的金额为 b,当 1000≤b<2000 时,给予九五折优惠;当 2000≤b<3000 时,给予九折优惠;当 3000≤b<5000 时,给予八五折优惠;当 b> 5000时,给予八折优惠。

编写程序,输入顾客的购物款,输出顾客的应付款。

该题修改:

b>=5000, 八折。

#include

#include

voidmain()

{

      doubleb;

      cin>>b;

      switch(int(b)/1000)

      {

       case0:

cout<<"m="<

       case1:

cout<<"m="<

       case2:

cout<<"m="<

       case3:

       case4:

       cout<<"m="<

       case5:

       default:

cout<<"m="<

      }    

}

3.9 编一程序,将输入数据归入某一类别,输出其类别。

数据分为四类:

小于 10、 10~ 99、 100~ 999、 1000以上。

例如,输入 358时,显示 “358is100to999”。

#include

#include

voidmain()

{

       intx;

       cin>>x;

       if(x<10)cout<

       if(x>=10&&x<=99)cout<

       if(x>=100&&x<=999)cout<

       if(x==1000)cout<

       if(x>1000)cout<

}

3.10 编一程序,输入字符 a时显示 America,b 时显示Britain, c时显示 China, d时显示 Denmark,其它字符时显示Harland。

#include

#include

voidmain()

{

      charc;

      cin>>c;

      if(c>='A'&&c<='Z')c=c+32;

      switch(c)

      {

       case'a':

cout<<"America"<

       case'b':

cout<<"Britain"<

       case'c':

cout<<"China"<

       case'd':

cout<<"Denmark"<

       default:

cout<<"Harland"<

      }    

}

3.11 编写程序,输入一整数,当其为 0~ 6范围内的整数时,输出对应的星期:

 Sunday、Monday 、Tuesday、 Wednesday、Thursday 、Friday、 Saturday,为其它整数时,输出error。

#include

voidmain()

{

        intn;

        cout<<"pleaseinputaninteger:

";

        cin>>n;

        switch(n)

        {

            case0:

cout<<"Sunday";break;

            case1:

cout<<"Monday";break;

            case2:

cout<<"Tuesday";break;

            case3:

cout<<"Wednesday";break;

            case4:

cout<<"Thursday";break;

            case5:

cout<<"Friday";break;

            case6:

cout<<"Saturday";break;

            default:

cout<<"error!

"<

        }

}

 

 

实验一熟悉 MicrosftVisualC++6.0开发环境

四思考题

#include

voidmain()

{

    inta,b,c;

    cout<<"Pleaseinputa,b=";     // 输出字符串

    cin>>a>>b;  //输入两个数据到变量a、 b中

    c=a*b;      //求乘积存入c

    cout<

}

 

实验二输入 /输出与顺序结构

三 实验内容

1. 阅读程序,写出运行结果。

i+j=15

i*j=50

a=9     b=5

 

2. 程序填空

①   k=i+j

②   i<<'+'<

①   a=c

②   "char"<

 

3. 程序改错

#include

voidmain()

{

    doubler,s,l;

    cout<<"PleaseinputR";

    cin>>r;

    s=3.1416*r*r;

    l=2.0*3.1416*r;

    cout<<"S="<

    cout<<"L="<

}

 

4. 编程题

⑴ 输入华氏温度F,计算输出对应的摄氏温度。

由华氏温度F求摄氏温度c的公式为:

#include

voidmain()

{

    doublef,c;

    cout<<"pleaseinputF=:

";

    cin>>f;

    c=(f-32)*5/9;   //或c=5.0/9*(f-32);

    cout<<"C="<

}

⑵ 输入学生的语文、数学、英语、物理 4门课程的成绩,计算该学生的总成绩和平均成绩并输出。

#include

voidmain()

{

    doubleeng,chin,math,phy,sum,aver;

    cout<<"pleaseinput4scores:

";

    cin>>eng>>chin>>math>>phy;      // 输入成绩

    sum=eng+chin+math+phy;      //计算总成绩

    aver=sum/4;                     // 计算平均分

    cout<<"Sum="<

}

⑶ 编写程序,从键盘输入一个大写英文字母,输出对应的小写字母。

#include

voidmain()

{

charc1,c2;

cout<<"Pleaseinputanupperletter:

";

cin>>c1;

c2=c1+32;

cout<<"c1="<

}

 

实验三选择结构程序设计

三 实验内容

1.选择题

⑴ C

⑵ B

⑶ D

⑷ C

⑸ C

⑹ C

⑺ D

 

2.

-4

4  5   99

2,1

1

 

3. 编程题

⑴ 由键盘输入三个字符,输出其中的最大者。

【源程序】

#include

voidmain()

{

    charx,max;

    cout<<"Pleaseinputthreecharacterss:

";

    cin>>x;

    max=x;

    cin>>x;   

    if(x>max)

        max=x;

    cin>>x;   

    if(x>max)

        max=x;

    cout<<"max="<

}

⑵ 输入三角形三边的长,求三角形的面积。

若输入的三个边能构成三角形,则计算其面积并输出;否则输出提示信息。

【源程序】

//参见教材 P44例 3.15

#include

#include

voidmain()

{

    doublea,b,c,s,area;

    cout<<"Pleaseinputa,b,c=";

    cin>>a>>b>>c;

    if(a+b<=c||a+c<=b||b+c<=a)

    cout<<"cantbeatriangle!

\n";

    else

    {

        s=(a+b+c)/2;

        area=sqrt(s*(s-a)*(s-b)*(s-c));

        cout<<"area="<

    }

}

 

⑶ 编程求下面符号函数值:

 

y=

 

0   (x=0) 

 

1   (x>0) 

 

-1   ( x<0)

 

【源程序】

#include

voidmain()

{

    intx,y;

    cout<<"Pleaseinputx=";

    cin>>x;

    if(x>0)

       y=1;

    elseif(x==0)

       y=0;

    else

       y=-1;

    cout<<"y="<

}

⑷ 计算奖金。

设企业利润为 L,当企业利润 L不超过 5000元时,奖金为利润的 1.5%,当 5000< L≤10000元时,超过5000 元部分奖金为 2%( 5000元以下仍按 1.5%);当 10000< L≤20000元,除10000 以下的按上述方法计算外,超过 10000元部分按 2.5%计算奖金;如果 20000< L≤50000元,超过20000 元部分按3%计算奖金;当 50000< L≤100000元时,超过50000 元部分按3.5%计算奖金;当 L超过 100000元时,超过100000 元部分按4%计算奖金。

由键盘输入 L的值,编程计算相应的奖金并输出。

【源程序】

#include

voidmain()

{

    doubleL,S;

    cout<<"pleaseinputL=";

    cin>>L;

    if(L<5000)

       S=L*0.015;

    elseif(L<10000)

       S=75+(L-5000)*0.02;

    elseif(L<20000)

       S=175+(L-10000)*0.025;

    elseif(L<50000)

       S=175+250+(L-20000)*0.03;

    elseif(L<100000)

       S=175+250+900+(L-50000)*0.035;

    else

       S=175+250+900+1750+(L-100000)*0.04;

    cout<<"S="<

}

 

⑸ 输入年龄,输出所处人群:

 9岁以下为儿童,输出 A; 10~ 19为少年,输出 B; 20~ 29为青年,输出 C; 30~ 49为中年,输出 D; 50以上为老年,输出 E。

【源程序】

#include

voidmain()

{

    intage;

    cout<<"Pleaseinputage:

";

    cin>>age;

    switch(age/10)

    {

    case0:

cout<<"A--儿童\n";break;

    case1:

cout<<"B--少年\n";break;

    case2:

cout<<"C--青年\n";break;

    case3:

    case4:

cout<<"D--中年\n";break;

    default:

cout<<"E--老年\n";break;

    }

}

 

⑹ 有如下函数:

0< t< 1

1≤t< 2

2≤t< 3

3≤t< 4

 

由键盘输入t 值,计算S的值。

【源程序】

//方法一

#include

voidmain()

{

 doublet,s;

cout<<"pleaseinputt=";

cin>>t;

if(t>0&&t<4)

{

   if(t<1)

      s=t*t;

   elseif(t<2)

      s=t*t-1;

   elseif(t<3)

      s=t*t-2*t+1;

   else

     s=t*t+4*t-17;

   cout<<"s="<

}

else

   cout<<"Error!

tcan\’tbelessthan0ormorethan4!

\n";

}

 

//方法二

#include

voidmain()

{

doublet;

cout<<"pleaseinpu

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

当前位置:首页 > 自然科学 > 数学

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

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