标签:
【标题】学生成绩管理的设计与实现
【开发语言】C语言
【概要设计】使用结构体存储学生的学号、姓名和成绩信息,实现对学生成绩类的基本操作:增加、删除、查询、排序
【测试数据】按提示输入5组正确的正确的数据和5组有问题的数据,查看程序能否运行正确
【测试结果】基本功能运行正确,异常输入无法处理(关于性能对比测试,查看C++版:http://www.cnblogs.com/forerve/p/3908716.html)
【详细设计】
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<time.h> 5 6 /*学生结构体*/ 7 struct student{ 8 char name[10]; 9 char no[10]; 10 double couse[4]; 11 double sum ; 12 }; 13 14 /*链表节点*/ 15 typedef struct node{ 16 struct student st; 17 struct node *Next; 18 }Node; 19 20 int Add(Node *Head);/*增加学生*/ 21 void Print(Node *Head);/*打印学生信息*/ 22 int Find(Node *Head);/*查询学生*/ 23 int Del(Node *Head);/*删除学生*/ 24 void sort_sum(Node *Head);/*按总成绩排序*/ 25 void sort_no(Node *Head);/*按学号排序*/ 26 void main_remid();/*主要提示信息*/ 27 void score_remind();/*成绩提示信息*/ 28 29 int main() 30 { 31 Node* Head = (struct node *)malloc(sizeof (Node)); 32 Head->Next = NULL; 33 char op = ‘ ‘; 34 main_remid(); 35 while(op != ‘q‘) 36 { 37 scanf("%c", &op); 38 switch(op) 39 { 40 case ‘1‘: 41 sort_no(Head); 42 break; 43 case ‘2‘: 44 Add(Head); 45 break; 46 case ‘3‘: 47 Find(Head); 48 break; 49 case ‘4‘: 50 Del(Head); 51 break; 52 case ‘5‘: 53 sort_sum(Head); 54 break; 55 case ‘6‘: 56 main_remid(); 57 break; 58 default: 59 printf("输入指令未知,请重新输入\n"); 60 break; 61 } 62 printf("请继续选择您想要的操作:\n"); 63 fflush(stdin); 64 } 65 return 0; 66 } 67 68 /*增加学生*/ 69 int Add(Node *Head) 70 { 71 Node* p = (struct node*)malloc(sizeof (Node)); 72 p->st.sum = 0.0; 73 printf("请输入学生的学号\n"); 74 scanf("%s", p->st.no); 75 printf("请输入学生的姓名\n"); 76 scanf("%s", p->st.name); 77 printf("请输入学生的英语成绩\n"); 78 scanf("%lf", &p->st.couse[0]); 79 p->st.sum += p->st.couse[0]; 80 printf("请输入学生的数学成绩\n"); 81 scanf("%lf", &p->st.couse[1]); 82 p->st.sum += p->st.couse[1]; 83 printf("请输入学生的语文成绩\n"); 84 scanf("%lf", &p->st.couse[2]); 85 p->st.sum += p->st.couse[2]; 86 printf("请输入学生的C语言成绩\n"); 87 scanf("%lf", &p->st.couse[3]); 88 fflush(stdin); 89 p->st.sum += p->st.couse[3]; 90 if(Head->Next == NULL) 91 { 92 p->Next = NULL; 93 Head ->Next = p; 94 } 95 else 96 { 97 p->Next = Head ->Next; 98 Head->Next = p; 99 } 100 return 0; 101 } 102 103 /*按总成绩排序*/ 104 void sort_sum(Node *Head) 105 { 106 Node *p = Head; 107 Node *temp = (struct node*)malloc(sizeof (Node)); 108 Node *first = (struct node*)malloc(sizeof (Node)); 109 memcpy(first, p->Next ,sizeof (Node)); 110 temp->Next = first; 111 first->Next = NULL; 112 p = p->Next; 113 while(p->Next != NULL) 114 { 115 Node *t = temp; 116 while(t->Next!=NULL && p->Next->st.sum < t->Next->st.sum) 117 t = t->Next; 118 Node *q = (struct node*)malloc(sizeof (Node)); 119 memcpy(q, p->Next, sizeof(Node)); 120 q->Next = t->Next; 121 t->Next = q; 122 p = p->Next; 123 } 124 Print(temp); 125 p = temp; 126 Node *q = temp->Next; 127 while(q->Next != NULL) 128 { 129 free(p); 130 p = q; 131 q = q->Next; 132 } 133 free(p); 134 free(q); 135 } 136 137 /*按学号排序*/ 138 void sort_no(Node *Head) 139 { 140 Node *p = Head; 141 Node *temp = (struct node*)malloc(sizeof (Node)); 142 Node *first = (struct node*)malloc(sizeof (Node)); 143 memcpy(first, p->Next ,sizeof (Node)); 144 temp->Next = first; 145 first->Next = NULL; 146 p = p->Next; 147 while(p->Next != NULL) 148 { 149 Node *t = temp; 150 while(t->Next!=NULL && atoi(p->Next->st.no)>atoi(t->Next->st.no)) 151 t = t->Next; 152 Node *q = (struct node*)malloc(sizeof (Node)); 153 memcpy(q, p->Next, sizeof(Node)); 154 q->Next = t->Next; 155 t->Next = q; 156 157 p = p->Next; 158 } 159 Print(temp); 160 p = temp; 161 Node *q = temp->Next; 162 while(q->Next != NULL) 163 { 164 free(p); 165 p = q; 166 q = q->Next; 167 } 168 free(p); 169 free(q); 170 } 171 172 /*void sort_sum1(Node *Head) 173 { 174 Node *temp; 175 Node *p = Head; 176 temp->Next2 = p->Next; 177 178 179 p = p->Next; 180 p->Next2 = NULL; 181 while(p->Next != NULL) 182 { 183 Node *t = temp; 184 while(t->Next2!=NULL && p->Next->st.sum<t->Next2->st.sum) 185 t = t->Next2; 186 p->Next->Next2 = t->Next2; 187 t->Next2 = p->Next; 188 189 p = p->Next; 190 } 191 Print(temp); 192 193 }*/ 194 195 /*打印学生信息*/ 196 void Print(Node *Head) 197 { 198 Node* p = Head; 199 score_remind(); 200 while(p->Next != NULL) 201 { 202 printf("\t%s", p->Next->st.no); 203 printf("\t%s", p->Next->st.name); 204 printf("\t%.1lf", p->Next->st.couse[0]); 205 printf("\t%.1lf", p->Next->st.couse[1]); 206 printf("\t%.1lf", p->Next->st.couse[2]); 207 printf("\t%.1lf", p->Next->st.couse[3]); 208 printf("\t%.1lf\n", p->Next->st.sum); 209 p = p->Next; 210 } 211 p = NULL; 212 } 213 214 /*查询学生*/ 215 int Find(Node *Head) 216 { 217 Node* p = Head; 218 printf("请输入要查找学生的学号:\n"); 219 char no[10]; 220 scanf("%s", no); 221 while(p->Next != NULL) 222 { 223 if(!strcmp(no, p->Next->st.no)) 224 { 225 score_remind(); 226 printf("\t%s", p->Next->st.no); 227 printf("\t%s", p->Next->st.name); 228 printf("\t%.1lf", p->Next->st.couse[0]); 229 printf("\t%.1lf", p->Next->st.couse[1]); 230 printf("\t%.1lf", p->Next->st.couse[2]); 231 printf("\t%.1lf", p->Next->st.couse[3]); 232 printf("\t%.1lf\n", p->Next->st.sum); 233 p = NULL; 234 return 1; 235 } 236 p = p->Next; 237 } 238 printf("查找失败,不存在次学号\n"); 239 p = NULL; 240 return 0; 241 } 242 243 /*删除学生*/ 244 int Del(Node *Head) 245 { 246 Node* p = Head; 247 printf("请输入要删除学生的学号:\n"); 248 char no[10]; 249 scanf("%s", no); 250 while(p->Next != NULL) 251 { 252 if(!strcmp(no, p->Next->st.no)) 253 { 254 Node *q = p->Next; 255 p->Next = p->Next->Next; 256 printf("%s %s删除成功\n",q->st.no, q->st.name); 257 free(q); 258 q = NULL; 259 return 1; 260 } 261 p = p->Next; 262 } 263 printf("不存在此学号\n"); 264 } 265 266 /*主要提示信息*/ 267 void main_remid() 268 { 269 printf("\t\t\t学生成绩类\n"); 270 printf("\t\t1.查询所有学生的成绩信息\n"); 271 printf("\t\t2.增加学生\n"); 272 printf("\t\t3.查找学生\n"); 273 printf("\t\t4.删除学生\n"); 274 printf("\t\t5.查看总分排名\n"); 275 printf("\t\t6.查看此提示\n"); 276 printf("\t\tq.退出系统\n\n"); 277 } 278 279 /*成绩提示信息*/ 280 void score_remind() 281 { 282 printf("\t\t\t 学生成绩信息\n"); 283 printf("\t学号\t姓名\t数学\t英语\t语文\tC语言\t总成绩\n"); 284 }
标签:
原文地址:http://www.cnblogs.com/forerve/p/4177245.html