(1)用C++基础中的语法编写:一球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下。求它在第5次落地时,共经过了多少米?第5次反弹多高?
(2)用C语言中的结构体编写:输入并显示五个学生的学号、姓名、手机号、语文成绩。
3.编写代码:建立长方体盒子类box,设计2个重载构造函数来为长方体对象进行初始化,设计类的成员函数vol()计算立方体的体积,并存储在一个double变量中。在主函数中,定义box类的2个对象,分别调用不同的构造函数,并显示这个对象的体积。
4、编写代码:定义一个类score, 它含有私有数据成员english(英语分数)、公有成员函数setscore ( )(和printscore( ),其中setscore( ) 可用带默认参数的方式来设置english的值,printscore( )用来输出english的值。在主程序中定义类score的两个对象stu1和stu2,其英语85.5和93.5,然后输出这两个分数。
最好有详细步骤哈,谢谢啦!
#include<iostream>
int main()
{
float distance = 0.0f;
float height = 100;
float bounce = 0.0f;
for (int i = 0; i < 5; i++)
{
distance += height;
height /= 2;
if (i != 4)
distance += height; // 加上反弹高度
else
bounce = height; // 第五次的反弹高度
}
std::cout << "第五次落地时,共经过" << distance << "米, 反弹" << bounce << "米"<<std::endl;
}
#include<stdlib.h>
#include<stdio.h>
struct student
{
char id[32]; // 学号
char name[64]; // 姓名
char phone[16]; // 手机
int yw_score; // 语文成绩
};
int main()
{
struct student students[5];
for (int i = 0; i < 5; ++i)
{
printf("请输入学生%d信息\n", i + 1);
printf("请输入学号:");
scanf("%s", students[i].id);
printf("请输入姓名:");
scanf("%s", students[i].name);
printf("请输入手机号:");
scanf("%s", students[i].phone);
printf("请输入语文成绩:");
scanf("%d", &students[i].yw_score);
}
for (int i = 0; i < 5; ++i)
{
printf("学生%d学号:%s,姓名:%s,手机号:%s,语文成绩:%d\n",
i + 1, students[i].id, students[i].name, students[i].phone, students[i].yw_score);
}
return 0;
}
#include<iostream>
class box
{
public:
box() : box(1, 1, 1) {}
box(double l, double w, double h) : length(l), width(w), height(h) {}
double vol() const { return length * width * height; }
double getLength()const { return length; }
double getWidth() const { return width; }
double getHeight() const { return height; }
private:
double length;
double width;
double height;
};
int main()
{
box b1; // 调用构造函数 box()
box b2(1, 2, 3); // 调用构造函数 box(double, double, double)
// 显示对象体积
std::cout << "b1的长宽高是:" << b1.getLength() << " " << b1.getWidth() << " " << b1.getHeight() << std::endl;
std::cout << "b1的体积是:" << b1.vol() << std::endl;
std::cout << "b2的长宽高是:" << b2.getLength() << " " << b2.getWidth() << " " << b2.getHeight() << std::endl;
std::cout << "b2的体积是:" << b2.vol() << std::endl;
}
#include<iostream>
class score
{
public:
void setscore(double e = 100.0)
{
english = e;
}
void printscore()
{
std::cout << english;
}
private:
double english;
};
int main()
{
score stu1, stu2;
stu1.setscore(85.5);
stu2.setscore(93.5);
std::cout << "stu1的英语分数是";
stu1.printscore();
std::cout << std::endl;
std::cout << "stu2的英语分数是";
stu2.printscore();
std::cout << std::endl;
return 0;
}
4道题都有了,你参考一下吧,望采纳!
using namespace std;
void main()
{
int i;
double sum = 0;
double height = 100;
sum += height; //第1次落地,只向下
//第2-9次落地,有下落和反弹
for(i=2; i<10; i++)
{
height /= 2;
sum += height * 2;
}
height /= 2;
sum += height; //第10次落地,只向下
cout<<"经过总距离:"<<sum<<endl;
cout<<"第10次反弹高度:"<<height<<endl;
}
//用结构体数组完成:有5个学生(包括学号,姓名,成绩),要求按照成绩高低输出学生信息
#include <stdio.h>
struct Stu
{
int num;
char name[20];
int score;
};
int main()
{
int i,j;
struct Stu student[5]={{317,"han",89},{318,"hu",50},{319,"kang",84},{320,"li",70},{321,"lian",76}};
struct Stu temp;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(student[i].score<student[j].score)
{
temp=student[i];
student[i]=student[j];
student[j]=temp;
}
}
}
for(i=0;i<5;i++)
{
printf("%d %6s %d\n",student[i].num,student[i].name,student[i].score);
}
return 0;
}
#include<iostream>
using namespace std;
class Box
{
public:
//(有参)构造函数
Box(int a)
{
length = a;
width = a;
heigth = a;
cout << "Box的构造函数已经调用,长宽高全部初始化为0" << endl;
}
//改变长度
void change_length()
{
int i;
cout << "请输入新的长度:" << endl;
cin >> i;
length=i;
cout << "长度已成功修改成:" << length << endl;
}
void change_width()
{
int i;
cout << "请输入新的宽度:" << endl;
cin >> i;
width =i;
cout << "宽度已成功修改成:" << width << endl;
}
void change_heigth()
{
int i;
cout << "请输入新的高度:" << endl;
cin >> i;
heigth = i;
cout << "高度已成功修改成:" << heigth << endl;
}
void boxS()
{
int i;
i = (2 * (length*width + length*heigth + width*heigth));
cout << "该立方体的表面积为:" << i<<endl;
}
void boxV()
{
int j;
j = length*width*heigth;
cout << "该立方体的体积为" << j << endl;
}
private:
int length;
int width;
int heigth;
};
在这里插入代码片
#include <iostream>
using namespace std;
class score
{
private:
int num;
int math, english, programming;
double a;
public:
void inscore(int n, int m, int e, int p);
void showscore();
};
void score::inscore(int n,int m,int e,int p)
{
num = n;
math = m;
english = e;
programming = p;
//平均
a = (m+e+p) / 3;
}
void score::showscore()
{
cout << "学号:" << "\t" << "数学" << "\t" << "英语" << "\t" << "程序设计" << "\t" << "平均分" << endl;
cout << num << "\t" << math << "\t" << english << "\t" << programming << "\t"<<"\t" << a;
}
int main()
{
//输入学生数
int x;
cout << "请输入学生的数目" << endl;
cin >> x;
cout << endl;
//申请对象数组
score* stu;
stu = new score[x];
//输入各个学生成绩
int nn=0;
double mm=0, ee=0, pp=0;
for (int j = 0; j < x; j++)
{
cout << "学号 ";
cin >> nn;
cout << "数学成绩 ";
cin >> mm;
cout << "英语成绩 ";
cin >> ee;
cout << "程序设计成绩 ";
cin >> pp;
stu[j].inscore(nn,mm,ee,pp);
cout << endl;
}
//列表输出学生成绩
for (int q = 0; q < x; q++)
{
stu[q].showscore();
cout << endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
float sn=100.0,hn=sn/2;
int n;
for(n=2;n<=5;n++)
{
sn=sn+2*hn;/*第n次落地时共经过的米数*/
hn=hn/2; /*第n次反跳高度*/
}
cout<<"一共经过"<<sn<<endl;
cout<<"第5次反弹"<<hn;
}