C++编程题

发布时间 : 星期一 文章C++编程题更新完毕开始阅读

1、编写一个程序根据输入的三角形的三条边判断是否能组成三角形,如果可以则输出它的面积和三角形类型(等边、等腰、直角三角形)。 #include #include using namespace std; int main() { double a,b,c; double v,p; cout<<\请输入三角形三条边:\ cin>>a>>b>>c; if(a+b>c&&a+c>b&&b+c>a) { p=(a+b+c)/2; v=sqrt(p*(p-a)*(p-b)*(p-c)); cout<<\该三角形面积是\ if(a==b&&a==c) cout<<\该三角形是等边三角形!\ else if(a==b&&a!=c||a==c&&a!=b||b==c&&b!=a) cout<<\该三角形是等腰三角形!\ if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(c*c+b*b==a*a)) cout<<\该三角形是直角三角形!\ } else cout<<\这三条边组不成三角形!\ return 0; }

2、定义一个学生类,其中有3 个数据成员:学号、姓名、年龄,以及若干成员函数。同时编写main 函数使用这个类,实现对学生数据的赋值和输出。 #include #include using namespace std; class student { int num; string name; int age; public: student(){num=0;name='\\0';age=0;} student(int,string,int); void show(); };

student::student(int a,string b,int c):num(a),name(b),age(c){} void student::show() { cout<<\ cout<<\ cout<<\}

int main() {

student s1(200803986,\梅寒芳\s1.show();

return 0; }

3、从键盘输入若干个学生成绩,统计并输出最高成绩和最低成绩,当输入负数时结束输入。

#include

using namespace std; int main() { double a[100]; double max=0,min=100,t; int i; for(i=0;i<100;i++) { cin>>a[i]; if(a[i]<0) break; else { if(a[i]>max) max=a[i]; if(a[i]

4、 编写一个程序,从键盘输入半径和高,输出圆柱体的底面积和体积。 #include

using namespace std; int main() {

double a,h,s,v; cout<<\半径为:\ cin>>a; cout<<\高为:\ cin>>h; s=3.14159*a*a; v=s*h; cout<<\底面积为:\ cout<<\体积为:\ return 0;

}

5、编写一个程序,输入年、月,打印出该年份该月的天数。 #include main() {

int y,m,d;

printf(\switch(m){ case 1: case 3: case 5: case 7: case 8: case 10:

case 12:d=31;break; case 4: case 6: case 9:

case 11:d=30;break;

case 2:if (y%4==0 && y0!=0 || y@0==0) d=29; else d=28;

}

printf(\}

6、编写函数将化氏温度转换为摄氏温度,公式为C=(F-32)*5/9;并在主函数中调用。 #include using namespace std; double fun(double a); int main() { double f=37.5,c; c=fun(f); cout<<\华氏温度为:\℉\ cout<<\摄氏温度为:\℃\ return 0; }

double fun(double a) { double b; b=(a-32)*5/9; return b; }

7、声明一个Tree(树)类,有成员ages(树龄),成员函数grow(int years)用以对ages 加上years,showage( )用以显示tree对象的ages值。在主函数中定义Tree类对象,并调用成员函数(学生自行指定实参数

#include using namespace std; class Tree {

private: int ages; public: int grow(int years) { ages=ages+years; return ages; } void getage() { cout<<\输入树的树龄:\ cin>>ages; } void showage() {cout<<\该树的年龄是:\};

int main() { Tree ages,years; ages.getage(); ages.grow(5); ages.showage(); return 0; }

8、定义一个复数类,用友元函数实现对双目运算符“+”的运算符重载,使其适用于复数运算。 #include class Complex { private:

double real; double imag; public: Complex(){real=0;imag=0;} Complex(double r,double i):real(r),imag(i){} friend Complex operator+(Complex &c1,Complex &c2); void display(); };

void Complex::display() { cout<

Complex operator+(Complex &c1,Complex &c2) { return Complex(c1.real+c2.real,c1.imag+c2.imag); }

int main() { Complex c1(3,4); Complex c2(4,2.3); Complex c3; c3=c1+c2; c3.display(); return 0; }

9、有一个函数如下: x (x<5) y= x+6 (5<=x<15) x-6 (x>=15)

输入x的值,计算出相应的y值。

#include using namespace std; int main() { int x,y; cin>>x; if(x<5) y=5; if(x>=5&&x<15) y=x+6; if(x>=15) y=x-6; cout<

10、14、17、使用函数重载的方法定义两个重名函数,分别求出整型数的两数之和和浮点数的两数之和,并在主函数中调用。 #include using namespace std; template T add(T a,T b) { T c; c=a+b; return c; }

int main() { int a,b,c;

联系合同范文客服:xxxxx#qq.com(#替换为@)