C Programming Language learning experience

First, FIG Mind

Second, understanding the syntax

1, if-else usage

if (条件一) {
  语句一;
}
else {
  语句二;
}

Question: Matching else and if the relationship is not clear, resulting in abnormal program.

Solution: keep in mind else paired off with its recent if, not used in conjunction with other if, and to standardize the code writing format, to make it look with added clarity.
Usage 2, while statement

while (循环条件) {
   循环语句;
}

Note: the while relatively easy to use, essentially all of the cyclic structure can be written while.

3, for use statement

for(表达式1; 表达式2; 表达式3)
{
    循环体语句
}

Question: When programming, often run timeout for loop, poor control cycles.

Solution: When using a for loop, to write again on rough paper, determining accurate, then transcribed onto your own code. And plenty of practice, increase their understanding of the for loop, a fundamental solution.

4, do-while to use

do
{
    循环体语句
}while(循环条件);

Question: Just started, he could not understand this cycle, this cycle of different ideas and other cycle, led to his always wrong.

Solution: understanding the principles of this statement and the cycle of circulation special order, pay more training, when he saw a problem you can determine whether a do-while statement, it means have a more accurate understanding of the do-while.

5, switch statements use

switch(整形表达式)
{ 
    case 常量表达式1:  语句1;break;
    case 常量表达式2:  语句2;break;
    case 常量表达式n:  语句n;break;
    default:  语句n+1;
}

Note: Not all back case has a break, need to break the rational use position to make their code more simple.

6.break and continue to use

switch(整形表达式)
{ 
    case 常量表达式1:  语句1;break;
    case 常量表达式n:  语句n;break;
    default:  语句n+1;

}

for(表达式1; 表达式2; 表达式3)
{
    语句1;
     continue;
    语句2;
}

Note: BREAK statement can appear in the select structure and loop structure in, but continue statement can only be used in the loop statement, continue statement after use, this cycle is immediately terminated, after the statement of no longer continue to perform, directly next round cycle.

Three, PTA scores screenshots

2.2, 2.3 jobs

4.1 Job

Chapter III operations

Four, PTA code analysis

topic:

Code:

#include <stdio.h>
int main()
{
    int a,b;
    char c;
    double x;
    scanf("%d %d %c",&a,&b,&c);
    if (b==90&&c=='m') {
        x = a*6.95*(1-0.05);
    }
    else if (b==90&&c=='e') {
        x = a*6.95*(1-0.03);
    }
    else if (b==93&&c=='m') {
        x = a*7.44*(1-0.05);
    }
    else if (b==93&&c=='e') {
        x = a*7.44*(1-0.03);
    }
    else if (b==97&&c=='m') {
        x = a*7.93*(1-0.05);
    }
    else if (b==97&&c=='e') {
        x = a*7.93*(1-0.03);
    }
    printf("%.2f",x);
    return 0;
}

Ideas: analysis of the topic, the topic of oil and the type of services synthesis, is divided into six cases, in turn solved.

Error summarized:

(1), too many if-else, people get confused mind can not have a clear idea.

(2), forget two decimal places, leading to wrong answer.

Harvest: make me a better understanding of the usage of if-else, and consolidation of knowledge, lay a solid foundation.

topic:

Code:

#include <stdio.h>
int main()
{
    int l,u,f;
    double c;
    scanf("%d %d",&l,&u);
    if (l>u||l>100||u>100) {
        printf("Invalid.\n");
    }
    else if (l<=u) {
        printf("fahr celsius\n");
        for(f=l;f<=u;f+=2) {
            c = 5.0*(f-32)/9;
            printf("%d%6.1lf\n",f,c);
        }
    }
    return 0;
}

Ideas: Firstly, if the input data value within a given range, based on the determination result, to perform different operations, the use of if-else, in one case, we need to calculate two successively applied, so use of the cyclic structure.

Error summarized:

(1), is divided is not the case, enters the circulation directly, calculated only the second case.

(2), which is given to "occupy six character width, aligned to the right" can not be expressed in the form of codes.

Harvest: I know occupy the n expression character width, and the consolidation of the for loop usage.

topic:

Code:

#include <stdio.h>
#include <math.h>
int main()
{
    int h,n,i;
    double s,H;
    scanf("%d %d",&h,&n);
    s = h;
    H = h*1.0/pow(2,n);
    if (n==0) {
        s = 0;
        H = 0;
    }
    else if (n==1) {
        s = s;
        H = h;
    }
    else {
        for(i=1;i<=n-1;i++) {
            s=s+2.0*h/pow(2,i);
        }
    }
    printf("%.1f %.1f",s,H);
    return 0;
}

Ideas: by on rough paper ball falling to the process of analysis, to understand the process of dropping the ball, and verify your guess is correct, many experiments and improve their own code, the final result obtained by the compiler.

Error summarized:

(1), subjective to his first image to write code, they are not familiar with the process leading to, so all bets are wrong.

(2), taking into account only a certain aspect of it, it can not lead to a perfect solution to the problem.

(3) For reservations decimal places, did not pay attention, leading to wrong answer.

Harvest: Let me also need to understand programming analysis on rough paper, in the solution.

V. Summary:

By more than a month of study, I learned some basic knowledge of C programming language, although my skills are not very skilled, but one has been significant progress, and I hope in the future be able to acquire more skills and abilities.

Guess you like

Origin www.cnblogs.com/xzxzxzx/p/11666223.html