找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1408|回复: 3

关于C++类的使用~

[复制链接]
发表于 2009-11-4 01:16:15 | 显示全部楼层 |阅读模式 IP:江苏扬州
#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;
}
发表于 2009-11-4 01:16:16 | 显示全部楼层 IP:江苏扬州
干什么的程序啊,,加点注释嘛,,不好看啊
回复

使用道具 举报

发表于 2009-11-4 01:16:17 | 显示全部楼层 IP:江苏扬州
你把错误都翻译成中文就知道是哪里错了
回复

使用道具 举报

发表于 2009-11-4 01:16:18 | 显示全部楼层 IP:江苏扬州
///都是一些细节的错误,太可惜了。程序修正如下:
#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;
}
///我只做了一些小的修改,注释是根据自己的理解做的,不知对不对~!
回复

使用道具 举报

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

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 01:26 , Processed in 0.103281 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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