1---在控制台上打印表格 ,新建一个头文件 tablePrint.h,然后再建一个空项目,tablePrin.cpp
tablePrint.cpp项目代码如下:
#include <stdio.h>
#include <string.h>
#include "tablePrint.h"
//打印表头
void printTableHead(int tableWidth) {
int margin = (WIN_WIDTH - tableWidth) / 2;
for (int i = 0; i < margin; i++) printf(" ");
printf("┌");
for (int i = 0; i < tableWidth - 2; i++)printf("─");
printf("┐\n");
}
//打印表尾
void printTableTail(int tableWidth) {
int margin = (WIN_WIDTH - tableWidth) / 2;
for (int i = 0; i < margin; i++) printf(" ");
printf("└");
for (int i = 0; i < tableWidth - 2; i++)printf("─");
printf("┘\n");
}
//打印一排数据
void printTableRow(int tableWidth, char cols[][COL_LEN_MAX], int n) {
int margin = (WIN_WIDTH - tableWidth) / 2;
for (int i = 0; i < margin; i++) printf(" ");
int colWidth = (tableWidth - (n + 1)) / n;
for (int i = 0; i < n; i++) {
printf("│");
int leftSpan = (colWidth - strlen(cols[i])) / 2;
int rightSpan = colWidth - leftSpan - strlen(cols[i]);
for (int j = 0; j < leftSpan; j++) printf(" ");
printf("%s", cols[i]);
for (int j = 0; j < rightSpan; j++) printf(" ");
}
printf("│\n");
}
//打印表头数据
void printTableHead(int tableWidth, int cols) {
int margin = (WIN_WIDTH - tableWidth) / 2;
int spanLen = (tableWidth - (cols + 1)) / cols;
for (int i = 0; i < margin; i++) printf(" ");
printf("┌");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < spanLen; j++) {
printf("─");
}
if (i < cols - 1) {
printf("┬");
}
else {
printf("┐");
}
}
printf("\n");
}
void printTableTail(int tableWidth, int cols) {
int margin = (WIN_WIDTH - tableWidth) / 2;
int spanLen = (tableWidth - (cols + 1)) / cols;
for (int i = 0; i < margin; i++) printf(" ");
//└─┴─┘
printf("└");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < spanLen; j++) {
printf("─");
}
if (i < cols - 1) {
printf("┴");
}
else {
printf("┘");
}
}
printf("\n");
}
//打印表中分割线
void printTableMidLine(int tableWidth, int cols) {
int margin = (WIN_WIDTH - tableWidth) / 2;
int spanLen = (tableWidth - (cols + 1)) / cols;
for (int i = 0; i < margin; i++) printf(" ");
printf("├");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < spanLen; j++) {
printf("─");
}
if (i < cols - 1) {
printf("┼");
}
else {
printf("┤");
}
}
printf("\n");
}
//打印表中数据
void printTableMidInfo(int tableWidth, const char* str) {
int margin = (WIN_WIDTH - tableWidth) / 2;
for (int i = 0; i < margin; i++) printf(" ");
printf("│");
int len1 = (tableWidth - 2 - strlen(str)) / 2;
for (int i = 0; i < len1; i++) printf(" ");
printf("%s", str);
int len2 = tableWidth - 2 - len1 - strlen(str);
for (int i = 0; i < len2; i++) printf(" ");
printf("│\n");
}
void printMidInfo(const char* str) {
int margin = (WIN_WIDTH - strlen(str)) / 2;
for (int i = 0; i < margin; i++) printf(" ");
printf("%s", str);
}
头文件 tablePrint.h 里面代码如下:
#pragma once
// 窗口宽度
#define WIN_WIDTH 80
// 窗口高度
#define WIN_HEIGHT 25
#define TABLE_WIDTH 78 //表格宽度
#define MENU_WIDTH 50 //菜单宽度
#define COL_LEN_MAX 74 //格子的最大宽度
void printTableHead(int tableWidth); //打印表头线
void printTableTail(int tableWidth); //答应表尾线
void printTableRow(int tableWidth, char cols[][COL_LEN_MAX], int n); //打印一排
void printTableHead(int tableWidth, int cols); //打印表头的数据
void printTableTail(int tableWidth, int cols); //打印带分隔线的表格尾线
void printTableMidLine(int tableWidth, int cols); //打印表格中间的线
void printTableMidInfo(int tableWidth, const char* str); //居中打印表中数据
void printMidInfo(const char* str);
main 函数代码
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<conio.h>
#include<Windows.h>
#include<string.h>
#include"tablePrint.h"
#define WIDE 80 //窗口宽读
#define HIGHT 25 //窗口高度
#define MENU_WIDE 50 //菜单宽度
#define TABLE_WIDE 78 //表格宽度
#define MAX_SIZE 100 //最多保存的显示数量
#define PAGES 10 //每一页有10个数据
char head[][74]={
"学号","姓名", "地址", "论文数量", "评议成绩", "是否班干部", "平均分"};
using namespace std;
//表示学生信息
struct student {
int num; //学号
char name[20]; //姓名
char address[20]; //地址(东部,西部)
int papers; //论文数量
int achievement; // 班级评议成绩
char classes[4]; //是否班干部
int average; //平均成绩
};
struct student stu[MAX_SIZE]; //定义一个结构图体数组
int currenlen = 0; //当前的个数
//奖学金信息
struct scholarship {
int work=0; //院士奖学金次数
int may=0; //五四奖学金次数
int achieve=0; //成绩优秀奖次数
int west=0; //西部奖学金次数
int devote=0; //班级贡献奖次数
int sum=0; //总获奖次数
int money=0; //获奖总金额
};
struct scholarship sch; //用来保存学生奖学金情况
//打印菜单
void menu() {
system("cls");
printTableHead(MENU_WIDE); //打印表头
printTableMidInfo(MENU_WIDE, "学生管理系统"); //居中打印信息
printTableMidInfo(MENU_WIDE, " ");
const char* Menu[] = {
"1.输入学生信息",
"2.查找学生信息",
"3.删除学生信息",
"4.修改学生信息",
"5.插入学生信息",
"6.显示学生所有信息",
"7.显示学生获奖情况",
"8.退出系统" };
int cont = sizeof(Menu) / sizeof(Menu[0]); //计算数组的项数
for(int i = 0; i < cont; i++) {
printTableMidInfo(MENU_WIDE, Menu[i]);
}
printTableMidInfo(MENU_WIDE, " ");
printTableTail(MENU_WIDE);
printMidInfo("请输入你的选择:"); //居中打印
}
//初始化界面
void init() {
//设置控制台窗口大小
char cmd[64];
sprintf_s(cmd, "mode con cols=%d lines=%d", WIDE, HIGHT); //按照mode con cols=%d lines=%d的形式打印到字符串 cmd
system(cmd);
//设置背景颜色
system("color 6E");
memset(stu, 0, sizeof(stu)); //将一整块内存直接清零
//从文件中读取数据,保存到数组中
FILE* fp;
errno_t err;
err = fopen_s(&fp, "data.txt", "rb"); //二进制形式读取,fopen_s 打开文件成功返回0,失败返回非0
if (err != 0) {
cout << "文件打开失败!" << endl;
currenlen = 0; //文件没有数据
return;
}
int i = 0;
while (!feof(fp)) { //feof()判断文件是否结束,如果结束返回一个非零值
int k=fread(&stu[i],sizeof(struct student),1,fp); //整块读取到 stu(结构图数组) 二进制读取
//判断是否成功读取
if (k == 1) {
i++;
}
}
currenlen = i;
fclose(fp);
}
struct student inputData() {
struct student s;
cout << "输入学号:";
cin >> s.num;
cout << "输入姓名:";
cin >> s.name;
cout << "输入地址:";
cin >> s.address;
cout << "输入论文数量:";
cin >> s.papers;
cout << "输入班级评议成绩:";
cin >> s.achievement;
cout << "是否班干部:";
cin >> s.classes;
cout << "输入平均成绩:";
cin >> s.average;
return s;
}
//返回-1表示没有这个学生 ,>=0表示这个学生在数组中存在
int searchStdent(int num) {
for (int i = 0; i < currenlen; i++) {
if (num == stu[i].num) {
return i;
}
}
return -1;
}
//保存信息
bool save() {
FILE* fp;
errno_t err;
//wb+覆盖式文件操作
err = fopen_s(&fp, "data.txt", "wb+ ");
if(err!=0){
cout << "文件data.txt打开失败!" << endl;
return false;
}for (int i = 0; i < currenlen; i++) {
int ret = (fwrite(&stu[i], sizeof(struct student), 1, fp)); //fwrite调用成功后返回写入对象的数量
if (ret != 1) {
cout << "写入失败!";
fclose(fp);
return false;
}
}
fclose(fp);
return true;
}
//输入学生信息
void input() {
string str;
while (1) {
system("cls");
cin.ignore();
cout << "输入学生信息:yes or no:";
cin >> str;
if (str == "no") {
break;
}
else {
struct student s = inputData();
if (searchStdent(s.num) >= 0) {
cout << "学生:%d,已经存在" << s.num << endl;
system("pause");
continue;
}
stu[currenlen] = s; //尾部添加
currenlen++;
//保存到文件
if (!save()) {
cout << "保存失败!" << endl;
system("pause");
}
else {
cout << "保存成功!" << endl;
system("pause");
}
}
}
}
//查找学生信息
void search() {
system("cls");
int snum = 0;
system("cls");
cout << "输入你的学号:";
cin >> snum;
int i = searchStdent(snum);
if (i < 0) {
cout << "没有找到这名学生!" << endl;
return;
}
char row[7][COL_LEN_MAX];
char head[][COL_LEN_MAX] = { "学号", "姓名","地址 ","论文数量", "评议成绩", "是否班干部", "平均分" };
printTableHead(TABLE_WIDTH, 7);
printTableRow(TABLE_WIDTH, head, sizeof(head) / sizeof(head[0])); //打印表头信息
printTableMidLine(TABLE_WIDTH, 7);
sprintf(row[0], "%d", stu[i].num);
sprintf(row[1], "%s", stu[i].name);
sprintf(row[2], "%s", stu[i].address);
sprintf(row[3], "%d", stu[i].papers);
sprintf(row[4], "%d", stu[i].achievement);
sprintf(row[5], "%s", stu[i].classes);
sprintf(row[6], "%d", stu[i].average);
printTableRow(TABLE_WIDE, row, 7);
printTableTail(TABLE_WIDTH, 7); //打印表尾的线
}
//删除学生信息
void deleted() {
system("cls");
FILE* fp;
int snum = 0;
string str;
system("cls");
cout << "输入学生学号:";
cin >> snum;
int i = searchStdent(snum);
if (i < 0) {
cout << "没有找到这名学生!" << endl;
return;
}
cout << "找到这个数据,是否删除,yes or no:";
cin >> str;
if (str == "yes") {
for (int j = i; j < currenlen; j++) { //重删除的位置,所有数据向前移
stu[j] = stu[j + 1];
}
currenlen--;
if (save()) {
cout << "删除成功" << endl;
}
else {
cout << "删除失败!" << endl;
}
}
if (str == "no") {
cout << "删除取消!" << endl;
system("pause");
return;
}
}
//提示可修改信息的界面
void suggestion(){
system("cls");
printTableHead(MENU_WIDE); //打印表头
printTableMidInfo(MENU_WIDE, "可修改信息"); //居中打印信息
printTableMidInfo(MENU_WIDE, " ");
const char* Suggestion[] = {
"1.修改学生地址",
"2.修改学生发表的论文数量",
"3.修改学生的班级评议成绩",
"4.修改学生的平均成绩",
"5.退出" };
int cont = sizeof(Suggestion) / sizeof(Suggestion[0]); //计算数组的项数
for (int i = 0; i < cont; i++) {
printTableMidInfo(MENU_WIDE, Suggestion[i]);
}
printTableMidInfo(MENU_WIDE, " ");
printTableTail(MENU_WIDE);
printMidInfo("请输入你的选择:"); //居中打印
}
//修改学生信息
void revise() {
system("cls");
int snum=0;
system("cls");
cout << "输入要修改学生的学号:";
cin >> snum;
int i = searchStdent(snum);
if (i < 0) {
cout << "没有找到这名学生!" << endl;
return;
}
cin.ignore(); //清空输入缓冲区
cout << "找到这名学生,你可以修改以下信息:";
int k = 0;
suggestion();
cin >> k;
switch (k)
{
case 1:cin >> stu[i].address; break;
case 2:cin >> stu[i].papers; break;
case 3:cin >> stu[i].achievement; break;
case 4:cin >> stu[i].average; break;
case 5:return;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
system("pause");
cin.ignore();
suggestion();
cin >> k;
if (save()) {
cout << "修改成功" << endl;
}
else {
cout << "修改失败!" << endl;
}
}
//增加学生信息
void add() {
system("cls");
int snum;
system("cls");
cout << "要插入的学生位置(学号): ";
cin >> snum;
int destIndex = searchStdent(snum);
if (destIndex < 0) {
cout << "找不到这个学生!" << endl;
return;
}
struct student t = inputData();
int i = searchStdent(t.num);
if (i >= 0) {
cout << "学号已经纯在" << t.num << endl;
return;
}
for (int j = currenlen - 1; j > destIndex; j--) {
stu[j + 1] = stu[j];
}
stu[destIndex + 1] = t;
currenlen++;
if (save()) {
cout << "插入成功" << endl;
}
else {
cout << "插入失败" << endl;
}
}
//从第几个开始打印到第几个结束
void showPage(int start, int end) {
if(end>=currenlen){ //如果最后一个树大于表的长度
end = currenlen - 1;
}
if (end - start + 1 > PAGES) {
end = start + PAGES-1;
}
system("cls");
//打印表头
printTableHead(TABLE_WIDE, 7);
//打印表头信息
printTableRow(TABLE_WIDE, head, sizeof(head) / sizeof(head[0]));
//打印表格之间的线
printTableMidLine(TABLE_WIDE, 7);
char row[7][74];
//开始打印数据
for (int i = start; i <= end; i++) {
sprintf(row[0], "%d", stu[i].num);
sprintf(row[1], "%s", stu[i].name);
sprintf(row[2], "%s", stu[i].address);
sprintf(row[3], "%d", stu[i].papers);
sprintf(row[4], "%d", stu[i].achievement);
sprintf(row[5], "%s", stu[i].classes);
sprintf(row[6], "%d", stu[i].average);
printTableRow(TABLE_WIDE, row, 7);
if (i < end) {
printTableMidLine(TABLE_WIDE, 7);
}
else {
printTableTail(TABLE_WIDE, 7);
}
}
}
//显示学生所有信息
void show(){
system("cls");
if (currenlen == 0) {
cout << "还没有学生信息!" << endl;
return;
}
//计算有几页
int count = (currenlen + PAGES - 1) / PAGES;
for (int i = 0; i <count; i++) {
showPage(i*PAGES,(i+1)*PAGES-1);
}
}
//获奖情况打印
void win() {
char row[7][COL_LEN_MAX]; //保存要打印的数据
//定义一个表头
char head[][COL_LEN_MAX] = { " 院士奖学金","五四奖学金", "成绩优秀奖", "西部奖学金", "班级贡献奖","获奖数","获奖总金额" };
printTableHead(TABLE_WIDTH, 7);
printTableRow(TABLE_WIDTH, head, sizeof(head) / sizeof(head[0])); //打印表头信息
printTableMidLine(TABLE_WIDTH, 7);
sprintf(row[0], "%d", sch.work);
sprintf(row[1], "%d", sch.may);
sprintf(row[2], "%d", sch.achieve);
sprintf(row[3], "%d", sch.west);
sprintf(row[4], "%d", sch.devote);
sprintf(row[5], "%d", sch.sum);
sprintf(row[6], "%d", sch.money);
printTableRow(TABLE_WIDE, row, 7);
printTableTail(TABLE_WIDTH, 7); //打印表尾的线
}
//显示学生获奖情况
void showWin() {
system("cls");
int num = 0;
cout << "输入要显示获奖学生的学号:";
cin >> num;
int k = searchStdent(num);
struct student s = stu[k];
if (k < 0) {
cout << "该学生不存在!" << endl;
return;
}
else {
//判断是否达到奖学金的要求
if (s.achievement > 80) {
sch.devote += 1;
sch.money += 850;
}
if (s.average > 80) {
if (s.papers > 1) {
sch.work += 1;
sch.money += 8000;
}
}
if (s.average > 85) {
if (s.achievement > 80) {
sch.may += 1;
sch.money += 4000;
}
}
else if (strcmp(s.address, "西部") == 0) {
sch.west += 1;
sch.money += 1000;
}
if (s.average > 90) {
sch.achieve += 1;
sch.money += 2000;
}
sch.sum = (sch.work + sch.may + sch.achieve + sch.west + sch.devote);
win();
}
}
int main(void) {
init();
menu();
int n = 0;
cin >> n;
while (1){
//选择菜单
switch (n) {
case 1:
input(); //输入
break;
case 2:
search(); //查找
break;
case 3:
deleted(); //删除
break;
case 4:
revise(); //修改
break;
case 5:
add(); //插入
break;
case 6:
show(); //显示
break;
case 7:
showWin(); //获奖情况
break;
case 8:
exit(1); //退出程序
default:
cout << "输入出错,请输入1-8" << endl;
break;
}
system("pause");
menu();
cin.ignore(); //清空输入缓冲区
cin >> n;
}
system("pause");
return 0;
}
运行效果展示: