C Language Blog Job--structure

Source: Internet
Author: User
Tags greatest common divisor strcmp

First, the PTA experimental work 1 (6-3) (1). PTA Submission List

(2) Design ideas
//定义student结构体变量stu;//定义循环变量i//遍历数组,对每一个编号变量进行匹配for i=0 to N-1    stu=*std//把std的值赋给stu结构体变量    if stu.num==num        //跳出循环    std++;end iif i大于等于N    //把stu各项归0;//返回stu变量
(3) Code

(4) Problems encountered in the commissioning process and PTA submission List status note
    • Note that NUM in the referee program is a character array that requires a string alignment using the strcmp function.
    • After rethinking, there is no need to define a struct variable, only the STD struct pointer, i.e.
    • Can be done as well.

      Topic 2 (7-5)
(1). PTA Submission List

(2) Design ideas
struct rational{    int a1;    int b1;    int a2;    int b2;};//存放两个有理数的分子和分母(a/b的形式)//定义rational结构体变量num//输入两个有理数//对a1*b2和a2*b1的值进行判断if num.a1*num.b2等于num.a2*num.b1    //原样输出两个有理数,在这两个数之间加上等于号else if num.a1*num.b2小于num.a2*num.b1    //原样输出两个有理数,在这两个数之间加上小于号else    //原样输出两个有理数,在这两个数之间加上大于号
(3) Code

(4) Problems encountered in the commissioning process and PTA submission List status note
    • At the beginning, the problem was complicated, and the number and the simplest of the two rational numerator and denominator were calculated and compared.
    • found that the more complex the writing, may be to write a custom function to find the numerator and denominator of the greatest common divisor, there is a function for the simplest, and then define four variables to hold two rational number of the initial value, and then compare the denominator: if the denominator is large, if the denominator is small: if the numerator is large, If the molecule is small: then output different results separately. Quite troublesome.
    • Then we use the mathematical knowledge: A/b and the C/D of the operation relationship.
    • Then just want to judge a by D and C multiply B relationship to do different output, so write only need to write three output.
Topic 3 (7-6) (1). PTA Submission List

(2) Design ideas
struct friends{    char name[20];    char birthday[20];    char sex;    char fixedline[30];    char phone[30];};//定义朋友的五个属性:姓名、生日、性别、固话、手机//定义朋友的个数n、循环变量i、查询次数k;//输入n;//定义friends结构体数组fid[n]//输入数组内容for  i=0 to n-1    //按顺序输入姓名、生日、性别、固话、手机end i;//查询for k to 1    //输入编号i;    if i不在0~n内        //输出Not Found    else         //按顺序输出姓名、固话、手机、性别、生日end k;
(3) Code

(4) Problems encountered in the commissioning process and PTA submission List status note
    • The reason for the mistake is my superfluous: in the K-loop, I judge the output of k=1. Then you get rid of the error and change the date that was stored with the integer variable to a string form.
Second, this week's topic set PTA Final ranking. PTA Rankings

Iii. reading code 1) strcmp function
int __cdecl strcmp ( const char * src, const char * dst ){        int ret = 0 ;        while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)//先对src和dst字符串进行无符号转换,再取它们相减的值给ret,再对ret进行取非,当ret=0时继续比较                ++src, ++dst;        //对ret不为0情况判断        if ( ret < 0 )                ret = -1 ;        else if ( ret > 0 )                ret = 1 ;        return( ret );}
    • The advantage lies in the part of the while statement, the first two strings are unsigned, expand the range of values, and then subtract, to avoid problems. The prefix increment and the comma operator are then used to allow a statement to perform the self-increment of two pointers.
2) Strrev function
char * __cdecl _strrev ( char * string ){        char *start = string;        char *left = string;        char ch;        while (*string++) ;                /* find end of string */                 //将string指到倒一位        string -= 2;        //        while (left < string)        {                ch = *left;                *left++ = *string;                *string-- = ch;        }        return(start);}
    • The advantage is the skillful use of pointers, monocular operators, loop conditions, because left++ = string, left does not need to be equal to string. The character exchange is performed, and finally another character pointer to the first character address is returned.
Iv. Summary of the study this week (3 points) 1. Summarize what you learned this week. The following two items must be consulted, and other knowledge can be included in this summary of the structure, the common body, enumeration of this structure data type characteristics. 1) Structural body
    • Members of that type can be customized (different or same type variables), which can be more convenient and flexible when dealing with large amounts of the same data
    • The struct type and the standard type provided by the system (such as int, char, float, double) can be used to define variables, except that struct types need to be declared by themselves beforehand
    • Depending on the compiler, memory storage will be different, when the structure is stored in memory alignment related processing, the system default alignment factor is 4 (that is, by the int type alignment, you can think that each adjacent two data member storage is a size of 4 integer times)
    • Types and variables are different concepts. You can only assign a value to a member in a struct variable, not to a struct type. At compile time, no space is allocated for the type, only space is allocated for the variable.
    • The member names in the struct can be the same as the variable names in the program, but they are not related.
    • A member of a struct can be another struct.
2) Shared body
    • Organize different data items into a single whole, which occupies the same storage unit in memory.
    • The same memory segment can be used to hold several different types of members, but only one of them is stored at each moment, not several at a time.
    • The member that functions in the common body variable is the last member to be deposited, and when a new member is deposited, the original member loses its role.
    • The address of the shared body variable and the address of its members are the same address.
    • An assignment to one of the members affects another member.
3) Enumeration
    • Used to declare a set of named constants, which can be defined as enumerated types when there are several possible values for a variable.
    • Typically (under no special definition) This set of variable names is defined as constants (not modifiable).
//例1:定义一星期编号enum week{Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};//此时Monday的值为0,Tuesday的值为1,顺序递增一。//例2:定义一星期工作日编号enum week{Sunday=7, Monday=1, Tuesday, Wednesday, Thursday, Friday, Saturday};//此时Tuesday值为2,即从下一位变量没有赋给值的变量开始递增一。//特殊:不能赋给负数
Principle of recursive function
    • Decompose a problem into the same many small problems, and then solve them in the same way.
    • It is divided into recursive and regression steps.
    • Recursion is the process of decomposition, so there must be a restriction that allows recursion to have a boundary
    • Regression: Recursion reaches the boundary, solves the smallest problem and returns, then steps back, resolves the problem, and finally returns the result
2. List some of the wrong questions this week.
    • No GetChar () is added before the input of the string. (but without this sentence it can be run).

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.