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
函數來顯示或輸出信息。根據具體需求,可以對這些示例進行修改和擴展。