cg平台大一c语言期末上机考试题
时间: 2025-02-14 20:03:51 浏览: 53
### 大一C语言期末上机考试题目示例
#### 题目1:计算学生成绩平均分
编写一个程序,输入若干学生的名字及其对应的三门课程成绩(每门课满分100),并输出每位学生的总分和平均分。
```c
#include <stdio.h>
#define NUM_STUDENTS 3
#define NUM_SUBJECTS 3
int main() {
char names[NUM_STUDENTS][50];
int scores[NUM_STUDENTS][NUM_SUBJECTS];
printf("请输入%d位同学的信息:\n", NUM_STUDENTS);
for (int i = 0; i < NUM_STUDENTS; ++i) {
printf("第 %d 名学生姓名:", i + 1);
scanf("%s", names[i]);
for (int j = 0; j < NUM_SUBJECTS; ++j){
printf("科目 %d 成绩:", j + 1);
scanf("%d", &scores[i][j]);
}
}
// 输出结果部分省略...
}
```
此题考察基本数组操作以及循环结构的应用[^1]。
#### 题目2:字符串处理函数实现
定义两个自定义函数`str_length()`用于返回给定字符指针所指向字符串长度;另一个为`reverse_string()`用来反转传入的字符串参数。不允许调用标准库中的任何现成方法来完成这两个功能。
```c
#include <stdio.h>
// 自定义求取字符串长度的方法
size_t str_length(const char *str);
void reverse_string(char*);
int main(){
const char* testStr="hello";
printf("原始字符串:%s\n其长度为:%zu\n",testStr,str_length(testStr));
char mutableTest[] ="world!";
puts(mutableTest);
reverse_string(mutableTest);
puts(mutableTest);
return 0;
}
/* 函数体实现 */
size_t str_length(const char *str){
size_t count=0;
while(*str++)count++;
return count;
}
void reverse_string(char*s){
if(!s||!*s)return ;
char *end=s,*temp;
while(*(++end));
end--;
while(s<end){
temp=*s;
*s++=*end;
*end--=temp;
}
}
```
这段代码展示了如何不依赖于预置函数而自行构建常用工具类函数的能力。
#### 题目3:文件读写练习
创建名为 `student.txt` 的文本文件,在其中存储多个学生的记录(每一行为一名学生)。每个字段之间以逗号隔开,如 "张三,98,76,85" 表示该生语文得分为98、数学得分76、英语得分85。之后再通过编程方式打开这个文件并将所有数据加载到内存中显示出来。
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct StudentRecord{
char name[50];
float chineseScore;
float mathScore;
float englishScore;
}Student;
int main(void){
FILE *file=fopen("students.txt","r+");
if(file==NULL){
perror("无法打开文件");
exit(EXIT_FAILURE);
}
Student studentList[10]; // 假设最多有十个学生
int index=0;
while(fscanf(file,"%[^,],%f,%f,%f",&(studentList[index].name),
&(studentList[index].chineseScore),&(studentList[index].mathScore),
&(studentList[index].englishScore))!=EOF)index++;
fclose(file);
/* 打印读取的数据 */
for(int i=0;i<index;++i){
printf("Name=%s Chinese=%.2f Math=%.2f English=%.2f\n",
studentList[i].name,
studentList[i].chineseScore,
studentList[i].mathScore,
studentList[i].englishScore);
}
return EXIT_SUCCESS;
}
```
上述例子有助于理解文件I/O流的概念,并能实际应用于简单的数据库管理场景下。
阅读全文
相关推荐
















