| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 386 人关注过本帖
标题:C语言程序编写有错,存储结构赋值出现问题!
只看楼主 加入收藏
xxt888888
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-6-16
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:2 
C语言程序编写有错,存储结构赋值出现问题!
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
//#include<string.h>
#define ERROR 0
#define OK 1
#define OVERFLOW -1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef struct Stu     {     
    char name[5];//姓名
    char stuno[1];//学号         
    int score1;//语文成绩     
    int score2;//数学成绩     
    int score3;//英语成绩
    }ElemType;
typedef struct LIST    {     
    ElemType *elem;
    int length;//顺序表长度
    int listsize;//链表最大值
    }List;

int init(List *L){ //构造一个空的线性表L
        
    L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L->elem)return OVERFLOW;     //存储分配失败
    L->length=0;                    //空表长为0  
    L->listsize=LIST_INIT_SIZE;    //初始存储容量
    return OK;
    }     

 int ListLength(List *L)  //返回链表的元素个数
    {     return L->length;     }

 int Insert_SqList(List *La,int i,ElemType e) {   //在第i个元素前插入元素e
     ElemType *p,*q,*newbase;     
     if(i<1||i>La->length+1)return ERROR;     //i值不合法
     if(La->length>=La->listsize) {         //存储已满,添加空间
         newbase=(ElemType*)realloc(La->elem,(La->listsize+LISTINCREMENT)*sizeof(ElemType));
         if(!newbase) return OVERFLOW;        //存储分配失败
         La->elem=newbase;                    //新基址
         La->listsize+=LISTINCREMENT;        //添加存储容量
     }     
     q=&(La->elem[i-1]);                    //q为插入位置
     for(p=&(La->elem[La->length-1]);p>=q;--p)  {   
           *(p+1)=*p;                      //从最后一个元素覆盖开始,向前进行
     }
     *q=e;                                 //插入e
     ++(La->length);                     //表长加1
     return OK;  
    }



 int Delete_SqList(List *La,int i,ElemType &e) {     //删除第i个元素,用e返回其值
    ElemType *p,*q;
    if(i<1||i>La->length)return ERROR;              //i不合法
    p=&(La->elem[i-1]);                            //p为被删元素位置
    e=*p;                                           //被删元素值赋给e
    q=La->elem+La->length-1;                        //q为表尾位置
    for(++p;p<=q;++p) {                              
        *(p-1)=*p;                                //被删元素之后元素左移
    }
    --La->length;                            //表长减1
    return OK;
    }

  
 void printList(List *L,int a) {     //遍历打印链表并写入e:\\test.txt文件中
    int i;
    int sum=0,sum1=0,sum2=0,sum3=0;
    FILE *fp = fopen("e:\\test.txt","w");
    for(i=0;i<L->length;i++)     {     
        fprintf(fp,"第%d个学生的信息:\n",i+1);
        printf("第%d个学生的信息:\n",i+1);
        fprintf(fp,"姓名:%s\t",L->elem[i].name);
        printf("姓名:%s\t",L->elem[i].name);
        fprintf(fp,"学号:%s\t",L->elem[i].stuno);
        printf("学号:%s\t",L->elem[i].stuno);
        fprintf(fp,"语文:%d\t",L->elem[i].score1);
        printf("语文:%d\t",L->elem[i].score1);
        fprintf(fp,"数学:%d\t",L->elem[i].score2);
        printf("数学:%d\t",L->elem[i].score2);
        fprintf(fp,"英语:%d\t",L->elem[i].score3);
        printf("英语:%d\t",L->elem[i].score3);
        fprintf(fp,"总成绩:%d\t",L->elem[i].score1+L->elem[i].score2+L->elem[i].score3);
        printf("总成绩:%d\t",L->elem[i].score1+L->elem[i].score2+L->elem[i].score3);
        fprintf(fp,"\n");
        printf("\n");
        sum1 += L->elem[i].score1;
        sum2 += L->elem[i].score2;
        sum3 += L->elem[i].score3;
        sum+=sum1+sum2+sum3;
      }
    fprintf(fp,"语文平均成绩:%d\t",sum1/a);
    printf("语文平均成绩:%d\t",sum1/a);
    fprintf(fp,"数学平均成绩:%d\t",sum2/a);
    printf("数学平均成绩:%d\t",sum2/a);
    fprintf(fp,"英语平均成绩:%d\t",sum3/a);
    printf("英语平均成绩:%d\t",sum3/a);
    fprintf(fp,"总平均成绩:%d\t",sum/a);
    printf("总平均成绩:%d\t",sum/a);
    fprintf(fp,"\n");
    printf("\n");
    fclose(fp);
 }

  int compare(ElemType e1, ElemType e2)
{
    if (e1.stuno==e2.stuno) return 0;
    else return 1;
}

 int LocateElem_Sq(List *La,ElemType e){ //在表中查找第一个与e满足compare()的元素的位置
     int i=1;
     ElemType *p;
     p=La->elem;            //p的初值为第一个元素的存储位置
     while((i<=La->length)&&compare(*p++,e))    //p与e不匹配
         ++i;
     if(i<=La->length)
       return i;
     else return ERROR;
 }


-----------------------------------------------------------------------------------------------------

#include"1.h"
void main() {
    int i,n=0,s=0;  
    List list;     
    ElemType stuck1;
    ElemType stuck2;
    ElemType stuck3;
    init(&list);
    //测试输入输出
    printf("请输入要建立学生信息的长度:\n",n);
    scanf("%d",&n);
    for(i=1;i<=n;i++)     {
        printf("请输入第%d个学生的信息\n",i);      
        printf("输入第%d个学生的姓名:\n",i);     
        scanf("%s",list.elem[i-1].name);      
        printf("请输入第%d个学生的学号:\n",i);     
        scanf("%s",list.elem[i-1].stuno);         
        printf("请输入第%d个学生的语文成绩:\n",i);     
        scanf("%d",&list.elem[i-1].score1);         
        printf("请输入第%d个学生的数学成绩:\n",i);
        scanf("%d",&list.elem[i-1].score2);      
        printf("请输入第%d个学生的英语成绩:\n",i);
        scanf("%d",&list.elem[i-1].score3);      
        list.length++;     
      }
      printList(&list,n);
     
     //测试删除
      printf("请输入要删除第几个学生信息:\n",s);
      scanf("%d",&s);
      Delete_SqList(&list,s,stuck1);
      printList(&list,n-1);
      printf("\n");
      FILE *Fp = fopen("e:\\delete.txt","w");
      printf("删除第%d个学生信息:\n",s);
      printf("删除学生姓名:%s\t",stuck1.name);
      printf("删除学生学号:%s\t",stuck1.stuno);
      printf("删除学生语文成绩:%d\t",stuck1.score1);
      printf("删除学生数学成绩:%d\t",stuck1.score2);
      printf("删除学生英语成绩:%d\t",stuck1.score3);
      fprintf(Fp,"删除学生信息:\n");
      fprintf(Fp,"删除学生姓名:%s\t",stuck1.name);
      fprintf(Fp,"删除学生学号:%s\t",stuck1.stuno);
      fprintf(Fp,"删除学生语文成绩:%d\t",stuck1.score1);
      fprintf(Fp,"删除学生数学成绩:%d\t",stuck1.score2);
      fprintf(Fp,"删除学生英语成绩:%d\t",stuck1.score3);
      
      //测试插入
      printf("输入学生的姓名:\n");
      scanf("%s",stuck2.name);                                                -----------------------------
      printf("输入学生的学号:\n");                                             代码无法赋值!!!!!
      scanf("%d",stuck2.stuno);
      printf("输入学生的语文成绩:\n");                                          导致运行时程序.exe无法工作!
      scanf("%d",stuck2.score1);
      printf("输入学生的数学成绩:\n");
      scanf("%d",stuck2.score2);
      printf("输入学生的英语成绩:\n");
      scanf("%d",stuck2.score3);                                           ---------------------------------
      Insert_SqList(&list,2,stuck2);                                       
      printList(&list,n);                                                
         
      //测试查找
      
      stuck3.stuno[1]=3;
      printf("%d",LocateElem_Sq(&list,stuck3));                           ----------------------无法判断,推断是  stuck3.stuno[1]=3没有赋值成功
}




两处错误求高手指点一二,感激不尽呐!!!!!!
搜索更多相关主题的帖子: 英语成绩 C语言 数学 
2011-06-16 18:41
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:10 
printf("输入学生的姓名:\n");
      scanf("%s",stuck2.name);                                                -----------------------------
      printf("输入学生的学号:\n");                                             代码无法赋值!!!!!
      scanf("%d",stuck2.stuno);
      printf("输入学生的语文成绩:\n");                                          导致运行时程序.exe无法工作!
      scanf("%d",&stuck2.score1);
      printf("输入学生的数学成绩:\n");
      scanf("%d",&stuck2.score2);
      printf("输入学生的英语成绩:\n");
      scanf("%d",&stuck2.score3);                                           ---------------------------------
      Insert_SqList(&list,2,stuck2);                                       
      printList(&list,n);      

忘了取地址了

                                         
===========深入<----------------->浅出============
2011-06-16 19:21
xxt888888
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-6-16
收藏
得分:0 
嗯是的,大神啊!非常感谢!
2011-06-16 22:46
快速回复:C语言程序编写有错,存储结构赋值出现问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.046464 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved