c为C ++盐我有关于投射价值的问题 [英] <C++> I have a question about casting a value

查看:89
本文介绍了c为C ++盐我有关于投射价值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一种算法,它接收数字,该数字的数字,然后输出输入数字中使用的所有数字的总和。问题是,如果我使用atoi函数替换每个数组元素中的字符,它将更改为正确的数字(例如,'5' - > 54321)。我犯了什么错误?



我的尝试:



This is an algorithm that receives the number, the digits of that number, and then outputs the sum of all the numbers used in the input number. The problem is that if I use atoi function to replace the character in each array element, it will change to an in correct number (For example, '5' -> 54321). What mistake did I make?

What I have tried:

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
	int N = 0, sum = 0;//N is the number of digits to input. 

	cin >> N;

	char * ptr = new char[N + 1]; //The number is input as a string

	cin >> ptr;

	for (int i = 0; i < N; i++) {
		sum += atoi(&ptr[i]); //Each character stored in the array element is converted to a number and added to the variable sum.
	}

	cout << sum << endl;

	return 0;
}

推荐答案

你不需要 atoi 而你可能会使用更多的 C ++ 功能。

You don't need atoi and you may use a bit more the C++ features.
#include<iostream>
using namespace std;

int main()
{
  string s{};
  cout << "please enter a number\n";
  cin >> s;
  int sum{};
  for ( const auto & c : s)
    sum += (int) (c - '0');

  cout << sum << endl;
}


错误是atoi接受一个字符串,并且您传递的是输入的各种字符的地址。换句话说,假设您输入3表示N和数字1,2和3.以下是将加起来的值:



sum + = 123

总和+ = 23

总和+ = 3



,结果将是149.



我想你想要做的就是像这样单独添加每个角色:



sum + = 1

总和+ = 2

总和+ = 3



这意味着你必须获得字符的二进制值已输入。在ASCII模式下输入字符(在MBCS项目中),这样你就可以通过减去ASCII代码'0'得到它们的二进制等价物:



int binaryValue = ptr [ i] - '0';



并为字符串中的每个值执行此操作。在调试器中尝试这个,或者在循环中打印出每个值和总和,以便更好地了解它是如何工作的。

_________________________________



这是我想到的第一种方式。这是另一种方式,它更接近你所尝试的。



这种方式涉及为每个值转换一个临时字符串。
The mistake is atoi takes a string and you are passing the address of the various characters that were entered. In other words, let's say you entered 3 for N and the numbers 1, 2, and 3. Here are the values that would be added up :

sum += 123
sum += 23
sum += 3

and the result would be 149.

I think what you want to do is add each character individually like this :

sum += 1
sum += 2
sum += 3

This means you have to obtain binary value of the characters that were entered. Characters are entered (in an MBCS project) in ASCII mode so you can get their binary equivalent by subtracting the ASCII code for '0' :

int binaryValue = ptr[i] - '0';

and do that for every value in the string. Try this in the debugger or by printing out each value and the sum in your loop to get a better idea of how this works.
_________________________________

That was the first way that came to mind. Here's another way that it is closer to what you tried.

This way involves making a temporary string to convert for each value.
// all of your existing code until the for loop :
   char temp[2] = { 0 } // initialize to all zeros
   for( int i = 0; i < N; ++i )
   {
       temp[0] = ptr[i];   // copy into temporary string's first position
       sum += atoi( temp );
   }

就是这样。 Temp是一个两个字符的字符串。第二个位置具有空终止符,并且不会更改。字符串中的字符将被放置到第一个位置,并被转换并添加到总和中。

and that's it. Temp is a two character string. The second position has the null terminator and that is not changed. The characters from the string will each be placed into the first position and that is converted and added to the sum.


这篇关于c为C ++盐我有关于投射价值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆