.
()
1.
(.txt) .
, eof()
//arb2014_file1
#include<fstream>
#include<iostream>
using namespace std;
void main()
{
ifstream in("arb.txt");
ofstream out("out.txt");
ofstream out1("out1.txt");
int x;
cout<<"file in"<<endl;
if(!in)
{
cout<<"can't open file!"<<endl;
exit(-1);
}
cout<<"file in:"<<endl;
out<<"file in:"<<endl;
out1<<"file in:"<<endl;
while(!in.eof())
{
in>>x;
cout<<"!in.eof()= "<<!in.eof()<<" "<<x<<endl;
out1<<"!in.eof()= "<<!in.eof()<<" "<<x<<endl;
out<<x<<' ';
}
out<<endl; out1<<endl;
out1.close(); out.close(); in.close();
cout<<endl;
}
File in(arb.txt)
1 2 3 4 5 6 7 8 9 10 11
File out(out.txt)
file in:
1 2 3 4 5 6 7 8 9 10 11
File out1(out1.txt)
file in:
!in.eof()= 1 1
!in.eof()= 1 2
!in.eof()= 1 3
!in.eof()= 1 4
!in.eof()= 1 5
!in.eof()= 1 6
!in.eof()= 1 7
!in.eof()= 1 8
!in.eof()= 1 9
!in.eof()= 1 10
!in.eof()= 0 11
2.
(.txt) .
,
//arb2014_file2
#include<fstream>
#include<iostream>
using namespace std;
void main()
{
ifstream in("arb.txt");
ofstream out2("out2.txt");
ofstream out1("out1.txt");
int x;
if(!in)
{
cout<<"can't open file!"<<endl;
exit(-1);
}
out1<<"file odd numbers:"<<endl;
out2<<"file even numbers:"<<endl;
while(!in.eof())
{
in>>x;
if (x%2) out1<<x<< ;else out2<<x<< ;
}
out1<<endl; out2<<endl;
out1.close(); out2.close(); in.close();
}
arb.txt
1 2 3 4 5 6 7 8 9 10
out1.txt
file odd numbers:
1 3 5 7 9
out1.txt
file even numbers:
2 4 6 8 10
3.
(.txt) .
, ( ).
//arb2014_file3
#include<fstream>
#include<iostream>
using namespace std;
void create_file(ifstream &f, ofstream &g1, ofstream &g2)
{
int x;
if(!f)
{
cout<<"can't open file!"<<endl;
exit(-1);
}
g1<<"file odd numbers:"<<endl;
|
|
g2<<"file even numbers:"<<endl;
while(!f.eof())
{
f>>x;
if (x%2) g1<<x<< ;else g2<<x<< ;
}
g1<<endl; g2<<endl;
}
/////////////////////////////////
void main()
{
ifstream in("arb.txt");
ofstream out2("out2.txt");
ofstream out1("out1.txt");
create_file(in,out1,out2);
out1.close();
out2.close();
in.close();
}
4.
(.txt) .
, max ( ).
//arb2014_file4
#include<fstream>
#include<iostream>
using namespace std;
////////////////////////////////
void create_file(ifstream &f, ofstream &g1, ofstream &g2)
{
int x;
if(!f)
{
cout<<"can't open file!"<<endl;
exit(-1);
}
while(!f.eof())
{
f>>x;
if (x%2) g1<<x<< ;else g2<<x<< ;
}
g1<<endl; g2<<endl;
}
/////////////////////////////////
int max(ifstream &f)
{
int x,m;
if(!f)
{
cout<<"can't open file!"<<endl;
exit(-1);
}
f>>m;
while(!f.eof())
{
f>>x;
if (m<x) m=x;
}
return m;
}
/////////////////////////////////
void main()
{
ifstream in("arb.txt");
ofstream out2("out2.txt");
ofstream out1("out1.txt");
create_file(in,out1,out2);
out1.close();
out2.close();
in.close();
ifstream in1(out1.txt); ifstream in2(out2.txt);
cout<<max odd= <<max(in1)<<endl;
cout<<max even= <<max(in2)<<endl;
in1.close();in2.close();
}
}
1.
#include <iostream.h>
#include <fstream.h>
const int n=10;
void create(ifstream&f, int x[][], int k)
{
int i,j;
for (i=0; i<k; i++)
for (j=0; j<k; j++)
f>>x[i][j];
}
void show(ofstream&f, int x[][], int k)
{
int i,j;
for (i=0; i<k; i++)
{
for (j=0; j<k; j++)
f<<a[i][j]<< ;
f<<endl;
}
}
void mult (int **x, int**y, int**z, int n)
{
int i, j, k;
for (i=0; i<n; i++)
for (j=0; j<n; j++)
{
z[i][j]=0;
for (k=0; k<n; k++)
z[i][j]=z[i][j]+x[i][k]*y[k][j];
}
}
Void main()
{
int a[n][n], b[n][n],c[n][n];
ifstream in1(in.txt);
// .
//
ifstream in2(in2.txt);
ofstream out(out.txt"); //
create(in,a,n);
create(in2,b,n);
in1.close();
in2.close();
// - !
mult(a,b,c,n);
show(out, c,n);
out.close();
}
Session results on faculty of computer science(VMK)
perfect | good | satisfy | poor | |
first | ||||
second | ||||
third | ||||
fourth | ||||
fifth | ||||
Total: |
|
|
Total
Session results on faculty of computer science
Perfect good satisfy poor
2 3 4 5
2 3 3 3
3 3 3 3
4 4 4 4
5 5 5 5
#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstring>
using namespace std;
const int numberCourse=5;
const int numberMark=4;
const int lengthIndent=7;//
const int lengthTable=56;//
const int lengthColumn=10;//
/////////////////////////////////////////////////
void star(int n,int m)
{
cout.width(n);
int i;
for (i=0;i<m;i++)
cout<<'*';
cout<<endl;
}
///////////////////////////////////////////////
void star1(int n)
{
int i;
cout.width(n);
cout<<"*";
cout.width(lengthColumn+1);
cout<<"*";
for (i=0;i<numberMark;i++)
{
cout.width(lengthColumn+1);
cout<<'*';
}
cout<<endl;
}
void main()
{
char name_Course[numberCourse][15];
char name_Mark[numberMark][20];
int number_Student[numberCourse][numberMark];
char name_Faculty[80];
int Total[numberMark];
ifstream in("arb.txt");
ofstream out("vmk.txt");
int i,j;
in.getline(name_Faculty,80);
for (i=0;i<numberCourse;i++)
in>>name_Course[i];
for (i=0;i<numberMark;i++)
in>> name_Mark[i];
for (i=0;i<numberCourse;i++)
for (j=0;j<numberMark;j++)
in>> number_Student[i][j];
/*
cout<<name_Faculty<<endl;
for (i=0;i<numberCourse;i++)
cout<<name_Course[i]<<" ";
cout<<endl;
for (i=0;i<numberMark;i++)
cout<< name_Mark[i]<<" ";
cout<<endl;
for (i=0;i<numberCourse;i++)
{
for (j=0;j<numberMark;j++)
cout<<number_Student[i][j]<<" ";
cout<<endl;
}
*/
cout<<endl<<endl;
cout.width(lengthTable + 4);
cout<<name_Faculty<<endl;
cout<<endl<<endl;
star(lengthIndent+1,lengthTable);//
star1(lengthIndent+1);
cout.width(lengthIndent+1);
cout<<"*";
cout.width(lengthColumn+1);
cout<<"*";
for(i=0;i<numberMark;i++)
{
cout<<" "<<name_Mark[i];
cout.width(lengthColumn-strlen(name_Mark[i]));
cout<<"*";
}
cout<<endl;
star1(lengthIndent+1);
star(lengthIndent+1,lengthTable);
for(i=0;i<numberCourse;i++)
{
star1(lengthIndent+1);
cout.width(lengthIndent+1);
cout<<"*";
cout<<" "<<name_Course[i];
cout.width(lengthColumn-strlen(name_Course[i]));
cout<<"*";
for(j=0;j<numberMark;j++)
{ cout<<setw(6)<<number_Student[i][j];
cout<<setw(5);
cout<<"*";
}
cout<<endl;
star1(lengthIndent+1);
star(lengthIndent+1,lengthTable);
}
for(j=0;j<numberMark;j++)
{
Total[j]=0;
for(i=0;i<numberCourse;i++)
Total[j]+=number_Student[i][j];
}
star(lengthIndent+1,lengthTable);
star1(lengthIndent+1);
cout.width(lengthIndent+1);
cout<<"*"<<" Total *";
for(j=0;j<numberMark;j++)
{ cout<<setw(6)<<Total[j];
cout<<setw(5);
cout<<"*";
}
cout<<endl;
star1(lengthIndent+1);
star(lengthIndent+1,lengthTable);
cout<<endl;
}
12
4
( )
#include <iostream.h>
#include <iomanip.h> //
#include <fstream.h>
const int n=10;
void create(ifstream&f, int x[n]);
|
|
void create(ifstream&f, double x[n]); // create
void show(ofstream&f, int x[n], int k);
void show(ofstream&f, double x[n], int k); // show
Void main()
{
ifstream in1(in1.txt);
ifstream in2(in2.txt);
ofstream out1(out1.txt);
ofstream out2(out2.txt);
out1.setf(ios::left); //
out1.setf(ios::show pos);
out1.setf(ios::oct);
out1.unset(ios::oct);
out1.setf(ios::right);
out2.setf(ios::saentific);
// saentific- ; fixed-
out2.unset(ios:: saentific);
//
out2. precision(2);
double b[n];
int a[n];
create (in1,a);
in1.close();
show(out1,a);
out1.close();
create (in2,b,20);
in2.close();
show(out2,b,20);
out2.close();
}
void create(ifstream&f, int x[n])
{int i;
for (i=0;i<n;i++)
f>>x[i];
}
void create(ifstream&f, double x[n])
{int i;
for (i=0;i<n;i++)
f>>x[i];
}
void show(ifstream&f, int x[n], int k)
{ int i;
for (i=0; i<n;i+)
{ f. width(k);
f.fill(#);
f<<x[i]<<endl;
}
void show(ifstream&f, double x[n], int k)
{ int i;
for (i=0; i<n;i+)
{ f. width(k);
f.fill(#);
f<<x[i]<<endl;
}
-.
1
// .
// fstream.h
// iostream.h
#include <iostream.h>
#include <fstream.h>
void main()
{
int i, min, mas[10];
ifstream myin("test.txt"); // .
for(i=0;i<10;i++)
myin >> mas[i];
min=mas[0];
for(i=1;i<10;i++)
if (mas[i]<min)
min=mas[i];
cout << "min = " << min << '\n';
}
2
// 1/x
// {} {} : 0 =1,
// 0=1-; (0<x<2)
// = -1(1+-1), =2-1, =1,2,
// n n, n ≤ ∂ (∂>0).
// iostream.h
// a b,
// fstream.h
#include <iostream.h>
#include <fstream.h>
const double eps= 0.001;
void main()
{
double a=1, b, x;
ofstream myout("result.txt"); // .
cout << "Enter x:";
cin >> x;
if (x<=0 || x>=2)
cout << "not decision";
else
{
b=1-x;
do
{
a=a*(1+b);
b=b*b;
myout << "a = " << a << " b = " << b << endl;
}
while(b>eps);
myout << "1/x=" << a << endl;
cout<< "<< 1/x <<endl;;
}
}
3
//
// - fstream.h
#include <fstream.h>
const int SIZE= 9;
void main()
{
int i, k, mas[SIZE];
ifstream myin("test.txt"); // .
ofstream myout("result.txt"); // .
for(i=0;i<SIZE;i++)
myin >> mas[i];
for(i=0;i<SIZE/2;i++)
{
k=mas[i];
mas[i]= mas[SIZE-i-1];
mas[SIZE-i-1] = k;
}
for(i=0;i<SIZE;i++)
myout << mas[i] << " ";
cout<<endl;
}
4
// .
// - fstream.h
#include <fstream.h>
|
|
void main()
{
int i, j;
const int n = 4;
double b[n][n], x;
ifstream myin("test.txt"); // .
ofstream myout("result.txt"); // .
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
myin >> b[i][j];
for (i = 0; i < n - 1; i++)
for (j = i + 1; j < n; j++)
{
x = b[i][j];
b[i][j] = b[j][i];
b[j][i] = x;
}
myout << " :<<endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
myout << b[i][j] << ' ';
myout << endl; // .
}
}
5
/* , , .
,
SIZE .
*/
// () .
// 29 .
// - fstream.h
#include <fstream.h>
#include <string.h>
const int SIZE =30;
void main()
{
char buf[30];
int n, len=0;
ifstream myin("text.txt");
ofstream myout("result.txt");
myin >> buf;
while(!myin.eof())
{
n = strlen(buf);
if (len+n <= SIZE)
len=len+n+1;
else
{
len=n+1;
myout << '\n';
}
myout << buf << ' ';
myin >> buf;
}
}
6
/* ʔ
- . ,
3bc3a bbbcaaa.
, , - .
*/
// .
// .
// get()
// .
// put(f) f .
// -.
#include <fstream.h>
void main()
{
char symb;
int n, i;
ifstream myin("text.txt");
ofstream myout("result.txt");
symb=myin.get();
while(!myin.eof())
{
if (symb >='0' && symb <='9')
{
n = symb - '0';
symb=myin.get();
if (!myin.eof())
for(i=1; i<=n; i++)
myout.put(symb);
}
else
myout.put(symb); // .
symb=myin.get();
}
}
7
/* , , .
.
*/
#include <fstream.h>
#include <string.h>
void main()
{
const size=81; // 80 .
char buf[size];
int n, i;
int first; //
int count_first; // -
int last; //
int count_last; // -
ifstream myin("text.txt"); //
ofstream myout("result.txt"); //
while(!myin.eof())
{ //
myin.getline(buf, size, '\n');
n=strlen(buf); // n - buf ( \0)
//
for(i=0; i<=n; i++)
if(buf[i]==' ')
myout.put(buf[i]);
else
break;
if(buf[i]=='\0') //
{
myout.put('\n');
continue; //
}
first=i; //
count_first=1;
// -
for(i=first+1; i<n; i++)
if(buf[i]!=' ' && buf[i]!='\0')
count_first++;
else
break;
//
for(i=n-1; i>=0 && buf[i]==' '; i--);
last=i; //
count_last=0;
// -
for(i=last; i>=0; i--)
if(buf[i]!=' ')
count_last++;
else
break;
//
if(first+count_first > last-count_last)
myout << buf << '\n'; //
else
{ //
for(i=last-count_last+1; i<=last; i++)
myout.put(buf[i]);
//
for(i=first+count_first; i<=last-count_last; i++)
myout.put(buf[i]);
//
for(i=first; i<first+count_first; i++)
myout.put(buf[i]);
myout << '\n';
}
}
}
8
// ,
#include <fstream.h>
#include <iostream.h>
const int SIZE=512;
void main()
{
int nl=0; // -
int nw=0; // -
|
|
int nc=0; // -
int n,i;
char buf[SIZE]; //
bool inword=false; //
ifstream myin("text.txt");
while(!myin.eof())
{
myin.read(buf, SIZE); //
n=myin.gcount();
nc+=n;
for(i=0; i<n; i++)
{
if(buf[i]=='\n')
++nl;
if(buf[i]=='\n' || buf[i]==' ')
inword=false;
else
if (!inword)
{
inword=true;
++nw;
}
}
}
cout<<"Lines= "<<nl<<endl;
cout<<"Words= "<<nw<<endl;
cout<<"Symbols= "<<nc<<'endl;
}
#include <iostream.h>
#include <iomanip.h>
void main()
{
int a=904;
double b=905.906901;
cout<<setw(10)<<a<<endl;
cout<<setw(10)<<setioflags(ios::right)<<a<<endl;
cout<<setw(10)<< setfil(*)<<a<<endl;
cout<<setw(10)<<setioflags(ios::left)<<b<<endl;
cout<<setw(10)<< setfil(*)<<b<<endl;
}