c++关于从前和现在^_^有难度哦~~

hhaoma 2008-06-11 01:40:44
谁能帮忙把下面的改成class类的面向型的???谢谢啦~~
#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!";
}
}
}
...全文
150 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
DarknessTM 2008-06-11
  • 打赏
  • 举报
回复
可以参考JAVA的入口点设计,在一个类中用一个静态方法……
hhaoma 2008-06-11
  • 打赏
  • 举报
回复
帮忙改改啊~谢谢~
iambic 2008-06-11
  • 打赏
  • 举报
回复
>呵呵~~用一楼的代码~~
一楼的代码还不如我的。
hhaoma 2008-06-11
  • 打赏
  • 举报
回复

//用 下面这个代码:
#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;
}
rularys 2008-06-11
  • 打赏
  • 举报
回复
楼上各位再不要误导人家了,要回答问题就认真点嘛~
楼主我只能先给你顶顶
hhaoma 2008-06-11
  • 打赏
  • 举报
回复
weight=(float * )realloc(weight,(total+1)*sizeof(float));
这句话真的无比难懂啊~~
hhaoma 2008-06-11
  • 打赏
  • 举报
回复
呵呵~~用一楼的代码~~
帮忙看看谢谢啊 ~·
hhaoma 2008-06-11
  • 打赏
  • 举报
回复
这句话应该是增加数字大小用的吧 weight=(float * )realloc(weight,(total+1)*sizeof(float));如果是增加char型的,怎么改呢?
sukyin 2008-06-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hhaoma 的回复:]
再加个问题
上面的只能输入和输出数字,而一输入中文,程序就会出错,望高手帮忙改改,在class类的前提下 增加一个,输入数字,和中文。然后显示中文和数字……。
如:
<输入>
Enter the0cargo's weight,please:123
Enter the0cargo's information,please:呵呵
<输出>
The weight is:123
The information is:呵呵
[/Quote]
你还真打算用楼上那几位的代码?为了class而class的class code?
hhaoma 2008-06-11
  • 打赏
  • 举报
回复
再加个问题
上面的只能输入和输出数字,而一输入中文,程序就会出错,望高手帮忙改改,在class类的前提下 增加一个,输入数字,和中文。然后显示中文和数字……。
如:
<输入>
Enter the0cargo's weight,please:123
Enter the0cargo's information,please:呵呵
<输出>
The weight is:123
The information is:呵呵

xkyx_cn 2008-06-11
  • 打赏
  • 举报
回复
收藏
iambic 2008-06-11
  • 打赏
  • 举报
回复
看到楼上的回复,忍不住也show一下:
#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;
}
xkyx_cn 2008-06-11
  • 打赏
  • 举报
回复
这个牛
All in object!

[Quote=引用 2 楼 zjw6861982 的回复:]
C/C++ codevoid main()
{
Weight w;
}
[/Quote]
zjw6861982 2008-06-11
  • 打赏
  • 举报
回复
void main()
{
Weight w;
}
zjw6861982 2008-06-11
  • 打赏
  • 举报
回复
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;
}

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧