mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4mobile wallpaper 5mobile wallpaper 6mobile wallpaper 7
72 字
1 分钟
【课堂代码】函数3
2026-07-12

1. 汉诺塔问题#

/*
作者:wyl
时间:2026-06-04 13:57:50
题目:汉诺塔
*/
#include<stdio.h>
void hanoi(int n, char a, char b, char c);
void move(int n, char a, char b);
int main()
{
int n;
printf("Input the Number:");
scanf("%d", &n);
printf("Steps of moving %d disks from A to B by means of C:\n",n);
hanoi(n, 'A', 'B', 'C');
return 0;
}
void hanoi(int n, char a, char b, char c)
{
if(n == 1)
move(n, a, b);
else
{
hanoi(n-1, a, c, b);
move(n, a, b);
hanoi(n-1, c, b, a);
}
}
void move(int n, char a, char b)
{
printf("Move disk %d from %c to %c\n", n, a, b);
}

2. 计算两个复数之和与积#

/*【例5-7】计算2个复数之和与之积。分别输入2个复数的实部与虚部,用函数实现计算2个复数之和与之积。
若2个复数分别为:c1 = x1 + (y1)i, c2 = x2 +(y2)i,则:
c1 + c2 = (x1+x2) + (y1+y2)i
c1 * c2 = (x1*x2 - y1*y2) + (x1*y2 + x2*y1)i
*/
#include <stdio.h>
void complex_prod(double real1, double imag1, double real2, double imag2);
void complex_add(double real1, double imag1, double real2, double imag2);
double result_real, result_imag;
int main(void)
{
double imag1, imag2, real1, real2;
printf("Enter 1st complex number(real and imaginary): ");
scanf("%lf%lf", &real1, &imag1);
printf("Enter 2nd complex number(real and imaginary): ");
scanf("%lf%lf", &real2, &imag2);
complex_add(real1, imag1, real2, imag2);
printf("addition of complex is %f+%fi\n", result_real, result_imag);
complex_prod(real1, imag1, real2, imag2);
printf("product of complex is %f+%fi\n", result_real, result_imag);
return 0;
}
void complex_add(double real1, double imag1, double real2, double imag2)
{
result_real = real1 + real2;
result_imag = imag1 + imag2;
return;
}
void complex_prod(double real1, double imag1, double real2, double imag2)
{
result_real = real1*real2 - imag1*imag2;
result_imag = real1*imag2 + real2*imag1;
return;
}

3. 学生信息管理系统(分模块设计)#

3.1 main.c - 主模块#

/* 分模块设计一个学生信息库系统。该系统包含学生基本信息的建立和输出
、计算学生平均成绩、按照学生的平均成绩排序以及查询、修改学生的成绩等功能。*/
#include<stdio.h>
#include<string.h>
struct student {
int num;
char name[10];
int computer, english, math;
double average;
};
int Count = 0;
#define MaxSize 50
#include "input_output.c"
#include "computing.c"
#include "update.c"
#include "search.c"
int main(void)
{
struct student students[MaxSize];
new_student(students);
average(students);
sort(students);
output_student(students);
modify(students);
output_student(students);
return 0;
}

3.2 input_output.c - 输入输出模块#

void new_student(struct student students[])
{
int i,n;
if(Count == MaxSize){
printf("The array is full!\n");
return;
}
printf("Input the Number of Students:");
scanf("%d", &n);
for(i = 0; i < n; i++){
printf("Input the student's num:");
scanf("%d", &students[i].num);
printf("Input the student's name:");
scanf("%s", students[i].name);
printf("Input the student's math score:");
scanf("%d", &students[i].math);
printf("Input the student's english score:");
scanf("%d", &students[i].english);
printf("Input the student's computer score:");
scanf("%d", &students[i].computer);
Count++;
}
}
void output_student(struct student students[])
{
int i;
if(Count == 0){
printf("Count of students is zero!\n");
return;
}
printf("num\tname\tmath\tenglish\tcomputer\taverage\n");
for(i = 0; i < Count; i++){
printf("%d\t", students[i].num);
printf("%s\t", students[i].name);
printf("%d\t", students[i].math);
printf("%d\t", students[i].english);
printf("%d\t", students[i].computer);
printf("%f\t\n", students[i].average);
}
}

3.3 computing.c - 计算模块#

void average(struct student s[])
{
int i;
for(i = 0; i < Count; i++){
s[i].average = (s[i].math + s[i].english + s[i].computer)/3.0;
}
}

3.4 update.c - 修改与排序模块#

void modify(struct student* p)
{
int num, course, score, i;
printf("Input the number of updated student: ");
scanf("%d", &num);
printf("Choice the course: 1.math 2.english 3.computer: ");
scanf("%d", &course);
printf("Input the new score: ");
scanf("%d", &score);
for (i = 0; i < Count; i++, p++) {
if (p->num == num) {
break;
}
}
if (i < Count) {
switch (course) {
case 1: p->math = score; break;
case 2: p->english = score; break;
case 3: p->computer = score; break;
}
}
}
void sort(struct student s[])
{
struct student temp;
int i, index, j;
for (i = 0; i < Count - 1; ++i) {
index = i;
for (j = i + 1; j < Count; j++) {
if (s[j].average > s[index].average) {
index = j;
}
}
temp = s[index];
s[index] = s[i];
s[i] = temp;
}
}

3.5 search.c - 查询模块#

void search_student(struct student students[], int num)
{
int i, flag = 0;
if (Count == 0) {
printf("Count of students is zero!\n");
return;
}
for (i = 0; i < Count; i++) {
if (students[i].num == num) {
flag = 1;
break;
}
}
if (flag != 0) {
printf("num:%d, ", students[i].num);
printf("name:%s, ", students[i].name);
printf("math:%d, ", students[i].math);
printf("english:%d, ", students[i].english);
printf("computer:%d, ", students[i].computer);
printf("average:%.2f\n", students[i].average);
}
else {
printf("Not Found!");
}
}
分享

如果这篇文章对你有帮助,欢迎分享给更多人!

【课堂代码】函数3
https://blog.radarweb.top/posts/c/课堂代码函数3/
作者
Sherry
发布于
2026-07-12
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

目录