点名系统

#include <iostream>#include <string> #include <conio.h> #include <sstream> #include <fstream> #include <map> #include <deque> #include <time.h> using namespace std; struct stu_node { string id; string name; int punishment; }; map<string, stu_node> data; map<string, stu_node>::iterator iter; deque<string> id; void XMB() { cout << "****************点名系统****************" << endl; cout << "1.录入同学信息(会把以前数据全部删掉,格式:学号 姓名 累计惩罚(如无可不写))。" << endl; cout << "2.新增同学信息。" << endl; cout << "3.必修课点名。" << endl; cout << "4.选修课点名。" << endl; cout << "5.查看全部同学的累计惩罚。" << endl; cout << "6.查看某位同学的累计惩罚。" << endl; cout << "7.保存数据。" << endl; cout << "8.退出。" << endl; cout << "请输入你的选择: "; } bool getData(string filename) { ifstream inf(filename); if(inf == NULL) { return false; } streambuf *strm_buf = cin.rdbuf(); cin.rdbuf(inf.rdbuf()); data.clear(); string line, str; stu_node tmp; while(getline(cin , line)) { istringstream stream(line); if(stream >> str) { tmp.id = str; }else { continue; } if(stream >> str) { tmp.name = str; }else { continue; } if(stream >> str) { tmp.punishment = atoi(str.c_str()); } else { tmp.punishment = 0; } data.insert(make_pair(tmp.id, tmp)); id.push_back(tmp.id); } cin.rdbuf(strm_buf); return true; } bool saveData(string str) { ofstream outf(str.c_str()); if(outf == NULL) { return false; } streambuf *strm_buf = cout.rdbuf(); cout.rdbuf(outf.rdbuf()); for(iter = data.begin(); iter != data.end(); iter++) { cout << iter->second.id << " " << iter->second.name << " " << iter->second.punishment << endl; } cout.rdbuf(strm_buf); return true; } int main() { char choice = ' '; bool ok = true; string str; stu_node tmp; int count; int num; srand((unsigned)time(NULL)); getData("data.txt"); while(ok) { system("cls"); XMB(); fflush(stdin); choice = getch(); cin.clear(); cout << choice << endl << endl << endl; switch(choice) { case '1': cout << "请输入文件名: "; cin >> str; if(getData(str)) cout << "信息录入成功。" << endl; else cout << "信息录入失败。" << endl; break; case '2': cout << "请输入学号:"; cin >> str; if((iter = data.find(str)) != data.end()) { cout << "此同学已经存在,信息如下:" << endl; cout << "学号\t\t姓名\t累计惩罚" << endl; cout << iter->second.id << "\t" << iter->second.name << "\t" << iter->second.punishment << endl; }else { tmp.id = str; cout << "请输入姓名:"; cin >> str; tmp.name = str; tmp.punishment = 0; data.insert(make_pair(tmp.id, tmp)); id.push_back(tmp.id); cout << "录入成功." << endl; } break; case '3': cout << "现在开始进行必修课点名。输入y/Y代表已到,否则算缺席。" << endl; count = data.size() * 0.3 + 1; while(count) { num = rand() % count; str = id.at(num); iter = data.find(str); id.erase(id.begin() + num); id.push_back(str); cout << iter->first << " " << iter->second.name << " 在么?(y/n): "; fflush(stdin); choice = getch(); cout << choice << endl; if(!(choice == 'y' || choice == 'Y')) iter->second.punishment += 2; count--; } break; case '4': cout << "现在开始进行选修课点名。输入y/Y代表已到,否则算缺席。" << endl; count = data.size() * 0.3 + 1; while(count) { num = rand() % count; str = id.at(num); iter = data.find(str); id.erase(id.begin() + num); id.push_back(str); cout << iter->first << " " << iter->second.name << " 在么?(y/n): "; fflush(stdin); choice = getch(); cout << choice << endl; if(!(choice == 'y' || choice == 'Y')) iter->second.punishment += 2; count--; } break; case '5': cout << "各同学信息如下:" << endl; cout << "学号\t\t姓名\t累计惩罚" << endl; for(iter = data.begin(); iter != data.end(); iter++) { cout << iter->second.id << "\t" << iter->second.name << "\t" << iter->second.punishment << endl; } break; case '6': cout << "请输入学号: "; cin >> str; if((iter = data.find(str)) != data.end()) { cout << "此同学信息如下:" << endl; cout << "学号\t\t姓名\t累计惩罚" << endl; cout << iter->second.id << "\t" << iter->second.name << "\t" << iter->second.punishment << endl; }else { cout << "此同学信息不存在。" << endl; } break; case '7': cout << "请输入文件名: "; cin >> str; if(saveData(str)) cout << "保存成功。" << endl; else cout << "保存失败。" << endl; break; case '8': ok = false; break; default: cout << "输入错误。" << endl; } system("pause"); } saveData("data.txt"); return 0; }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java点名系统可以用来随机点名学生,可以使用Java的GUI界面来实现。以下是一个简单的示例代码: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class RollCallSystem extends JFrame implements ActionListener { private JButton button = new JButton("点名"); private JLabel label = new JLabel("请点击'点名'按钮进行点名"); private String[] students = {"张三", "李四", "王五", "赵六", "钱七", "孙八", "周九", "吴十"}; private ArrayList<String> list = new ArrayList<>(Arrays.asList(students)); public RollCallSystem() { setTitle("Java点名系统"); setLayout(new BorderLayout()); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); panel.add(button); button.addActionListener(this); add(panel, BorderLayout.NORTH); add(label, BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { if (list.isEmpty()) { label.setText("所有学生已被点名"); } else { int randomIndex = new Random().nextInt(list.size()); String student = list.remove(randomIndex); label.setText("被点名的学生是:" + student); } } } public static void main(String[] args) { RollCallSystem rollCallSystem = new RollCallSystem(); rollCallSystem.setVisible(true); } } ``` 上述代码中,我们定义了一个`RollCallSystem`类,继承自`JFrame`类,并实现了`ActionListener`接口。在`RollCallSystem`类中,我们定义了一个“点名”按钮`button`和一个标签`label`,用来显示被点名的学生。我们还定义了一个学生名字数组`students`和一个学生名字列表`list`,用来存储还未被点名的学生名字。 在`RollCallSystem`类的构造方法中,我们设置了窗口的标题、大小、关闭操作,并使用`BorderLayout`布局管理器来布局窗口中的组件。我们将“点名”按钮添加到窗口的北部,并为按钮添加了一个事件监听器。我们将标签添加到窗口的中央。 在`actionPerformed`方法中,我们判断事件源是否为“点名”按钮。如果是,“点名”按钮被点击,我们首先判断学生名字列表`list`是否为空。如果为空,说明所有学生都已被点名,我们将标签文本设置为“所有学生已被点名”。如果不为空,我们使用`Random`类生成一个随机数,用来随机选择学生名字列表`list`中的一个学生名字。然后,我们从列表中移除该学生名字,并将标签文本设置为“被点名的学生是:学生名字”。 最后,在`main`方法中,我们创建了一个`RollCallSystem`对象,并将其设置为可见。运行程序后,我们可以点击“点名”按钮进行点名。每次点击按钮后,程序会随机选择一个还未被点名的学生,并将其名字显示在标签中。当所有学生都被点名后,点击“点名”按钮将显示“所有学生已被点名”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值