程序中用到了map函数,现在想把map函数得到的结果保存到文件中,应该怎么编程? 20
程序中用到了map函数,现在想把map函数得到的结果保存到文件中,应该怎么编程(自己写的程序报错了)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <map>
using namespace std;
map<float,float> g_mapData;
FILE *fp = NULL;
void insert_map(float jie1,float jiao1)
{
if (g_mapData.find(jiao1) != g_mapData.end() )
{
float jie = g_mapData[jiao1];
g_mapData[jiao1] = jie + jie1;
}else
g_mapData[jiao1]= jie1;
}
void print_map()
{
FILE *fp = NULL;
printf("打印全部\n";
map<float,float>::iterator it=g_mapData.begin();
for(;it!=g_mapData.end();++it)
printf("%lf:%lf | ",it->first,it->second);
printf("\n end \n";
fp = fopen("test.txt", "a";
fprintf(fp, "%2.5f\n", it->first,it->second);
fclose(fp);
}
int main()
{
int n;
double a,b,c,d,e,f,jit,ji1,sum,jiw,xiangliangx,xiangliangy,zongjiao;
sum=0;
jiw=0;
while(1)
{
printf("请输入坐标及两半径\n";
scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f);
double x=fabs(a-c);
double y=fabs(b-d);
double j=sqrt(x*x+y*y);
double h=e+f;
if(j<h)
{
double A;
A=(e*e+j*j-f*f)/(2*j*e);
double ji=0;
ji=acos(A);
ji=ji/3.1415926*180;
int jir=int(ji);
ji=jir;
jit= ji/180*3.1415926;
n=ji/0.1;
printf("与第一个圆的夹角%lf\n",ji);
xiangliangx=c-a;
xiangliangy=d-b;
if(xiangliangy>0)
if(xiangliangx>0)
zongjiao=atan(xiangliangy/xiangliangx);
else
zongjiao=atan(-xiangliangy/xiangliangx)+3.1415926/2;
else
if(xiangliangx>0)
zongjiao=atan(xiangliangy/xiangliangx);
else
zongjiao=atan(-xiangliangy/xiangliangx)-3.1415926/2;
zongjiao = int(zongjiao/3.1415926*180);
zongjiao=zongjiao/180*3.1415926;
for(int i=1;i<n;i++)
{
double x1,x2,x3,jieg1,jieg2,jieg,B,jie0,jie1,jiao1,jiao2,jiao;
B=jit/n;
x1=1+tan(i*B)*tan(i*B);
x2=-2*j;
x3=j*j-f*f;
jieg1=(-x2+sqrt(x2*x2-4*x1*x3))/(2*x1);
jieg2=jieg1*tan(i*B);
jieg=sqrt(jieg1*jieg1+jieg2*jieg2);
jie0=(jieg-2);
jiao1=(zongjiao+B*i)/3.1415926*180;
jiao2=(zongjiao-B*i)/3.1415926*180;
jiao=i*B/3.1415926*180;
jie1=pow(jie0,1.5)*cos(i*B)*f;
sum=sum+jie1;
printf("输出A%d=%lf\n",i+1,jie1);
printf("输出角A%d=%lf\n",i+1,jiao1);
printf("输出角A%d=%lf\n",i+1,jiao2);
insert_map(jie1,jiao1);
}
}
else
printf("两圆无交点";
print_map();
}
printf("输出:%lf\n",sum);
system("pause";
return 0;
}