超载“;cin";对于一个新的任意浮点数 我想在C++中重载输入操作符,以便我能正确地替换新类型的输入,我有以下的 struct{ uint64_t frac; unit64_t exp; };

超载“;cin";对于一个新的任意浮点数 我想在C++中重载输入操作符,以便我能正确地替换新类型的输入,我有以下的 struct{ uint64_t frac; unit64_t exp; };,c++,C++,我想要在frac中浮点数之后和exp中浮点数之前出现的内容 请帮忙 试试这样做 struct Number //Changed this because I don't know what you are looking for { int frac; double exp; }; istream& operator>>(istream& in, Number& number) { double temp

我想要在frac中浮点数之后和exp中浮点数之前出现的内容


请帮忙

试试这样做

struct Number               //Changed this because I don't know what you are looking for
{
    int frac;
    double exp;
};


istream& operator>>(istream& in, Number& number)
{
    double temp;
    in >> temp;
    number.frac = (int)temp;             //You need to calculate frac here not sure what you are looking for
    number.exp = temp - number.frac;     //Same for exp
    return in;
}

假设您不知道用户定义类型的流输入是如何实现的,并且您要求的是该信息,而不是要求某人为您编写自定义输入函数:

要为自己的类型重载输入操作,只需在定义类型的同一名称空间中提供一个具有兼容签名的非成员
运算符>>

struct S {int i}; // type to read from input stream

std::istream &operator >> (std::istream &is, S &s) {
  // read from stream
  int i;
  is >> i;
  // check for errors, validate stream input, etc.
  // ...

  // return the input stream
  return i;
}

int main() {
    S s;
    std::cin >> s;
}
通常,
operator>
需要访问该类型的私有成员。您可以将操作员声明为朋友。事实上,您可以同时定义非成员
运算符>>
,它将被放入正确的命名空间中:

class C {
  int i;

  friend std::istream &operator >> (std::istream &is, S &s) {
    // ...
  }
};
模板
std::basic\u istream&operator>>(std::basic\u istream&str,
数字(个){
char ch;
返回str>>num.exp>>ch>>num.frac;
}

然而,这是一种有点奇怪的输入格式。通常小数点左侧的数字是分数的一部分,这些数字的数字用于调整指数。

您可以使用字符串输入数字,然后通过重载
填充
结构的
元素
预编码#包括iostream
#包含字符串
结构压裂{
无符号长分形;//uint64\u t
无符号长exp;//uint64\u t
friend std::istream&operator>(std::istream&is,压裂与压裂)
{
std::字符串s;
是>>s;
std::size\u t n=s.find(“.”);
if(n!=std::string::npos)
{
f、 frac=std::环礁(s.substr(0,n.c_str());
f、 exp=std::环礁(s.substr(n+1).c_str());
}
其他的
{
f、 frac=std::环礁(s.c_str());
f、 exp=0;
}
}
};
int main()
{
压裂液f;
标准:cin>>f;


std::难道我不明白。请举个例子?您遇到了什么问题?基本上,如果用户输入132.3232,则>>将需要自动将132置于frac中,将3232置于exp中。您是否真的要求此代码不在没有精确的64位无符号类型的系统上编译?
uint\u fast64\u t
可能会更好的方法是,
unsigned long long
@Pepe No不会。Streams不会忽略
。作为friend的替代方法,您可以定义一个read(std::istream&in)成员函数,然后在操作符>>(std::istream in,S&S)中调用read on S并将其传入ie.S.read(in);read做了所有的工作。也许这就是他对变量的意思。你不是说小数点右边的数字是分数的一部分吗?@KeithThompson-不,我是说:通常小数点左边的数字是分数的一部分,而不是问题中指定的指数的一部分。使用以10为基数,.111的分数为.111,指数为0;1.11的分数为.111,指数为1.u,但是如果将frac分为两部分,例如struct{uint64_t exp;uint32_t frac1;uint32_t frac2;},我该怎么做呢;你能给我一个输入示例吗?我不确定你所说的frac和exp是什么意思。不,没有输入只是一个浮点数,但我的意思是,我决定将分数部分设为96位,我想到的最好方法是将frac分为3部分frac1、frac2、frac3,那么我如何将从用户那里得到的信息分配到这些frac中s@user3038489:何时你说输入是一个浮点值,你真的是说它是一个可以表示浮点值的字符串吗?如果输入是一个类型为
float
的值,你的问题就不清楚了。@user3038489好吧,这是一个完全不同的问题,我的朋友,需要更多的处理。这对
给出了合理的结果吗>1.23
1.230
1.023
?@KeithThompson是的,不,但这是问题缺陷如果你认识到问题中的缺陷,我建议你应该指出它。@KeithThompson好的,谢谢如果分数是
double
类型,那么你最好忘记结构,用
double
来保存分数号码。
template <class Elem, class Tr>
std::basic_istream<Elem, Tr>& operator>>(std::basic_istream<Elem, Tr>& str,
    Number num) {
    char ch;
    return str >> num.exp >> ch >> num.frac;
}
#include <iostream>
#include <string>

 struct Frac {
           unsigned long  frac; //uint64_t
           unsigned long exp;  //uint64_t
   friend std::istream& operator >>(std::istream& is, Frac &f)
   {
      std::string s;
      is >> s;
      std::size_t n = s.find(".");

      if (n != std::string::npos)
      {
         f.frac = std::atoll(s.substr(0,n).c_str());
         f.exp = std::atoll(s.substr(n+1).c_str());
      }
      else
      {
         f.frac = std::atoll(s.c_str());
         f.exp  = 0 ;
      }
   }
};

int main()
{
  Frac f;

  std::cin>>f;

  std::cout << f.frac <<" "<< f.exp;
}