目录
一、程序说明
昨天我的妈妈需要实现一个程序,用于转换机械刀指令。
于是今天我用C++写了一个机械刀指令转换,实现的功能和付费软件MAX-PP一模一样。
需要实现的功能如下:
源文件是一系列的伪代码指令,无法运用至机器上,需要进行一定的转换,转换成相应指令即可。
比如有这样一行语句:
GOTO/2.9225,85.8759,4.9841,0.054564,0.599282,0.798676 $$PT14
其中有6个数,分别表示六个切割坐标。
我们需要将其转换成:
LN X2.9225 Y85.8759 Z4.9841 TX0.054564 TY0.599282 TZ0.798676
并附上序号。
二、整体思路
2.1 数字提取
首先需要一个数字提取函数,用于把原来语句中的字符串类型的数字提取至一个容器中:
vector<string> getNumber(string s)
{
int begin = 5;
int end = 5;
vector<string> ans;
while (1)
{
while (s[end] != ',' && s[end] != '\ ')
{
end++;
}
string sub = s.substr(begin, end - begin);
ans.push_back(sub);
if (s[end] == '\ ')
{
break;
}
end++;
begin = end;
}
return ans;
}
2.2 转换目标字符串
有了数字,就可以构造目标字符串:
string getDest(vector<string> ans)
{
string dest = "LN ";
vector<string> way;
way.push_back("X");
way.push_back("Y");
way.push_back("Z");
way.push_back("TX");
way.push_back("TY");
way.push_back("TZ");
for (int i = 0; i < ans.size(); i++)
{
dest += way[i];
dest += ans[i];
dest += " ";
}
return dest;
}
2.3 序号(实现数字转换成字符串)
string numberToString(int number)
{
string ans;
vector<int> v;
if (number == 0)
{
v.push_back(0);
}
else
{
while (number)
{
int num = number % 10;
v.push_back(num);
number = (number - num) / 10;
}
}
for (int i = 0; i < v.size()/2; i++)
{
int ret = v[i];
v[i] = v[v.size() - i - 1];
v[v.size() - i - 1] = ret;
}
for (int i = 0; i < v.size(); i++)
{
char n = v[i] + '0';
ans += n;
}
return ans;
}
2.4 主函数逻辑
用到了读文件和写文件操作,以及上述函数组件。
int main()
{
ofstream ofs;
ofs.open("C:\\Users\\asus\\Desktop\\dest.txt", ios::out);
ifstream ifs;
ifs.open("C:\\Users\\asus\\Desktop\\text.txt", ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return 0;
}
string buf;
int number = 0;
while (getline(ifs, buf))
{
string num = numberToString(number);
vector<string> v = getNumber(buf);
string dest = getDest(v);
ofs << num << dest << endl;
number++;
}
ifs.close();
return 0;
}
三、总代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
vector<string> getNumber(string s)
{
int begin = 5;
int end = 5;
vector<string> ans;
while (1)
{
while (s[end] != ',' && s[end] != '\ ')
{
end++;
}
string sub = s.substr(begin, end - begin);
ans.push_back(sub);
if (s[end] == '\ ')
{
break;
}
end++;
begin = end;
}
return ans;
}
string getDest(vector<string> ans)
{
string dest = "LN ";
vector<string> way;
way.push_back("X");
way.push_back("Y");
way.push_back("Z");
way.push_back("TX");
way.push_back("TY");
way.push_back("TZ");
for (int i = 0; i < ans.size(); i++)
{
dest += way[i];
dest += ans[i];
dest += " ";
}
return dest;
}
string numberToString(int number)
{
string ans;
vector<int> v;
if (number == 0)
{
v.push_back(0);
}
else
{
while (number)
{
int num = number % 10;
v.push_back(num);
number = (number - num) / 10;
}
}
for (int i = 0; i < v.size()/2; i++)
{
int ret = v[i];
v[i] = v[v.size() - i - 1];
v[v.size() - i - 1] = ret;
}
for (int i = 0; i < v.size(); i++)
{
char n = v[i] + '0';
ans += n;
}
return ans;
}
int main()
{
ofstream ofs;
ofs.open("C:\\Users\\asus\\Desktop\\dest.txt", ios::out);
ifstream ifs;
ifs.open("C:\\Users\\asus\\Desktop\\text.txt", ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return 0;
}
string buf;
int number = 0;
while (getline(ifs, buf))
{
string num = numberToString(number);
vector<string> v = getNumber(buf);
string dest = getDest(v);
ofs << num << dest << endl;
number++;
}
ifs.close();
return 0;
}