cho tệp 'D:\Dulieu.txt' chứa n các số nguyên . Hãy lập trình tìm các số lẻ sau đó ghi vào 'D:\Ketqua'
Viết chương trình nhập vào 3 số nguyên , sau đó ghi 3 số nguyên vừa nhập đó vào tệp Dulieu.txt ở ổ D:\
uses crt;
const fi='dulieu.txt'
var f1:text;
a,b,c:integer;
begin
clrscr;
assign(f1,fi); rewrite(f1);
write('Nhap a='); readln(a);
write('Nhap b='); readln(b);
write('Nhap c='); readln(c);
writeln(f1,a);
writeln(f1,b);
writeln(f1,c);
close(f1);
end.
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] mod 3=0 then write(f2,a[i]:4);
close(f1);
close(f2);
end.
Lập trình đọc số nguyên dương N vào tệp có tên ‘Dulieu.txt’ Tính tổng các số chẵn từ 1 đến N, kết quả ghi ra tệp ‘ Uot.txt’ Mọi người giúp em gấp với ạ!!!!!! Em cảm ơn!
#include <bits/stdc++.h>
using namespace std;
long long n,i,t;
int main()
{
freopen("dulieu.txt","r",stdin);
freopen("uot.txt","w",stdout);
cin>>n;
t=0;
for (i=1; i<=n; i++)
if (i%2==0) t+=i;
cout<<t;
return 0;
}
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ố lẻ 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 = 1 then Tong:= Tong+i;
write(f2, Tong);
close(f1);
close(f2);
End.
const fi='dulieu.txt'
fo='ketqua.txt'
var f1,f2:text;
a,b,c,d:array[1..100]of integer;
i,n,dem,dem1: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;
dem:=0;
dem1:=0;
for i:=1 to n do
if a[i]>b[i] then
begin
inc(dem);
inc(dem1);
c[dem]:=a[i];
d[dem]:=b[i];
end
else begin
inc(dem);
inc(dem1);
c[dem]:=b[i];
d[dem]:=a[i];
end;
for i:=1 to dem do
write(f2,c[i]:4);
writeln(f2);
for i:=1 to dem1 do
write(f2,d[i]:4);
close(f1);
close(f2);
end.
cho 2 số nguyên a và b chứa trong tệp dulieu. dat. Lập trình để tính thương số t=a/b đưa kết quả vào tệp ketqua. dat
cho tệp DATA.TXT các số nguyên, các số cách nhau bởi một dấu cách.Hãy lập chương trình đếm các số lẻ. Ghi kết quả vào tệp TES.TXT.
Tạo ra tệp DEBAI.DAT trong ổ đĩa D. Nhập dữ liệu gồm chữ hoa, chữ thường, chữ số. Hãy lập trình thực hiện các công việc sau: a. Tìm số nhỏ nhất và ghi vào tệp SONHONHAT.OUT ở ổ đĩa D. b. Tính tổng các số chẵn và ghi kết quả vào tệp SOCHAN.OUT ở ổ đĩa D c. Đếm xem có bao nhiêu kí tự dấu cách và in kết quả vào tệp DAUCACH.OUT d. Xóa tất cả các kí tự là dấu cách và in kết quả vào tệp KETQUA.OUT
Đây là một bài tập lập trình, dưới đây là đoạn mã Python để thực hiện các yêu cầu trong bài tập:
`python
import os
Tạo tệp DEBALDAT trong ổ đĩa D
with open('D:/DEBALDAT', 'w') as f:
data = input("Nhập dữ liệu: ")
f.write(data)
Đọc dữ liệu từ tệp DEBALDAT
with open('D:/DEBALDAT', 'r') as f:
data = f.read()
Tìm số nhỏ nhất và ghi vào tệp SONHONHAT.OUT
numbers = [int(x) for x in data if x.isdigit()]
min_number = min(numbers)
with open('D:/SONHONHAT.OUT', 'w') as f:
f.write(str(min_number))
Tính tổng các số chẵn và ghi vào tệp SOCHAN.OUT
even_numbers = [int(x) for x in data if x.isdigit() and int(x) % 2 == 0]
sum_even_numbers = sum(even_numbers)
with open('D:/SOCHAN.OUT', 'w') as f:
f.write(str(sum_even_numbers))
Đếm số kí tự dấu cách và ghi vào tệp DAUCACH.OUT
num_spaces = data.count(' ')
with open('D:/DAUCACH.OUT', 'w') as f:
f.write(str(num_spaces))
Xóa các kí tự dấu cách và ghi vào tệp KETQUA.OUT
data_without_spaces = data.replace(' ', '')
with open('D:/KETQUA.OUT', 'w') as f:
f.write(data_without_spaces)
Xóa tệp DEBALDAT
os.remove('D:/DEBALDAT')
`
Lưu ý: Đoạn mã này chỉ thực hiện được trên hệ điều hành Windows, nếu sử dụng hệ điều hành khác cần thay đổi đường dẫn ổ đĩa.
Cho tệp BAIKT.INP chứa các số nguyên bất kì. Viết chương trình đếm xem trong tệp có bao nhiêu số lẻ rồi ghi kết quả vào tệp KETQUA.OUT