C Language Experiment Report

Source: Internet
Author: User

A Experimental topics, design ideas, implementation methods

1 Experimental topics

7-7 calculating the train running time (15 minutes)

Depending on the departure time and time of the train, the program is programmed to calculate the time spent on the journey.

4-5 finding the first N and (15 points) of the simple staggered sequence

The subject asks to write the program, computes the sequence 1-1/4 + 1/7-1/10 + ... The sum of the first n items.

4-2-8 calculation days (15 points)

The subject asks the program to calculate the day of the year in which a certain month is the first of the year.

2. Design Ideas

7-7 calculating the train run time I was the first two numbers directly subtracted, the number obtained by using the method of redundancy to get the answer. 4-5 finding the first n of a simple staggered sequence and

Imitate the method of C language book to sum up.

4-2-8 Calculation days

From the learning of the switch algorithm to obtain.

3. Implementation methods

7-7 calculating the train running time using the IF statement and the% seeking redundancy limit answer, that is, using the IF statement to limit the number of hours of the resulting results, to prevent boundary problems. 4-5 finding the first n of a simple staggered sequence and

The key is the definition of I and the execution of the loop body, and the conversion of flag.

4-2-8 Calculation days

Using the switch algorithm, and using the IF statement to distinguish between leap years, in February the number of days to distinguish between 28 or 29 days.

Two SOURCE program (only the most critical parts can be intercepted)

7-7 calculating the train run time

#include <stdio.h>
int main (void)
{
int x,y,a,b;
scanf ("%d%d", &x,&y);
a=y/100-x/100;
b=y%100-x%100;
if (b<0)
{
b+=60;
A-=1;
}
printf ("%02d:%02d", A, b);
return 0;

}

4-5 finding the first n of a simple staggered sequence and

#include <stdio.h>
int main (void)
{
int i,n,flag,denominator;
Double Sum,item;
scanf ("%d", &n);
sum=0;
flag=1;
Denominator=1;
for (i=1;i<=n*3;i+=3)
{
Item=flag*1.0/denominator;
Sum=sum+item;
Flag=-flag;
denominator=denominator+3;

}
printf ("sum =%.3lf\n", sum);
return 0;

}

4-2-8 Calculation days

#include <stdio.h>
int main (void) {
int year, month, day;
int days;
int i;
scanf ("%d/%d/%d", &year, &month, &day);
Days = day;
for (i = 1; i < month; ++i) {
switch (i) {
Case 1:
Case 3:
Case 5:
Case 7:
Case 8:
Case 10:
Case 12:
Days + = 31;
Break
Case 4:
Case 6:
Case 9:
Case 11:
Days + = 30;
Break
Case 2:
If ((year% 4 = = 0 && Year% 100! = 0) | | (Year% 400 = = 0))
Days + = 29;
Else
Days + = 28;
}
}
printf ("%d\n", days);
return 0;
}

Three Problems encountered and solutions, experience

The train's problem has been stuck in the boundary value that, do not know to use if to limit, the loop body sometimes because the code is wrong to get the wrong answer; These can only be programmed slowly by debugging;

Write code to cautious, serious, but also to pay attention to the problem of the side corner of the program, and through the usual practice of accumulating, to enhance their ability.

C Language Experiment Report

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.