Tải bản đầy đủ (.pdf) (19 trang)

tiểu luận lab report exercises kỹ thuật lập trình

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (625.59 KB, 19 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

<b>BỘ CÔNG THƯƠNG</b>

<b>TRƯỜNG ĐẠI HỌC CÔNG NGHIỆP TP HỒ CHÍ MINH</b>

<b>LAB REPORT EXERCISES</b>

<b>KỸ THUẬT LẬP TRÌNH</b>

</div><span class="text_page_counter">Trang 2</span><div class="page_container" data-page="2">

<b>NỘI DUNG 5: FILE</b>

<b>Câu 1: Code: </b>

#include <stdio.h>#include <stdbool.h>//c

bool isPrime(int n) { if (n <= 1) { return false; }

for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; }

} return true;}

int main() { FILE *file; int n;//a

file = fopen("array1.txt", "r"); if (file == NULL) {

printf("Khong the mo file!\n"); return 1;

fclose(file);

~ 2 ~

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

printf("Mang doc duoc tu file:\n"); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); }

printf("\n"); //c

file = fopen("array1.txt", "a"); if (file == NULL) {

printf("Khong the mo file!\n"); return 1;

}

printf("Cac so nguyen to trong mang va ghi vao file:\n"); for (int i = 0; i < n; i++) {

if (isPrime(arr[i])) { fprintf(file, "%d ", arr[i]); printf("%d ", arr[i]); }

} printf("\n");

fclose(file); return 0;}

~ 3 ~

</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">

<b>Kết quả:</b>

<b>Câu 2: Code: </b>

<b>Too long to read onyour phone? Save to</b>

read later on yourcomputer

Save to a Studylist

</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">

#include<conio.h> #include<stdlib.h> #include<stdbool.h> #include<time.h>#define MAX 100

#define duongdan "E:\\Documents\\KTLT\\BTTH_5_FILE\\test.inp" void SinhMT(int a[MAX][MAX], int d, int c) {

for(int i=0; i<d; i++){ for(int j=0; j<c; j++){ a[i][j] = rand()%100;

} }}

void LuuFile(int a[MAX][MAX], int d, int c) { FILE *f; f=fopen(duongdan, "wt"); if(f==NULL) {

printf("\nKhong tao duoc file."); getch();

exit(0); }

fprintf(f, "%d %d\n", d, c); for(int i=0; i<d; i++){ for(int j=0; j<c; j++){ fprintf(f, "%d\t", a[i][j]); }

fprintf(f, "\n");

} fclose(f); }

void DocFile(int a[MAX][MAX], int &d, int &c) { FILE *f; f=fopen(duongdan, "rt");

if(f==NULL) {

printf("\nKhong mo duoc file."); getch();

exit(0); }

fscanf(f,"%d%d",&d, &c ); for(int i=0; i<d; i++){

for(int j=0; j<c; j++){fscanf(f,"%d", &a[i][j]);}

~ 5 ~

</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">

fclose(f); }

void XuatMT(int a[MAX][MAX], int d, int c) { {

for(int i = 0; i < d; i++) {

for(int j = 0; j < c; j++) printf("%d\t", a[i][j]); printf("\n"); }

}} int main(){ srand(time(NULL)); int a[MAX][MAX], d=5, c=6; int b[MAX][MAX], x, y; SinhMT(a, d, c); LuuFile(a, d, c); DocFile(b, x, y); XuatMT(b, x, y); return 0; }

<b>Kết quả:</b>

~ 6 ~

</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">

<b>Câu 3:</b>

#include <stdio.h>#include <stdlib.h>#include <conio.h>

~ 7 ~

</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">

#define MAX 100void Nhap(int arr[], int n){

if(n==0) return; Nhap(arr,n-1);

printf("Nhap gia tri cho mang thu %d : ", n-1); scanf("%d", &arr[n-1]);

void LuuFile(int arr[], int n){

FILE* f=fopen("NguyenAm.txt","wt"); if(f==NULL){

printf("Khong tao duoc file");getch();

for(int i=0; i<n; i++){ fprintf(f, "%d\t", arr[i]); }

fclose(f); }

void DocFile(int arr[], int n){

FILE* f=fopen("NguyenAm.txt", "rt");

~ 8 ~

</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">

if(f==NULL){

printf("Khong the mo file");getch();

for(int i =0; i<n; i++){fscanf(f,"%d", &arr[i]);}

fclose(f);}

void Xuat(int arr[], int n){

printf("Cac ky tu nguyen am doc duoc tu file: ");for(int i=0; i<n; i++){

printf("%d ", arr[i]);}

int main(){int a[MAX];int n;

printf("Nhap so luong ky tu (n<=100): ");scanf("%d", &n);

~ 9 ~

</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">

Nhap(a,n);LuuFile(a,n);DocFile(a,n);Xuat(a,n);return 0;}

<b>Kết quả:</b>

~ 10 ~

</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">

<b>Câu 4: Code:</b>

#include <stdio.h>

~ 11 ~

</div><span class="text_page_counter">Trang 12</span><div class="page_container" data-page="12">

#include <stdlib.h>#include <conio.h> #define MAX 100

typedef struct HH{char mh[6];int sl;float dg;float st = sl * dg;};

void Nhap1HH(HH &a){ fflush(stdin);

printf("Ma Hang: "); gets(a.mh); printf("So Luong: "); scanf("%d", &a.sl); printf("Don Gia: "); scanf("%f", &a.dg); printf("So Tien: "); scanf("%f", &a.st);

</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">

printf("Khong tao duoc file");getch();

fprintf(f, "Danh sach hang hoa:\n");

fprintf(f, "Ma hang\t\tSo luong\tDon gia\t\tSo tien\n"); for(int i=0; i<n; i++){

fprintf(f,"%s\t\t%d\t\t%.1f\t\t%.1f\n", a[i].mh, a[i].sl, a[i].dg, a[i].st); }

fclose(f); }

void Xuat1HH(HH a){

printf("%s\t\t%d\t\t%.1f\t\t%.1f\n",a.mh, a.sl, a.dg, a.st);

~ 13 ~

</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">

int main(){HH b[MAX];int n;

printf("Nhap so luong ky tu (n<=100): ");scanf("%d", &n);

printf("\nDanh sach hang hoa:\n");

printf("Ma hang\t\tSo luong\tDon gia\t\tSo tien\n");XuatDSHH(b,n);

return 0;}

<b>Kết quả:</b>

~ 14 ~

</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">

<b>Câu 5: Code:</b>

#include <stdio.h>#include <stdlib.h>#include <conio.h>

~ 15 ~

</div><span class="text_page_counter">Trang 16</span><div class="page_container" data-page="16">

typedef struct PERSON { unsigned int code; char firstname[32]; char lastname[32]; char address[32]; char birthday[9]; };

void Output(PERSON person) {

printf("%s %s\n", person.firstname, person.lastname); printf("Address: %s\n", person.address);

printf("Birthday: %s\n", person.birthday); printf("Code: %u\n\n", person.code);}

int main() { FILE *f; char line[200]; PERSON person;

f = fopen("PERSON.DAT", "r"); if (f == NULL) {

~ 16 ~

</div><span class="text_page_counter">Trang 17</span><div class="page_container" data-page="17">

perror("Khong the mo file"); getch();

exit(1); }

while (fgets(line, 200, f) != NULL) {

sscanf(line, "%u:%[^,], %[^:]:%[^\n]\n", &person.code, &person.firstname,&person.address, &person.birthday);

Output(person); }

fclose(f);

return 0;}

<b>Kết quả:</b>

~ 17 ~

</div>

×