mirror of
https://github.com/renbaoshuo/202401-programming-assignments.git
synced 2024-12-16 15:44:39 +00:00
【实践课内】15.结构体1
This commit is contained in:
parent
c7b7c64b35
commit
cae42be0cb
6
【实践课内】15.结构体1/6-1 计算两个复数之积.c
Normal file
6
【实践课内】15.结构体1/6-1 计算两个复数之积.c
Normal file
@ -0,0 +1,6 @@
|
||||
struct complex multiply(struct complex x, struct complex y) {
|
||||
struct complex z;
|
||||
z.real = x.real * y.real - x.imag * y.imag;
|
||||
z.imag = x.real * y.imag + x.imag * y.real;
|
||||
return z;
|
||||
}
|
50
【实践课内】15.结构体1/6-1 计算两个复数之积.md
Normal file
50
【实践课内】15.结构体1/6-1 计算两个复数之积.md
Normal file
@ -0,0 +1,50 @@
|
||||
# 6-1 计算两个复数之积
|
||||
本题要求实现一个计算复数之积的简单函数。
|
||||
|
||||
### 函数接口定义:
|
||||
```c++
|
||||
struct complex multiply(struct complex x, struct complex y);
|
||||
```
|
||||
其中`struct complex`是复数结构体,其定义如下:
|
||||
```c++
|
||||
struct complex{
|
||||
int real;
|
||||
int imag;
|
||||
};
|
||||
```
|
||||
|
||||
### 裁判测试程序样例:
|
||||
```c++
|
||||
#include <stdio.h>
|
||||
|
||||
struct complex{
|
||||
int real;
|
||||
int imag;
|
||||
};
|
||||
|
||||
struct complex multiply(struct complex x, struct complex y);
|
||||
|
||||
int main()
|
||||
{
|
||||
struct complex product, x, y;
|
||||
|
||||
scanf("%d%d%d%d", &x.real, &x.imag, &y.real, &y.imag);
|
||||
product = multiply(x, y);
|
||||
printf("(%d+%di) * (%d+%di) = %d + %di\n",
|
||||
x.real, x.imag, y.real, y.imag, product.real, product.imag);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 你的代码将被嵌在这里 */
|
||||
```
|
||||
|
||||
### 输入样例:
|
||||
```in
|
||||
3 4 5 6
|
||||
```
|
||||
|
||||
### 输出样例:
|
||||
```out
|
||||
(3+4i) * (5+6i) = -9 + 38i
|
||||
```
|
12
【实践课内】15.结构体1/6-2 结构体数组中查找指定编号人员.c
Normal file
12
【实践课内】15.结构体1/6-2 结构体数组中查找指定编号人员.c
Normal file
@ -0,0 +1,12 @@
|
||||
struct student fun(struct student *std, char *num) {
|
||||
struct student res = {"\0", 0, 0, 0};
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
if (strcmp(std[i].num, num) == 0) {
|
||||
res = std[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
45
【实践课内】15.结构体1/6-2 结构体数组中查找指定编号人员.md
Normal file
45
【实践课内】15.结构体1/6-2 结构体数组中查找指定编号人员.md
Normal file
@ -0,0 +1,45 @@
|
||||
# 6-2 结构体数组中查找指定编号人员
|
||||
|
||||
人员的记录由编号和出生年、月、日组成,N名人员的数据已在主函数中存入结构体数组std中,且编号唯一。
|
||||
函数fun的功能是:找出指定编号人员的数据,作为函数值返回,由主函数输出,若制定编号不存在,返回数据中的编号为空串。
|
||||
|
||||
### 函数接口定义:
|
||||
```c++
|
||||
struct student fun(struct student *std, char *num)
|
||||
```
|
||||
|
||||
其中 `std` 和 `num` 都是用户传入的参数。 函数fun的功能是:在 `std` 结构体数组中找出指定编号 `num` 人员的数据,作为函数值返回,由主函数输出,若制定编号不存在,返回数据中的编号为空串。
|
||||
|
||||
### 裁判测试程序样例:
|
||||
```c++
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#define N 8
|
||||
struct student
|
||||
{ char num[10];
|
||||
int year,month,day ;
|
||||
};
|
||||
struct student fun(struct student *std, char *num)
|
||||
int main()
|
||||
{
|
||||
struct student std[N]={ {"111111",1984,2,15},{"222222",1983,9,21},{"333333",1984,9,1},{"444444",1983,7,15},{"555555",1984,9,28},{"666666",1983,11,15},{"777777",1983,6,22},{"888888",1984,8,19}};
|
||||
struct student p;
|
||||
char n[10]="666666";
|
||||
p=fun(std,n);
|
||||
if(p.num[0]==0)
|
||||
printf("Not found !\n");
|
||||
else
|
||||
{ printf("Succeed !\n ");
|
||||
printf("%s %d-%d-%d\n",p.num,p.year,p.month,p.day);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 请在这里填写答案 */
|
||||
```
|
||||
|
||||
### 输出样例:
|
||||
```out
|
||||
Succeed !
|
||||
666666 1983-11-15
|
||||
```
|
3
【实践课内】15.结构体1/6-3 综合成绩.c
Normal file
3
【实践课内】15.结构体1/6-3 综合成绩.c
Normal file
@ -0,0 +1,3 @@
|
||||
double getAverage(Applicant *a) {
|
||||
return a->presentation * 0.4 + a->logical * 0.5 + a->humanistic * 0.3 + a->scientific * 0.6 + a->computational * 0.8;
|
||||
}
|
47
【实践课内】15.结构体1/6-3 综合成绩.md
Normal file
47
【实践课内】15.结构体1/6-3 综合成绩.md
Normal file
@ -0,0 +1,47 @@
|
||||
# 6-3 综合成绩
|
||||
|
||||
小明的公司为了招聘新员工设计了一套考试方案。从表达能力、逻辑能力、人文素质、科学素质、计算思维5个方面进行考察。综合成绩 = 表达能力*0.4+逻辑能力*0.5+人文素质*0.3+科学素质*0.6+计算思维*0.8。要求定义一个计算综合成绩的函数。
|
||||
|
||||
|
||||
|
||||
### 函数接口定义:
|
||||
```c
|
||||
double getAverage(Applicant *a);
|
||||
```
|
||||
|
||||
a中存放一个应聘者的考试成绩。要求返回综合成绩。
|
||||
|
||||
### 裁判测试程序样例:
|
||||
```c
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
typedef struct{
|
||||
int presentation;
|
||||
int logical;
|
||||
int humanistic;
|
||||
int scientific;
|
||||
int computational;
|
||||
}Applicant;
|
||||
double getAverage(Applicant* a);
|
||||
int main()
|
||||
{
|
||||
Applicant a;
|
||||
double overall;
|
||||
scanf("%d%d%d%d%d", &a.presentation,&a.logical,&a.humanistic,&a.scientific,&a.computational);
|
||||
overall = getAverage(&a);
|
||||
printf("%.2f\n",overall);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 您提交的代码将放置在这里 */
|
||||
```
|
||||
|
||||
### 输入样例:
|
||||
```in
|
||||
100 100 100 100 100
|
||||
```
|
||||
|
||||
### 输出样例:
|
||||
```out
|
||||
260.00
|
||||
```
|
33
【实践课内】15.结构体1/6-4 结构体数组按总分排序.c
Normal file
33
【实践课内】15.结构体1/6-4 结构体数组按总分排序.c
Normal file
@ -0,0 +1,33 @@
|
||||
void calc(struct student *p,int n) {
|
||||
for (int i = 0 ; i < n; i++) {
|
||||
p[i].sum = p[i].score[0] + p[i].score[1] + p[i].score[2];
|
||||
}
|
||||
}
|
||||
|
||||
void swap(struct student *a, struct student *b) {
|
||||
struct student temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
}
|
||||
|
||||
void sort(struct student *p,int n) {
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
for (int j = 0; j < n - i - 1; j++) {
|
||||
if (p[j].sum < p[j + 1].sum) {
|
||||
swap(&p[j], &p[j + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// or use qsort:
|
||||
|
||||
int cmp(struct student *a, struct student *b) {
|
||||
return b->sum - a->sum;
|
||||
}
|
||||
|
||||
void sort(struct student *p,int n) {
|
||||
qsort(p, n, sizeof(struct student), cmp);
|
||||
}
|
||||
*/
|
72
【实践课内】15.结构体1/6-4 结构体数组按总分排序.md
Normal file
72
【实践课内】15.结构体1/6-4 结构体数组按总分排序.md
Normal file
@ -0,0 +1,72 @@
|
||||
# 6-4 结构体数组按总分排序
|
||||
|
||||
有一组学生数据,每个数据中含有三门课成绩,请按成绩总和从高到低对这组数据进行排序。
|
||||
编写函数calc求出每名学生的总分。
|
||||
编写函数sort按每名学生的总分从高到低对这组数据进行排序
|
||||
|
||||
### 函数接口定义:
|
||||
```c++
|
||||
void calc(struct student *p,int n);
|
||||
void sort(struct student *p,int n);
|
||||
```
|
||||
|
||||
其中 `p` 和 `n` 都是用户传入的参数。
|
||||
函数calc求出`p`指针所指的结构体数组中 `n` 名学生各自的总分。
|
||||
函数sort对`p`指针所指的结构体数组的学生数据按总分降序排序。
|
||||
|
||||
### 裁判测试程序样例:
|
||||
```c++
|
||||
#include <stdio.h>
|
||||
struct student
|
||||
{
|
||||
int num;
|
||||
char name[15];
|
||||
float score[3];
|
||||
float sum;
|
||||
};
|
||||
void calc(struct student *p,int n);
|
||||
void sort(struct student *p,int n);
|
||||
int main()
|
||||
{
|
||||
struct student stu[5];
|
||||
int i,j;
|
||||
float f;
|
||||
for(i=0;i<5;i++)
|
||||
{
|
||||
scanf("%d%s",&stu[i].num,stu[i].name);
|
||||
for(j=0;j<3;j++)
|
||||
{
|
||||
scanf("%f",&f);
|
||||
stu[i].score[j]=f;
|
||||
}
|
||||
}
|
||||
calc(stu,5);
|
||||
sort(stu,5);
|
||||
for(i=0;i<5;i++)
|
||||
{
|
||||
printf("%5d%15s",stu[i].num,stu[i].name);
|
||||
printf(" %.1f %.1f %.1f %.1f\n",stu[i].score[0],stu[i].score[1],stu[i].score[2], stu[i].sum);
|
||||
}
|
||||
return 0;
|
||||
|
||||
|
||||
/* 请在这里填写答案 */
|
||||
```
|
||||
|
||||
### 输入样例:
|
||||
```in
|
||||
1 zhang 89 87 85
|
||||
2 liu 92 98 96
|
||||
3 li 74 71 72
|
||||
4 xion 95 98 99
|
||||
5 liu 99 100 100
|
||||
```
|
||||
|
||||
### 输出样例:
|
||||
```out
|
||||
5 liu 99.0 100.0 100.0 299.0
|
||||
4 xion 95.0 98.0 99.0 292.0
|
||||
2 liu 92.0 98.0 96.0 286.0
|
||||
1 zhang 89.0 87.0 85.0 261.0
|
||||
3 li 74.0 71.0 72.0 217.0
|
||||
```
|
Loading…
Reference in New Issue
Block a user