Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.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+的每次迭代后向字符串添加字符+;_C++ - Fatal编程技术网

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

C++ 如何在循环c+的每次迭代后向字符串添加字符+;,c++,C++,我正在尝试创建一个罗马计算器,可以读取文件。我正在努力弄清楚如何在字符串中添加字符。我想在循环的每次迭代后添加一个没有空格的新字符,这将在程序编写答案时使用 我试过这个 #include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; string convert_to_Roman(int num) { string

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

我试过这个

#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;
    }
#包括
#包括
#包括
#包括
使用名称空间std;
字符串转换为罗马(int num)
{
字符串c;
while(num>0)
{
字符串c;
如果(数值>=1000)
{
num=num-1000;
返回c='M';
}

否则,如果(num>=500&&num=100&&num=50&&num=10&&num=5&&num,您可以像这样简单地使用串联

char addThis;
string toThis;

addThis = 'I';
toThis = "V";

toThis += addThis;

如果要将数字放在字符串末尾以外的某个位置,可以访问字符串的元素,如数组
到该[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;
}
#包括
使用名称空间std;
void addCharToArray(char*&数组、int-physicalSize、int&logicalSize、char-addThis)
{
char*temptr;
if(physicalSize==logicalSize)
{
tempPtr=新字符[逻辑大小+物理大小];
for(int i=0;i你确定他使用的是
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;
}