Basic topic 1 of C language

This topic is related to C language structure, function pointer and sorting

subject

Xiao Wang is the warehouse keeper of the company. One day, he received a task: find a steel pipe from the warehouse (the number of steel pipes to be tested in the warehouse is not more than 1000). This doesn't sound like much, but the requirements of this steel pipe really make him difficult. The requirements are as follows:

1. This steel pipe must be the shortest in the warehouse;

2. This steel pipe must be the thickest of the shortest steel pipes;

3. This steel pipe must conform to the code of the first two steel pipes (each steel pipe has a different code, the larger the code is, the closer the production date is).

There are relevant materials, but the one that meets the requirements is selected from hundreds of steel pipe materials by hand

Otherwise, please write a program to help him solve this problem.

Input the number of steel pipes to be tested n, integer n (n < = 1000) indicates the number of groups of test data)

Each steel pipe has three information, i.e. length (in mm), diameter (in mm) and code (a string containing 9 digits) of a steel pipe.

Output the qualified steel pipe

Steel tube structure is defined as follows:

typedef struct m
{
int a;//length
int b;//diameter
char c[10];//number
}Infor;

Enter the prompt: "please enter the number of steel pipes: \ n"

Input data format: "% d"

Input prompt: "please input steel pipe information (length, diameter and code):"

Input data format: "% d%d%s"

Output prompt information: printf("the information of the qualified steel pipe in the warehouse is: \ n");

Output data format: "% d\t%d\t%s\n"

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

const int maxn = 1010;
typedef struct m info;
struct m
{
    int a;
    int b;
    char c[10];
};

void sort(int n, info a[],int (*cmp)(info, info));
int SortBya(info, info);
int SortByb(info, info);
int SortByc(info, info);

int main(void)
{
    int n;
    info a[maxn];
    printf("Please enter the number of steel pipes:\n");
    scanf("%d",&n);
    printf("Please input steel pipe information(Length, diameter and coding:\n");
    int i;
    for(i=0;i<n;i++) scanf("%d %d %s",&a[i].a, &a[i].b, a[i].c);
    sort(n, a, SortBya);
    int find = 0, mark=0;
    int t = a[0].a;
    while(!find)
    {
        mark ++;
        if(t != a[mark].a) find = 1;
    }
    sort(mark, a, SortByb);
    find = 0;
    mark = 0;
    t = a[0].b;
    while(!find)
    {
        mark ++ ;
        if(t != a[mark].b) find = 1;
    }
    sort(mark, a, SortByc);
    printf("The information of the qualified steel pipe in the warehouse is:\n");
    printf("%d\t%d\t%s\n", a[0].a, a[0].b, a[0].c);
    return 0;
}

void sort(int n, info a[],int (*cmp)(info, info))
{
    int i,j;
    for(i=1;i<n;i++)
        for(j=0;j<n-i;j++)
            if((*cmp)(a[j], a[j+1]))
            {
                info temp;
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
}

int SortBya(info a, info b)
{
    if(a.a > b.a) return 1;
    else return 0;
}
int SortByb(info a, info b)
{
    if(a.b < b.b) return 1;
    else return 0;
}
int SortByc(info a, info b)
{
    long int t1,t2;
    t1 = atol(a.c);
    t2 = atol(b.c);
    if(t1 < t2) return 1;
    else return 0;
}

Keywords: C

Added by allex01 on Tue, 03 Dec 2019 04:42:53 +0200