C++版
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
int i;
float price = 1;//上机单价
int n; //学生总数
class Student{
public:
void setInfo(){
cout << "请输入姓名:";
cin >> name;
cout << "请输入学号:";
cin >> id;
cout << "请输入专业:";
cin >> major;
cout << "请输入班极:";
cin >> grade;
}
void setBenigTime(){
cout << "请输入上机时长(xx:xx):";
cin >> Time;
}
float getMoney(){
return t*price;
}
void clacTime(){
unsigned int loc = Time.find(":", 0);
if (loc == string::npos){
cout << "出现异常错误!" << endl;
exit(0);
}
float hour = atof(Time.substr(0, loc).c_str());
int min = atoi(Time.substr(loc+1).c_str());
if (0 < min && min < 30){
hour += 0.5;
}else if (30 <= min && min <60){
hour += 1;
}
t = hour;
}
void showInfo(){
cout << name << " " << id << " " << getMoney()<<"元" << endl;
}
void showAllInfo(){
cout << "姓名:" << name << endl;
cout << "学号:" << id << endl;
cout << "专业:" << major << endl;
cout << "年级:" << grade << endl;
cout << "上机时长:" << Time << endl;
cout << "所需费用:" << getMoney() << endl;;
}
bool isSameMajor(string str){
return str.compare(major) == 0;
}
bool isSameid(string str){
return str.compare(id) == 0;
}
bool isSameName(string str){
return str.compare(name) == 0;
}
private:
string name; // 姓名
string id; // 学号
string major; // 专业
string grade; // 班级
string Time; // 上机时间
float t; // 上机总时间
};
Student* p = NULL; // 学生数据地址
int showInfo(){
int m;
cout << "1.修改上机每小时价格."<< endl;
cout << "2.输入n名上机学生." << endl;
cout << "3.计算各学生上机费用." << endl;
cout << "4.查询学生上机情况." << endl;
cout << "5.机器使用情况." << endl;
cout << "6:退出." << endl;
cout << "请输入序号:";
cin >> m;
return m;
}
void menu4(){
int n;
cout << "1.查找条件为班级" << endl;
cout << "2.查找条件为学号" << endl;
cout << "3.查找条件为姓名" << endl;
cout << "请输入序号:";
cin >> n;
switch (n)
{
default:
break;
}
string str;
cout << "请输入可找条件:";
cin >> str;
switch (n)
{
case 1:
for (i = 0; i < n; i++){
if (p[i].isSameMajor(str)){
p[i].showInfo();
}
}
break;
case 2:
for (i = 0; i < n; i++){
if (p[i].isSameid(str)){
p[i].showInfo();
}
}
break;
case 3:
for (i = 0; i < n; i++){
if (p[i].isSameName(str)){
p[i].showInfo();
}
}
break;
default:
break;
}
}
int main(){
bool flag = true;
while (flag){
system("cls");
int m = showInfo();
switch (m)
{
case 1:
cout << "请输入上机单价:";
cin >> price;
break;
case 2:
cout << "请输入学生总数:";
cin >> n;
p = new Student[n];
for (i = 0; i < n; i++){
p[i].setInfo();
p[i].setBenigTime();
p[i].clacTime();
cout << endl;
}
break;
case 3:
for (i = 0; i < n; i++)
p[i].showInfo();
system("pause");
break;
case 4:
menu4();
system("pause");
break;
case 5:
for (i = 0; i < n; i++)
p[i].showAllInfo();
system("pause");
break;
case 6:
flag = false;
break;
}
}
delete[] p;
return 0;
}
C语言版
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
float price = 1;//上机单价
int n; //学生总数
typedef struct Student_def
{
char name[512]; // 姓名
char id[512]; // 学号
char major[512]; // 专业
char grade[512]; // 班级
char Time[512]; // 上机时间
float t; // 上机总时间
}Student;
void setInfo(Student* p){
printf("请输入姓名:");
scanf("%s", p->name);
printf("请输入学号:");
scanf("%s", p->id);
printf("请输入专业:");
scanf("%s", p->major);
printf("请输入班极:");
scanf("%s", p->grade);
printf("请输入上机时长(xx:xx):");
scanf("%s", p->Time);
}
float getMoney(Student stu){
return stu.t*price;
}
void clacTime(Student* stu){
int i, j;
char temp[512] = {0};
float hour;
int min;
for (i = 0; i < strlen(stu->Time); i++){
if (stu->Time[i] == ':'){
break;
}
}
strncpy(temp, stu->Time, i);
hour = atof(temp);
for (j = i+1; j < strlen(stu->Time); j++){
temp[j - i-1] = stu->Time[j];
}
temp[j - i - 1] = 0;
min = atoi(temp);
if (0 < min && min < 30){
hour += 0.5;
}else if (30 <= min && min <60){
hour += 1;
}
stu->t = hour;
}
void showInfoOfStudent(Student stu){
printf("%s %s %.2f元
", stu.name, stu.id, getMoney(stu));
}
void showAllInfo(Student stu){
printf("姓名:%s
",stu.name );
printf("学号:%s
", stu.id );
printf("专业:%s
",stu.major);
printf("年级:%s
",stu.grade);
printf("上机时长:%s
",stu.Time);
printf("所需费用:%.2f
",getMoney(stu));
}
int isSameMajor(Student stu1, char* str){
return strcmp(stu1.major, str) == 0;
}
int isSameid(Student stu1, char* str){
return strcmp(stu1.id, str) == 0;
}
bool isSameName(Student stu1, char* str){
return strcmp(stu1.name, str) == 0;
}
Student* p = NULL; // 学生数据地址
int showInfo(){
int m;
printf("1.修改上机每小时价格.
");
printf("2.输入n名上机学生.
");
printf("3.计算各学生上机费用.
");
printf("4.查询学生上机情况.
");
printf("5.机器使用情况.
");
printf("6:退出.
");
printf("请输入序号:");
scanf("%d", &m);
return m;
}
void menu4(){
int n, i;
char str[512] = {0};
printf("1.查找条件为班级
");
printf("2.查找条件为学号
");
printf("3.查找条件为姓名
");
printf("请输入序号:");
scanf("%d", &n);
printf("请输入可找条件:");
scanf("%s", str);
switch (n)
{
case 1:
for (i = 0; i < n; i++){
if (isSameMajor(p[i], str)){
showInfoOfStudent(p[i]);
}
}
break;
case 2:
for (i = 0; i < n; i++){
if (isSameid(p[i],str)){
showInfoOfStudent(p[i]);
}
}
break;
case 3:
for (i = 0; i < n; i++){
if (isSameName(p[i],str)){
showInfoOfStudent(p[i]);
}
}
break;
}
}
int main(){
int flag = 1;
int i;
while (flag){
system("cls");
int m = showInfo();
switch (m)
{
case 1:
printf("请输入上机单价:");
scanf("%f", &price);
break;
case 2:
printf("请输入学生总数:");
scanf("%d", &n);
p = (Student*)malloc(sizeof(Student)*n);
for (i = 0; i < n; i++){
setInfo(p+i);
clacTime(p+i);
printf("
");
}
break;
case 3:
for (i = 0; i < n; i++)
showInfoOfStudent(p[i]);
system("pause");
break;
case 4:
menu4();
system("pause");
break;
case 5:
for (i = 0; i < n; i++)
showAllInfo(p[i]);
system("pause");
break;
case 6:
flag = false;
break;
}
}
free(p);
return 0;
}