| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1716 人关注过本帖
标题:关于C++类的使用~
只看楼主 加入收藏
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
结帖率:100%
收藏
 问题点数:0 回复次数:3 
关于C++类的使用~

#include<iostream>
#include<cstring>
using namespace std;
class stock
{
private:
char copany[30];
int shares;
double share_val;
double total_val;
void set_tot (){total_val = shares * share_val;}
public:
void acquire (const char * co,int n,double pr);
void buy (int num ,double price);
void sell (int num,double price);
void update (double price);
void show();
};
void stock::acquire(const char * co,int n,double pr)
{
strncpy(company,co,29);
company [29] = '\0';
if(n<0)
{
cerr << "numble of shares can't be negative:"
<<"shares set to 0.\n";
shares = 0;
}
else shares =n;
shares_val =pr;
set_tot();
}
void stock:: buy (int num,double price)
{
if (num<0)
{
cerr<<"number of shares purchaseed can't be negative."
<<"Transaction is aborted.\n";
}
else{
shares += num;
share_val = price;
set_tot ();
}
}

void stock:: sell (int num,double price)
{
if (num<0)
{
cerr<< "number of shares sold can't be negative."
<<"Transaction is aborted.\n";
}
else if (num > shares)
{
cerr<<"You can't sell more than you have !"
<<"Thansaction is aborted.\n";
}
else
{
shares -=num;
share_val = price;
set_tot();
}
}
void stock:: update (double price)
{
share_val = price;
set_tot();
}

void stock::show()
{
cout<<"company:"<<company
<<"shares:"<<shares<<'\n'
<<"share price: $"<<share_val
<<"total worth:$"<<total_val<<'\n';
}
int main()
{
stock stock1;
stock1.acquire("nanosmart",20,12.50);
cout.self(ios_base::fixed);
cout.precision(2);
cout.self(ios_base::showpoint);
stock1.show();
stock1.buy(15,18.25);
stock1.show();
stock.sell(400,20.00);
stock1.show();
return 0;
}



[此贴子已经被作者于2006-11-22 12:29:37编辑过]

搜索更多相关主题的帖子: void double val int 
2006-11-21 17:39
dlcdavid
Rank: 3Rank: 3
来 自:成都
等 级:新手上路
威 望:6
帖 子:193
专家分:0
注 册:2005-12-23
收藏
得分:0 
干什么的程序啊,,加点注释嘛,,不好看啊

为了C++,我放弃了课本
为了高考,我又放弃了C++
现在而今眼目下,我能做什么?www.
2006-11-21 18:28
smartwind
Rank: 1
等 级:新手上路
威 望:1
帖 子:277
专家分:0
注 册:2006-11-13
收藏
得分:0 
你把错误都翻译成中文就知道是哪里错了

2006-11-22 10:35
flyayi2006
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2006-11-22
收藏
得分:0 

///都是一些细节的错误,太可惜了。程序修正如下:
#include <iomanip> ///输出输入流的格式设置
#include<iostream>
using namespace std;
class stock ///库存类
{
private:
char company[30];
int shares; ///数量
double share_val; // 单价
double total_val; ///总价格
void set_tot (){total_val = shares * share_val;} ///求总价格的函数
public:
void acquire (const char * co,int n,double pr); ///需求
void buy (int num ,double price); //买
void sell(int num ,double price); //卖
void update (double price);
void show(); //显示
};
void stock::acquire(const char * co,int n,double pr) ///需求
{
strncpy(company,co,29);
company [29] = '\0';
if(n<0)
{
cerr << "numble of shares can't be negative:"
<<"shares set to 0.\n";
shares = 0;
}
else shares =n; ///数量
share_val =pr; ///价格
set_tot(); ///求出总价格
}
void stock:: buy (int num,double price)
{
if (num<0)
{
cerr<<"number of shares purchaseed can't be negative."
<<"Transaction is aborted.\n";
}
else{
shares += num; ///原来的数量加上num等于现在的数量
share_val = price; /// 新价格
set_tot (); ///求出新的总价格
}
}

void stock:: sell (int num,double price)
{
if (num<0)
{
cerr<< "number of shares sold can't be negative."
<<"Transaction is aborted.\n";
}
else if (num > shares)
{
cerr<<"You can't sell more than you have !"
<<"Thansaction is aborted.\n";
}
else
{
shares -=num;
share_val = price;
set_tot();
}
}
void stock:: update (double price)
{
share_val = price;
set_tot();
}

void stock::show()
{
cout<<"company:"<<company<<" "
<<"shares:"<<shares<<'\n'
<<"share price: $"<<share_val<<" "
<<"total worth:$"<<total_val<<'\n';
cout<<endl;
}
int main()
{
stock stock1;
stock1.acquire("nanosmart",20,12.50);
cout<<setiosflags(ios_base::fixed); ///输出流的格式设置
cout.precision(2);
cout<<setiosflags(ios_base::showpoint); ///输出流的格式设置
stock1.show();

stock1.buy(15,18.25);
stock1.show();
stock1.sell(8,20.00);
stock1.show();
return 0;
}

///我只做了一些小的修改,注释是根据自己的理解做的,不知对不对~!


2006-11-22 13:21
快速回复:关于C++类的使用~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011973 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved