C++逐行读取文件并把读取每行的各个数值赋值到变量进行数据计算

待处理的数据如下:
(小写字母行的字母是自行加上的,GOTO后的数据分别要赋值到x y z i j k u v w中,各数据间使用逗号分开的,$$后面的三个数据是u v w)
小写字母: x y z i j k u v w

GOTO/23.4932,-81.4323,107.7596,-0.6389673,-0.4383417,0.6321213 $$ 23.4397,-81.6183,107.5881

GOTO/23.4202,-81.2891,107.7604,-0.6391031,-0.4390556,0.6314882 $$ 23.3878,-81.4743,107.6062

GOTO/23.3478,-81.1456,107.7612,-0.6392378,-0.4397696,0.6308548 $$ 23.3212,-81.3314,107.6113

GOTO/23.2754,-81.0020,107.7620,-0.6393714,-0.4404837,0.6302209 $$ 23.2486,-81.1889,107.6112

GOTO/23.2030,-80.8585,107.7628,-0.6395038,-0.4411979,0.6295866 $$ 23.1760,-81.0464,107.6110

逐行读取文件的程序如下:
include //ifstream
include
include //包含getline()
include
using namespace std;
int main(){

string s;

ifstream inf;

inf.open(“F://九坐标生成.txt”); //打开F为文件所在位置,

//打开输出文件

ofstream outf;

outf.open(“F://out.txt”);
while (getline(inf, s)) //getline(inf,s)是逐行读取inf中的文件信息

{

outf << s << ‘\n’;

cout << s << endl << endl;

}

inf.close();

outf.close();

return 0;

}

然后读取文件后把得到的x y z i j k u v w进行如下计算:
include
include
include
define PI 3.1415926
define d 100 //A C轴之间的距离;
define m 50 //刀心点到工件坐标系原点的距离;
int main()
{

float i=0.0f,j=0.0f,k=0.0f; //定义坐标轴矢量;

float A=0.0f,C=0.0f; //定义旋转轴的角度;

float x=0.0f,y=0.0f,z=0.0f; //工件坐标系下的刀心点x y z值;

float X=0.0f,Y=0.0f,Z=0.0f; /机床坐标系下的刀心点X Y Z值/

float u=0.0f,v=0.0f,w=0.0f; /工件坐标系下的切触点u v w值/

float U=0.0f,V=0.0f,W=0.0f; /机床坐标系下的切触点U V W值/
//输入各工件坐标系下的坐标值
printf(“请输入工件坐标系下x y z i j k u v w的值:”);
scanf("%f %f %f %f %f %f %f %f %f",&x,&y,&z,&i,&j,&k,&u,&v,&w);

//坐标值的计算
A=acos(k);
if((k!=-1&&j0&&i>0)||(k!=1&&j0&&i>0))
printf(“C=PI/2”);
else if((k!=-1&&j0&&i<0)||(k!=1&&j0&&i<0))
printf(“C=-PI/2”);
else if(k==-1||k==1)
printf(“C为任意值”);
else
C=atan(i/-j);
X=xcos©+ysin©;
Y=-x*cos(A)sin©+ycos(A)cos©+(z+d)sin(A);
Z=-d+x
sin(A)sin©-ysin(A)cos©+(z+d)cos(A)-m;
U=u
cos©+v
sin©;
V=-u
cos(A)sin©+vcos(A)*cos©+(w+d)sin(A);
W=-d+u
sin(A)sin©-vsin(A)cos©+(w+d)cos(A)-m;
printf("\nX Y Z A C U V W的值是X%.3f Y%.3f Z%.3f A%.3f C%.3f U%.3f V%.3f W%.3f",X,Y,Z,A
180/PI,C
180/PI,U,V,W);
return 0;
}

解决方案:

#include //ifstream
#include
#include //包含getline()
#include
using namespace std;

int main(){
string s;
ifstream inf;
inf.open("/Users/xiaokang/Desktop/test/test.txt"); //打开F为文件所在位置,
//打开输出文件
ofstream outf;
outf.open(“test.txt”);

int tag =0;
int pos =0;
while (getline(inf, s)) //getline(inf,s)是逐行读取inf中的文件信息
{
    double value[1005];
    tag=0;
    string s1 = s.substr(5,s.length()-5);
    string s2 = "";
    for(int i=0;i<s1.length();i++)
    {
        if(s1[i]==','||s1[i]==' ')
        {
            value[tag++] = atof(s2.c_str());
            s2="";
        }
        else if(s1[i]!='$')
        {
            s2+=s1[i];
        }
    }
    if(s2!="")
        value[tag++]=atof(s2.c_str());
    
    for(int i=0;i<tag;i++)
    {
        cout<<value[i]<<" ";
    }
    cout<<endl;
}
inf.close();
outf.close();
return 0;

}

路径须是绝对路径,value数组是保存x y z i j k u v w的值的数组
输出结果:
23.4932 -81.4323 107.76 -0.638967 -0.438342 0.632121 0 23.4397 -81.6183 107.588

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值