Cho tệp D:\DuLieu.txt. gồm nhiều dòng, mỗi dòng chứa 2 số nguyên. VCT đọc các cặp số nguyên trong tệp đã cho và xuất ra màn hình tổng của từng cặp số tương ứng
uses crt;
const fi='dulieu.txt'
var f1:text;
a,b: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;
readln(a[n],b[n]);
end;
for i:=1 to n do
writeln(a[i]+b[i]);
close(f1);
readln;
end.
cho tệp Dulieu.txt gồm nhiều dòng, mỗi dòng chứa 2 số nguyên. Viết chương trình đọc dữ liệu từ tệp Dulieu.txt rồi tìm GTNN của mỗi dòng, rồi hiển thị ra màn hình.
uses crt;
const fi='dulieu.txt'
var f1:text;
a,b:array[1..100]of integer;
i,n:integer;
begin
clrscr;
assign(f1,fi); reset(f1);
n:=0;
while not eof(f1) do
begin
inc(n);
readln(f1,a[n],b[n]);
end;
for i:=1 to n do
begin
if a[i]<b[i] then writeln(a[i])
else writeln(b[i]);
end;
readln;
end.
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 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
cho tẹp dulieu.txt gồm nhiều dòng, chứa 2 số nguyên. viết ct đọc dữ liệu từ tệp DULIEU.TXT tìm max mỗi dòng rồi đưa ra màn hình
uses crt;
const fi='dulieu.txt'
var f1:text;
a,b:array[1..100]of integer;
i,n:integer;
begin
clrscr;
assign(f1,fi); reset(f1);
n:=0;
while not eof(f1) do
begin
n:=n+1;
readln(f1,a[n],b[n]);
end;
for i:=1 to n do
if a[i]<b[i] then writeln(b[i])
else writeln(a[i]);
readln;
end.
Cho tệp 'input.txt' gồm nhiều dòng, mỗi dòng chứa từng cặp số nguyên a,b là hệ số của phương trình bâc nhất ax+b=0.Viết phương trình đọc các số nguyên từ tệp dữ liệu trên, tính và ghi vào tệp 'output.txt' nghiệm của ptb 1
uses crt;
const fi='input.txt'
fo='output.txt'
var f1,f2:text;
a,b:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
readln(f1,a,b);
if (a=0) and (b=0) then writeln(f2,'Phuong trinh co vo so nghiem');
if (a<>0) then writeln(f2,-b/a:4:2);
if (a=0) and (b<>0) then writeln(f2,'Phuong trinh vo nghiem');
close(f1);
close(f2);
end.
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;
}
tệp xau.inp gồm các dòng kí tự, mỗi dòng không vượt quá 255 kí tự. viết chương trình đọc dữ liệu từ tệp xau.inp. đếm số kí tự là chữ'a'. ghi kết quả ra tệp xau.out chứa số nguyên duy nhất là số lượng kí tự'a' có trong tệp xau.inp
#include <bits/stdc++.h>
using namespace std;
string st;
int d,i,dem;
int main()
{
freopen("xau.inp","r",stdin);
freopen("xau.out","w",stdout);
cin>>st;
d=st.length();
dem=0;
for (i=0; i<=d-1; i++)
if (st[i]=='a') dem++;
cout<<dem;
return 0;
}