免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3760 | 回复: 5
打印 上一主题 下一主题

[C++] 关于showpoint问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-07-23 18:06 |只看该作者 |倒序浏览
真的无语了 是编译器问题还是怎么回事呢?
刚刚写了一个程序 使用了showpoint precision  fixed floatfield 但是程序输出并没有显示精度和小数点。。是不是编译器问题呢

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
2 [报告]
发表于 2012-07-23 18:33 |只看该作者
直接贴代码得了

论坛徽章:
0
3 [报告]
发表于 2012-07-23 19:00 |只看该作者
回复 2# bruceteen


#include<iostream>
#include<cstring>

class Stock
{
private:
        char company[30];
        int shares;
        double share_val;
        double total_val;
        void set_tot();
        //{total_val = share_val * shares;};

public:
        Stock();
        Stock(char*co,int n,double pr);
        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() const;
        const Stock& topval(const Stock & s) const;
};


#include<iostream>
#include"stocks.h"

Stock::Stock()
{
        strcpy(company,"no name");
        shares = 0;
        share_val = 0.0;
        total_val = 0.0;
}
Stock::Stock(char *co,int n,double pr)
{
        strncpy(company,co,29);
   company[29] = '\0';
   if(n<0)
   {
           std::cout << "Numbers of shares can't be negative."
                   << company << " shares set to 0.\n";
           shares = 0;
   }
           else
           {
                   shares = n;
          share_val = pr;
          set_tot();
           }
}
void Stock::set_tot()
{total_val = shares * share_val;}
void Stock::acquire(const char* co,int n,double pr)
{
        strncpy(company,co,29);
   company[29] = '\0';
   if(n<0)
   {
           std::cout << "Numbers of shares can't be negative."
                   << company << " 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)
        {
                std::cerr << "Number of shares purchaesd can't be negative. "
                        "Transaction is aborted.\n";
        }
        else
        {
                shares += num;
                share_val = price;
                set_tot();
        }
       
}

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

void Stock::update(double price)
{
        share_val = price;
        set_tot();
}

void Stock::show()const
{
        using std::cout;
        using std::endl;
  cout << "Company: " << company << endl;
  cout << "Shares: " << shares << endl;
  cout << "Share price: $" << share_val << endl;
  cout << "Total worth: $" << total_val << endl;
}

const Stock& Stock::topval(const Stock& s)const
{
        if(s.total_val>total_val)
                return s;
        else return *this;
}

#include<iostream>
#include"stocks.h"

const int STKS = 4;
int main()
{
        using std::cout ;
        using std::ios;
       
        Stock stocks[STKS] =
        {
                Stock("NanoSmart",12,20.0),
                Stock("Boffo Objects",200,2.0),
                Stock("Monolithic",130,3.25),
                Stock("Fleep Enterprises",60,6.5)
        };
        cout.precision(2);
//        cout.setf(ios::fixed|ios::floatfield);
//        cout.setf(ios::showpoint);
        cout << "Stock hodings:\n";
        int st;
        for(st=0;st<STKS;st++)
                stocks[st].show();
        Stock top = stocks[0];
        for(st=1;st<STKS;st++)
                top = top.topval(stocks[st]);
        cout << "\nMost valuable holding:\n";
        top.show();
   

        return 0;
}

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
4 [报告]
发表于 2012-07-23 20:43 |只看该作者
本帖最后由 bruceteen 于 2012-07-24 08:29 编辑

你的代码太长了,我看不了

论坛徽章:
0
5 [报告]
发表于 2012-07-23 21:26 |只看该作者
回复 4# bruceteen


    感觉一定是编译器问题了 我用的vc6.0

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
6 [报告]
发表于 2012-07-24 08:42 |只看该作者
cout.setf(ios::fixed|ios::floatfield);
应该是
cout.setf( ios::fixed, ios::floatfield );
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP