Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用递归C++; int-toInteger(常量字符串和str、int-count、int-value){ 如果(str.length()==1){ 值+=(str[0]-'0')*pow(10,计数); 返回值; } 否则{ 值+=(str[str.length()-1]-'0')*pow(10,计数); cout_C++_String_Recursion_Integer - Fatal编程技术网

C++ 使用递归C++; int-toInteger(常量字符串和str、int-count、int-value){ 如果(str.length()==1){ 值+=(str[0]-'0')*pow(10,计数); 返回值; } 否则{ 值+=(str[str.length()-1]-'0')*pow(10,计数); cout

C++ 使用递归C++; int-toInteger(常量字符串和str、int-count、int-value){ 如果(str.length()==1){ 值+=(str[0]-'0')*pow(10,计数); 返回值; } 否则{ 值+=(str[str.length()-1]-'0')*pow(10,计数); cout,c++,string,recursion,integer,C++,String,Recursion,Integer,看看你的代码实际上是如何工作的(正如一些评论中所说的),这实际上并不是对你的问题的回答,但是我应该指出,你的函数是不必要的复杂,并且在调用之间使用共享的可变状态会失去递归的一半 它可以变得更简单和优雅,例如: int toInteger(const string& str, int count,int&value){ if(str.length() == 1) { value += (str[0] - '0')*pow(10,count); retu

看看你的代码实际上是如何工作的(正如一些评论中所说的),这实际上并不是对你的问题的回答,但是我应该指出,你的函数是不必要的复杂,并且在调用之间使用共享的可变状态会失去递归的一半

它可以变得更简单和优雅,例如:

int toInteger(const string& str, int count,int&value){
   if(str.length() == 1) {
      value += (str[0] - '0')*pow(10,count);
      return value;
   }
   else{
      value +=(str[str.length()-1] - '0')*pow(10,count);
      cout << value << endl << str << endl;
      toInteger(str.substr(0,str.length()-1),count+1,value);
   }
   return value;
}

int main(){
   string str;
   cout << "Enter String : ";
   cin >> str;
   int count = 0;
   int value = 0;
   int toint = toInteger(str, count, value);
   cout << toint << endl;   
}

从浮点类型到整数类型的转换舍入为零

如果浮点值非常接近123…但不完全,则会丢失所有分数值,最终得到122

这在这里适用

int toInteger(const string& str) {
    if(str.length() == 0)
         return 0;
    return str[str.length() - 1] - '0' +
        toInteger(str.substr(0, str.length() - 1)) * 10;
}
尝试制作一个非常基本的整数版本的
pow
,供您的函数使用。只是为了测试它

  value += (str[0] - '0')*pow(10,count);
intpow(intx,inty)
{
int结果=1;
而(y--)结果*=x;
返回x;
}
int-toInteger(常量字符串和str、int-count、int-value){
如果(str.length()==1){
值+=(str[0]-'0')*pow(10,计数);
返回值;
}
否则{
值+=(str[str.length()-1]-'0')*pow(10,计数);

cout对我有效-假设你用0来调用它以开始
count
。这是正确的代码吗?你确定你不是在为开始
value
传递-1吗?确信值=0。如果值是-1,字符串长度为2“12”应该是11。你给我们看的并不能解释为什么你会得到
122
。我还可以验证代码是否确实有效。我看到这个程序的一个版本是这样做的,但是我不理解返回调用。现在看它是有意义的。我在想我需要如何为每个字符串字符获得10的幂,所以我继续使用电源。谢谢。不确定为什么否决?它没有回答用户的直接问题(所以我不会否决),但它确实试图回答OP的实际问题所隐含的问题。不是我这么做的!但是,在这种情况下,他的编译器、C运行时或CPU都有问题,因为整型值完全可以用浮点表示。再次感谢。我将返回值改为double,然后在主体中将其转换为int修复了问题。@Dolda2000类似“马车”的东西,我会怀疑。是的。我不知道
pow
能保证什么样的准确性。@用户3249265请记住,如果你有
float
int
转换,你仍然会因为一个错误而冒着这些风险。对于可以用
int
表示的数学,我建议永远不要转换成floats@DrewDormann例如我承认我可能有无论如何,我有点天真。我假设
pow()
应该返回整数参数的精确结果,但四处搜索表明实际上没有这样的保证。所以我想公平地说,这种失败模式毕竟是有效的。尽管这确实让我好奇这是在什么环境下运行的。
int pow( int x, int y )
{
  int result = 1;
  while ( y-- ) result *= x;
  return x;
}

int toInteger(const string& str, int count,int&value){
   if(str.length() == 1) {
      value += (str[0] - '0')*pow(10,count);
      return value;
   }
   else{
      value +=(str[str.length()-1] - '0')*pow(10,count);
      cout << value << endl << str << endl;
      toInteger(str.substr(0,str.length()-1),count+1,value);
   }
   return value;
}