#include<stdio.h> struct Stu { char Name[12]; int ID; int Math; int Chinese; int English; }; struct Stu Student[100]; void Menu() { puts("1.Browse"); puts("2.Add"); puts("3.Delete"); puts("4.Modify"); puts("5.Find"); puts("0.Exit"); } void Browse() { int i = 0; puts("Name\tIDnumber\tMath\tChinese\tEnglish\n"); while (Student[i].ID != 0) { if (Student[i].ID != -1) { printf("%s\t%-8d\t%d\t%d\t%d\n", Student[i].Name, Student[i].ID, Student[i].Math, Student[i].Chinese, Student[i].English); i++; } } } int Add() { int i = 0; puts("Put ID"); int ID; scanf("%d", &ID); while (Student[i].ID != 0&&Student[i].ID != -1) { if (Student[i].ID == ID) { return 1; } i++; } Student[i].ID = ID; puts("Name"); scanf("%s", &Student[i].Name); puts("Math,Chinese,English"); scanf("%d,%d,%d",&Student[i].Math, &Student[i].Chinese, &Student[i].English); return 0; } int Delete() { int i = 0; puts("Put ID"); int ID; scanf("%d", &ID); while (Student[i].ID != 0) { if (Student[i].ID == ID) { Student[i].ID = -1; break; } i++; } if (Student[i].ID == 0) return 1; return 0; } int Modify() { int i = 0; puts("Put ID"); int ID; scanf("%d", &ID); while (Student[i].ID != 0) { if (Student[i].ID == ID) { break; } i++; } if (Student[i].ID == 0) return 1; puts("Math,Chinese,English"); scanf("%d,%d,%d", &Student[i].Math, &Student[i].Chinese, &Student[i].English); return 0; } int Find() { int i = 0; puts("Put ID"); int ID; scanf("%d", &ID); while (Student[i].ID != 0) { if (Student[i].ID == ID) { printf("%s,%d,%d,%d\n", Student[i].Name, Student[i].Math, Student[i].Chinese, Student[i].English); break; } i++; } if (Student[i].ID == 0) return 1; return 0; } int main(void) { while (1) { Menu(); char s = getchar(); while (getchar() != ‘\n‘)continue; switch (s) { case ‘1‘: {Browse(); while (getchar() != ‘\n‘)continue; break; } case ‘2‘: {if (Add() == 1)puts("ID is duplicated"); else puts("Add success!"); while (getchar() != ‘\n‘)continue; break; } case ‘3‘: {if (Delete() == 1)puts("ID doesn‘t exist"); else puts("Delete success!"); while (getchar() != ‘\n‘)continue; break; } case ‘4‘: {if (Modify() == 1)puts("ID doesn‘t exist"); else puts("Modify success!"); while (getchar() != ‘\n‘)continue; break; } case ‘5‘: {if (Find() == 1)puts("ID doesn‘t exist"); else puts("Find success!"); while (getchar() != ‘\n‘)continue; break; } case ‘0‘: {return 0; } default: {puts("Please obey!"); while (getchar() != ‘\n‘)continue; break; } } } return 0; }
简介:100个表示学生的结构体,有添加,浏览所有学生,根据学号查找、修改、删除的功能。具有自动学号查重的功能。
写这段代码收获;
1.if配合break跳循环很好用.
2.scanf里面要是用输入%s格式,如
scanf("%s", &Student[i].Name);
就不要用scanf接受多个格式的变量了,这个scanf就专门用来接%s,因为如果不这样做,输入的时候后面你想要输入的不属于%s格式的内容无法与前面你想输入进去属于%s的内容分开,造成你不想要的结果.
3.\t用于对齐时,如果一列上的数据,有的大于等于8位,有的小于8位就会造成对不齐的效果,这个时候机智的将小于8位的数利用类似-%8d这种格式强制将其转化成8位的就可以了.
时间: 2024-10-24 00:32:59