disp
函数在 C 语言项目中通常用于显示或输出信息
disp
函数来显示计算结果。#include<stdio.h>
void disp(double result) {
printf("Result: %.2lf\n", result);
}
int main() {
double num1, num2, result;
char operation;
printf("Enter the first number: ");
scanf("%lf", &num1);
printf("Enter the second number: ");
scanf("%lf", &num2);
printf("Enter the operation (+, -, *, /): ");
scanf(" %c", &operation);
switch (operation) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 == 0) {
printf("Error: Division by zero.\n");
return 1;
}
result = num1 / num2;
break;
default:
printf("Error: Invalid operation.\n");
return 1;
}
disp(result);
return 0;
}
disp
函数来显示学生的成绩信息。#include<stdio.h>
typedef struct {
int id;
char name[50];
float score;
} Student;
void disp(Student student) {
printf("ID: %d\nName: %s\nScore: %.2f\n", student.id, student.name, student.score);
}
int main() {
Student student;
printf("Enter student ID: ");
scanf("%d", &student.id);
printf("Enter student name: ");
scanf("%s", student.name);
printf("Enter student score: ");
scanf("%f", &student.score);
disp(student);
return 0;
}
这些实际应用案例展示了如何在 C 语言项目中使用 disp
函数来显示或输出信息。根据具体需求,可以对这些示例进行修改和扩展。