# 小朋友四则运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
srand(time(NULL));

int num1, num2, result, answer,accuracy,correctnum,x;
char operator;

correctnum=0;
for (int i = 0; i < 10; i++) {
num1 = rand() % 10 + 1;
num2 = rand() % 10 + 1;
operator = rand() % 4;
if (operator==0)
{
printf("%d + %d = ", num1, num2);
result = num1 + num2;
}
else if (operator==1)
{
printf("%d - %d = ", num1, num2);
result = num1 - num2;
}
else if (operator==2)
{
printf("%d * %d = ", num1, num2);
result = num1 * num2;
}
else
{
while (num1%num2!=0)
{
num1 = rand() % 10 + 1;
num2 = rand() % 10 + 1;
}
printf("%d / %d = ", num1, num2);
result = num1 / num2;
}
scanf("%d", &answer);
if (answer == result)
{
printf("回答正确!\n");
correctnum++;
}
else
{
printf("回答错误!正确答案是:%d\n", result);
}
sleep(1);
system("cls");
//getchar();
}
accuracy=correctnum*10;
printf("小朋友你的得分为:%d\n",accuracy);
return 0;
}

# 成绩表的整理

# C 语言的实现方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 定义学生结构体
typedef struct Student {
char sno[5]; // 学号
char sname[10]; // 姓名
int score; // 分数
struct Student *rear; // 指向下一个学生节点的指针
} Student;

// 创建一个新学生节点
Student* createStudent(const char* sno, const char* sname, int score)
{
Student* newStudent = (Student*)malloc(sizeof(Student));
if (newStudent)
{
strncpy((*newStudent).sno, sno, 5); // 设置学号
strncpy((*newStudent).sname, sname, 10); // 设置姓名
(*newStudent).score = score; // 设置分数
(*newStudent).rear = NULL; // 设置下一个节点指针为空
}
return newStudent; // 返回新创建的学生节点
}

// 将新学生插入到链表中,保持链表按分数降序排列
Student* insertStudent(Student* head, Student* newStudent)
{
// 如果链表为空或新学生分数高于头节点分数,插入到头部
if (head == NULL || (*newStudent).score > (*head).score)
{
(*newStudent).rear = head;
return newStudent;
}

// 否则找到合适的插入位置
Student* current = head;
while ((*current).rear != NULL && (*((*current).rear)).score > (*newStudent).score)
{
current = (*current).rear;
}

// 插入新学生
(*newStudent).rear = (*current).rear;
(*current).rear = newStudent;
return head; // 返回链表头部指针
}

// 打印整个链表
void printList(Student* head) {
Student* current = head;
while (current != NULL)
{
printf("%s %s %d\n", (*current).sno, (*current).sname, (*current).score);
current = (*current).rear;
}
}

// 根据学号删除学生
Student* deleteStudent(Student* head, const char* sno) {
Student* current = head;
Student* previous = NULL;

// 遍历找到要删除的学生节点
while (current != NULL && strcmp((*current).sno, sno) != 0)
{
previous = current;
current = (*current).rear;
}

// 如果找不到,返回原链表
if (current == NULL) return head;

// 如果删除的是头节点
if (previous == NULL)
{
head = (*current).rear;
}
else // 删除的是中间或尾节点
{
(*previous).rear = (*current).rear;
}

free(current);
return head; // 返回链表头部指针
}

int main() {
Student* head = NULL;
// 创建并插入几个学生节点
head = insertStudent(head, createStudent("1001", "学生甲", 95));
head = insertStudent(head, createStudent("1002", "学生乙", 75));
head = insertStudent(head, createStudent("1003", "学生丙", 83));
head = insertStudent(head, createStudent("1004", "学生丁", 93));
head = insertStudent(head, createStudent("1005", "学生戊", 80));

printf("初始链表:\n");
printList(head); // 打印初始链表

head = deleteStudent(head, "1003");

printf("删除后的链表:\n");
printList(head); // 打印删除后的链表

while (head != NULL) {
Student* temp = head;
head = (*head).rear;
free(temp);
}

return 0;
}

# Python 的实现方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Student:
# 学生类初始化方法
def __init__(self, sno, sname, score):
self.sno = sno # 学号
self.sname = sname # 学生姓名
self.score = score # 分数
self.rear = None # 指向下一个学生的指针

# 创建一个新的学生对象
def create_student(sno, sname, score):
return Student(sno, sname, score)

# 将新学生按分数插入链表
def insert_student(head, new_student):
if not new_student:
return head
# 如果链表为空或新学生分数高于头节点,插入头部
if not head or new_student.score > head.score:
new_student.rear = head
return new_student
# 寻找插入位置
current = head
while current.rear and new_student.score <= current.rear.score:
current = current.rear
# 插入学生
new_student.rear = current.rear
current.rear = new_student
return head

# 从链表中删除具有特定学号的学生
def delete_student(head, sno):
current = head
previous = None
# 寻找要删除的学生
while current and current.sno != sno:
previous = current
current = current.rear
# 如果找到了,从链表中移除
if not current:
return head
if previous:
previous.rear = current.rear
else:
head = current.rear
return head

# 打印链表中的所有学生信息
def print_list(head):
current = head
while current:
print(f"{current.sno} {current.sname} {current.score}")
current = current.rear

head = None # 链表头部初始化为空
# 插入学生信息
head = insert_student(head, create_student("1001", "学生甲", 95))
head = insert_student(head, create_student("1002", "学生乙", 75))
head = insert_student(head, create_student("1003", "学生丙", 83))
head = insert_student(head, create_student("1004", "学生丁", 93))
head = insert_student(head, create_student("1005", "学生戊", 80))

# 打印初始链表
print("初始链表:")
print_list(head)

# 删除一个学生后打印链表
head = delete_student(head, "1003")
print("\n删除后的链表:")
print_list(head)

更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

泠鹇 微信支付

微信支付

泠鹇 支付宝

支付宝

泠鹇 贝宝

贝宝