65,210
社区成员
发帖
与我相关
我的任务
分享#include <iostream.h>
#include <stdlib.h>
#define Min 0
void main()
{
int total=0;
float * weight;
weight=new float[Min];
int i;
for(i=0;i<Min;i++)
{
weight[i]=0.0;
}
char c;
while(true)
{
cout<<"\n* * * * * * * * * * * * * * * * * * * * * * * * * *";
cout<<"\n a: 输入数据;";
cout<<"\n t:显示所有;";
cout<<"\n q:退出;";
cout<<"\n* * * * * * * * * * * * * * * * * * * * * * * * * *";
cout<<"\n 请选择:";
cin>>c;
switch(c)
{
case'a':
if(total>=Min)
{
weight=(float * )realloc(weight,(total+1)*sizeof(float));
}
cout<<"Enter the"<<total<<"cargo's weight,please:";
cin>>weight[total];
total++;
break;
case't':
for(i=0;i<total;i++)
{
cout<<"The"<<i<<" weight is:"<<weight[i]<<endl;
}
break;
case'q':
delete[]weight;
return;
default:
cout<<"\n Your enter is error,select again!";
}
}
}
//用 下面这个代码:
#include <iostream.h>
#include <stdlib.h>
#define Min 0
class Weight
{
private:
float *weight;
int total;
public:
Weight();
~Weight();
};
Weight::Weight()
{
weight=new float[0];
total = 0;
int i = 0;
char c=0;
while(true)
{
cout<<"\n* * * * * * * * * * * * * * * * * * * * * * * * * *";
cout<<"\n a: 输入数据;";
cout<<"\n t: 显示所有;";
cout<<"\n q: 退出;";
cout<<"\n* * * * * * * * * * * * * * * * * * * * * * * * * *";
cout<<"\n请选择:";
cin>>c;
switch(c)
{
case'a':
weight=(float * )realloc(weight,(total+1)*sizeof(float));
cout<<"Enter the "<<total<<" cargo's weight,please:";
cin>>weight[total];
total++;
break;
case't':
for(i=0;i<total;i++)
{
cout<<"The "<<i<<" weight is: "<<weight[i]<<endl;
}
break;
case'q':
return;
default:
cout<<"\n Your enter is error,select again!";
break;
}
}
}
Weight::~Weight()
{
delete []weight;
}
void main()
{
Weight w;
}#include <iostream>
#include <stdlib.h>
using namespace std;
#define Min 0
class Main
{
public:
Main()
{
int total = 0;
float * weight;
weight = new float[Min];
int i;
for (i = 0;i < Min;i++)
{
weight[i] = 0.0;
}
char c;
while (true)
{
cout << "\n* * * * * * * * * * * * * * * * * * * * * * * * * *";
cout << "\n a: 输入数据;";
cout << "\n t:显示所有;";
cout << "\n q:退出;";
cout << "\n* * * * * * * * * * * * * * * * * * * * * * * * * *";
cout << "\n 请选择:";
cin >> c;
switch (c)
{
case'a':
if (total >= Min)
{
weight = (float * )realloc(weight, (total + 1) * sizeof(float));
}
cout << "Enter the" << total << "cargo's weight,please:";
cin >> weight[total];
total++;
break;
case't':
for (i = 0;i < total;i++)
{
cout << "The" << i << " weight is:" << weight[i] << endl;
}
break;
case'q':
delete[]weight;
return;
default:
cout << "\n Your enter is error,select again!";
}
}
}
};
int main()
{
Main m;
}
void main()
{
Weight w;
}class Weight
{
private:
float *weight;
int total;
public:
Weight();
~Weight();
};
Weight::Weight()
{
weight=new float[0];
total = 0;
int i = 0;
char c=0;
while(true)
{
cout<<"\n* * * * * * * * * * * * * * * * * * * * * * * * * *";
cout<<"\n a: 输入数据;";
cout<<"\n t: 显示所有;";
cout<<"\n q: 退出;";
cout<<"\n* * * * * * * * * * * * * * * * * * * * * * * * * *";
cout<<"\n请选择:";
cin>>c;
switch(c)
{
case'a':
weight=(float * )realloc(weight,(total+1)*sizeof(float));
cout<<"Enter the "<<total<<" cargo's weight,please:";
cin>>weight[total];
total++;
break;
case't':
for(i=0;i<total;i++)
{
cout<<"The "<<i<<" weight is: "<<weight[i]<<endl;
}
break;
case'q':
return;
default:
cout<<"\n Your enter is error,select again!";
break;
}
}
}
Weight::~Weight()
{
delete []weight;
}