车库 一个车库有 20 辆汽车待售。
一辆汽车参数如下:
registration brand model ·power。 year of commercialization price
编写程序:
1.获取 20 个车库汽车结构成员 √
2。查看汽车列表 √
3.向用户询问品牌并显示该品牌的汽车列表 ×
4.计算车库汽车的平均价格 √
5.确定最昂贵的汽车 √
#include<stdio.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
class Student{
//创建对象
public:
int price;
int year;
int pow;
char* name ="car";
void print(){
cout<<name<<"的价格是"<<price<<",年份是"<<year<<",功率是"<<pow<<endl;
}
};
int main(){
Student stu;
stu.price=100;
stu.year=2001;
stu.pow=100;
stu.print();
//for循环创建20个车对象
Student arr[20] ;
for(int i=0;i<20;i++){
Student stui;
stui.price=i*100;
stui.year=2001;
srand((unsigned)time(NULL));
stui.pow=i*rand();
stui.print();
arr[i]= stui;
}
int total=0;
int max=0;
for(int i=0;i<20;i++){
//计算平均价格和总价格
total+=arr[i].price;
if(arr[i].price>max){
max=arr[i].price;
}
}
int pjz= total/20;
printf("总价格是:%d\n", total);
printf("平均价格是:%d\n",pjz);
printf("最大的价格是:%d\n",max);
}