如何在每次循环迭代后向字符串中添加一个字符?
How do I add a character to a string after each iteration of a loop 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<<"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;
}
相关文章:
- 只能向C++添加一定数量的字符
- 如何在C++中为字符串添加字符?
- 如何将字符添加到已经大小的数组中?
- 向字符串添加字符
- 如何编写模板函数来添加整数但连接字符串和字符类型?
- 在字符串中的字符前添加空格C++插入函数
- QT - C++ 向 QString 添加字符' " '
- 添加字符时,编译器会停止优化未使用的字符串
- 如何在预定义字符串中添加字符
- 如何在C++中获取子字符串并在字符串之间添加字符
- 宏在字符串末尾添加字符而不传递源字符串
- 如何在BST C ++中添加字符类型
- 向字符数组 c++ 添加字符
- 向char*[]添加字符时出现问题
- fstream正在为我的文件的每一行添加字符
- 在C++中向字符串添加字符
- 在C++中的其他字符之间添加字符
- 读取文本文件,然后添加字符到列表
- 向QString中的每行添加字符
- 如何添加字符缓冲区的STD字符串放置在结构体