Viết chương trình nhập 3 số nguyên từ bàn phím. Ghi giá trị 3 số nguyên đó vào tệp có tên dulieuvao.txt
Đọc dữ liệu từ tệp vào ba biến x, y, z, tính tổng T=x+y+x và lưu giá trị của tổng vào tệp dulieura.txt
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;
}
Bài tập tệp Bài 1: Viết chương trình mở tệp dayso.TXT để đọc các số nguyên ra biến n và tính tổng các số nguyên n chia hết cho 3 hoặc 5? Bài 2: Viết chương trình nhập 50 số nguyên bất kì từ bàn phím và ghi các số nguyên lẻ đã nhập vào tập sole.TXT
*Viết chương trình ghi hai số 5 và 15 vào tệp so.txt:
const f='so.txt'
var f1:text;
begin
assign(f1,f); rewrite(f1);
writeln(f1,'5 15');
close(f1);
end.
*Viết chương trình tính tổng, hiệu, tích, thương của x,y và in kết quả ra màn hình:
uses crt;
const fi='so.txt'
var x,y:integer;
f1:text;
begin
clrscr;
assign(f1,fi); reset(f1);
readln(f1,x,y);
writeln('Tong la: ',x+y);
writeln('Hieu la: ',x-y);
writeln('Tich la: ',x*y);
writeln('Thuong la: ',x/y:4:2);
close(f1);
readln;
end.
viết chương trình nhập 2 số x,y từ bàn phím, tính tổng, hiệu, tích, thương của 2 số và ghi kết quả vào tệp 'KQ.TXT'
#include <bits/stdc++.h>
using namespace std;
long long x,y;
int main()
{
cin>>x>>y;
freopen("kq.txt","w",stdout);
cout<<x+y<<endl;
cout<<x-y<<endl;
cout<<x*y<<endl;
cout<<fixed<<setprecision(2)<<(x*1.0)/(y*1.0);
return 0;
}
Viết chương trình tính tổng của n số nguyên có trong tệp Tong.inv. đưa giá trị tìm được ra tệp Tong.out. Dữ liệu vào gồm hai dòng dòng 1 ghi số nguyen dương n dòng 2 ghi n số nguyên dương dữ liệu ra một số nguyên duy nhất là tổng tìm được
const fi='tong.inp'
fo='tong.out'
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);
readln(f1,n);
for i:=1 to n do
read(f1,a[i]);
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 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 DULIEU TXT gồm nhiều dòng mỗi dòng gồm 2 số nguyên x và y. Đọc dữ liệu từ tệp vào ghi ra tệp KETQUA TXT số nhỏ hơn trong 2 số,mỗi số ghi trên 1 dòng.
const fi='dulieu.txt'
fo='ketqua.txt'
var f1,f2:text;
a,b:array[1..100]of integer;
n,i: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],b[n]);
end;
for i:=1 to n do
begin
if (a[i]<b[i]) then writeln(f2,a[i])
else writeln(f2,b[i]);
end;
close(f1);
close(f2);
end.
1) Cho tệp dulieu.in là tệp vban, chứa các số nguyên cách nhau 1 kí tự trống. Vt ctrinh đọc dữ liệu trog tệp r thựtc hiện các công vc sau a) Ghi vào tệp ketqua.out các số bội số nguyên K nhập từ bàn phím b) Đếm và tính tổng các số thỏa mãn yêu cầu rồi in ra màn hình
Lưu ý có sd ctrinh con