cho tệp DULIEU.TXT chứa liên tiếp các số nguyên, các số cách nhau bởi dấu cách và kết thúc bằng ký tự xuống dòng. Viết chương trình tính tổng bình phương các số có trong tệp DULIEU.TXT .Kết quả tính được ghi vào tệp KETQUA.TXT
Câu 1: Cho tệp HCN.TXT chứa liên tiếp các cặp số nguyên (a, b) là cạnh của các hình chữ nhật (các số cách nhau bởi dấu cách và không kết thúc bởi ký tự xuống dòng). Viết chương trình đọc các cặp cạnh a, b từ tệp HCN.TXT, tính chu vi và diện tích của các hình chữ nhật tương ứng và ghi vào tệp KETQUA.TXT.
const fi='hcn.txt'
fo='ketqua.txt'
var f1,f2:text;
a,b:array[1..100]of integer;
n:integer;
cv,dt:real;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eof(f1) do
begin
inc(n);
readln(f1,a[n],b[n]);
end;
for i:=1 to n do
begin
cv:=(a[i]+b[i])/2;
dt:=a[i]*b[i];
writeln(f2,cv:4:2,' ',dt:4:2);
end;
close(f1);
close(f2);
end.
cho tệp chứa liên tiếp các số nguyên cách nhau bằng dấu cách và không kết thúc bằng kí tự xuống dòng. hãy viết chương trình của dãy số nguyên đó ra màn hình
uses crt;
const fi='mang.inp'
var f1:text;
a:array[1..10000]of integer;
i,n:integer;
begin
clrscr;
assign(f1,fi); reset(f1);
n:=0;
while not eof(f1) do
begin
n:=n+1;
read(f1,a[i]);
end;
for i:=1 to n do write(a[i]:4);
close(f1);
readln;
end.
Viết chương trình nhập dữ liệu từ tệp Dulieu.txt chứa các số nguyên. Tính tổng giá trị các số có trong tệp và xuất kết quả ra màn hình
Cho tệp DATA.TXT chứa liên tiếp các số nguyên cách nhau bằng dấu cách và không kết thúc bằng kí tự xuống dòng, hãy lập trình tìm và in các số chẵn trong tệp DATA.TXT ra màn hình
uses crt;
const fi='data.txt'
var f1:text;
a:array[1..100]of integer;
n,i:integer;
begin
clrscr;
assign(f1,fi); reset(f1);
n:=0;
while not eof(f1) do
begin
n:=n+1;
read(f1,a[n]);
end;
for i:=1 to n do
if a[i] mod 2=0 then write(a[i]:4);
close(f1);
readln;
end.
Cho tệp dulieuvao.txt gồm nhiều dòng, mỗi dòng chứa 2 số nguyên a, b. Các số cách nhau bởi dấu cách. Viết chương trình đọc dữ liệu từ tệp, tính trung bình cộng và ghi kết quả ra tệp dulieura
1. Tệp Dulieu.txt có một dòng chứa các số nguyên (cách nhau khoảng trắng). Viết chương trình kiểm tra và đưa vào tệp Ketqua.txt những số nguyên dương chẵn trên cùng một dòng và cách nhau một khoảng trắng. 2.Viết chương trình nhập vào một mảng số nguyên, tínhvà đưa ra màn hình tổng của các số không âm có trong mảng.Yêu cầu sử dụng chương trình con để viết các thủ tục nhập, xuất mảng và hàm để tính tổng.
1:
const fi='Dulieu.txt'
fo='ketqua.txt'
var f1,f2:text;
a:array[1..100]of integer;
i,n:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eof(f1) do
begin
inc(n);
read(f1,a[n]);
end;
for i:=1 to n do
if (a[i]>0) and (a[i] mod 2=0) then write(f2,a[i]:4);
close(f1);
close(f2);
end.
P/s: Cái này mình viết chương trình chính thôi nhé, còn hàm và thủ tục bạn tự viết nhé(chỉ cần lấy ý tưởng trong chương trình chính ra viết là được rồi)
2:
uses crt;
var a:array[1..100]of integer;
i,n,t:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
t:=0;
for i:=1 to n do
if a[i]>=0 then t:=t+a[i];
writeln(t);
readln;
end.
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
Viết chương trình: Đọc từ tệp “DULIEU.TXT” 2 số nguyên M và N (M < N) Tính tổng các số chẵn trong phạm vi từ M đến N Ghi kết quả ra tệp “KETQUA.TXT”.
const fi='dulieu.txt'
fo='ketqua.txt'
var f1,f2:text;
m,n,i,t:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
readln(f1,m,n);
t:=0;
for i:=m to n do
if i mod 2=0 then t:=t+i;
writeln(f2,t);
close(f1);
close(f2);
end.
Viết chương trình:
Đọc từ tệp “DULIEU.TXT” 2 số nguyên M và N (M < N)
Tính tổng các số chẵn trong phạm vi từ M đến N
Ghi kết quả ra tệp “KETQUA.TXT”.
Var f1, f2: text;
Tong, m, n, i: integer;
Begin
assign(f1, ‘DULIEU.TXT’);
reset(f1);
assign(f2, ‘KETQUA’);
rewrite(f2);
Tong;= 0;
read(f1, m, n);
for i:= m to n do
if i mod 2 = 0 then Tong:= Tong+i;
write(f2, Tong);
close(f1);
close(f2);
End.