在c ++中添加卢比和paise

时间:2016-12-15 12:28:47

标签: c++

我用c ++编写了一个用于添加卢比和paisa的程序。 我的头文件如下:

#ifndef CURRENTY_H
#define CURRENT_H
class current
{
long Rs;
int paise;
public:
    current()
      {
       Rs=0;
       paise=0;
      }
   void get();
   void operator+=(current &t);
   void put();

};

和我的current.cpp文件是

#include<iostream>
#include"current.h"
void current::get()
 {
    std::cout<<"\nEnter currency in Rs:";
    std::cin>>Rs;
    std::cout<<"\nEnter currency in Paise:";
    std::cin>>paise;
 }
 void current::operator+=(current &t)
 {
  Rs+=t.Rs;
  paise+=t.paise;
  std::cout<<"\n\nThe added currncy Rs: "<<Rs<<"."<<paise;
 }

void current:: put()
{
 std::cout<<Rs<<""<<paise;
 }

我的main.cpp是

  #include <QCoreApplication>
  #include "current.h"
  #include<iostream>
  int main(int argc, char *argv[])
  {
    QCoreApplication a(argc, argv);
    current c1,c2;
    c1.get();
    c2.get();
    std::cout<<"\n\tFirst currency Rs:";
    c1.put();
    std::cout<<"\n\tSecond currncy Rs:";
    c2.put();
    c1+=c2;
    return a.exec();
  }

我得到的输出是

Enter currency in rs: 40
Enter currency in paisa : 90
Enter currency in rs : 50
Enter currency in paisa: 90

first currency is : 40.90
second currency is 50.90

Added currency is : 90.180 

现在我面临的问题是我想要在产出中转换,即添加的货币是:90.180。转换100以上的数字(paisa)将其转换为卢比

 Expected output shoud be :Added currency is : 91.8

如何实现这一目标?

2 个答案:

答案 0 :(得分:-1)

将paise转换为相应的卢比和剩余的paise原样。

 void current::operator+=(current &t)
 {
  Rs+=t.Rs;
  paise+=t.paise;
  Rs+= paise / 100;
  paise = paise % 100;
  std::cout<<"\n\nThe added currncy Rs: "<<Rs<<"."<<paise;
 }

答案 1 :(得分:-1)

简单的方法是将货币RS和paise视为单个值并对该值进行舍入以将paise转换为Rs,如果需要使用以下代码分隔paise,则使用以下代码。

int result = static_cast<int>(number*10)%10;