File 1 s qator berilgan. Agar s yaroqli fayl nomi bo’lsa, u holda shu nom bilan bo’sh fayl yarating va true deb chop eting. Agar s nomli fayl yaratilmasa,false deb chop eting. Kod (Python)
File 1
S qator berilgan.Agar S yaroqli fayl nomi bo’lsa , u holda shu nom bilan bo’sh fayl yarating va TRUE deb chop eting.Agar S nomli fayl yaratilmasa ,FALSE deb chop eting.
Kod (Python)
file_name = input('Fayl nomini kiriting: ')
try:
f = open(file_name, "w")
try:
f.write('')
print('TRUE')
finally:
f.close()
except IOError:
print('FALSE')
File 2
Fayl nomi va N(>1) butun soni berilgan.Berilgan nomli butun sonlardan iborat fayl yarating va unga birinchi N musbat juft sonni (2,4,6 …) yozing.
Kod (C/C++)
#include
using namespace std;
int main()
{
ifstream fin("input.txt");
ofstream fout("output.txt");
int n;
fin >> n;
for(int i = 2; i <= n * 2; i+=2){
fout << i << " ";
}
return 0;
}
Kod (Python)
import random
file_name = "file02.txt"
N = random.randrange(1,10)
try:
f = open(file_name, "w")
f.write("N = " + (str)(N) + '\n')
try:
for i in range(N):
f.write((str)((i+1)*2) + '\n')
finally:
f.close()
except IOError:
print('Fayl yaratilmadi')
File 3
Fayl nomi va haqiqiy sonlar A va D berilgan.Berilgan nomli haqiqiy sonlar faylini tuzing va unga arifmetik progressiyaning dastlabki 10 a’zosini A boshlang’ich a’zosi va D farqi bilan yozing:A,A+D, A + 2·D, A + 3·D, ...
Kod (Python)
import random
N = random.randrange(1,10)
A = random.randrange(-10,10)
D = round(random.uniform(-5,5),1)
print("N = ", N)
print("A = ", A)
print("D = ", D)
a = [A + i*D for i in range(N)]
print(a)
file_name = "file03.txt"
try:
f = open(file_name, "w")
f.write("N = " + (str)(N) + '; ')
f.write("A = " + (str)(A) + '; ')
f.write("D = " + (str)(D) + '\n')
f.write(str(a))
except IOError:
print('Fayl ishlamadi')
finally:
f.close()
Do'stlaringiz bilan baham: |