#include <stdio.h> #include <string.h> struct Students { char id[10]; char name[20]; int score1; int score2; int score3; }; int main() { struct Students srr[50]; int n; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%s%s%d%d",&srr[i].id,&srr[i].name,&srr[i].score1,&srr[i].score2,&srr[i].score3); } int i=0; printf("%s%s%d%d",&srr[i].id,&srr[i].name,&srr[i].score1,&srr[i].score2,&srr[i].score3); return 0; }哪里有问题
时间: 2024-02-22 13:57:16 浏览: 17
在输入学生信息的部分,scanf的格式控制字符串应该为"%s%s%d%d%d",因为输入了3个整数分数,而不是2个。此外,在输出学生信息的部分,printf的格式控制字符串应该为"%s%s%d%d%d",并且需要使用数组下标来输出所有的学生信息,而不是只输出第一个学生的信息。正确的输出方式是:
```
for(int i=0;i<n;i++)
{
printf("%s %s %d %d %d\n",srr[i].id,srr[i].name,srr[i].score1,srr[i].score2,srr[i].score3);
}
```
相关问题
#include <stdio.h> #include <string.h> #include <stdlib.h> // 定义结构体类型 struct Student { char id[10]; // 学号 char name[20]; // 姓名 char gender[10]; // 性别 int chinese; // 语文成绩 int math; // 数学成绩 int english; // 英语成绩 }; // 初始化几个学生的基本信息 struct Student students[] = { {"1001", "张三", "Male", 80, 90, 85}, {"1002", "李四", "Female", 75, 85, 90}, {"1003", "王五", "Male", 90, 80, 95}, {"1004", "赵六", "Female", 85, 95, 80}, {"1005", "钱七", "Male", 95, 85, 90} }; int count=5; // 学生数量 编写一个函数,实现能根据其中一项成绩对学生排序
下面是一个根据语文成绩对学生排序的示例代码:
```
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Student {
char id[10]; // 学号
char name[20]; // 姓名
char gender[10]; // 性别
int chinese; // 语文成绩
int math; // 数学成绩
int english; // 英语成绩
};
struct Student students[] = {
{"1001", "张三", "Male", 80, 90, 85},
{"1002", "李四", "Female", 75, 85, 90},
{"1003", "王五", "Male", 90, 80, 95},
{"1004", "赵六", "Female", 85, 95, 80},
{"1005", "钱七", "Male", 95, 85, 90}
};
int count = 5; // 学生数量
// 根据语文成绩排序
void sortStudentsByChinese() {
struct Student temp;
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (students[j].chinese < students[j + 1].chinese) {
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
int main() {
sortStudentsByChinese();
for (int i = 0; i < count; i++) {
printf("学号:%s 姓名:%s 性别:%s 语文成绩:%d 数学成绩:%d 英语成绩:%d\n",
students[i].id, students[i].name, students[i].gender, students[i].chinese, students[i].math, students[i].english);
}
return 0;
}
```
这个示例代码中,我们定义了一个 sortStudentsByChinese() 函数,它使用了冒泡排序的算法,根据语文成绩对学生进行排序。在主函数中,我们先调用该函数,然后循环输出排好序的学生信息。
c语言的学生管理系统在此代码基础上写一个删除学生信息#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> typedef struct _student{ int ID; char name[20]; char sex[5]; char Class[20]; char date[20]; char Tel[20]; char home[20]; }student; typedef struc
t _node{ student data; struct _node *next; }node; node *createList() { node *head,*p; head = p = (node*)malloc(sizeof(node)); p->next = NULL; return head; } void addStudent(node *head, student *newStu) { node *p, *q; p = head; while(p->next != NULL) { p = p->next; } q = (node*)malloc(sizeof(node)); q->data = *newStu; q->next = p->next; p->next = q; } void displayList(node *head) { node *p; p = head->next; while(p != NULL) { printf("%d\t%s\t%s\t%s\t%s\t%s\t%s\n", p->data.ID, p->data.name, p->data.sex, p->data.Class, p->data.date, p->data.Tel, p->data.home); p = p->next; } } void deleteStudent(node *head, int ID) { node *p, *q; p = head->next; while(p != NULL && p->data.ID != ID) { q = p; p = p->next; } if(p == NULL) { printf("No student with ID %d exists.\n", ID); } else { q->next = p->next; free(p); printf("Student with ID %d has been deleted.\n", ID); } } int main() { node *head; student newStu; int choice, ID; char name[20], sex[5], Class[20], date[20], Tel[20], home[20]; head = createList(); do { printf("Menu:\n"); printf("1. Add student\n"); printf("2. Display all students\n"); printf("3. Delete student\n"); printf("4. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch(choice) { case 1: printf("Enter student ID: "); scanf("%d", &newStu.ID); printf("Enter student name: "); scanf("%s", &newStu.name); printf("Enter student sex: "); scanf("%s", &newStu.sex); printf("Enter student class: "); scanf("%s", &newStu.Class); printf("Enter student date of birth: "); scanf("%s", &newStu.date); printf("Enter student telephone number: "); scanf("%s", &newStu.Tel); printf("Enter student home address: "); scanf("%s", &newStu.home); addStudent(head, &newStu); break; case 2: displayList(head); break; case 3: printf("Enter student ID to delete: "); scanf("%d", &ID); deleteStudent(head, ID); break; case 4: printf("Exiting program...\n"); break; default: printf("Invalid choice. Please try again.\n"); break; } } while(choice != 4); return 0; }