C Programming Language (five)

Chapter selection control structures

Divide and Conquer: task decomposition refinement

Programming language: To allow the computer to execute program instructions written by high-level language, these instructions must be converted from high-level language into machine language form form the computer can understand, this conversion is done by the compiler

 

Algorithms: determined to solve a specific problem and take, limited, orderly, perform steps

 

Algorithms + data structures = programs (in this formula only established procedure-oriented languages)

Computer storage data structure is a way of organizing data, it refers to the presence of one or more data elements of the particular relationship between each set

Algorithm is operating or behavior description, the algorithm represents the strategies described in problem solving in a systematic way

 

Algorithm described by:

  1. Natural language (straightaway; lengthy, difficult to translate directly into the program)
  2. Description flowchart (visual image, clear, easy to understand and discover errors present in the program; larger space occupied)
  3. Structured flowchart describing NS
  4. The pseudocode then describes

 

Relational Operators

  • <=
  • >=
  • ==
  • !=

 Note: == is more, = is the assignment, the "==" mistakenly written as "=" in the C language syntax is allowed, not an error, but will lead to erroneous results of the operation

 

Conditional expression: it is represented by a non-zero value "true" for "false" with a value of 0

 

// L5-1 (single branch control)

#include  <stdio.h>
main()
{
    int a, b, max;
    printf("Input a, b:");
    scanf("%d,%d", &a, &b);
    if (a > b)  max = a;
    if (a <= b) max = b;
    printf("max = %d\n", max);    
}
// Run a result 
the Input A, B: 3,5 
max. 5 =

// L5-2 (double branch control)

#include  <stdio.h>
main()
{
    int a, b, max;
    printf("Input a, b:");
    scanf("%d,%d", &a, &b);
    if (a > b)  max = a;
    else    max = b;      /* 相当于a<=b的情况 */ printf("max = %d\n", max); }

// L5-3 (conditional operator)

#include <stdio.h> 
main () 
{ 
    int A, B, max; 
    the printf ( "the Input A, B:" ); 
    Scanf ( "% D, D%", & A, & B);  max = A> B ? a: b; / * calculate maximum two integer * / conditional expression with  the printf ( "% D max = \ n-" , max);}

 

A pair of braces set of logically related statement enclosed, called compound statement (similar to the Verilog begin end)

//L5-4

#include <stdio.h> 
#include   <stdlib.h> 
#include   <math.h>
 #define    the EPS-1E. 6 
main () 
{ 
    a float   A, B, C, Disc, P, Q; 
    the printf ( " Please Enter The Coefficients A, B, C: " ); 
    Scanf ( " % F,% F,% F " , & A, & B, & C);    
     IF (FABS (A) <= the EPS)          / * A = 0, the output" not a quadratic equation " * / 
    // real number on the float is not really, but its approximate within a certain range and, therefore, can only approximate method real number is compared to zero 
    { 
        printf ( " it iS not ! A for a Quadratic Equation \ the n- " );
        exit(0 ); // Exit C language standard library function is provided, is to terminate the entire program to be executed, the operating system forced back
         // call the function exit () need to be included at the beginning of the program header <stdlib.h> 
    } 
    Disc = B * B - . 4 * A * C; / * calculate a discriminant * / 
    P = - B / ( 2 * A); 
    Q = sqrt (FABS (Disc)) / ( 2 * A);
     IF (FABS (Disc) < the EPS =)     / * discriminant is equal to 0, the output is equal to two real roots * / 
    { 
        the printf ( " X1 = X2 =% .2f \ n- " , P); 
    } 
    the else                       
    { 
        IF (Disc> the EPS)        / * When the discriminant is greater than 0, the output of two real roots ranging * / 
        { 
            the printf ( " X1 =% .2f, X2 =% .2f \ n- " , P + Q, p- Q); 
        } 
        the else                      / * discriminant is less than 0, the output of two complex conjugate roots * / 
        { 
            the printf ( " X1 +% =% .2f 2FI,. " , P, Q); 
            the printf ( " .% .2f X2 = - 2FI% \ n- " , P, Q); 
        } 
    } 
}

 

switch statement is used multiple choice (similar to the case statement in Verilog)

//L5-5

#include  <stdio.h>
main()
{
    int    data1, data2;          
    char  op;                      
    printf("Please enter an expression:");
    scanf("%d%c%d", &data1, &op, &data2);  /* 输入运算表达式 */
    switch (op)                        /* 根据输入的运算符确定执行的运算 */
    {
    case '+':                         /* 加法运算 */
        printf("%d + %d = %d \n", data1, data2, data1 + data2); 
        break;//只有switch语句和break语句配合使用,才能形成真正意义上的多分支
    case '-':                         /* 减法运算 */
        printf("%d - %d = %d \n", data1, data2, data1 - data2);
        break;
    case '*':                         /* 乘法运算 */
        printf("%d * %d = %d \n", data1, data2, data1 * data2); 
        break;
    case '/':                         /* 除法运算 */
        if (0 == data2)         /* 为避免除0错误,检验除数是否为0 */
            printf("Division by zero!\n");
        else  
            printf("%d / %d = %d \n", data1, data2, data1 / data2); 
        break;
    default:                          /* 处理非法运算符 */
        printf("Invalid operator! \n");
    }
}

 

每个case后常量的次序发生改变时,不影响程序的运行结果,但不能有重复的case出现

从执行效率角度考虑,一般将发生频率高的情况放在前面

 

//L5-6

#include  <math.h>
#include  <stdio.h>
main()
{
    float  data1, data2;            
    char   op;                        
    printf("Please enter the expression:\n");
    scanf("%f %c%f", &data1, &op, &data2);  /* %c前有一个空格 */ 
    switch (op)                        
    {
    case '+':                    
        printf("%f + %f = %f \n", data1, data2, data1 + data2); 
        break;
    case '-':                    
        printf("%f - %f = %f \n", data1, data2, data1 - data2);
        break;
    case '*':    
    case 'x':
    case 'X':                
        printf("%f * %f = %f \n", data1, data2, data1 * data2); 
        break;
    case '/':                    
        if (fabs(data2) <= 1e-7)        /* 实数与0比较 */
            printf("Division by zero!\n");
        else 
            printf("%f / %f = %f \n", data1, data2, data1 / data2); 
        break;
    default: 
        printf("Invalid operator! \n");
    }
}

 

逻辑运算符

  • !:逻辑非
  • &&:逻辑与
  • ||:逻辑或

运算符&&和||都具有“短路”特性

 

程序测试:

  • 程序测试是确保程序质量的一种有效手段
  • 穷尽测试、抽样检查
  • 程序测试只能证明程序有错,而不能证明程序无错
  • 程序测试的目的是为了尽可能多的发现程序中的错误

测试用例:

  • 白盒测试(结构测试):测试程序的内部结构已知,按程序的内部逻辑设计测试用例
  • 黑盒测试(功能测试):不考虑程序内部的逻辑结构和处理过程,只检查程序的功能是否符合它的功能说明
  • 结合使用:选择有限数量的重要路径进行白盒测试,对重要的功能需求进行黑盒测试

 

//L5-7

#include <stdio.h>
#include <math.h>
#define EPS  1e-1
main()
{
    float a, b, c;
    int flag = 1;                            
    printf("Input a,b,c:");
    scanf("%f,%f,%f", &a, &b, &c);                                
    if (a+b>c && b+c>a && a+c>b)          
    {
        if (fabs(a-b)<=EPS && fabs(b-c)<=EPS && fabs(c-a)<=EPS) 
        {
            printf("等边");                  /* 等边 */
            flag = 0;                       /* 置标志变量flag为0值 */
        }
        else if (fabs(a-b)<=EPS || fabs(b-c)<=EPS|| fabs(c-a)<=EPS)
        {
            printf("等腰");                /* 等腰 */
            flag = 0;                       /* 置标志变量flag为0值 */
        }        
        if (fabs(a*a+b*b-c*c)<=EPS    || fabs(a*a+c*c-b*b)<=EPS
            || fabs(c*c+b*b-a*a)<=EPS)      
        {
            printf("直角");
            flag = 0;                 
        }
        if (flag)                       
        {
            printf("一般");
        }
        printf("三角形\n");
    }
    else                                 
    {
        printf("不是三角形\n");
    }
}
//测试结果
Input a,b,c:3,4,5
直角三角形
Input a,b,c:4,4,5
等腰三角形
Input a,b,c:,3,4,6
不是三角形
Input a,b,c:3,4,9
不是三角形
Input a,b,c:10,10,14.14
等腰直角三角形
Input a,b,c:4,4,4 等边三角形

 

 

边界测试:

在选用测试用例时,不仅要选用合理的输入数据,还应选用不合理的以及某些特殊的输入数据或者临界的点,对程序进行测试,称为边界测试

//L5-8

#include <stdio.h>
main()
{
    int score, mark; 
    printf("Please enter score:");
    scanf("%d", &score);
    if (score<0 || score>100)
    {
        mark = -1;
    }
    else
    {
        mark = score / 10;    
    }
    switch (mark)    
    {
    case 10:
    case 9:  
        printf("%d--A\n", score);
        break;
    case 8:  
        printf("%d--B\n", score); 
        break;
    case 7:  
        printf("%d--C\n", score); 
        break;
    case 6:  
        printf("%d--D\n", score); 
        break;
    case 5:
    case 4:
    case 3:
    case 2:
    case 1:
    case 0: 
        printf("%d--E\n", score); 
        break;
    default:
        printf("Input error!\n"); 
    }    
}

//5-9

#include  <stdio.h>
main()
{
    int a, b, max;
    printf("Input a, b:");
    scanf("%d,%d", &a, &b);
    max = a > b ? a : b;
    printf("max = %d\n", max); }

//5-10

#include  <stdio.h>
main()
{
    int a, b, max, ret;
    printf("Input a, b:");
    ret = scanf("%d,%d",&a, &b); /* 记录scanf函数的返回值 */
    if (ret != 2)  /* 根据scanf函数返回值,判断输入数据个数或者格式是否错误 */
    {
        printf("Input data quantity or format error!\n");
        while(getchar() != '\n');  /* 清除输入缓冲区中的错误数据 */
    }
    else             /* 此处可以是正确读入数据后应该执行的操作 */
    {
        max = a > b ? a : b;
        printf("max = %d\n", max);
    }
}

 

虽然函数scanf()不进行参数类型匹配检查,但是通过检验scanf()的返回值是否为应读入的数据项数,可以判断输入的数据项数和格式(包括输入格式错误、存在非法字符、无数据可读等)是否正确

 

位运算符:

  • 位运算就是对字节或字内的二进制数位进行测试、抽取、设置或移位等操作
  • 其操作对象不能是float、double、long double等其他数据类型,只能是char和int类型

 

  • -:按位取反
  • <<,>>:左移位、右移位(无论是左移位还是右移位,从一端移走的位不移入另一端,移出的位的信息都丢失了)
  1. 算术移位
  2. 逻辑移位
  • &:按位与,可用于对字节中的某位清零
  • ^:按位异或,可用于对字节中的某位置1
  • |:按位或

 

//L5-11

#include  <stdio.h>
main()
{
    int  x=12, y=8;
    printf("\n%5d%5d%5d", !x, x||y, x&&y);
    printf("\n%5u%5d%5d", ~x, x|y, x&y);
    printf("\n%5d%5d%5d\n", ~x, x|y, x&y);
}
//运行结果
    0    1    1
4294967283   12    8
  -13   12    8
#include <stdio.h> main() { int a, b, max; printf("Input a, b:"); scanf("%d,%d", &a, &b); if (a > b) max = a; if (a <= b) max = b; printf("max = %d\n", max); }

Guess you like

Origin www.cnblogs.com/dingdangsunny/p/11371733.html