设为首页收藏本站

新微赢技术网

 找回密码
 注册
搜索
热搜: 回贴
查看: 1402|回复: 3
打印 上一主题 下一主题

关于C++类的使用~

[复制链接]
跳转到指定楼层
1#
发表于 2009-11-4 01:16:15 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
#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;
}
4#
发表于 2009-11-4 01:16:18 | 只看该作者
///都是一些细节的错误,太可惜了。程序修正如下:
#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;
}
///我只做了一些小的修改,注释是根据自己的理解做的,不知对不对~!
回复 支持 反对

使用道具 举报

3#
发表于 2009-11-4 01:16:17 | 只看该作者
你把错误都翻译成中文就知道是哪里错了
回复 支持 反对

使用道具 举报

2#
发表于 2009-11-4 01:16:16 | 只看该作者
干什么的程序啊,,加点注释嘛,,不好看啊
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

申请友链|小黑屋|最新主题|手机版|新微赢技术网 ( 苏ICP备08020429号 )  

GMT+8, 2024-11-18 23:35 , Processed in 0.118339 second(s), 9 queries , Gzip On, Memcache On.

Powered by xuexi

© 2001-2013 HaiAn.Com.Cn Inc. 寰耽

快速回复 返回顶部 返回列表