Thứ Bảy, 31 tháng 1, 2015

Bài tập ngày 3.3

I.Lý Thuyết.
- Switch ...case dùng để biểu diễn cấu trúc điều kiện( cũng như if ...else)
-Cấu Trúc :
switch (expression) {
case constant1:
block of instructions 1
break;
case constant2:
block of instructions 2
break;
.
.
.
default:
default block of instructions
}

Trong đó:
Switch mô tả lại biểu thức, theo sau switch là một biểu thức được đặt trong ngoặc tròn có thể nhận 2 giá trị là hằng số nguyên và hằng kí tự.
Case biểu diễn hằng số và sau đó là các câu lệnh
Kết thúc mỗi case là lệnh Break dùng để thoát khỏi case.
Nếu không có case nào thỏa mãn thì sẽ thực hiện lệnh default.
VD : Thực hiện điều kiện sắp xếp số điểm thỏa mãn :
II. Bài Tập
Bài 1 :  Declare two variables x and y. Assign values to these variables. Number x should be printed only if it
is less than 2000 or greater than 3000, and number y should be printed only if it is between 100 and 500. 
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 x,y;
    printf("Nhap X =");
    scanf("%d",&x);
    printf("Nhap Y=");
    scanf("%d",&y);
    if(x>2000&&x<3000){printf("\nX= %d Thoa Man",x);
    }
    else {printf("\nX=%d khong thoa man",x);
    }
    if(y>100&&y<500){printf("\nY= %d Thoa Man",y);
    }
    else {printf("\nY= %d Khong thoa man",y);
    }
    return 0;
}

Bài 2
  Write a program to show your computer’s capabilities. The user types in a letter of the alphabet and your program should display the corresponding language or package available. Some sample input ans output is given below :
                           Input                                  Output
                           A or a                                Ada
                           B or b                                 Basic
                           C or c                                 COBOL
                           D or d                                dBASE III
                           f or F                                  Fortran
                           p or P                                 Pascal
                           v or V                                Visual C++
Using the ‘switch’ statement to choose and display the appropriate message. Use the
default label to display a message if the input does not match any of the above         
letters.

#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;
    printf("Input :");
    scanf("%c",&a);
    switch(a){
        case 'A':
            printf("Ada");
            break;
        case'B':
            printf(" Basic");
            break;
        case'C':
            printf("COBOL");
            break;
        case'D':
            printf("dBASE III");
            break;
        case'F':
            printf("Fortran");
            break;
        case'P':
            printf("Pascal");
            break;
        case'V':
            printf("Visual C++");
            break;
    }
    return 0;
}
Bài 3 :
Accept values in three variables and print the highest value.
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 a,b,c;
    printf("Nhap A =");
    scanf("%d",&a);
    printf("Nhap B =");
    scanf("%d",&b);
    printf("Nhap C =");
    scanf("%d",&c);
    if(a>b&&a>c){
        printf("A= %d la so lon nhat.",a);
    }
    if(b>a&&b>c){
        printf("B= %d la so lon nhat.",b);
    }
    if(c>a&&c>b){
        printf("C= %d la so lon nhat",c);
    }
    return 0;
}





Kết luận : Qua bài ta học thêm được một cấu trúc điều kiện tương tự if ...else tuy nhiên dù tương tự nó cũng có mặt ưu và nhược vì vậy ta nên sử dụng những cấu trúc nào mà bản thân hoàn thiện nhất

Bài tập ngày 3.2

I.CẤU TRÚC IF ELSE
- Cấu trúc :
If(expression)statements;
VD: If(x==12)
prinf("x=12");
Dùng để khai báo một lệnh khi một điều kiện nào đó được chấp nhận.
Nếu khai báo một khối lệnh ta dùng cấu trúc :
If(expression)
{    statements1;
      statements2;
       ....
  }
Vd :If(x==12)
{ printf("x=%d",x);
}
-Ngoài điều kiện được chấp nhận để chương trình chạy chúng ta cũng có thể khai báo song song với if nếu điều kiện không thỏa mãn bằng cách sử dụng Else.
Cấu trúc
if (condition)
 statement1;
 else
 statement2;
VD: 
If(x==10)
printf("x bang 10");
else
printf("x khong bang 10");
II-Bài tập
Bài 1:  Write a program that accepts two numbers a and b and checks whether or not a is divisible by b.
 A chia hết cho B

A không chia hết cho B
#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 a,b;
printf("Nhap A:");
 scanf("%d",&a);
 printf("\nNhap B: ");
 scanf("%d",&b);
 if (0==a%b)
 {
  printf("\n\n A chia het cho B");
 }
 else
 {
  printf ("\n\n A khong chia het cho B");
}


 printf("\n\n\n");
 return 0;
}

Bài 2:

 Write a program to accept 2 numbers and tell whether the product of the two numbers is equal to or greater than 1000.
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 a,b,T;
    printf("Nhap A :");
    scanf("%d",&a);
    printf("Nhap B :");
    scanf("%d",&b);
    printf("Tong cua A va B =%d",T=a+b );
    if(T>1000){printf("\n\nT se lon hon 1000");
    }
    else{ if(T<1000)
    printf("\n\nT se nho hon 1000");
    }
    return 0;
}

Bài 3 


 #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 a,b,c;
    printf("Nhap A =");
    scanf("%d",&a);
    printf("Nhap B=");
    scanf("%d",&b);
    printf("\n\nTong A Va B =%d",c=a+b);
    if(a==c){printf("\n\nTa co A=A-B=C-B=%d=C",c=a-b);}
    else{printf("\n\nC se khac A va B");
    }
  
    return 0;
}

Bài 4
Montek company gives allowances to its employees depending on their grade as follows :
  Grade                                 Allowance
  A                                        300
  B                                        250
     Others                                100        
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 a,T;
char cap='F';

printf("\nNhap Cap : ");
scanf("%c",&cap);
printf("Tien Luong Co Ban =");
scanf("%d",&a);

if(cap=='A'){printf("Tong Luong = %d",T=a+300);
}
else if(cap=='B'){printf("Tong Luong = %d",T=a+250);
}
else if(cap=='C'){printf("Tong Luong = %d",T=a+100);
}

return 0;
}

Bài 5
Write a program to evaluate the Grade of a student for the following constraints
If marks >75 – grade A
If 60< marks < 75 – grade B
If 45<marks<60 – grade C
If 35<marks<45 - grade D
If marks < 35 – grade E
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 a;
printf("Nhap vao so hoc sinh :");
scanf("%d",&a);
if(a>75){printf("Lop A");
}
else if(a<=75&&a>=60){printf("Lop B");
}
else if(a<=60&&a>=45){printf("Lop C");
}
else if(a<=45&&a>=35){printf("Lop D");
}

return 0;
}


Kết luận: qua bài ta tìm hiểu được về lệnh điều kiện If else...
đó là bước đầu trong việc tìm hiểu về lập trình,tuy dễ nhưng việc đọc hiểu và giải những bài toán bằng ngôn ngữ lập trình cũng vẫn khá là vấn đề nan giải....

Bài tập ngày 3.1

I.Lý thuyết
1 . Hàm Gets
- Gets nằm trong thư viện : #include <stdio.h>
- Gets dùng để nhập dữ liệu.
- Cú pháp :
            Gets ("Chuỗi định dạng");
2.Hàm Puts
- Puts nằm trong thư viện : #include <stdio.h>
-Puts dùng để xuất dữ liệu.
-Cú pháp :
               Puts("Chuỗi định dạng");
+chuỗi định dạng là mảng khai báo được định dạng.
II. Bài tập

1.   Write a program to accept and add three numbers.
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 a,b,c;
    printf("So thu nhat :");
    scanf("%d",&a);
    printf("So thu hai :");
    scanf("%d",&b);
    printf("So thu ba :");
    scanf("%d",&c);
    printf("Ket thuc.");
    return 0;
Bài 2 :
2.  For the following values, write a program to evaluate the expression

z = a*b+(c/d)-e*f ;

  a=10
  b=7
  c=15.75
  d=4
  e=2
  f=5.6       
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[]) {
    float a,b,c,d,e,f,z;
    printf("Nhap a =");
    scanf("%f",&a);
        printf("Nhap b =");
    scanf("%f",&b);
        printf("Nhap c =");
    scanf("%f",&c);
        printf("Nhap d =");
    scanf("%f",&d);
        printf("Nhap e =");
    scanf("%f",&e);
        printf("Nhap f =");
    scanf("%f",&f);
    printf("z = a*b+(c/d)-e*f= %f ",z = a*b+(c/d)-e*f );
    printf("\n\nKet thuc.");
    return 0;
Bài 3
Write a program to evaluate the area and perimeter of the rectangle.
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[]) {
printf("Dien tich hinh chu nhat :");
int a,b,S,D;
printf("\nChieu dai a =");
scanf("%d",&a);
printf("\nChieu rong b =");
scanf("%d",&b);
printf("\n\nDien tich hinh chu nhat la :");
printf("\nS=a*b=%d",S=a*b);
printf("\n\nChu vi hinh chu nhat la :");
printf("\n\nD=(a+b)*2=%d",D=(a+b)*2);
printf("\n\nKet thuc.");
return 0;
Bài 4:  Write a program to evaluate the volume of a cylinder.
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 r,h,V;

printf("Nhap ban kinh r =");
scanf("%d",&r);
printf("Nhap chieu cao h =");
scanf("%d",&h);
printf("The tich cua mot xi lanh la :");
printf("\n\nV=h*3.14*r*2=%d",V=h*3.14*r*2);
Bài 5
Write a program to evaluate the net salary of an employee given the following constraints
Basic salary : $ 12000
DA : 12% of Basic salary
HRA : $150
TA : $120
Others : $450
Tax cuts – a) PF :14% of Basic salary and b) IT: 15% of Basic salary
Net Salary = Basic Salary + DA + HRA + TA + Others – (PF + IT)
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[]) {
printf("Muc Luong Cua Nhan Vien :");
int a,b,c,d,e,f,g,h;
printf("\nSalary =$");
scanf("%d",&a);
printf("DA=(Salary*12%)/100%= $%d",b=(a*12)/100);
printf("\nHRA =");
scanf("%d",&c);
printf("\nTA=");
scanf("%d",&d);
printf("\nOther =");
scanf("%d",&e);
printf("\n\nPF=(Salary*14%)/100%=$%d",f=(a*14)/100);

printf("\n\nIT=(Salary*15%)/100%=$%d",g=(a*15)/100);

h=a+b+c+d+e-(f+g);
printf("\n\n\nNet Salary =Basic Salary + DA+HRA+TA+Other-(PF+IT)= %d",h);
return 0;
}