【C/PTA —— 14.结构体1(课内实践)】

6-1 计算两个复数之积

在这里插入图片描述

struct complex multiply(struct complex x, struct complex y)
{
    
    
    struct complex product;
    product.real = x.real * y.real - x.imag * y.imag;
    product.imag = x.real * y.imag + x.imag * y.real;
    return product;
}

6-2 结构体数组中查找指定编号人员

在这里插入图片描述

struct student fun(struct student* std, char* num)
{
    
    
	struct student product;
	for (int i = 0; i < 8; i++)
	{
    
    
		if (std[i].num[0] == num[0])
			product = std[i];
	}
	return product;

}

6-3 综合成绩

在这里插入图片描述

double getAverage(Applicant* a)
{
    
    
    double sum = 0.0;
    sum = a->computational * 1.0 * 0.4 + a->humanistic * 1.0 * 0.5 + a->logical * 1.0 * 0.3 + a->presentation * 1.0 * 0.6 + a->scientific * 1.0 * 0.8;
    return sum;
}

6-4 结构体数组按总分排序

在这里插入图片描述

void calc(struct student* p, int n)
{
    
    
    for (int i = 0; i < n; i++)
    {
    
    
        for (int j = 0; j < 3; j++)
        {
    
    
            p[i].sum += p[i].score[j];
        }
    }
}
void sort(struct student* p, int n)
{
    
    
    struct student tmp;
    for (int i = 0; i < 4; i++)
    {
    
    
        for (int j = i+1; j < 5; j++)
        {
    
    
            if (p[i].sum < p[j].sum)
            {
    
    
                tmp = p[j];
                p[j] = p[i];
                p[i] = tmp;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_73900397/article/details/134784379