100 cases of classic C language (91-100)

Source of all questions: Rookie tutorial C language classic 100 cases

Attach the previous title: 100 cases of classic C language (81-90)

catalogue

C exercise example 91
C exercise example 92
C exercise example 93
C exercise example 94
C exercise example 95
C exercise example 96
C exercise example 97
C exercise example 98
C exercise example 99
C exercise example 100

C exercise example 91

Title: time function example 1

Program analysis: none.

Program source code:

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//

#include <stdio.h>
#include <time.h>

int main ()
{
    time_t rawtime;
    struct tm * timeinfo;
    
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    printf ( "Current local time is: %s", asctime (timeinfo) );
    
    return 0;
}

The output results of the above examples are:

Current local time: Tue Nov 10 16:28:49 2015

C exercise example 92

Title: time function example 2

Program analysis: none.

Program source code:

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//

#include <stdio.h>
#include <time.h>

int main()
{
    time_t start,end;
    int i;
    start=time(NULL);
    for(i=0;i<300000;i++)
    {
        printf("\n");  // Return two times_ Time interval between t-variables
    }
    end=time(NULL);
    
    // Output execution time
    printf("The time interval is %6.3f\n",difftime(end,start));
}

The output results of the above examples are:

The time interval is 1.000

C exercise example 93

Title: time function example 2

Program analysis: none.

Program source code:

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    long i=10000000L;
    clock_t start,finish;
    double TheTimes;
    printf("do%ld The time required for the second empty cycle is",i);
    start=clock();
    while(i--);
    finish=clock();
    TheTimes=(double)(finish-start)/CLOCKS_PER_SEC;
    printf("%f Seconds.\n",TheTimes);
    return 0;
}

The output results of the above examples are:

The time required for 10000000 empty cycles is 0.025367 seconds.

C exercise example 94

Title: guessing game.

Program analysis: none.

example

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
void caizi(void)
{
    int n;
    char begin;
    int count = 1;
    srand((int)time(NULL));
    int m = (rand() % 100) + 1;
    puts("The game begins. Please enter a number:");
    while (1)
    {
        scanf("%d", &n);
        if (n == m)
        {
            printf("Yes, I did %d Times!\n", count);
            if (count == 1)
            {
                printf("You are a god! Worship\n");
                getchar();
                printf("You have reached the highest level. Do you still need to play? Y/N \n");
                scanf("%c", &begin);
                if (begin == 'Y' || begin == 'y')      //Repeat a nested loop of play
                {
                    caizi();
                }
                else
                {
                    printf("Thank you. Bye!\n");
                }
            }
            else if (count <= 5)
            {
                printf("You are a king! Great\n");
                getchar();
                printf("Need to challenge the highest level? Y/N \n");
                scanf("%c", &begin);
                if (begin == 'Y' || begin == 'y')
                {
                    caizi();
                }
                else
                {
                    printf("Thank you. Bye!\n");
                }
            }
            else if (count <= 10)
            {
                printf("You are a master! Crazy praise\n");
                getchar();
                printf("Need to challenge the highest level? Y/N \n");
                scanf("%c", &begin);
                if (begin == 'Y' || begin == 'y')
                {
                    caizi();
                }
                else
                {
                    printf("Thank you. Bye!\n");
                }
            }
            else if (count <= 15)
            {
                printf("You're a diamond! Angry praise\n");
                getchar();
                printf("Need to challenge the highest level? Y/N \n");
                scanf("%c", &begin);
                if (begin == 'Y' || begin == 'y')
                {
                    caizi();
                }
                else
                {
                    printf("Thank you. Bye!\n");
                }
            }
            else
            {
                getchar();
                printf("Your skills need to be improved! Play again? Y/N\n");
                scanf("%c",&begin);
                if (begin == 'Y' || begin == 'y')
                {
                    caizi();
                }
                else
                {
                    printf("Thank you. Bye!\n");
                }
            }
            break;
        }
        else if (n < m)
        {
            puts("It's too small!");
            puts("Re-enter:");
        }
        else
        {
            puts("It's too big!");
            puts("Re-enter:");
        }
        count++;//Counter
        
        
    }
}
 
 
int main(void)
{
    
    caizi();
    system("pause");
    return 0;
}

The output results of the above examples are:

When the game starts, please enter a number:
50
It's too big!
Re-enter:
25
It's too small!
Re-enter:
40
It's too big!
Re-enter:
30
It's too big!
Re-enter:
27
It's too small!
Re-enter:
28
Guessed right, used 6 times!
You are a master! Crazy praise
Need to challenge the highest level? Y/N
N
Thank you. Bye!

C exercise example 95

Title: application examples of simple structures.

Program analysis: none.

Program source code:

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//

#include <stdio.h>

struct programming
{
    float constant;
    char *pointer;
};

int main()
{
    struct programming variable;
    char string[] = "Rookie tutorial: http://www.runoob.com";
    
    variable.constant = 1.23;
    variable.pointer = string;
    
    printf("%f\n", variable.constant);
    printf("%s\n", variable.pointer);
    
    return 0;
}

The output results of the above examples are:

1.230000
Rookie tutorial: http://www.runoob.com

C exercise example 96

Title: calculate the number of occurrences of substrings in a string.

Program analysis: none.

example

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int i,j,k,TLen,PLen,count=0;
    char T[50],P[10];
    printf("Please enter two strings separated by carriage return, with the parent string first and the child string last:\n");
    gets(T);
    gets(P);
    TLen=strlen(T);
    PLen=strlen(P);
    for(i=0;i<=TLen-PLen;i++)
    {
        for(j=0,k=i;j<PLen&&P[j]==T[k];j++,k++)
            ;
        if(j==PLen)count++;
    }
    printf("%d\n",count);
    system("pause");
    return 0;
}

The output results of the above examples are:

Please enter two strings separated by carriage return, with the parent string first and the child string last:
abca
a
2

C exercise example 97

Title: input some characters from the keyboard and send them to the disk one by one until one # is entered.

Program analysis: none.

example

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include<stdio.h>
#include<stdlib.h>
int main()
{
    FILE*fp=NULL;
    char filename[25];
    char ch;
    printf("Enter the name of the file you want to save to:\n");
    gets(filename);
    if((fp=fopen(filename,"w"))==NULL)
    {
        printf("error: cannot open file!\n");
        exit(0);
    }
    printf("Now you can enter some characters you want to save to#End: \ n "");
    getchar();
    while((ch=getchar())!='#'){
        fputc(ch,fp);
    }
    fclose(fp);
    system("pause");
    return 0;
}

The output results of the above examples are:

Enter the name of the file you want to save to:
test.txt
Now you can enter some characters you want to save to # end:
www.runoob.com
#

C exercise example 98

Title: input a string from the keyboard, convert all lowercase letters into uppercase letters, and then output it to a disk file "test" to save. The input string ends with.

Program analysis: none.

Program source code:

example

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 
int main()
{
    FILE*fp=NULL;
    char str[50];
    int i,len;
    printf("Enter a string:\n");
    gets(str);
    len=strlen(str);
    for(i=0;i<len;i++)
    {
        if(str[i]<='z'&&str[i]>='a')
            str[i]-=32;
    }
    if((fp=fopen("test","w"))==NULL)
    {
        printf("error: cannot open file!\n");
        exit(0);
    }
    fprintf(fp,"%s",str);
    fclose(fp);
    
    system("pause");
    return 0;
}

The output results of the above examples are:

Enter a string:
www.runoob.com

C exercise example 99

Title: there are two disk files A and B, each storing A line of letters. It is required to combine the information in these two files (in alphabetical order) and output them to A new file C.

Program analysis: you need to create A.txt and B.txt first.

A.txt file content:
123
B.txt file content:
456
Program source code:

example

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    FILE*fa,*fb,*fc;
    int i,j,k;
    char str[100],str1[100];
    char tem;
    if((fa=fopen("A.txt","r"))==NULL) // A.txt file needs to exist
    {
        printf("error: cannot open A file!\n");
        exit(0);
    }
    fgets(str,99,fa);
    fclose(fa);
    if((fb=fopen("B.txt","r"))==NULL)  // B.txt file needs to exist
    {
        printf("error: cannot open B file!\n");
        exit(0);
    }
    fgets(str1,100,fb);
    fclose(fb);
    strcat(str,str1);
    for(i=strlen(str)-1;i>1;i--)
        for(j=0;j<i;j++)
            if(str[j]>str[j+1])
            {
                tem=str[j];
                str[j]=str[j+1];
                str[j+1]=tem;
            }
    
    if((fc=fopen("C.txt","w"))==NULL)  // Merge into C.txt
    {
        printf("error: cannot open C file!\n");
        exit(0);
    }
    fputs(str,fc);
    fclose(fc);
    system("pause");
    return 0;
}

After the above example runs and outputs the results, open C.txt. The contents are as follows:

123456

C exercise example 100

Title: there are five students. Each student has the scores of three courses. Input the above data from the keyboard (including student number, name and scores of three courses) to calculate the average score. The original data and the calculated average score are stored in the disk file "study".

Program analysis: none.

Program source code:

example

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include<stdio.h>
#include<stdlib.h>
typedef struct{
    int ID;
    int math;
    int English;
    int C;
    int avargrade;
    char name[20];
}Stu;
int main()
{
    FILE*fp;
    Stu stu[5];
    int i,avargrade=0;
    printf("Please enter the information of 5 students: student number, name and 3 grades:\n");
    for(i=0;i<5;i++)
    {
        scanf("%d %s %d %d %d",&(stu[i].ID),stu[i].name,&(stu[i].math),&(stu[i].English),&(stu[i].C));
        stu[i].avargrade=(stu[i].math+stu[i].English+stu[i].C)/3;
    }
    
    if((fp=fopen("stud","w"))==NULL)
    {
        printf("error :cannot open file!\n");
        exit(0);
    }
    for(i=0;i<5;i++)
        fprintf(fp,"%d %s %d %d %d %d\n",stu[i].ID,stu[i].name,stu[i].math,stu[i].English,
                stu[i].C,stu[i].avargrade);
    
    fclose(fp);
    // system("pause");
    return 0;
}

After the above example runs and outputs the results:

Please enter the information of 5 students: student number, name, 3 grades:
1 a 60 70 80
2 b 60 80 90
3 c 59 39 89
4 e 56 88 98
5 d 43 88 78
Open the stud y file as follows
1 a 60 70 80 70
2 b 60 80 90 76
3 c 59 39 89 62
4 e 56 88 98 80
5 d 43 88 78 69

Keywords: C Back-end

Added by kokomo310 on Mon, 08 Nov 2021 00:52:28 +0200