鱼C论坛

 找回密码
 立即注册
查看: 1346|回复: 1

[作品展示] C语言+Mysql+centos 7 实现学生信息管理系统

[复制链接]
发表于 2023-8-14 15:29:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
此前写的文章https://blog.csdn.net/ht4091/article/details/132218033
在此平台公布源码,大家参考参考
项目要求在附件
  1. #include "include/mysql.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <signal.h> //当按下ctrl+c前执行一个函数
  6. #include <math.h>

  7. /*定义一些数据库连接需要的宏*/
  8. #define HOST "192.168.50.138" /*MySql服务器地址*/
  9. #define USERNAME "root"       /*用户名*/
  10. #define PASSWORD "123456"     /*数据库连接密码*/
  11. #define DATABASE "mysql"      /*需要连接的数据库*/
  12. #define PORT 5555

  13. void ShowInterface(void);
  14. void InputScore(MYSQL *my_connection);         //T
  15. int CountAverage2(MYSQL *my_connection);       // A
  16. int ShowScore(MYSQL *my_connection);           //L
  17. int Sort_ByAverage_DESC(MYSQL *my_connection); //P
  18. int Search(MYSQL *my_connection);              //S
  19. void MysqlInit(void);

  20. struct Student
  21. {
  22.     int ID;
  23.     float ChineseScore;
  24.     float MathScore;
  25.     float EnglishScore;
  26.     float Average;
  27. } student;

  28. MYSQL *my_connection;

  29. //信号处理函数的参数必须是int ,这是被signal()约束了
  30. void handleSignal(int sig)
  31. {
  32.     printf("Received SIGINT signal. Exiting...\n");
  33.     mysql_close(my_connection);
  34.     exit(0);
  35. }
  36. int main(void)
  37. {
  38.     char CMD;
  39.     int res;
  40.     char SqlSentence[100] = "CREATE TABLE Student (ID INT PRIMARY KEY,Chinese FLOAT,Math FLOAT,English FLOAT,Average FLOAT);";

  41.     MysqlInit();
  42.     signal(SIGINT, handleSignal); //注册信号处理函数,ctrl+c前关闭mysql的连接
  43.     res = mysql_query(my_connection, SqlSentence);
  44.     if (res)
  45.     {
  46.         printf("创建表失败Error: mysql_query !\n");
  47.         fprintf(stderr, "Query error: %s\n", mysql_error(my_connection));
  48.     }
  49.     else
  50.     {
  51.         printf("创建表成功");
  52.     }
  53.     ShowInterface();
  54.     while (1)
  55.     {
  56.         printf("请输入命令=");
  57.         scanf(" %c", &CMD);
  58.         getchar();
  59.         switch (CMD)
  60.         {
  61.         case 'H':
  62.             ShowInterface();
  63.             break;
  64.         case 'T':
  65.             InputScore(my_connection);
  66.             break;
  67.         case 'A':
  68.             CountAverage2(my_connection);
  69.             break;
  70.         case 'L':
  71.             ShowScore(my_connection);
  72.             break;
  73.         case 'P':
  74.             Sort_ByAverage_DESC(my_connection);
  75.             break;
  76.         case 'S':
  77.             Search(my_connection);
  78.             break;
  79.         case 'C':
  80.             system("clear");
  81.             break;
  82.         case 'Q':
  83.             /*不要忘了关闭连接*/
  84.             mysql_close(my_connection);
  85.             exit(0);
  86.         default:
  87.             //getchar();
  88.             printf("输入错误,");
  89.             break;
  90.         }
  91.     }

  92.     return 0;
  93. }

  94. //显示界面
  95. void ShowInterface(void)
  96. {
  97.     printf("**********************************************\n");
  98.     printf("*        学生成绩管理系统————帮助菜单        *\n");
  99.     printf("**********************************************\n");
  100.     printf("*          H = 显示帮助菜单                  *\n");
  101.     printf("*          T = 成绩录入                      *\n");
  102.     printf("*          A = 计算学生平均分                *\n");
  103.     printf("*          L = 列出成绩表                    *\n");
  104.     printf("*          P = 按平均成绩由高到低排序        *\n");
  105.     printf("*          S = 按学校查询学生成绩            *\n");
  106.     printf("*          C = 清屏                          *\n");
  107.     printf("*          Q = 退出系统                      *\n");
  108.     printf("**********************************************\n");
  109.     printf("*     Copyright <C> 2023.08.10 By李海涛      *\n");
  110.     printf("**********************************************\n");
  111. }

  112. //输入成绩T
  113. void InputScore(MYSQL *my_connection)
  114. {
  115.     char SqlSentence[200] = "";
  116.     int Count, res;
  117.     float totalScore = 0.0;

  118.     printf("请输入学生人数:");
  119.     scanf("%d", &Count);
  120.     printf("请输入%d名学生的三门课成绩: \n", Count);
  121.     printf("学号    语文    数学    外语\n");

  122.     while (Count > 0)
  123.     {

  124.         scanf("%d", &student.ID);
  125.         scanf("%f", &student.ChineseScore);
  126.         scanf("%f", &student.MathScore);
  127.         scanf("%f", &student.EnglishScore);
  128.         getchar();
  129.         totalScore = student.ChineseScore + student.MathScore + student.EnglishScore;
  130.         student.Average = totalScore / 3.0;

  131.         sprintf(SqlSentence, "INSERT INTO Student (ID, Chinese, Math, English,Average) VALUES (%d, %f, %f, %f, %f);", student.ID, student.ChineseScore, student.MathScore, student.EnglishScore, student.Average);
  132.         res = mysql_query(my_connection, SqlSentence);
  133.         if (res)
  134.         {
  135.             printf("输入成绩失败Error: mysql_query !\n");
  136.             fprintf(stderr, "Query error: %s\n", mysql_error(my_connection)); //打印报错信息
  137.         }
  138.         else
  139.         {
  140.             printf("sql:%s 执行成功 \n", SqlSentence);
  141.         }
  142.         totalScore = 0;
  143.         Count = Count - 1; //下个学生
  144.     }
  145. }

  146. //列出成绩表L
  147. int ShowScore(MYSQL *my_connection)
  148. {
  149.     int rowID;
  150.     float rowScore;
  151.     MYSQL_RES *res_ptr;   /*执行结果*/
  152.     MYSQL_ROW result_row; /*按行返回查询信息*/
  153.     int row, column;      /* 定义行数,列数*/
  154.     int res;
  155.     char SqlSentence[200] = "SELECT ID,Chinese,Math,English,Average FROM Student;";
  156.     res = mysql_query(my_connection, SqlSentence);
  157.     //printf("res = %d \n", res);
  158.     //res == 1 表示失败
  159.     if (res)
  160.     {
  161.         printf("显示成绩失败Error: mysql_query !\n");
  162.         fprintf(stderr, "Query error: %s\n", mysql_error(my_connection));
  163.     }
  164.     else
  165.     {

  166.         /*mysql_affected_rows会返回执行sql后影响的行数*/
  167.         //printf("%llu 行受到影响!\n", mysql_affected_rows(my_connection));
  168.         printf("执行语句:%s 成功\n", SqlSentence);
  169.         // 把查询结果装入 res_ptr
  170.         res_ptr = mysql_store_result(my_connection);
  171.         //printf("res_ptr = %p \n", res_ptr);
  172.         // 存在则输出
  173.         if (res_ptr)
  174.         {
  175.             // 获取行数,列数
  176.             row = mysql_num_rows(res_ptr);
  177.             column = mysql_num_fields(res_ptr);
  178.             printf("row = %d ,column = %d \n", row, column);
  179.             if (row == 0)
  180.             {
  181.                 printf("成绩表为空!请先使用命令T录入学生信息。\n");
  182.                 //则没有记录
  183.                 return 0;
  184.             }
  185.             else
  186.             {
  187.                 printf("学生成绩如下:\n");
  188.                 printf("学号    语文    数学    外语    平均分\n");
  189.                 // 执行输出结果,从第二行开始循环(第一行是字段名)
  190.                 for (int i = 1; i < row + 1; i++)
  191.                 {
  192.                     // 一行数据
  193.                     result_row = mysql_fetch_row(res_ptr);
  194.                     for (int j = 0; j < column; j++)
  195.                     {
  196.                         if (j == 0)
  197.                         {
  198.                             rowID = atoi(result_row[j]);
  199.                             printf(" %d", rowID);
  200.                         }
  201.                         else
  202.                         {
  203.                             rowScore = atoi(result_row[j]);
  204.                         }

  205.                         if (rowID < 10 && rowID > 0)
  206.                         {
  207.                             printf("      ");//六个
  208.                         }
  209.                         else if (rowID >= 10)
  210.                         {
  211.                             printf("     ");//五个
  212.                         }
  213.                         if (j != 0)
  214.                         {
  215.                             printf("%.1f", rowScore);
  216.                             if (rowScore == 100)
  217.                             {
  218.                                 printf("   ");//三个
  219.                             }
  220.                             else if (rowScore >= 10 && rowScore < 100)
  221.                             {

  222.                                 printf("    ");//四个
  223.                             }
  224.                             else if (rowScore < 10)
  225.                             {

  226.                                 printf("     ");//五个
  227.                             }
  228.                         }

  229.                         rowID = 0;
  230.                         rowScore = 0;
  231.                         //printf("%s", result_row[j]);
  232.                     }
  233.                     printf("\n");
  234.                 }
  235.             }
  236.         }
  237.     }
  238. }

  239. //计算平均分A
  240. int CountAverage2(MYSQL *my_connection)
  241. {
  242.     char SqlSentence[200] = "SELECT ID FROM Student;";
  243.     MYSQL_RES *res_ptr;
  244.     MYSQL_ROW result_row;
  245.     int row, column;
  246.     int res;
  247.     res = mysql_query(my_connection, SqlSentence);
  248.     //printf("res = %d \n", res);
  249.     //res == 1 表示失败
  250.     if (res)
  251.     {
  252.         printf("计算平均分Error: mysql_query !\n");
  253.         fprintf(stderr, "Query error: %s\n", mysql_error(my_connection));
  254.     }
  255.     else
  256.     {

  257.         /*mysql_affected_rows会返回执行sql后影响的行数*/
  258.         //printf("%llu 行受到影响!\n", mysql_affected_rows(my_connection));
  259.         printf("执行语句:%s 成功\n", SqlSentence);
  260.         // 把查询结果装入 res_ptr
  261.         res_ptr = mysql_store_result(my_connection);
  262.         //printf("res_ptr = %p \n", res_ptr);
  263.         // 存在则输出
  264.         if (res_ptr)
  265.         {
  266.             // 获取行数,列数
  267.             row = mysql_num_rows(res_ptr);
  268.             column = mysql_num_fields(res_ptr);
  269.             printf("row = %d ,column = %d \n", row, column);
  270.             if (row == 0)
  271.             {
  272.                 printf("成绩表为空!请先使用命令T录入学生信息。\n");
  273.                 //则没有记录
  274.                 return 0;
  275.             }
  276.             else
  277.             {
  278.                 printf("平均分已计算,请使用L命令查看。\n");
  279.             }
  280.         }
  281.     }
  282. }

  283. //平均分由高到低排序P
  284. int Sort_ByAverage_DESC(MYSQL *my_connection)
  285. {
  286.     char SqlSentence[200] = "SELECT * FROM Student ORDER BY Average DESC;";
  287.     MYSQL_RES *res_ptr;   /*执行结果*/
  288.     MYSQL_ROW result_row; /*按行返回查询信息*/
  289.     int row, column;      /* 定义行数,列数*/
  290.     int res;
  291.     res = mysql_query(my_connection, SqlSentence);
  292.     //printf("res = %d \n", res);
  293.     //res == 1 表示失败
  294.     if (res)
  295.     {
  296.         printf("显示成绩失败Error: mysql_query !\n");
  297.         fprintf(stderr, "Query error: %s\n", mysql_error(my_connection));
  298.     }
  299.     else
  300.     {

  301.         /*mysql_affected_rows会返回执行sql后影响的行数*/
  302.         //printf("%llu 行受到影响!\n", mysql_affected_rows(my_connection));
  303.         printf("执行语句:%s 成功\n", SqlSentence);
  304.         // 把查询结果装入 res_ptr
  305.         res_ptr = mysql_store_result(my_connection);
  306.         //printf("res_ptr = %p \n", res_ptr);
  307.         // 存在则输出
  308.         if (res_ptr)
  309.         {
  310.             // 获取行数,列数
  311.             row = mysql_num_rows(res_ptr);
  312.             column = mysql_num_fields(res_ptr);
  313.             printf("row = %d ,column = %d \n", row, column);
  314.             if (row == 0)
  315.             {
  316.                 printf("成绩表为空!请先使用命令T录入学生信息。\n");
  317.                 //则没有记录
  318.                 return 0;
  319.             }
  320.             else
  321.             {
  322.                 printf("学号    语文    数学    外语    平均分\n");
  323.                 // 执行输出结果,从第二行开始循环(第一行是字段名)
  324.                 for (int i = 1; i < row + 1; i++)
  325.                 {
  326.                     // 一行数据
  327.                     result_row = mysql_fetch_row(res_ptr);
  328.                     for (int j = 0; j < column; j++)
  329.                     {
  330.                         printf(" %s", result_row[j]);
  331.                         for (int k = 0; k < 4; k++)
  332.                         {
  333.                             printf(" ");
  334.                         }
  335.                     }
  336.                     printf("\n");
  337.                 }
  338.             }
  339.         }
  340.     }
  341. }

  342. //根据学号查信息S
  343. int Search(MYSQL *my_connection)
  344. {
  345.     int ID;
  346.     char SqlSentence[200] = "";
  347.     MYSQL_RES *res_ptr;   /*执行结果*/
  348.     MYSQL_ROW result_row; /*按行返回查询信息*/
  349.     int row, column;      /* 定义行数,列数*/
  350.     int res;

  351.     sprintf(SqlSentence, "SELECT ID, Chinese, English, Math, Average FROM Student WHERE ID = %d;", ID);
  352.     res = mysql_query(my_connection, SqlSentence);
  353.     //printf("res = %d \n", res);
  354.     //res == 1 表示失败
  355.     if (res)
  356.     {
  357.         printf("根据学号查信息Error: mysql_query !\n");
  358.         fprintf(stderr, "Query error: %s\n", mysql_error(my_connection));
  359.     }
  360.     else
  361.     {

  362.         /*mysql_affected_rows会返回执行sql后影响的行数*/
  363.         //printf("%llu 行受到影响!\n", mysql_affected_rows(my_connection));
  364.         printf("执行语句:%s 成功\n", SqlSentence);
  365.         // 把查询结果装入 res_ptr
  366.         res_ptr = mysql_store_result(my_connection);
  367.         //printf("res_ptr = %p \n", res_ptr);
  368.         // 存在则输出
  369.         if (res_ptr)
  370.         {
  371.             // 获取行数,列数
  372.             row = mysql_num_rows(res_ptr);
  373.             column = mysql_num_fields(res_ptr);
  374.             printf("row = %d ,column = %d \n", row, column);
  375.             if (row == 0)
  376.             {
  377.                 printf("成绩表为空!请先使用命令T录入学生信息。\n");
  378.                 //则没有记录
  379.                 return 0;
  380.             }
  381.             else
  382.             {
  383.                 printf("请输入要查询的学生学号:");
  384.                 scanf("%d", &ID);
  385.                 printf("学生成绩如下:\n");
  386.                 printf("学号    语文    数学    外语    平均分\n");
  387.                 // 执行输出结果,从第二行开始循环(第一行是字段名)
  388.                 for (int i = 1; i < row + 1; i++)
  389.                 {
  390.                     // 一行数据
  391.                     result_row = mysql_fetch_row(res_ptr);
  392.                     for (int j = 0; j < column; j++)
  393.                     {
  394.                         printf(" %s", result_row[j]);
  395.                         for (int k = 0; k < 4; k++)
  396.                         {
  397.                             printf(" ");
  398.                         }
  399.                     }
  400.                     printf("\n");
  401.                 }
  402.             }
  403.         }
  404.     }
  405. }

  406. //Mysql数据库初始化
  407. void MysqlInit(void)
  408. {
  409.     printf("数据库IP:%s,端口:%d,连接的数据库:%s,用户名:%s,密码:******** \n", HOST, PORT, DATABASE, USERNAME);
  410.     my_connection = (MYSQL *)malloc(sizeof(MYSQL)); /*数据库连接*/
  411.     mysql_init(my_connection);

  412.     if (mysql_real_connect(my_connection, HOST, USERNAME, PASSWORD, DATABASE, PORT, NULL, CLIENT_FOUND_ROWS))
  413.     {
  414.         printf("                数据库连接成功!                \n");
  415.         /*设置查询编码为 utf8, 支持中文*/
  416.         mysql_query(my_connection, "set names utf8");
  417.     }
  418.     else
  419.     {
  420.         printf("数据库连接失败!\n");
  421.     }
  422. }

  423. //计算学生平均分,废弃
  424. /*
  425. int CountAverage(MYSQL *my_connection)
  426. {
  427.     float Chinese, Math, Egnlish, Average;
  428.     char SqlSentence_QueryRow[200] = "SELECT ID FROM Student;";
  429.     char SqlSentence_QueryScoreChinese[200] = "";
  430.     char SqlSentence_QueryScoreEnglish[200] = "";
  431.     char SqlSentence_QueryScoreMath[200] = "";
  432.     MYSQL_RES *res_ptr;   
  433.     MYSQL_ROW result_row;
  434.     int row, column;      
  435.     int res;
  436.     res = mysql_query(my_connection, SqlSentence);
  437.     printf("res = %d \n", res);
  438.     //res == 1 表示失败
  439.     if (res)
  440.     {
  441.         printf("显示成绩失败Error: mysql_query !\n");
  442.         fprintf(stderr, "Query error: %s\n", mysql_error(my_connection));
  443.     }
  444.     else
  445.     {
  446.         printf("%llu 行受到影响!\n", mysql_affected_rows(my_connection));
  447.         printf("执行语句:%s 成功\n", SqlSentence);
  448.         // 把查询结果装入 res_ptr
  449.         res_ptr = mysql_store_result(my_connection);
  450.         printf("res_ptr = %p \n", res_ptr);
  451.         // 存在则输出
  452.         if (res_ptr)
  453.         {
  454.             // 获取行数,列数
  455.             row = mysql_num_rows(res_ptr);
  456.             column = mysql_num_fields(res_ptr);
  457.             printf("row = %d ,column = %d \n", row, column);
  458.             if (row == 0)
  459.             {
  460.                 printf("成绩表为空!请先使用命令T录入学生信息。\n");
  461.                 //则没有记录
  462.                 return 0;
  463.             }
  464.             else
  465.             {
  466.                 // 执行输出结果,从第二行开始循环(第一行是字段名)
  467.                 for (int ID = 1; i < row + 1; i++)
  468.                 {
  469.                     sprintf(SqlSentence_QueryScoreChinese, "SELECT Chinese FROM student_scores WHERE ID = %d;", ID);
  470.                     sprintf(SqlSentence_QueryScoreMath, "SELECT Chinese FROM student_scores WHERE ID = %d;", ID);
  471.                     sprintf(SqlSentence_QueryScoreEnglish, "SELECT Chinese FROM student_scores WHERE ID = %d;", ID);
  472.                 }
  473.             }
  474.         }
  475.     }
  476. }
  477. */
复制代码

C语言项目要求及原码.zip

520.67 KB, 下载次数: 6

评分

参与人数 1鱼币 +1 收起 理由
琅琊王朝 + 1 无条件支持楼主!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-16 08:56:42 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 00:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表