Ws 2
Ws 2
Workshop 02
Objectives:
Practicing skills at analyzing and implementing simple programs
Contents: 7 programs
Program 1 2 3 4 5 6 7
Mark 2 2 1 1 2 1 1
Program 1 ( 2 marks)
Write a program that allows user inputting a simple expression containing one of four
operators +, -, *, / then the result is printed out to the monitor. Input format: num1
operator num2,
An example of user interface
Enter an expression (+ - * /): 4*5
Result: 20
Sample Analysis
Content Implementation
Nouns Expression, double num1, num2
format num1 operator char op
num2 double result
result
Verbs Begin
Accept num1, op, num2 scanf( “%lf%c%lf”, &num1, &op, &num2)
Calculate result switch (op)
Print out result { case ‘+’ : result = num1 + num2;
End print out result;
break;
case ‘-’ : result = num1 - num2;
print out result;
break;
case ‘*’ : result = num1 * num2;
print out result;
break;
case ‘/’ : if ( num2==0)
print out “Divide by 0
“
else
{ result = num1 /
num2;
print out result;
}
break;
default: print out “Op is not
supported”
}
scanf ("%lf%c%lf",&a,&op,&b);
switch (op){
case '+':
kq = a + b;
printf("%lf",kq);
break;
case '-':
kq = a - b;
printf("%lf",kq);
break;
case '*':
kq = a * b;
printf("%lf",kq);
break;
case '/':
if(b==0){
printf("Divide by 0 ");
}
else{
kq = a / b;
printf("%lf",kq);
}
default:
printf("Op is not supported");
}
getchar()
return 0;
}
Rules:
Tax-free income:
Personal pending amount (tiền nuôi bản thân) pa= 9 000 000$/month
Alimony (tiền cấp dưỡng) for each his/her dependent pd= 3 600
000$/month/dependent
With n dependents, Yearly tax-free income: tf = 12*(pa + n*pd)
Based on taxable income, the employee has to pay his/her income tax with levels
pre-defined in the following table:
Write a program which will compute income tax of a people using the following
interface:
Case 1:
Case 1:
return 0;
}
C2:
#include <stdio.h>
#include <stdio.h>
int main (){
int S=0;
int x;
do {
printf ("nhap vao x:");
scanf("%d",&x);
if (x!=0)
S = S + x;
}
while (x!=0);
printf ("S = %d",S);
return 0;
}
Program 4 (1 mark)
Program 5: (2 marks)
#include <stdio.h>
#include <ctype.h>
int main (){
char ch;
int nVowels =0, nConsonants = 0, nOthers=0;
do {
ch=getchar();
ch=toupper(ch);
if (ch>='A' && ch <='Z') {
switch (ch) {
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' :
nVowels ++;
break;
default: nConsonants++;
}
}
else {
nOthers++;
}
}
while (ch!='\n');
printf ("nVowels: %d\n",nVowels);
printf ("nConsonants: %d\n",nConsonants);
printf ("nOthers: %d",nOthers);
return 0;
}
Program 6: (1 marks)
Related Each character will be stored as its ASCII code with value 0..255
knowledge
Problem Write a C program that will print out the ASCII code table.
Analysis Suggested algorithm (logical order of verbs)
ASCII code Begin
→ int code For each code = 0 to 255
{ Print out (“%c : %d, %o, %X\n”, code, code, code, code);
If (code !=0 && code %20==0) getchar(); /* code page of 20 lines */
}
End.
#include <stdio.h>
int main() {
int code;
for (code = 0; code <= 255; code++) {
printf("%4c, %4d, %4o, %4X\n", code, code, code, code);
if (code != 0 && code % 20 == 0) {
getchar();
}
}
return 0;
}
Program 7: (1 marks)
d = c2 - c1;
printf ("ASCII code difference: %d\n", d);
return 0;
}
END