C Language Blog Job--structure

Source: Internet
Author: User

One, PTA laboratory work. Title 1:6-3 The specified number of persons in the array of structures 1. The results of PTA experiment

2. Design Ideas
struct student{ char  num[10];  int  year,month,day ;};//该结构体表示人员的编号,和出生年月日  struct student fun(struct student  *std, char  *num){    定义变量i表示指针的指向,定义x=0来判断是否有此人员;    定义结构变量 p,便于输出空串;    for( i = 0 to 7        if(*(std+i)).num和num相同的话){            x=1;返回 *(std+i);        end if;     if(x为0;p.num[0]=‘\0‘;返回 p;
3. Code

4. Problems encountered during commissioning and solutions
    • Problem: The pointer std points to a value that hasn't changed, even if it's written as std++.

    • Workaround: Change std++ to Std+i

Topic 2:7-2 Time conversion 1. PTA Experiment Results

2. Design Ideas
struct time {    int hour;    int minute;    int second;};结构体表示时间;int main(){    定义结构变量 clock来存放起始时间;        scanf(时,分,秒);    定义变量 n表示后来加的秒数;    scanf(n的值);    clock.second=clock.second+n;    if(秒>=60){        clock.second-60;        clock.minute++;        if(分>=60){                clock.minute-60;            clock.hour++;            if(时==24){                clock.hour=0;    定义变量i表示分别输出时,分,秒;    定义变量x,y表示各时段的十位和个位;    for(i=1 to 3        if(i==1){            x=clock.hour/10;y=;clock.hour%10)            输出x,y        else if(i==2){            x=clock.minute/10;y=clock.minute%10;            输出x,y);                     else{            x=clock.second/10;y=clock.second%10;            输出x,y);                             end if                  换行;
3. Code


4. Problems encountered during commissioning and solutions
    • The problem encountered:

      • In the case of an int, the output is only 0. not 00; it's too cumbersome to change to a character variable.

      • When the second hand does not carry, the seconds do not change, but in fact it is changed.

    • Workaround: The time, minutes, seconds output two times, once for the output of 10 bits, one output hundred.

Topic 3:7-6 the entry and display of the Address Book 1. PTA Experiment Results

2. Design Ideas
struct people{    char name[11];    char date[20];    char sex;    char gudingphone[81];    char yidongphone[81];};该结构体表示人的名字,出生年月日,性别,固定电话,手机号码。int main(){    定义N表示执行的次数;    scanf(N);    定义结构变量 std[81]来表示人员,便于存放其信息;    定义变量i表示循环次数;    for(i=0 to N-1        scanf(人的名字,出生年月日,性别,固定电话,手机号码    end if    定义变量k表示人员的编号;    scanf(k);    定义变量i=0表示循环次数;定义变量j来判断是否有这个人;    for(i=0 to k-1        scanf(j            if(输入的j有人员表示){            输出(std[j].name,std[j].gudingphone,std[j].yidongphone,std[j].sex,std[j].date);                       else{输出("Not Found\n");         end if
3. Code

4. Problems encountered during commissioning and solutions
    • The problem encountered:

      • No output no found! Should output 10 times but only output five times to stop, should be the program to the limit and automatically exit.
    • WORKAROUND: First let it enter a number, and then directly determine whether there is and output, so not only to avoid the complexity of the code, but also to make the output more accurate.

Second, this week's topic set PTA Final ranking. 1.PTA Rankings

Third, read the code (1 points)
    • 7-2 Time Conversion: the need to write a program, in HH:MM:SS format to output a given time after n seconds after the time value (more than 23:59:59 from 0 o'clock to start the clock)

      • The start time is entered through the struct, and then the end time is output. The greatest advantage of this code is to use "%02d", which means that if, minutes, seconds only a single number, then will automatically add 0 in front. This is not what you learned, and his code is very imitative, more intuitive and simple to the effect of the problem presented.

    • 7-6 entry and display of contacts

      • The code uses the struct and the struct array variable to assign the information to different people, and then outputs the desired number or outputs no found directly. This code intuitively reflects his way of thinking, the layout is organized, reasonable, not like my code is sometimes very confusing, so this is where I can learn.

Iv. Summary of this week's study 1. Summarize this week's learning content 1.1 nested calls to functions

The case of calling another function in one function is called a nested invocation of a function. If function A calls function B, function B calls function C again, and a call is nested to make a nested call to the function. Programs that have nested call functions need to define several different functions separately, each with different functions, and together they can solve complex problems.

1.2 Principles of the organizational function
    • (1) Top-down: The program design, should first consider the overall steps, after considering the details of the steps, the first consideration of the global goals, and then consider the local targets. Start from the top of the overall goal of the design, and gradually make the problem materialized, do not start with the pursuit of numerous details.

    • (2) Stepwise refinement: For complex problems, the large operation steps should be decomposed to form the relationship between functions.

    • (3) Function realization: Through the gradual refinement, the program to solve the global target decomposition into a local target, and then further decomposed into a specific small target, and finally the small target with a function to achieve.

1.3 Issues to be aware of when designing a function.
    • (1) Limit the length of the function: a function of the number of statements should not be too much, that is, easy to read, understand. Also facilitates the debugging of the program;

    • (2) Avoid the repetition of function function, in order to achieve a definition, the purpose of multiple use.

    • (3) Reduce the use of global variables. You define a global variable only if you do need data that is shared by multiple functions.

1.4 Two points for recursive functions

Recursive egress and recursive invocation of the equation.

1.5 structure, common body, enumeration this construction data type characteristics.
    • (1) Structs: structs and arrays are structured data types, and arrays are different, and structs can handle different types of data. Structure has three types of data, you can see the structure can also have the structure type, the type of the common body of data.

    • (2) Common body: A common body is a type of construction of a multivariable shared storage space that allows several different variables to share the same storage space.

      • Differences between common and structural bodies:

        - 1.结构体每一位成员都用来表示一种具体事务的属性,共用体成员可以表示多种属性(同一存储空间可以存储不同类型的数据)。- 2.结构体总空间大小,等于各成员总长度,共用体空间等于最大成员占据的空间。- 3.共用体不能赋初值。
    • (3) enumeration: The enumeration type refers to the value of the variable can be all listed, after defining an enumeration variable, the value of the variable is determined in the definition. The difference between it and the struct, the common body is that the enumeration element is a constant and can only be assigned during the definition phase

1.6 Principle of recursive function
    • Recursion (recursion) is a subroutine (or function) calling itself directly or invoking itself indirectly through a series of call statements, which is a basic way to describe the problem and solve the problem.

    • Recursion has two basic elements: (1) boundary condition: Determines when recursion terminates, also known as recursive exit. (2) Recursive mode: Big problem is how to decompose into small problems, also known as recursive body. Recursive functions only have these two elements in order to produce results after a finite number of calculations

    • A recursive function calls a procedure similar to a nested invocation of multiple functions, except that the calling function and the called function are the same function. In order to ensure the proper execution of recursive functions, a working stack is set up for the system. Specifically, the internal execution of recursive calls is as follows:

      • (1) At the beginning of the movement, a work stack is first established for recursive invocation, and its structure includes value parameter, local variable and return address;

      • (2) The value parameter of the recursive function and the current value of the local variable and the return address of the call are stacked before each recursive call.

      • (3) At the end of each recursive call, stack the top element of the stack so that the corresponding value parameter and local variable revert to the value before the call, and then move on to the location specified by the return address to continue execution.

    • In the recursive function, the calling function and the called function are the same function, it is necessary to pay attention to the recursive function of the call hierarchy, if the call recursive function of the main function is called the No. 0 layer, after entering the function, the first recursive call itself called the 1th layer call, from the level I recursive call itself called the i+1 layer. Conversely, exiting the i+1 layer call should return to layer I.

2. List some of the wrong questions this week.
    • The argument is the address of an array a[9], so A/b parameter is right, and C is a pointer, so it is pointing to the address, so it is correct, and D is an integer variable, so it is wrong.

    • 1. No structural variables are defined and should be added emp[10];
      2. Add a GetChar () to store newline characters because you want to wrap, prevent line breaks from entering the structure members, 3. The input should be added to emp[i] before it can be entered correctly. 4.gechar () is also used as a line break.
      5. Output real wages with ' base pay + floating wage-expenditure ' and not just direct output ' expense '.

C Language Blog Job--structure

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.