帮忙 :谁帮我加几句话,把输出结果输出到一个TXT文件中去?
我是新手,还不会文件操作,程序如下,谁能帮我写句话啊,把结果输出到c:\dev\c++\1.txt下啊,先谢了
#include <iostream>
#include <cctype>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
float radiu=1.15,length=7.96;
const float pi=3.14159;
float volume=0;
float area1=0;
float area2=0;
float height_triangle=0;
float total_area=0;
float height=0;
double angle=0;
total_area=pi*1.15*1.15;
for(height=0;height <=1.16;height+=0.01)
{
angle=acos(float((radiu-height)/radiu));
height_triangle=sqrt(radiu*radiu-(radiu-height)*(radiu-height));
area1=angle*radiu*radiu-height_triangle*(radiu-height);
area2=total_area-area1;
volume=area2*length;
cout < <setw(30) < < "高度为 " < <height < <setw(30) < < "体积为 " < <volume < <endl;
}
for(height=1.15;height <=2.30;height+=0.01)
{
angle=acos(float((height-radiu)/radiu));
height_triangle=sqrt(radiu*radiu-(height-radiu)*(height-radiu));
area2=angle*radiu*radiu-height_triangle*(height-radiu);
volume=area2*length;
cout < <setw(30) < < "高度为 " < <height < <setw(30) < < "体积为 " < <volume < <endl;
}
system( "Pause ");
return 0;
}
[解决办法]
简单搞一下:
#include <iostream>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <fstream> ////////////////这是加上去的;
using namespace std;
int main()
{
float radiu=1.15,length=7.96;
const float pi=3.14159;
float volume=0;
float area1=0;
float area2=0;
float height_triangle=0;
float total_area=0;
float height=0;
double angle=0;
total_area=pi*1.15*1.15;
ofstream myfile( "c:\\dev\\c++\\1.txt "); /////文件目录自己要先建好。我没检查这个错 否则。。
for(height=0;height <=1.16;height+=0.01)
{
angle=acos(float((radiu-height)/radiu));
height_triangle=sqrt(radiu*radiu-(radiu-height)*(radiu-height));
area1=angle*radiu*radiu-height_triangle*(radiu-height);
area2=total_area-area1;
volume=area2*length;
cout < <setw(30) < < "高度为 " < <height < <setw(30) < < "体积为 " < <volume < <endl;
myfile < <setw(30) < < "高度为 " < <height < <setw(30) < < "体积为 " < <volume < <endl; ///////////////
}
for(height=1.15;height <=2.30;height+=0.01)
{
angle=acos(float((height-radiu)/radiu));
height_triangle=sqrt(radiu*radiu-(height-radiu)*(height-radiu));
area2=angle*radiu*radiu-height_triangle*(height-radiu);
volume=area2*length;
cout < <setw(30) < < "高度为 " < <height < <setw(30) < < "体积为 " < <volume < <endl;
myfile < <setw(30) < < "高度为 " < <height < <setw(30) < < "体积为 " < <volume < <endl; //////////////
}
myfile.close();////////////////
system( "Pause ");
return 0;
}
[解决办法]
最简单的办法,比如编译产生了 test.exe这个最终文件
就在命令行执行
test.exe > > 1.txt