Thứ Tư, 28 tháng 1, 2015

Bài tập (ngày 2)

I : Hệ thống kiến thức lý thuyết ...
1.Printf
Là hàm kết xuất dữ liệu được định dạng,khi sử dụng hàm phải khai báo tiền xử lý  #include <stdio.h>.
Cú pháp : Printf("Chuỗi định dạng"[,đối mục 1,2....]);
*Chuỗi định dạng : được đặt trong cặp nháy kép (" "), gồm 3 loại:
+Đối với chuỗi kí tự ghi như thếnào in ra giống như vậy
+Đối với những kí tự chuyển đổi dạng thức cho phép kết xuất giá trị của các đối mục ra màn hình tạm gọi là mã định dạng( VD : %c,%f,%d....)
+Các ký tự điều khiển và ký tự đặc biệt (VD :\n , \t....)
*Đối mục 1,2... : là các mục dữ kiện cần in ra màn hình. Các đối mục này có thể là biến, hằng hoặc biểu thức phải được định trị trước khi in ra.
*Một vài ví dụ về cách sử dụng hàm printf :
-Printf("Bai Tap Ngay 2");

-Giả sử có biến x=4 y=5 in ra tổng 2 số x+y=z
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int x,y,z;
x=4;
y=5;
z = x+y;
printf("Tong giua hai so la: ");
printf("%d",z);
return 0;

}
*Nhận xét : Chúng ta cần nắm vững các dấu mô tả định dạng,các ký tự điều khiển,đặc biệt, và cú 2.Scanf
Scanf Là hàm định dạng khi nhập liệu.
Khi sử dụng hàm cần khai báo tiền xử lý  #include <stdio.h>
Cú Pháp :
scanf("Chuỗi định dạng"[,đối mục 1,2...]);
-scanf: tên hàm, phải viết bằng chữ thường.

-Chuỗi định dạng: được đặt trong cặp nháy kép (" ") là hình ảnh dạng dữ liệu nhập vào
-Đối mục 1,2…: là danh sách các đối mục cách nhau bởi dấu phẩy, mỗi đối mục sẽ tiếp nhận giá trị nhập vào.
*Một vài ví dụ về cách sử dụng hàm Scanf :
-Nhập số kẹo được chia cho 2 người
#include <stdio.h>

#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int keo1,keo2;
printf("So keo nguoi 1:");
scanf("%d",&keo1);
printf("So keo nguoi 2 :");
scanf("%d",&keo2);
return 0;
Màn hình sẽ hiện như này và việc chúng ta cần làm là nhập giá trị cho người 1 và người 2.
*Nhận Xét : Cũng như printf, scanf cũng cần thuộc cấu trúc và các lệnh, nó sẽ giúp chúng ta làm bài một cách nhanh và xuôn xẻ hơn khi cứ phải dò dẫm từng bước quên cấu trúc này , lệnh kia
II.Bài tập
1. Use the printf( )  statement and do the following
a)    Print out the value of the integer variable sum
b)  Print out the text string "Welcome", followed by a new line.
c)  Print out the character variable letter
d)  Print out the float variable discount
e) Print out the float variable dump using two decimal places

Bài Giải
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int sum;
sum =5;
printf("%d",sum);
printf("\nWellcome\n\n");
char *letter;
letter="kho hieu\n";
printf("%s",letter);
float discount;
discount= 13.1415;
printf("%f\n\n",discount);
float dump;
dump=11.02;
printf("%f",dump);
return 0;
return 0;
}
Nhận xét : bài cũng khó hơn vì một dấu *, làm đi làm lại cả tiếng nhưng không phát hiện ra...từ đó cho thấy việc học kĩ cấu trúc là việc vô cùng quan trọng.
Bài 2:  Use the scanf( )  statement and do the following:

a) To read a decimal value from the keyboard, into the integer variable sum

b) To read a float variable into the variable discount_rate
Bài Làm

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int Sum;
printf("Sum =");
scanf("%d",&Sum);
printf("Bien Sum vua nhap : %d",Sum);
float discount_rate;
printf("\nVi du : So thuc =");
scanf("\n%f",&discount_rate);
printf("\nSo thuc vua nhap la :%f",discount_rate);

return 0;
}
Bài 3

Write a program which takes name, basic , daper ( ie, percentage of D.A), bonper (ie, percentage bonus) and loandet ( loan amount to be debited) for an employee. Calculate the salary using the following relation:



salary = basic + basic * daper /100 +bonper                 

        *  basic/100 - loandet

Data is :

 
  
name
basic
daper
bonper
loandet
MARK
2500
55
33.33
250.00


Calculate salary and then print the result under the following headings.
(Salary to be printed to the nearest dollar.)
        Name                                    Basic                        Salary
Bài Làm


#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { char name[5]; int basic, daper; float bonper, loandet, salary; printf("Name: "); scanf("%s",name); printf("\nbasic = "); scanf("%d",&basic); printf("\ndaper = "); scanf("%d",&daper); printf("\nbonper = "); scanf("%f",&bonper); printf("\nloandet = "); scanf("%f",&loandet); salary = basic + basic * daper/100 + bonper * basic/100 - loandet; printf("\nName \tBasic \tSalary"); printf("\n%s\t%d\t%0.2f",name,basic,salary); printf("\n\n"); system("pause"); return 0; }

Nhận xét : bài khó hiểu đề bài,nếu lạc đề mong cô bỏ qua...
Bài 4 : Write a program that asks for your first name and last name, and then prints the names in the format last name, first name.
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
char a[15],b[15],c[15];
printf("Ho : ");
scanf("%s",a);
printf("Ten dem : ");
scanf("%s",b);
printf("Ten : ");
scanf("%s",c);
printf("\n\nFull Name: %s %s %s  ",a,b,c);
printf("\n\n");
system("pause");
return 0;

}

Không có nhận xét nào:

Đăng nhận xét