++ DOS, . (*.txt) MS Excel (*.xls).
, . . 10, 200. DOS, *.xls, MS Excel . , .
. , - . , - . .
, . fstream.
:
1. fstream:
#include <fstream>
2. :
fstream f;
3. :
) :
f.open("1.txt", ios::out);
) :
f.open("1.txt", ios::in);
4. :
) :
f<<"x="<<x;
) :
f>>x;
5. :
f.close();
. . , . , :
f.open("1.txt", ios::app);
1. .
. , Visual Studio, Borland C++.
Visual Studio, 8.1. . 8.1\8.1 , 1.txt, . 1.
. 1
1.txt , , -3.27, . 2.
. 2 1
. Visual Studio .
1:
// 8.1.cpp: .
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
double x;
fstream f;
f.open("1.txt", ios::in);
f>>x;
f.close();
cout<<"x="<<x<<endl;
return 0;
}
:
Borland C++, F:/BC/FILES. , . 1.txt F:/BC/FILES, -3.27.
FILES/1.txt, .
|
|
:
#include <iostream.h>
#include <fstream.h>
int main()
{
double x;
fstream f;
f.open("FILES/1.txt", ios::in);
f>>x;
f.close();
cout<<"x="<<x<<endl;
return 0;
}
:
2. , *.txt *.xls.
. a b, =7,2; b =-10.89. , .. . 8.2 Visual Studio.
ftxt 1.txt, fxls 2.xls.
, 1.txt :
a=7.2 b=-10.89
2.xls . \t.
2:
// 8.2.cpp: .
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
double a, b;
fstream ftxt, fxls;
a=7.2;
b=-10.89;
cout<<"a="<<a<<" b="<<b<<endl;
ftxt.open("1.txt", ios::out);
fxls.open("2.xls", ios::out);
ftxt<<"a="<<a<<" b="<<b;
fxls<<"a=\t"<<a<<"\tb=\t"<<b;
ftxt.close();
fxls.close();
return 0;
}
:
DOS:
1.txt:
2.xls:
. 1.txt 2.xls 8.2\8.2, . 3.
. 3
Borland C++.
:
#include <iostream.h>
#include <fstream.h>
int main()
{
double a, b;
fstream ftxt, fxls;
a=7.2;
b=-10.89;
cout<<"a="<<a<<" b="<<b<<endl;
ftxt.open("FILES/1.txt", ios::out);
fxls.open("FILES/2.xls", ios::out);
ftxt<<"a="<<a<<" b="<<b;
fxls<<"a=\t"<<a<<"\tb=\t"<<b;
ftxt.close();
fxls.close();
return 0;
}
:
DOS:
1.txt:
2.xls:
. .. 2 1.txt , 1, 2 FILES . 1.txt . -3.27, 1.
Visual Studio.
3. y =sin x 0≤ x ≤2 0,1.
. . for. , 1.xls MS Excel.
-:
:
// 8.3.cpp: .
//
#include "stdafx.h"
|
|
#include <iostream>
#include <math.h>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
double x, y;
fstream f;
f.open("1.xls", ios::out);
cout<<setw(10)<<"x"<<setw(10)<<"y"<<endl;
f<<"x"<<"\t"<<"y"<<endl;
for(x=0; x<=2; x=x+0.1){
y=sin(x);
cout<<setw(10)<<x<<setw(10)<<y<<endl;
f<<x<<"\t"<<y<<endl;
}
f.close();
return 0;
}
:
DOS:
1.xls:
1.xls :
4. (5, 5) , , , 2.
. , . a[ i ][ j ]=7sin(2,3 ij). 1.xls.
-:
:
// 8.4.cpp: .
//
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
double a[5][5], s, p;
int i, j, k;
fstream f;
f.open("1.xls", ios::out);
cout<<"matrix:"<<endl;
f<<":"<<endl;
for(i=0; i<5; i=i+1){
for(j=0; j<5; j=j+1){
a[i][j]=7.0*sin(2.3*i*j);
cout<<setw(10)<<a[i][j];
f<<'\t'<<a[i][j];
}
cout<<endl;
f<<endl;
}
s=0;
for(i=0; i<5; i=i+1){
for(j=0; j<5; j=j+1){
if(a[i][j]>0){
s=s+a[i][j];
}
}
}
cout<<"s="<<s<<endl;
f<<" =\t"<<s<<endl;
p=1;
for(i=0; i<5; i=i+1){
for(j=0; j<5; j=j+1){
if(i==j){
p=p*a[i][j];
}
}
}
cout<<"p="<<p<<endl;
f<<". . .=\t"<<p<<endl;
k=0;
for(i=0; i<5; i=i+1){
for(j=0; j<5; j=j+1){
if(a[i][j]>2){
k=k+1;
}
}
}
cout<<"k="<<k<<endl;
f<<" .>2 =\t"<<k<<endl;
f.close();
return 0;
}
:
DOS:
1.xls:
. Borland C++, .