C Language Blog Job--structure

Source: Internet
Author: User
Tags array length new set

First, the PTA experimental work problem 1:1. PTA Submission List

2. Design Ideas
void calc(struct student *p,int n){    for i=0 to i=n-1        累加三门成绩存于sum    end for; } void sort(struct student *p,int n){    定义结构体变量tmp;        /*选择法*/    for i=0 to i=n-2         定义变量index=i;        for j=i+1 to j=n-1            判断数组最大数,并将下标存于index;        end for        交换p[i]与p[index]的位置    end for }
3. Code

4. Problems encountered in commissioning process and PTA submission List status note

Problems encountered: The first bubble method, the sort reversed
Solution: Change the judging condition of inner layer cycle of bubbling method, instead of replacing position if (P[j].sum < p[j+1].sum), or using selection method

Topic 2: Rational number comparison 1. PTA Submission List

2. Design Ideas
定义结构体 fraction{    包含分子,分母 }; //该结构体表示分数 int main(){    定义结构体数组 s[2];    定义整形数组 real[2];    for i=0 to i=2        输入每个分数        计算分数的大小存于real[i]中;    end for    判断real[0]与real[1]的大小,输出相应的情况  
3. Code

4. Problems encountered in commissioning process and PTA submission List status note

There's no problem.

Topic 3: Entry and display of contacts 1. PTA Submission List

2. Design Ideas
定义结构体 info{    包含姓名,出生年月日,性别,固定电话和手机};//该结构体存放朋友的信息 int main() {    定义变量    输入要录入的人数n;    定义结构体数组 s[n];    for i=0 to i=n-1        输入每个人的信息;    end for    输入查询次数k    定义数组num[11],存放查询的序号;    for i=0 to i=k-1        输入num[i];    end for    for i=0 to i=k-1        判断输入的序号num[k]是否在有效,若有效输出对应信息,否则输出Not found    
3. Code

4. Problems encountered in commissioning process and PTA submission List status note

Problem encountered: Array out of bounds, just beginning to define the phone num length is 16, does not take into account the Terminator;

Workaround: Define the array length as 20

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

Third, read the code 1. Hangman Games

Game rules:
1, the answer word is written on the paper (each character a piece of paper), and is covered up, each time the player guesses an English character (letter).
2, if the English text Yuan Guess (in the English word of the answer appears), guessed the character is opened. For example: The answer is book, if you guess two o in O,book will be considered guessed.
3. If the English text element does not appear in the answer of the word, it will be added a row in the hangman figure. A total of 7 strokes are required to complete the hangman chart, such as. Note: The same wrong-guessing character can only be drawn on the map, for example: The answer is book, the first time you guess a (not guessed) will draw a stroke on the picture, but the second time to guess a and will not draw more.
4. If the player has guessed all the characters in the answer before the Hangman diagram is complete, the player wins (win).
5, if the player has not guessed all the answers in the characters and hangman graph completed, then the player loses (lose).
6, if the player does not win or lose the situation will not play, then we say the player timid give up (chicken out)

Code:

  #include <stdio.h> #include <string.h> #define MAXN 100int left, Chance;  Also need to guess left position, the wrong chance times after the loss of Char S[MAXN],S2[MAXN];   The answer string s, the player guesses the letter sequence S2 int win,lose;    Win=1 said win, Lose=1 said lost void guess (char ch); int main () {int rnd;        while (scanf ("%d%s%s", &rnd,s,s2) ==3&&rnd!=-1) {printf ("Round%d\n", Rnd);     win=lose=0;        Solving a new set of data is initialized Left=strlen (s);        chance=7;   for (int i=0;i<strlen (s2); i++) {guess (s2[i]); Guess a letter if (win| |    lose) break;    Check Status}//According to the structure of the output if (win) printf ("You win.\n");    else if (lose) printf ("You lose.\n");     else printf ("You chickened out.\n"); } return 0;}    void guess (char ch) {int bad=1;            for (int i=0;i<strlen (s); i++) if (s[i]==ch) {left--;     S[i]= ";            The bad=0 will be changed to a space with the guessed letter;    } if (bad)--chance;    if (!chance) lose=1; if (!left) win=1;} 
    • For the subject, the need to maintain more content, such as whether to win or lose, as well as the remaining number of opportunities, such as the code with the global variables to solve, if not the global variables, they are passed to the function guess, some of the parameters will be modified by guess.
    • The question is said to guess the letter again wrong, the code does not save the already guessed letters, to save the need to add a character array in the program, let Guessed[ch] identify the letter Ch has been guessed. But there's a better way in the program to change the already-guessed letters into spaces.
2. Design the function char *insert (s1,s2,n), using the pointer to insert the string at the specified position n in the string S1 s2

Code:

#include<stdio.h>char* insert(char *s1,char *s2,int n){    int j=0;    char *ss=new char[100];    char *tsptr=ss;    for(int i=0;i<n;i++)        *ss++=*s1++; //s1前n-1个存于新数组 ss    while(*s2!=0)        *ss++=*s2++;    //将s2存入新 ss    while(s1!=0)        *ss++=*s1++     //将 s1 剩余的字符存入 ss     *ss==0; //添加结束符     return tsptr;}void main(){    char s1[]="123456789";    char s2[]="1234";    char *ss=intsert(s1,s2,4);  //调用函数     

With pointers in this function, the code is greatly reduced

Iv. Study Summary of the Week 1. Summarize what you learned this week. 1. Structural body
struct 结构名{    类型名 结构成员名1;    类型名 结构成员名2;    ……    类型名 结构成员名n; }结构变量1,结构变量2,……;
2. Shared body
union 结构名{           类型名 结构成员名1;           类型名 结构成员名2;            ……};

Characteristics:

    • The same memory segment can be used to hold several different types of members, but only one at a time, instead of storing several
    • The member that functions in the common body variable is the last member to be deposited, and the original member loses its role after depositing a new member.
    • The address of the shared body variable and the address of its members are the same address
    • You cannot assign a value to a shared body variable name, you cannot attempt to reference a variable name to get a value, and you cannot initialize a shared body variable when you define it.
    • You cannot use a shared body variable as a function parameter, and you cannot bring a function back to a shared body variable, but you can work with a pointer to a shared body variable
    • A common body type can appear in a struct type definition, or you can define a common body array. Conversely, structs can also appear in the definition of a common body type, and arrays can also be members of a common body
3. Enumeration
enum DAY{      MON=1, TUE, WED, THU, FRI, SAT, SUN};

Characteristics:

    • An enumerated type is a collection of elements (enumeration members) that are named integral constants, separated by commas.
    • Day is an identifier that can be seen as the name of the collection, an option, or an optional item.
    • The default value for the first enumeration member is 0 of the integer type, and the value of the subsequent enumeration member is added 1 on the previous member.
    • The value of an enumeration member can be set artificially to customize an integer within a range.
    • The enum type is an alternative to the pre-processing directive # define.
    • The type definition is semicolon-terminated;

Note: An enumeration type with the same name cannot be defined in the same program, and a named constant with the same name cannot exist in a different enumeration type.

Since an enumeration is also a data type, it can declare variables as well as basic data types.

Method One: The definition of the enumeration type and the declaration of the variable are separated:

enum DAY{      MON=1, TUE, WED, THU, FRI, SAT, SUN};enum DAY yesterday;enum DAY today;enum DAY tomorrow; //变量tomorrow的类型为枚举型enum DAYenum DAY good_day, bad_day; //变量good_day和bad_day的类型均为枚举型enum DAY

Method Two: Type definition and variable declaration at the same time:

enum //跟第一个定义不同的是,此处的标号DAY省略,这是允许的。{    saturday,    sunday = 0,    monday,    tuesday,    wednesday,    thursday,    friday} workday; //变量workday的类型为枚举型enum DAYenum week { Mon=1, Tue, Wed, Thu, Fri Sat, Sun} days; enum BOOLEAN { false, true } end_flag, match_flag; //定义枚举类型并声明了两个枚举型变量

Method Three: Use the TypeDef keyword to define an enumeration type as an alias and use that alias for variable declaration:

typedef enum workday{    saturday,    sunday = 0,    monday,    tuesday,    wednesday,    thursday,    friday} workday; //此处的workday为枚举型enum workday的别名workday today, tomorrow;
2. List some of the wrong questions this week.


I didn't notice that the array in the D option is out of bounds


We should define the structure array in the blank question.

C Language Blog Job--structure

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.