Cho 2 tệp in.txt và out.txt:
1. Trên tệp in.txt lưu giá trị n, tính n! sau đó ghi lại kết quả vào tệp out.txt
2. Trên tệp in.txt ghi tên 4 học sinh trên 4 dòng, đọc và ghi lại tên của các học sinh đó trên tệp out.txt
cho tệp văn bản XAU.INP lưu trũ họ tên của các học sinh trong lớp. Mỗi học sinh được lưu trữ trên một dòng. Viết chương trình đọc dữ liệu từ tệp XAU.INP, kết quả ghi vào XAU.OUT là độ dài tên từng học sinh trong lớp
with open('XAU.INP', 'r') as input_file, open('XAU.OUT', 'w') as output_file:
for line in input_file:
length = len(line.strip())
output_file.write(f'{length}\n')
Cho 1 tệp có tên là mang.txt viết chương trình đọc dữ liệu từ tệp sau đó tính tổng các giá trị đọc được ghi vào tệp tong.txt
const fi='mang.txt'
fo='tong.txt'
var f1,f2:text;
a:array[1..100]of integer;
n,i,t:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewriteln(f2);
n:=0;
while not eof(f1) do
begin
inc(n);
read(f1,a[n]);
end;
t:=0;
for i:=1 to n do
t:=t+a[i];
writeln(f2,t);
close(f1);
close(f2);
end.
Viết chương trình đọc từ tệp Dữ liệu.int chứa 2 số nguyên bất kì . Tính và ghi kết quả vào tệp Kết quả.out 2 dòng , dòng đầu ghi giá trị tổng 2 số , dòng thứ 2 ghi giá trị trung bình của 2 số đó
#include <bits/stdc++.h>
using namespace std;
long long a,b;
double tb;
int main()
{
freopen("dulieu.inp","r",stdin);
freopen("ketqua.out","w",stdout);
cin>>a>>b;
cout<<a<<" "<<b;
cout<<fixed<<setprecision(2)<<(a*1.0+b*1.0)/(2*1.0);
return 0;
}
cho tệp văn bản HinhTron.INP chứa bán kính R các số viết trên 1 dòng , hãy đọc dữ liệu từ tệp HinhTron.INP để tính chu vi và diện tích của mỗi hình tròn. Kết quả ghi vào tệp HinhTron.Out
const fi='hinhtron.inp'
fo='hinhtron.out'
var f1,f2:text;
a:array[1..100]of real;
i,n:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eof(f1) do
begin
n:=n+1;
read(f1,a[n]);
end;
for i:=1 to n do
writeln(f2,'Hinh tron thu ',i,': Chu vi la ',(2*a[n]*pi):4:2,' va Dien tich la: ',a[n]*a[n]*pi:4:2);
close(f1);
close(f2);
end.
Cho tệp dulieu.inp chứa các bộ số nguyên ( mỗi bộ ghi trên 1 dòng gồm 2 số nguyên cách nhau bởi 1 kí tự trống). Vt ctrinh đọc dữ liệu trong tệp trên rồi tính tổng của mỗi bộ số nguyên, ghi các kq vào tệp tong.out( mỗi kq trên 1 dòng ). Lưu ý có sd ctrinh con
Cho tệp SN3.INP chứa n số nguyên dương (mỗi số cách nhau một dấu cách trên một dòng). Viết chương trình đọc dữ liệu từ tệp đã cho Tính ƯCLN dãy các số nguyên dương đó và lưu kết quả vào tệp UC.OUT .
#include <bits/stdc++.h>
using namespace std;
unsigned long long a[1000],i,n,uc;
//chuongtrinhcon
unsigned long long ucln(long long a,long long b)
{
if (b==0) return(a);
else return(ucln(b,a%b));
}
//chuongtrinhchinh
int main()
{
freopen("sn3.inp","r",stdin);
freopen("uc.out","w",stdout);
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
uc=ucln(a[1],a[2]);
for (i=3; i<=n; i++)
uc=ucln(uc,a[i]);
cout<<uc;
return 0;
}
Cho tệp ăn.txt chứa dãy các số nguyên liên tiếp viết cách nhau 1 dấu cách .viết CT đọc dữ liệu từ tệp txt tính tổng các số chẵn và tổng các số lẻ ghi kết quả vào tập kq.txt trên 2 dòng
cho tệp Data.txt chứa liên tiếp các số nguyên, mỗi số trên 1 dòng. viết chương trình đọc các số vào chương trình pascal và tính trùng bình cộng các số đó. ghi kết quả vào TBC.txt
const fi='data.txt'
fo='tbc.txt'
var f1,f2:text;
a:array[1..100]of integer;
n,i,t:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eof(f1) do
begin
n:=n+1;
readln(f1,a[n]);
end;
t:=0;
for i:=1 to n do
t:=t+a[i];
writeln(f2,t/n:4:2);
close(f1);
close(f2);
end.
Cho tệp 'dayso.in' chứa các dãy số nguyên A1, A2....,An mỗi dãy sô nằm trên 1 dòng. Viết chương trình tính tổng theo dòng trong tệp 'dayso.in' sau đó ghi các số tổng này lên tệp 'tong.out', mỗi số ghi trên một dòng