这个课程属于哪里 | 计算机基础与程序设计 |
作业要求 | |
作业目的 | 自学教材 |
作业正文 | https://www.cnblogs.com/CCCY12345/p/17909622.html |
结构体:一种构造类型。
内部成员由一种或多种基本类型或构造类型构成。
实验六:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996)
typedef struct Student //定义结构体
{
long studentnumber;
char name[20];
float MT;
float EN;
float PH;
float total;
float average;
} STU;
void average(STU* A, int n);
void total(STU* A, int n);
void sortbyscore(STU* A, int n);
void dictionaryorder(STU* A, int n);
int searchbyname(STU* A, int n);
int main()
{
printf("(1)Append record\n(2)Calculate total and average score of every student\n");
printf("(3)Sort in ascending order by total score of every student\n(4)Sort in dictionary order by name\n");
printf("(5)Search by name\n(6)Write to a file\n");
printf("(7)Read from a file\n(0)Exit\n");
int n, p, y = 1;
printf("Input instruction (only 1):"); // 输入指令,现在没有数据,只能先输入数据
scanf("%d", &p);
printf("Please input the number of students:"); //输入学生数,确定结构体数组大小
scanf("%d", &n);
STU A[n];
if (p == 1)
{
int i;
for (i = 0; i < n; i++)
{
printf("Please input student number of student %d:", i + 1); //输入学生数据
scanf("%ld", &A[i].studentnumber);
getchar();
printf("Please input the name of student %d:", i + 1);
fgets(A[i].name,sizeof(A[i].name),stdin);
printf("Please input the score of math:");
scanf("%f", &A[i].MT);
printf("Please input the score of English:");
scanf("%f", &A[i].EN);
printf("Please input the score of Physics:");
scanf("%f", &A[i].PH);
}
}
else
{
printf("No data!\n");
}
while (y = 1)
{
printf("Please input the number of instruction:");
scanf("%d", &p);
if (p == 2)
{
total(A, n);
average(A, n);
}
if (p == 3)
{
sortbyscore(A, n);
}
if (p == 4)
{
dictionaryorder(A, n);
}
if (p == 5)
{
total(A, n); //先计算总分平均分,再输出
average(A, n);
int a = searchbyname(A, n);
printf("Name:%s\n", A[a].name);
printf("Student number:%ld\n", A[a].studentnumber);
printf("Score of MT:%.2f\n", A[a].MT);
printf("Score of En:%.2f\n", A[a].EN);
printf("Score of PH:%.2f\n", A[a].PH);
printf("Total score:%.2f\n", A[a].total);
printf("Average score:%.2f\n", A[a].average);
}
if (p == 6)
{
}
if (p == 7)
{
}
if (p == 0)
{
break;
}
}
}
void average(STU* A, int n)
{
int i;
for (i = 0; i <= n - 1; i++)
{
A[i].average = A[i].total / 3;
}
}
void total(STU* A, int n)
{
int i;
for (i = 0; i <= n - 1; i++)
{
A[i].total = A[i].MT + A[i].EN + A[i].PH;
}
}
void sortbyscore(STU* A, int n)
{
int i, j;
STU change;
for (i = 0; i <= n - 1; i++)
{
for (j = i + 1; j <= n - 1; j++)
{
if (A[i].MT + A[i].EN + A[i].PH < A[j].MT + A[j].EN + A[j].PH) //比较总分
{
change = A[i];
A[i] = A[j];
A[j] = change;
}
}
}
}
void dictionaryorder(STU* A, int n)
{
STU change;
int i, j;
for (i = 0; i <= n - 1; i++)
{
for (j = i + 1; j <= n - 1; j++)
{
int a = strcmp(A[i].name, A[i].name);
if (a > 0)
{
change = A[i];
A[i] = A[j];
A[j] = change;
}
}
}
}
int searchbyname(STU* A, int n) //返回符合条件的数组序号
{
int i, a;
char B[20];
getchar();
printf("Please input the name you want to search:");
fgets(B,sizeof(B),stdin);
for (i = 0; i <= n - 1; i++)
{
if (strcmp(A[i].name, B) == 0)
{
a = i;
}
}
return a;
}