鱼C论坛

 找回密码
 立即注册
查看: 4584|回复: 13

我写的一个学生管理系统,但老是不对,请高手指点,谢谢。

[复制链接]
发表于 2013-2-13 11:24:26 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 名人堂_丿_帅 于 2013-2-14 10:57 编辑

对不起,这是整个程序。。
注释中加问号的都是我想问的。。。高手帮帮忙。。
谢谢。。。

/*
2013年2月13日13:51:11
功能:
  输入学生信息,进行操作,输出排序后的学生信息
目的:
  编写学生管理系统
*/
# include <stdio.h>
# include <malloc.h>
struct Student
{
char name[100];
char sex;
float score;
};
void InputStudent(struct Student * p1, int len1);
void OutputStudent(struct Student * p2, int len2);
void sort(struct Student * p3, int len3);
int chacuo(struct Student * p4, int len4);
int main(void)
{
int len;
struct Student * pArr;
printf("请输入学生的个数:\n");
printf("len = ");
scanf("%d", &len);
pArr = (struct Student *)malloc(len * sizeof(struct Student));
InputStudent(pArr, len); //输入学生信息
if (0 == chacuo(pArr, len) )  //对输入的学生信息查错
  printf("你输入的学生信息 分数 不合理\n");      
                                                    //有了错误怎么结束或者重新开始???
sort(pArr, len);  //对输入的学生信息操作
printf("\n");
printf("---------------------------------超级分割线---------------------------\n");
printf("\n");
OutputStudent(pArr, len);  //输出操作之后的学生信息
return 0;
}
void InputStudent(struct Student * p1, int len1)
{
int i = 0;

for (i=0; i<len1; ++i)
{
  printf("亲,请输入第%d个学生的信息:\n", i+1);
  printf("姓名:");
  scanf("%s", p1.name);
  printf("性别:");
  scanf("%c", &p1.sex);   //本语句是否正确???
                  //输入姓名和分数都可以,但不能输入性别,为什么???
  printf("分数:");
  scanf("%f", &p1.score);
}
}
void OutputStudent(struct Student * p2, int len2)   //输出排序后的学生信息
{
int i;
for (i=0; i<len2; ++i)
{
  printf("排名第%d名学生信息是:\n", i+1);
  printf("姓名:%s\n", p2.name);
  printf("性别:%c\n", p2.sex);
  printf("分数:%f\n", p2.score);
}
}
void sort(struct Student * p3, int len3)    //对输入的学生信息排序
{
int i, j;
struct Student t;
for (i=0; i<len3-1; ++i)
  for (j=0; j<len3-1-i; ++j)
   if (p3[j].score < p3[j+1].score)
   {
    t = p3[j];
    p3[j] = p3[j+1];
    p3[j+1] = t;
   }
}
int chacuo(struct Student * p4, int len4)  //本函数对用户输入的分数查错,如果分数大于100或者                //小于0返回0;
{
int i;
for (i=0; i<len4; ++i)
  if (p4.score>100 && p4.score<0)
   return 0;
}
/*
在VC++6.0中输出结果:
-------------------------
请输入学生的个数:
len = 2
亲,请输入第1个学生的信息:
姓名:zhangsan
性别:分数:100   //为什么这里连了起来???
亲,请输入第2个学生的信息:
姓名:lisi
性别:分数:98   //为什么这里连了起来???
---------------------------------超级分割线---------------------------
排名第1名学生信息是:
姓名:zhangsan
性别:     //为什么没有输出???
    //为什么这里多了一个换行???
分数:100.000000
排名第2名学生信息是:
姓名:lisi
性别:    //为什么没有输出???
    //为什么这里多了一个换行???
分数:98.000000
Press any key to continue
-------------------------
*/
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-2-13 14:03:42 | 显示全部楼层
你的结构体怎么定义的?
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-2-13 14:37:14 | 显示全部楼层
亲 ,能把你整个程序的源码拷上来么 。你这只给出部分我们不好调试啊 ~~:L
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-2-13 14:45:03 | 显示全部楼层
对不起。。。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-2-13 14:54:23 | 显示全部楼层
本帖最后由 不能为空 于 2013-2-13 15:11 编辑

我猜你是这么定义的:
#include <stdio.h>
struct Student
{
     char name[20];
     char sex;
     float score;
};
void InputStudent(struct Student *p, int len)
{
     int i = 0;
     for (; i<len; ++i)
     {
        printf("亲,请输入第%d个学生的信息:\n", i+1);
        printf("姓名:");
        gets(p->name);             //这里用gets吧,scanf在输入回车时候,这个“\n”会输入到“性别”中
        printf("性别:");
        scanf("%c", &(p->sex));                           
        printf("分数:");
        scanf("%f", &(p->score));
    }
}  
void OutputStudent(struct Student *p, int len)
{
      int i;
      for (i=0; i<len; ++i)
      {
         printf("第%d个学生信息是:\n", i+1);
         printf("姓名:%s\n", p->name);
         printf("性别:%c\n", p->sex);
         printf("分数:%f\n", p->score);
      }
}
void main(void)
{
     struct Student p;
     InputStudent(&p,1);
     OutputStudent(&p,1);
     getchar();
}

小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-2-13 15:19:10 | 显示全部楼层
本帖最后由 名人堂_丿_帅 于 2013-2-13 15:25 编辑

楼上的前辈
你的void OutputStudent函数中p->name,p->sex,p->score
      void InputStudent函数中p->name,&(p->sex),&(p->score)
      如果p不加1指向下一个元素,不就老是第一个学生了吗???
    是不?求解???
还有,我的程序换行的问题解决了,但性别还是不能输出。。。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-2-13 17:51:41 | 显示全部楼层
本帖最后由 不能为空 于 2013-2-13 17:55 编辑

我这里是假设只有一个学生,而且我没看见你申请的结构体数组在哪里,你的函数传的是一个结构体的指针,另外scanf这个函数比较神奇,msdn上是这样说的:
(如果你确实要用scanf,你可以试试把成绩放在中间,%s和%c不要连续在一起)

s
String, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed in scanf Width Specification.
When used with scanf functions, signifies single-byte character array; when used with wscanf functions, signifies wide-character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended.
Required. Size includes space for a null terminator.
S
Opposite-size character string, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed in scanf Width Specification.
When used with scanf functions, signifies wide-character array; when used with wscanf functions, signifies single-byte–character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended.
Required. Size includes space for a null terminator
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-2-13 19:06:49 | 显示全部楼层
刚刚测试了下,作为输入结束的回车被存入下一个scanf中,你用%d来测试会显示10,也就是'\n'。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-2-13 20:08:35 | 显示全部楼层
:L这不是我大一学C的时候留下的作业题么 。。。。。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-2-14 10:11:02 | 显示全部楼层
诶  菜鸟 来学习学习:lol
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-2-14 10:53:20 | 显示全部楼层
不能为空 发表于 2013-2-13 17:51
我这里是假设只有一个学生,而且我没看见你申请的结构体数组在哪里,你的函数传的是一个结构体的指针,另外 ...


前辈,我还是初中生,貌似太。。。
    我调试过了,如果输入性别用gets(&p1[i].sex);需要写两个,第一个接收'\n', 第二个接收用户输入的。
    解决是解决了,但我想还有没有更好的办法???
    还有一个问题就是查错的函数,如果查出错误,怎么提示用户之后终止或者重新输入???
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-2-14 11:18:21 | 显示全部楼层
假设你已经查出错误了,可以设个标志变量,bool isRight = false,然后套在while(isRight )里面,剩下就自己发挥了。话说现在初中生就可以学这些真好啊,我读大学的时候还不会用QQ。。。。。。:Q
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-2-14 11:22:40 | 显示全部楼层
不能为空 发表于 2013-2-14 11:18
假设你已经查出错误了,可以设个标志变量,bool isRight = false,然后套在while(isRight )里面,剩下就自己 ...

自学的嘻嘻。。小白一个。。。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-2-14 11:26:32 | 显示全部楼层
我也是自学啊,甲鱼老师也是自学,自学改变人生~
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-8-8 10:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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