利用指针的方法,输入学生的成绩判断等级
时间: 2023-08-29 21:06:04 浏览: 149
好的,以下是用指针判断学生等级的示例代码:
```c
#include <stdio.h>
void grade(int* score) {
if (*score >= 90) {
printf("A\n");
} else if (*score >= 80) {
printf("B\n");
} else if (*score >= 70) {
printf("C\n");
} else if (*score >= 60) {
printf("D\n");
} else {
printf("E\n");
}
}
int main() {
int score;
printf("请输入学生的成绩:");
scanf("%d", &score);
grade(&score);
return 0;
}
```
在上面的代码中,我们定义了一个 `void` 类型的函数 `grade`,它接受一个指向整型变量的指针作为参数 `score`。函数内部根据学生的成绩判断出其等级并输出。在 `main` 函数中,我们先输入学生的成绩,然后调用 `grade` 函数并将其地址传入。这样,在 `grade` 函数中就可以直接操作 `score` 指向的变量了。
相关问题
利用指针判断输入的字符串是否为回文
利用指针判断输入的字符串是否为回文的方法如下:
```c
#include <stdio.h>
#include <string.h>
int isPalindrome(char *str) {
int len = strlen(str);
char *p1 = str; // 指向字符串开头的指针
char *p2 = str + len - 1; // 指向字符串末尾的指针
while (p1 < p2) {
if (*p1 != *p2) {
return 0; // 不是回文字符串
}
p1++;
p2--;
}
return 1; // 是回文字符串
}
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
if (isPalindrome(str)) {
printf("这是回文字符串\n");
} else {
printf("这不是回文字符串\n");
}
return 0;
}
```
用指针求输入若干个学生成绩中最高分
好的,针对您的问题,可以使用以下的代码来实现:
```c
#include <stdio.h>
int main() {
int n; // 学生人数
printf("请输入学生总人数:");
scanf("%d", &n);
double* scores = (double*)malloc(n * sizeof(double)); // 动态分配内存
double max_score = 0.0; // 初始化最高分为0
// 输入每个学生的成绩,并找出最高分
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的成绩:", i + 1);
scanf("%lf", &scores[i]);
if (scores[i] > max_score) {
max_score = scores[i];
}
}
// 输出最高分
printf("最高分为:%.2f\n", max_score);
free(scores); // 释放动态分配的内存
return 0;
}
```
这个程序中,我们首先通过 `scanf` 函数获取学生人数 `n`,然后根据学生人数动态地分配了一个 `double` 类型的数组 `scores`。接下来,我们通过 `for` 循环获取每个学生的成绩,并且每次输入成绩时都会与当前最高分比较,如果当前输入的成绩比最高分还高,那么就更新最高分。最后,我们输出最高分,并释放动态分配的内存。
希望这个回答对您有所帮助!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)