[C++] 纯文本查看 复制代码 //
// main.cpp
// Hello
//
// Created by 余西安 on 16/5/19.
// Copyright © 2016年 Sian. All rights reserved.
//
#include <stdio.h>
#include <string.h>
// 创建基本数据结构
struct Student{
int id;
char name[10];
int score[3];
};
// 创建数据存储体
Student sa[100];
int count = 0;
// 创建实体对象
Student create(){
Student s;
printf("ID:");
scanf("%d", &s.id);
printf("Name:");
scanf("%s", s.name);
printf("Score:");
scanf("%d, %d, %d", &s.score[0], &s.score[1], &s.score[2]);
return s;
}
// 创建添加函数
int add(const Student *s){
if (count > 100) return -1;
sa[count] = *s;
count++;
return 0;
}
// 创建查找方法
int find(char name[10], Student *s){
for(int i = 0; i < count ; i++){
Student fs = sa[i];
if (strcmp(name, fs.name) == 0){
s->id = fs.id;
strcpy(s->name, fs.name);
s->score[0] = fs.score[0];
s->score[1] = fs.score[1];
s->score[2] = fs.score[2];
return 0;
}
}
return -1;
}
// 创建展现方法
void list(){
if (count == 0){
printf("Nothing...\n");
return;
}
printf("---------------------\n");
printf("ID\tName\tScore\t\n");
printf("---------------------\n");
for (int i = 0; i < count; i++){
Student s = sa[i];
printf("%d\t%s\t%d,%d,%d\n", s.id, s.name, s.score[0], s.score[1], s.score[2]);
}
printf("---------------------\n");
}
int main(){
while(true){
char cmd[10];
printf(">");
scanf("%s", cmd);
// 录入成绩
if(strcmp(cmd, "add") == 0){
Student s = create();
int resutl = add(&s);
if (resutl == 0) printf("添加成功!\n");
if (resutl == -1) printf("添加失败!\n");
continue;
}
if (strcmp(cmd, "list") == 0){
list();
continue;
}
if(strcmp(cmd, "exit") == 0){
printf("Bye!\n");
break;
}
if (strcmp(cmd, "count") == 0){
printf("Total:%d\n", count);
continue;
}
if (strcmp(cmd, "find") == 0){
Student s;
char name[10];
printf("请输入需要查询的学生姓名:");
scanf("%s", name);
int result = find(name, &s);
if (result == 0) printf("%d\t%s\t%d,%d,%d\n", s.id, s.name, s.score[0], s.score[1], s.score[2]);
if (result == -1) printf("没有查询到相关信息\n");
continue;
}
printf("Error Code, please input code like \"add\", \"list\", \"exit\", \"count\"\n");
}
return 0;
}
|