如何将输入的数字字符串转换为int数组? [英] How do I convert an inputted string of numbers into an int array?

查看:475
本文介绍了如何将输入的数字字符串转换为int数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的编程任务是通过使用数组对长度最大为20位的大整数执行加法和减法运算。指令告诉我们从数组末尾开始存储数字时要执行算术运算。例如:

My programming assignment is to perform addition and subtraction operations with large integers up to 20 digits in length by using arrays. The instructions tell us to perform the arithmetic operations when digits are stored starting from the end of the array. For example:

string value1 = "";
cin >> value1; //input 1234
test[19] = 4;
test[18] = 3;
test[17] = 2;
test[16] = 1;

因此可以更轻松地执行求和和运算。任何未使用的数字都应初始化为0。

so it'll be easier to perform sum and difference operations. Any unused digits should be initialized to 0.

我首先编写了一个for循环,以将test []数组的最后一个索引读取到第一个索引。变量numDigits会跟踪数组中所有非零值。

I started off by writing a for loop to read the last index of the test[] array to the first index. The variable, numDigits, keeps track of all the nonzero values in the array.

include<iostream>
include<string>
using namespace std;

int main()
{
        string value1 = "";
        int numDigits = 0;
        const int Max_Digits = 20;
        int test[Max_Digits] = {0}; 
        test[19] = 10;
        //cin >> value1;

    for (int i = Max_Digits - 1; i >= 0; i--)
    {
        if (test[i] != 0)
            numDigits++;
    }
    cout << "There are " << numDigits << " nonzero values."; //numDigits == 1
       /*cout << "Your number is: " << test[];*/
       return 0;
}

因此,如果用户在字符串变量value1中输入 1234,我希望程序将字符串转换为数字数组并在继续分配之前将其输出为1234(无逗号或空格)。

So if the user enters "1234" into the string variable, value1, I would want the program to convert the string into an array of digits and output it like 1234 (no commas or spaces) before I continue the assignment.

推荐答案

我不确定您是否需要向后插入或向前插入,因此下面的演示会同时进行。选择您的选择。

I wasn't sure whether you needed backwards insertion or forward, so the following demo does both. Pick your choice.

这个想法很简单。对于向后插入,您需要创建一个迭代器 i ,该迭代器被初始化为 Max_Digits-1 ,并随着迭代器的进行而递减通过字符串递增。对于正向插入,您需要使用 std :: string :: length()获得字符串的长度,并使用 Max_Digits-( strLen-i) std :: string :: length()函数将在每次调用时重新计算字符串的长度。

The idea is simple. For backwards insertion you need to create an iterator i which is initialized to Max_Digits-1 and decrements as the iterator that goes through the string increments. For forward insertion, you need to get the length of the string using std::string::length() and assign the value with Max_Digits-(strLen-i). The std::string::length() function will recalculate the length of the string every time which is called. It unnecessary to pay for that cost, so might as well store it in a variable.

#include <iostream>
#include <string>

int main()
{
    std::string value1 = "";

    std::cout<< "Enter a number up to 20 ints long >> ";
    std::cin >> value1;
    std::cout<<std::endl<< "Entered string: " << value1 <<std::endl;

    constexpr int Max_Digits = 20;

    int backwards[Max_Digits] = {0}; 
    int bck_itr = Max_Digits-1;
    for(int i=0; value1[i]!='\0'; ++i, --bck_itr)
        backwards[bck_itr] = value1[i] - '0';

    std::cout<< "Backwards ints: ";
    for (int i=0; i<Max_Digits; ++i)
        std::cout<< backwards[i] <<",";
    std::cout<<std::endl;

    int forward[Max_Digits] = {0};
    int strLen = value1.length();
    for(int i=0; value1[i]!='\0'; ++i)
        forward[Max_Digits-(strLen-i)] = value1[i] - '0';

    std::cout<< "Forward ints:   ";
    for (int i=0; i<Max_Digits; ++i)
        std::cout<< forward[i] <<",";
    std::cout<<std::endl;
}

输入:

12345678

结果是:

Enter a number up to 20 ints long >> 
Entered string: 12345678
Backwards ints: 0,0,0,0,0,0,0,0,0,0,0,0,8,7,6,5,4,3,2,1,
Forward ints:   0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,

示例: https://rextester.com/MDTL51590

ps。如果您不知道 constexpr 是什么,那么在这种情况下,只需将其视为增强的 const

ps. if you don't know what the constexpr is, then in this case, simply consider it as a beefed up const.

这篇关于如何将输入的数字字符串转换为int数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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