定义输入函数 int add (student stu【】, int n),要求 1) 将一条学生信息添加在最后面,2)输入成功返回1;否则返回0;
时间: 2024-02-22 12:55:34 浏览: 18
可以这样定义输入函数:
```c
int add(student stu[], int n) {
FILE *fp;
fp = fopen("students.dat", "ab");
if (fp == NULL) {
printf("文件打开失败!\n");
return 0;
}
fwrite(&stu[n-1], sizeof(student), 1, fp);
fclose(fp);
return 1;
}
```
其中,`student`是一个结构体类型,表示学生信息。`stu`是一个数组,存储了多个学生信息。`n`是一个整数,表示要添加的学生在数组中的下标。函数的作用是将指定下标的学生信息添加在文件`students.dat`的末尾。如果文件打开失败,则返回0;否则将学生信息写入文件,并返回1表示输入成功。注意,这里使用了`fwrite`函数来将结构体写入文件,需要保证结构体的成员变量类型和顺序与读取时一致。
相关问题
完善这段代码:int main() { int max = 5; //定义当前学生信息的最大长度 int counter = 0; //当前仓库中的学生信息数目 stu* storage; //定义学生信息的存储仓库 void add(string, int);//定义添加学生的函数 storage = new student[max];;//初始化仓库,申请能存储max个学生信息结构体的空间 for (int i = 0; i <= 9; i++) {//添加10个学生信息 //姓名为“姓名”+“i”的形式,使用to_string(i),将i转换为字符。 //年龄使用生成一个17到21之间的随机整数。 //调用函数add,进行信息的添加。 } //打印学生的信息:cout << "+--------------学生信息列表 ------------+" << endl; //通过for循环打印学生信息。 //释放申请的空间。 }
在代码中添加添加学生信息的 `add` 函数,以及循环添加学生信息和打印学生信息的代码。修改后的代码如下:
```
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
struct student {
string name;
int age;
};
int main() {
int max = 5; //定义当前学生信息的最大长度
int counter = 0; //当前仓库中的学生信息数目
student* storage; //定义学生信息的存储仓库
void add(string, int); //定义添加学生的函数
storage = new student[max];//初始化仓库,申请能存储max个学生信息结构体的空间
srand(time(NULL)); // 设置随机数种子
for (int i = 0; i <= 9; i++) {//添加10个学生信息
string name = "姓名" + to_string(i);
int age = rand() % 5 + 17; //生成一个17到21之间的随机整数
add(name, age);//调用函数add,进行信息的添加。
}
//打印学生的信息
cout << "+--------------学生信息列表 ------------+" << endl;
for (int i = 0; i < counter; i++) {
cout << "姓名:" << storage[i].name << ", 年龄:" << storage[i].age << endl;
}
//释放申请的空间
delete[] storage;
return 0;
}
void add(string name, int age) {
extern student* storage;
extern int max, counter;
if (counter == max) {
// 如果仓库已满,扩容为原来的两倍
max *= 2;
student* new_storage = new student[max];
for (int i = 0; i < counter; i++) {
new_storage[i] = storage[i];
}
delete[] storage;
storage = new_storage;
}
storage[counter].name = name;
storage[counter].age = age;
counter++;
}
```
在这个示例中,我们首先定义了一个 `add` 函数,用于向学生信息存储仓库中添加一个新的学生信息。然后,我们使用 `rand()` 函数生成一个 17 到 21 之间的随机整数作为学生的年龄,并使用 `add` 函数添加学生信息。如果仓库已满,则在添加学生信息之前将仓库扩容为原来的两倍。最后,我们使用一个循环来打印学生的信息,并使用 `delete[]` 运算符释放申请的空间。
用定义一个课程类 course,要求所有数据成员定义成 private,理解类的定义和使用。 (1) 使用#define 定义课程容量 MAXCAP (2) 定义课程类的属性:课程号 id、课程名 name、教室 loc(可选)、课程容量 cap、 教师 tea、学生 stu、已有学生数量 stucnt 和节次 time(可选); (提示:教室 loc 和教师 tea 使用前面定义类的对象,学生 stu 使用学生类数组指 针) (3) 定义总课程数 crscnt (提示:使用静态变量,在构造函数中加 1) (4) 定义无参和有参的构造函数;(提示:初始化 stu 数组,长度为 cap) (5) 定义拷贝构造函数(提示:初始化 stu 数组,长度为 cap,并将参数的数据成员值 复制到数据成员中) (6) 定义析构函数,delete 释放 stu 数组资源,输出课程的简单信息; (7) 定义课程类的功能: 增加选课的学生 bool addstu(const student &s);(提示:给数组元素赋值,增加 stucnt) 录入课程内所有学生的分数 bool setcoursescore();(提示:访问 stu 数组,通过 cin 录入所有选课学生的分数) 获取班级平均分 double averagescore(); 求班级最高分的同学 student topstu(); 成绩排序 void sort_bubble(); 输出班级所有同学的信息和成绩 void stuinfo(); 输出班级的课程信息 void courseinfo(); (8) 在 main 函数,继续声明两个 course 的变量,使用这些成员函数进行计算。
以下是课程类 course 的代码实现:
```cpp
#include <iostream>
#include <cstring>
#include "student.h" // 学生类的头文件
#define MAXCAP 50 // 定义课程容量
class course {
private:
int id; // 课程号
char name[20]; // 课程名
char loc[10]; // 教室
int cap; // 课程容量
char tea[20]; // 教师
student *stu; // 学生指针
int stucnt; // 已有学生数量
int time; // 节次
static int crscnt; // 总课程数
public:
course() : id(0), cap(MAXCAP), stucnt(0), time(0) {
stu = new student[cap]; // 初始化 stu 数组
crscnt++; // 总课程数加 1
}
course(int _id, const char *_name, const char *_loc, int _cap, const char *_tea, int _time)
: id(_id), cap(_cap), stucnt(0), time(_time) {
strcpy(name, _name);
strcpy(loc, _loc);
strcpy(tea, _tea);
stu = new student[cap]; // 初始化 stu 数组
crscnt++; // 总课程数加 1
}
course(const course &c) : id(c.id), cap(c.cap), stucnt(c.stucnt), time(c.time) {
strcpy(name, c.name);
strcpy(loc, c.loc);
strcpy(tea, c.tea);
stu = new student[cap]; // 初始化 stu 数组
for (int i = 0; i < stucnt; i++) {
stu[i] = c.stu[i]; // 将参数的数据成员值复制到数据成员中
}
crscnt++; // 总课程数加 1
}
~course() {
delete[] stu; // 释放 stu 数组资源
std::cout << "Course " << id << " has been deleted." << std::endl;
}
bool addstu(const student &s) {
if (stucnt >= cap) { // 判断是否已满
return false;
}
stu[stucnt++] = s; // 给数组元素赋值,增加 stucnt
return true;
}
bool setcoursescore() {
for (int i = 0; i < stucnt; i++) {
std::cout << "Please input the score of student " << i + 1 << ": ";
std::cin >> stu[i].score; // 通过 cin 录入所有选课学生的分数
}
return true;
}
double averagescore() {
double sum = 0;
for (int i = 0; i < stucnt; i++) {
sum += stu[i].score;
}
return sum / stucnt; // 求班级平均分
}
student topstu() {
int maxidx = 0;
for (int i = 1; i < stucnt; i++) {
if (stu[i].score > stu[maxidx].score) {
maxidx = i;
}
}
return stu[maxidx]; // 求班级最高分的同学
}
void sort_bubble() {
for (int i = 0; i < stucnt - 1; i++) {
for (int j = 0; j < stucnt - 1 - i; j++) {
if (stu[j].score < stu[j + 1].score) {
student temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
void stuinfo() {
for (int i = 0; i < stucnt; i++) {
std::cout << "Student " << i + 1 << ": ";
stu[i].printinfo(); // 输出班级所有同学的信息和成绩
}
}
void courseinfo() {
std::cout << "ID: " << id << std::endl;
std::cout << "Name: " << name << std::endl;
std::cout << "Location: " << loc << std::endl;
std::cout << "Capacity: " << cap << std::endl;
std::cout << "Teacher: " << tea << std::endl;
std::cout << "Time: " << time << std::endl; // 输出班级的课程信息
}
static int getcrscnt() { // 获取总课程数
return crscnt;
}
};
int course::crscnt = 0; // 初始化总课程数为 0
```
在 main 函数中,可以使用以下方式声明两个 course 的变量,并使用成员函数进行计算:
```cpp
int main() {
// 声明两个 course 的变量
course c1(1, "Math", "101", 50, "Mr. Wang", 2);
course c2(2, "English", "102", 40, "Ms. Li", 3);
// 添加学生并录入分数
c1.addstu(student(1, "Tom", 18));
c1.addstu(student(2, "Jerry", 19));
c1.addstu(student(3, "Alice", 20));
c1.setcoursescore();
// 计算班级平均分和班级最高分的同学
std::cout << "The average score of class 1 is: " << c1.averagescore() << std::endl;
std::cout << "The top student of class 1 is: ";
c1.topstu().printinfo();
// 对成绩进行排序并输出学生信息
c1.sort_bubble();
std::cout << "The sorted students of class 1 are: " << std::endl;
c1.stuinfo();
// 输出课程信息和总课程数
std::cout << "The information of course 1 is: " << std::endl;
c1.courseinfo();
std::cout << "The total number of courses is: " << course::getcrscnt() << std::endl;
return 0;
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)