1 The syntax of switch

  • toy example
#include <stdio.h>

void action1(int x, int y){
    printf("x+y = %d\n",x+y);
}

void action2(int x, int y){
    printf("x*y = %d\n",x*y);
}

void action3(int x, int y){
    printf("x-y = %d\n",x-y);
}

int main()
{
    // input 'a' or 'A', print a plus b
    // input 'b' or 'B', print a times b
    // input 'c' or 'C', print a minus b
    // output a voice warning otherwise   
    char ch;
    int a=15,b=23;
    ch = getchar();
    printf("a = %d,\tb = %d\n",a,b);
    switch(ch){
        case 'a': printf("\nCalculate a + b, if 'a' is inputted.\n");
        case 'A': action1(a,b); break;
        case 'b': printf("\nCalculate a * b, if 'b' is inputted.\n");
        case 'B': action2(a,b); break;
        case 'c': printf("\nCalculate a - b, if 'c' is inputted.\n");
        case 'C': action3(a,b); break;
        default: putchar('\a');
    }
    return 0;
}

2 The syntax of both for and while

  • toy example
#include<stdio.h>
int main()
{
    // The comparison between statements "while" and "for"
    // calculate sum = 1+2+...+100
    int i=1,sum=0;
    while(i<=100)
    {
        sum=sum+i;
        i++;
    }
    printf("sum=%d\n",sum);

    i = 1;
    sum = 0;
    while(i<=100)   sum += i++;
    printf("sum=%d\n",sum);
    

    for(i=1,sum=0;i<=100;i++) sum+=i;
    printf("sum=%d\n",sum);

    for(i=1,sum=0;i<=100;++i) sum+=i;
    printf("sum=%d\n",sum);

    for(i=1,sum=0;i<=100;sum+=i,i++);
    printf("sum=%d\n",sum);

    for(i=0,sum=0;i<100;i++,sum+=i);
    printf("sum=%d\n",sum);

    for(i=0,sum=0;i<100;++i,sum+=i);
    printf("sum=%d\n",sum);

    for(i=1,sum=0;i<=100;sum+=i++);
    printf("sum=%d\n",sum);

    for(i=0,sum=0;i<100;sum+=++i);
    printf("sum=%d\n",sum);

    return 0;
}

3 Sugar functions

  • Print each digit of fraction 3.1415926 as characters.
#include <stdio.h>
#define N 7
int main(){
    // print each digit of fraction 3.1415926 as characters.
    int i,b = 3;
    double a=3.1415926;
    printf("The result is \n");
    printf("3.");
    for(i=0;i<N;i++){
        a = 10*(a - b);
        b = (int)a;
        printf("%c", b+48);
    }
    return 0;
}
  • Encryption
#include <stdio.h>
#include <string.h>
int main(){
    // encryption rule:
    // print the origin character minus 2 if the origin character is capital
    // print the origin character plus 5 if the origin character is lowercase
    int i,N;
    char s[] = "ShanghaiUniversity";
    N = strlen(s);
    for(i=0;i<N;i++){
        if(s[i]>='A'&&s[i]<='Z')
            printf("%c", s[i]-2);
        else
            printf("%c", s[i]+5);

    }
    return 0;
}
  • Find all primes between positive integers a and b
#include<stdio.h>
#include<math.h>
int check_prime(int n){
    // check if n is a prime
    int i,upper_bound = sqrt(1.0*n), flag = 1;
    if (n == 1) return 0;
    for (i = 2; i <= upper_bound; i++){
        if (n % i == 0){
            flag = 0;
            break;
        }
    }
    return flag;
}

int main(){
    // find all primes between a and b
    int isprime, output_counter;
    int lower_range, upper_range;
    printf("Please input starting number.\n");
    scanf("%d", &lower_range);
    printf("Please input ending number.\n");
    scanf("%d", &upper_range);
    printf("Searching for prime numbers from %d to %d.\n", lower_range, upper_range);
    output_counter = 0;
    for (int i = lower_range; i <= upper_range; i++){
        isprime = check_prime(i);
        /*
        if (isprime){
            printf("%d\n", i);
        }
        */

        if (isprime){
      printf("%d\t", i);
      if(i>2)  i++;
      output_counter++;
      if(!(output_counter%5)) printf("\n");
        }

        /*
        if (output_counter == 5){
            // start a new line every 10 outputs.
            printf("\n");
            output_counter = 0;
        }
        */

    }
    return 0;
}
  • Print Fibonacci series with a fixed length
#include <stdio.h>
int main(){
    // print Fibonacci series with fixed length
    void Fibonacci(int Fib_len);
    int Fib_len;
    printf("Input the length of Fibonacci series : ");
    scanf("%d",&Fib_len);
    printf("The series of Fibonacci with length %d is \n",Fib_len);
    Fibonacci(Fib_len);
    return 0;
}

void Fibonacci(int Fib_len){
    // Fibonacci series
    int i, Fib1 = 1, Fib2 = 1, Fib3;
    printf("%d\t",Fib1);
    printf("%d\t",Fib2);
    for(i=3; i<Fib_len; i++){
        Fib3 = Fib1+Fib2;
        Fib1 = Fib2;
        Fib2 = Fib3;
        printf("%d\t",Fib3);
    }
}

4 Problems in the previous week

  • Calculate 1/2019 with 100-digit precesion. Print it with 10 digits per line.
#include <stdio.h>
#define N 100
int main(){
  // calculate 1/2019 with 100-digit precesion
    int i,a=1,b=2019;
    printf("The result is \n");
    printf("0.\n");
    for(i=1;i<=N;i++){
        a = (a%b)*10;
        printf("%d", a/b);
        if(i%10==0) printf("\n");
    }
    return 0;
}

5 Problem

  • Decrypt a sentence “Rmj lwjfyjxy Atrrzsnxy Nfwy~ tk Amnsf!”
  • Decryption rule:
    • print the origin character plus 2 if the origin character is between 63 and 88
    • print the origin character minus 5 if the origin character is between 102 and 127
    • print the origin character otherwise