如何在每次循环迭代后向字符串中添加一个字符?

How do I add a character to a string after each iteration of a loop c++

本文关键字:添加 字符 一个 字符串 循环 迭代      更新时间:2023-10-16

我试图创建一个罗马计算器,从文件读取。我正在努力弄清楚如何添加字符到字符串。我希望在每次循环迭代后添加一个没有空格的新字符,这将在程序编写答案时使用。

我试过了。

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

string convert_to_Roman(int num)
{
string c;
while (num>0)
{
    string c;
    if (num >= 1000)
    {
        num = num - 1000;
        return c='M';
    }
    else if (num >= 500 && num<1000)
    {
        num = num -500;
        return c = 'D';
    }
    else if (num >= 100 && num<500)
    {
        num = num -100;
        return c= 'C';
    }
    else if (num >= 50 && num<100)
    {
        num = num - 50;
        return c = 'L';
    }
    else if (num >= 10 && num<50)
    {
        num = num - 10;
        return c = 'X';
    }
    else if (num >= 5 && num<10)
    {
        num = num - 5;
        return c = 'V';
    }
    else if (num<5)
    {
        num = num - 1;
        return c = 'I';
    }
    c +=c;
    //cout<<"answerswer= "<< + answer<<endl;
}
cout << c;
}
int convert_from_Roman(string & s)
{
    int num=0;
    int length; //length of string
    length = s.length();
    for (int i = 0; i < length; i++)
    {
        char c = s[i];
        int digit;
        if (c == 'M')
        {
            return num = 1000;
        }
        else if (c == 'D')
        {
            return num = 500;
        }
        else if (c == 'C')
        {
            return num = 100;
        }
        else if (c == 'L')
        {
            return num = 50;
        }
        else if (c == 'X')
        {
            return num = 10;
        }
        else if (c == 'V')
        {
            return num = 5;
        }
        else if (c == 'I')
        {
            return num = 1;
        }
        else
        {
            cout << "invalid entry" << endl;
            continue;
        }
        num += num;
    }
    cout<<num<<endl;
}

void print_Result(/* figure out the calling sequence */)
{
    // fill in your code
}
// Note the call by reference parameters:
string finalAnswer()
{
    string operand1, operand2;
    char oper;
    cout << "enter operation: " << endl;
    cin >> operand1 >> operand2 >> oper;
    int value1, value2, answer;
    value1 = convert_from_Roman(operand1);
    value2 = convert_from_Roman(operand2);
    switch (oper)
    {
    case '+':
        {
        answer = value1 + value2;
        break;
        }
    case '-':
        {   
        answer = value1 - value2;
        break;
        }
    case '*':
        {
        answer = value1*value2;
        break;
        }
    case '/':
        {
        answer = value1 / value2;
        break;
        }
    default:
    {
        cout << "bad operator : " << oper << endl;
        return;
    }
        string answerInRoman = convert_to_Roman(answer);
        return answerInRoman;
        cout << "answerswer= " << answerInRoman << " (" << answer << ") " << endl;
    }

您可以简单地像这样使用连接。

char addThis;
string toThis;
addThis = 'I';
toThis = "V";
toThis += addThis;

toThis = toThis + addThis;

如果你想把数字放在字符串末尾以外的地方,你可以访问字符串的元素,比如数组toThis[0]等于'V'。

如果您没有使用下面提到的std::string,这可以通过动态字符数组和插入方法来完成,该方法可以正确地调整数组的大小,如下所示:

#include <iostream>
using namespace std;
void addCharToArray(char * & array, int physicalSize, int & logicalSize, char addThis)
{
   char * tempPtr;
   if (physicalSize == logicalSize)
   {
      tempPtr = new char[logicalSize + physicalSize];
      for (int i = 0; i < logicalSize; i++)
      {
         tempPtr[i] = array[i];
      }
      delete [] array;
      array = tempPtr;
   }
   array[logicalSize] = addThis;
   logicalSize++;
}
int main()
{
   char addThis = 'I';
   char * toThis;
   int physicalSize = 1;
   int logicalSize = 0;
   toThis = new char[physicalSize];
   toThis[0] = 'V';
   logicalSize++;
   //when adding into the array, you must perform a check to see if you must add memory
   addCharToArray(toThis, physicalSize, logicalSize, addThis);
   for (int i = 0; i < logicalSize; i++)
   {
      cout << toThis[i];
   }
   cout << endl;
   return 0;
}