C--third time job

Source: Internet
Author: User
Tags print format strcmp

Job Requirements One

Topic 6-1 Output Month English name

Topic 6-2 Find Week

Topic 6-3 Calculating the longest string length

Topic 6-4 specifying the position output string

Job: C Advanced Third Job (2) There is no idea, the knowledge point also did not understand, unfinished.

Job requirements two topics 6-1 output month English name

1. Design Ideas
(1) Main description problem algorithm
The first step: The topic requires the design of the GetMonth function to return the input n the corresponding month of the English word.
The second step: Define the variable I, define a string array, containing the English words for each month.
Step three: Use the For loop to iterate through the array, using the If judgment to return n the corresponding month. Because the input n is the month, it is greater than or equal to 1 to get the corresponding month, so the IF condition within the loop is n=i+1.
Fourth step: Finally, returns NULL if n is not a month.
(2) Flowchart
Main function:

Call Function:

2. Experiment Code

char *getmonth( int n ){  int i;  char *month[12]={"January","February","March","April","May","June","July","August","September","October","November","December"};  for(i=0;i<12;i++)  {    if(n==i+1)    {      return *(month+i);    }  }  if(n<=0||n>=13)  {    return NULL;  }}

3. Problems encountered during commissioning and solutions
There is no problem, but at the first commit there is a semicolon hit a comma resulting in compilation errors, through the dev-c++ compiled to find, has been corrected, should be noted next time.

Topic 6-2 Find Week

1. Design Ideas
(1) Main description problem algorithm
The first step: the need to design getindex function, find the input week corresponding to the ordinal.
Step Two: Define the variable I, which defines the return variable "Xuhao" with a value of-1, which is used as the return value. Defines an array of strings that contain seven-week English names. According to the corresponding serial number attention will be placed in the front of Sunday.
Step three: Use the For loop to iterate through the array, using the If judgment to assign I to "Xuhao" when the input is found to match the string in the array.
Fourth step: Return to "Xuhao".
2. Experiment Code

int getindex( char *s ){  int i;  int xuhao=-1;  char *week[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};  for(i=0;i<7;i++)  {    if(strcmp(s,*(week+i))==0)    {      xuhao=i;    }  }  return xuhao;}

3. Problems encountered during commissioning and solutions
Problem: The If judgment condition uses "= =" to directly determine if the string is equal and the answer is wrong.
WORKAROUND: Use the function to learn that the C language does not support the use of relational operators for comparison strings in the e-query. The answer is correct using the strcmp function comparison.

Topic 6-3 Calculating the longest string length

1. Design Ideas
(1) Main description problem algorithm
The first step is to implement a function that calculates the length of the longest string in a pointer array s with n elements.
Step two: Define the loop variable i, max Max and length Len.
Step three: Use the For loop to iterate through the array, using the Strlen function to find out the length of each string, max initially the length of the string, and when it is longer than that, it assigns Len to Max.
2. Experiment Code

int max_len( char *s[], int n ){  int i;  int max=0,len=0;  for(i=0;i<n;i++)  {    len=strlen(*(s+i));    if(max<len)    {      max=len;    }  }  return max;}

3. Problems encountered during commissioning and solutions
The error has been corrected because of the wrong letter.

Topic 6-4 specifying the position output string

1. Design Ideas
(1) Main description problem algorithm
The first step: the need to implement a function, the output of a given string of specific characters, the definition of variable i,j, length len. Pointer variable p to calculate the given string length assigned to Len.
Step two: Iterate through the array, first using if to find the same character as CH1, and assign its address to p. In the use of the loop to find the same character as CH2, if present, the output character and newline, if not present the output character.
The third step: if the ch1 is not found, the output line, and the whole loop after the same output line wrapping.
2. Experiment Code

char *match( char *s, char ch1, char ch2 ){    int i=0,j=0;    int len=0;     char *p;    len=strlen(s);    for(i=0;i<len;i++)    {        if(s[i]==ch1)        {            p=&s[i];            for(j=i;j<len;j++)            {                if(s[j]!=ch2)                {                    printf("%c",s[j]);                }                if(s[j]==ch2)                {                    printf("%c\n",s[j]);                    return p;                }            }            printf("\n");            return p;         }    }    printf("\n");    return s+i;}

3. Problems encountered during commissioning and solutions
There is no train of thought, on-line after the code to write the idea of the code, submit the situation "CH1 found, CH2 found" error, the reason is the last return when the wrong return p, p is changed to S+i, correct.

A programming problem

There is an array of AXB, which sequentially stores numbers from 1 to a*b. Where A is your university number of the first three digits, B is your university number of the last four digits, such as your study number is 2017023936, then the array size is 201 x 3936, the array in order to store 1 to 791136 (201 and 3936 of the product) of the integer. It is required to use the filter method to find and print the prime numbers in the array, with a print format of 5 prime numbers, separated by a space between the numbers.

The method of screening is: first, the n natural numbers are sorted in order. 1 is not a prime, nor a composite, to be crossed. The second number 2 is the prime number left, and all the numbers after 2 that can be divisible by 2 are crossed. 2 The number after the first not to be crossed is 3, leave 3, and then divide 3 all the numbers divisible by 3. 3 The number after the first not to be crossed is 5, leave 5, and then divide 5 all the numbers divisible by 5. This has been done, will be not more than N of all composite are screened out, leaving is not more than N of all prime numbers.

Job Requirements Three

1. Summarize the knowledge points learned in two weeks and answer the following questions?
(1) How does an array of pointers relate to pointers and arrays? Why can I manipulate an array of pointers with a level two pointer?
A: The pointer array is the array that holds the pointer elements, which hold the pointer in the form of an array. A pointer array can be used as a parameter when used, similar to a normal array usage method.
(2) C Advanced Third PTA Job (1) Any of the topics to use a two-level pointer to the array of pointers operation.
(3) What are the advantages of using a pointer array to handle multiple strings? Can I enter multiple strings directly to an uninitialized array of pointers? Why?
2.Git Address: Https://git.coding.net/ZJY15/ZJY15.git

3, Reviews 3 students of the week homework (in the homework to be reviewed students blog link), and invited 3 students to review your homework

4. Please use the table and line chart to present the number of lines and time of your week (4/9 8:00~4/23 8:00), blog count and Time spent

C--third time job

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.