如何在循环c ++的每次迭代后向字符串添加字符

时间:2014-06-20 02:19:12

标签: c++

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

我试过了。

#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<<"answer= "<< + 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 << "answer= " << answerInRoman << " (" << answer << ") " << endl;
    }

1 个答案:

答案 0 :(得分:0)

你可以简单地使用这样的连接。

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;
}