1. Trang chủ
  2. » Luận Văn - Báo Cáo

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

19 0 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Lab Report Exercises Kỹ Thuật Lập Trình
Tác giả Quách Tấn Minh
Trường học Trường Đại Học Công Nghiệp Tp Hồ Chí Minh
Chuyên ngành Kỹ Thuật Lập Trình
Thể loại Lab Report
Thành phố Tp Hồ Chí Minh
Định dạng
Số trang 19
Dung lượng 625,59 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

BỘ CÔNG THƯƠNGTRƯỜNG ĐẠI HỌC CÔNG NGHIỆP TP HỒ CHÍ MINHLAB REPORT EXERCISESKỸ THUẬT LẬP TRÌNH... Kết quả:Câu 2: Code: Too long to read onyour phone?. Save toread later on yourcomputerSav

Trang 1

BỘ CÔNG THƯƠNG TRƯỜNG ĐẠI HỌC CÔNG NGHIỆP TP HỒ CHÍ MINH

LAB REPORT EXERCISES

KỸ THUẬT LẬP TRÌNH

Trang 2

NỘI DUNG 5: FILE

Câu 1:

Code:

#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;

}

fscanf(file, "%d", &n);

int arr[n];

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

fscanf(file, "%d", &arr[i]);

}

fclose(file);

~ 2 ~

Trang 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 ~

Trang 4

Kết quả:

Câu 2:

Code:

Too long to read on your phone? Save to

read later on your computer

Save to a Studylist

Trang 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 ~

Trang 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;

}

Kết quả:

~ 6 ~

Trang 7

Câu 3:

Code:

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

~ 7 ~

Trang 8

#define MAX 100

void 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();

exit(1);

}

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 ~

Trang 9

if(f==NULL){

printf("Khong the mo file");

getch();

exit(1);

}

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 ~

Trang 10

LuuFile(a,n);

DocFile(a,n);

Xuat(a,n);

return 0;

}

Kết quả:

~ 10 ~

Trang 11

Câu 4:

Code:

#include <stdio.h>

~ 11 ~

Trang 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);

}

void NhapDSHH(HH a[], int n){

if(n==0)

return;

~ 12 ~

Trang 13

NhapDSHH(a, n-1);

printf("\nNhap thong tin hang hoa thu %d\n", n);

Nhap1HH(a[n-1]);

}

void LuuFile(HH a[], int n){

FILE* f=fopen("DSHH.txt","w");

if(f==NULL){

printf("Khong tao duoc file");

getch();

exit(1);

}

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 ~

Trang 14

void XuatDSHH(HH a[], int n)

{

for(int i = 0; i<n; i++)

Xuat1HH(a[i]);

}

int main(){

HH b[MAX];

int n;

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

NhapDSHH(b,n);

LuuFile(b,n);

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

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

return 0;

}

Kết quả:

~ 14 ~

Trang 15

Câu 5:

Code:

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

~ 15 ~

Trang 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 ~

Trang 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;

}

Kết quả:

~ 17 ~

Ngày đăng: 25/05/2024, 17:38

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w