温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用C语言怎么实现一个学生选课系统

发布时间:2021-04-20 17:17:20 来源:亿速云 阅读:342 作者:Leah 栏目:编程语言

本篇文章给大家分享的是有关使用C语言怎么实现一个学生选课系统,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

C语言是什么

C语言是一门面向过程的、抽象化的通用程序设计语言,广泛应用于底层开发,使用C语言可以以简易的方式编译、处理低级存储器。

代码:

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
 
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int  uint32_t;
 
#define CLASS_CLS  system("cls")
#define CLASS_NAME  80
 
typedef struct class
 {
  char name[CLASS_NAME]; /* 课程名称 -- 唯一性 */
  uint32_t nature;    /* 课程性质(必修或者选修) */
  uint32_t total_period; /* 课程总学时 */
  uint32_t teach_period; /* 授课学时 */
  uint32_t exper_period; /* 上机学时 */
  uint32_t start_time;  /* 课程开始时间 */
  uint8_t score;     /* 课程学分 */
  uint8_t is_exsit;    /* 课程是否存在 */
  struct class *next;
 } class_t; // 课程结构体
 
class_t *head = NULL;
static uint32_t count = 1;
 
void play(char *text, int display, int time, int nu) //动画打印
{
 CLASS_CLS;
 int i, len;
 for(i = 0; i <= nu; i++)
 {
  printf("\n");
 }
 for(i = 0; i < 25; i++)
 {
  printf(" ");
 }
 len = strlen(text);
 for(i = 0; i < len; i++)
 {
  printf("%c", text[i]);
  Sleep(display);
 }
 Sleep(time);
}
 
void titile(char *text, char *str)
{
 CLASS_CLS;
 uint8_t i;
 for(i = 0; i < 25; i++)
 {
  printf(" ");
 }
 printf("%s\n", text);
 for(i = 0; i <= 60; i++)
 {
  printf("%s", str);
 }
 printf("\n");
}
 
void menu(void)
{
 titile("【学生选课系统】", "-");
 printf("\n\t|-----------------------------------|");
 printf("\n\t|      [1]--增加课程     |");
 printf("\n\t|      [2]--浏览课程     |");
 printf("\n\t|      [3]--查询课程     |");
 printf("\n\t|      [4]--删除课程     |");
 printf("\n\t|      [5]--修改课程     |");
 printf("\n\t|      [Q]--退出系统     |");
 printf("\n\t|-----------------------------------|");
}
 
void get_bat_data(void)
{
 class_t *point, *q;
 uint32_t count = 0;
 FILE *fp = fopen("c:\\student_elective.dat", "rb");
 rewind(fp);
 
 point = (class_t *)malloc(sizeof(class_t));
 head = point;
 
 while(!feof(fp))
 {
  count++;
  fread(point, sizeof(class_t), 1, fp);
  point->next = (class_t *)malloc(sizeof(class_t));
  q = point;
  point = point->next;
 }
 q->next = NULL;
 fclose(fp);
}
 
void save_bat_data(void)
{
 class_t *point = head;
 FILE *fp = fopen("c:\\student_elective.dat", "w+");
 
 while(NULL != point)
 {
  count++;
  fwrite(point, sizeof(class_t), 1, fp);
  point = point->next;
 }
 fclose(fp);
}
 
uint32_t num_check(void)
{
 char ch;
 uint32_t sum = 0;
 
 while(1)
 {
  ch = getch();
  if('\n' == ch || '\r' == ch)
  {
   return sum;
  }
  else if('\b' == ch)
  {
   sum /= 10;
   printf("\b \b");
  }
  else if(('0' <= ch) && ('9' >= ch))
  {
   sum *= 10;
   sum += ch - '0';
   printf("%d", ch - '0');
  }
 }
 
}
 
void create(void)
{
 class_t *point, *q;
 char tmp[CLASS_NAME], ch;
 uint8_t flag = 0;
 
 while(1)
 {
  if(1 != count)
  {
   printf("是否继续增加课程(y/n):");
   gets(tmp);
   if(strcmp(tmp, "n") == 0)
   {
    break;
   }
  }
 
  point = (class_t *)malloc(sizeof(class_t));
  point->is_exsit = 0;
  printf("\n====请输入第%d个选修课程信息====\n", count);
  printf("选择课程名称:");
  gets(point->name);
  q = head;
  while(NULL != q)
  {
   if(strcmp(q->name, point->name) == 0)
   {
    flag = 1;
    printf("课程名称重复或者不合格,请重新输入...\n");
    break;
   }
   q = q->next;
  }
  if(1 == flag)
  {
   continue;
  }
 
  printf("课程性质:");
  printf("\n[B]--【必修】 [X]--【选修】");
  while(1)
  {
   ch = getch();
   if(ch == 'b' || ch == 'B')
   {
    point->nature = 1;
    break;
   }
   if(ch == 'x' || ch == 'X')
   {
    point->nature = 2;
    break;
   }
  }
 
  printf("\n输入总学时:(只接受数字!)");
  point->total_period = num_check();
  printf("\n输入授课学时:(只接受数字!)");
  point->teach_period = num_check();
  printf("\n输入上机学时:(只接受数字!)");
  point->exper_period = num_check();
  printf("\n输入本课程学分:(只接受数字!)");
  point->score = num_check();
  printf("\n输入开课学期:(只接受数字!)");
  point->start_time = num_check();
  point->is_exsit = 1;
 
  point->next = head;
  head = point;
  count++;  
 }
 
 printf("信息录入完毕,按任意键继续……");
 getch();
}
 
void display(void)
{
 class_t *point = head;
 
 CLASS_CLS;
 titile("【查看课程】", "-");
 printf("\n名称      \t性质\t总学时\t授课学时\t上机学时\t学分\t开课学期");
 
 while(NULL != point)
 {
  if(1 == point->is_exsit)
  {
   printf("\n%-14s  ", point->name);
   if(1 == point->nature)
   {
    printf("必修课");
   }
   else
   {
    printf("选修课");
   }
   printf("   %d时   %d时      %d时      %d分   %d时", point->total_period, point->teach_period, point->exper_period, point->score, point->start_time);
  }
  point = point->next;
 }
 getch();
}
// 对照学生管理系统自行拓展
void search(void)
{
 
}
 
void modify(void)
{
 
}
 
void delete(void)
{
 
}
 
int main(void)
{
 uint8_t value;
 uint8_t movie = 1;
 char choice[3];
 
 FILE *fp = fopen("c:\\student_elective.dat", "a");
 fclose(fp);
 
 system("color 30");
 system("mode con:cols=100 lines=35");
 system("title 【选修课系统】");
 
 if(1 == movie)
 {
  play("欢迎使用【选修课系统】", 80, 1500, 10);
 }
 
 while(1)
 {
  CLASS_CLS;
  menu();
  do
  {
   gets(choice);
   value = atoi(choice);
  }
  while((value > 12) || (value < 0));
  switch(value)
  {
  case 1:
   create();
   break;
  case 2:
   display();
   break;
  case 3:
   search();
   break;
  case 4:
   modify();
   break;
  case 5:
   delete();
   break;
  case 6:
   save_bat_data();
   break;
  case 7:
   get_bat_data();
   break;
  case 8:
   exit(1);
   break;
 
  default:
   break;
  }
 }
 
 return 0;
}

以上就是使用C语言怎么实现一个学生选课系统,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI