二、课后练习答案:(不抄题目,没有课本的人可以到网上去下一本) 2.6. (1)自己运行一下就知道了 (2)这个程序的作用是已知三角形三边求三角形的面积 (3)转载 //#include <stdio.h> #include <iostream.h> #include <iomanip.h> #include <math.h> void main() { float a,b,c,s,area; //printf("please input 3 sides of one triangle:\n"); cout <<"please input 3 sides of one triangle:\n"; //scanf("%f,%f,%f",&a,&b,&c); //输入时以逗号作为数据间隔 cin >>a >>b >>c; //输入时以空格作为数据间隔 s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); //printf("a=%7.2f,b=%7.2f,c=%7.2f\n",a,b,c); cout <<setiosflags(ios::fixed) <<setprecision(2) <<"a=" <<setw(7) <<a <<",b=" <<setw(7) <<b <<",c=" <<setw(7) <<c <<endl; //printf("area of triangle is %10.5f",area); cout <<"area of triangle is " <<setw(10) <<setprecision(5) <<area <<endl; } (4)转载 #include <iostream.h> #include <iomanip.h> #include <math.h> float area(float a, float b, float c); //函数声明 void main() { float a,b,c; cout <<"please input 3 sides of one triangle:\n"; cin >>a >>b >>c; //输入时以空格作为数据间隔 float result = area(a,b,c); //函数调用 cout <<setiosflags(ios::fixed) <<setprecision(2) <<"a=" <<setw(7) <<a <<",b=" <<setw(7) <<b <<",c=" <<setw(7) <<c <<endl; cout <<"area of triangle is " <<setw(10) <<setprecision(5) <<result <<endl; } float area(float a, float b, float c) //函数定义 { float s=(a+b+c)/2; return sqrt(s*(s-a)*(s-b)*(s-c)); } 2.7 说明: (1).这个程序主要是在演示函数调用的调用过程 (2).最后的语句getche();的作用是输入一个字符,当然你可以把这个输入的字符赋值给你所定义的某个字符 结果是: In main(): Enter two numbers: 5 6 Calling add(): In add(),received 5 and 6 and return 11 Back in main(): c was set to 11 Exiting... aPress any key to continue 2.8 #include<iostream> using namespace std; const double PI=3.14159; double Calvolume(double,double);//函数声明 void main() { double radius,height,volume; cout<<"Enter The radius&height of column:\n"; cin>>radius>>height; volume=Calvolume(radius,height);//函数调用 cout<<"The volume of colunm is "<<volume<<endl; } double Calvolume(double radius,double height)//函数定义 { return PI*radius*radius*height; } That is OK! Thank you!

评论