| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4543 人关注过本帖, 6 人收藏
标题:【解剖麻雀】通过一道小型课题解答一些常见问题
只看楼主 加入收藏
hgs121746
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-1-20
收藏
得分:0 
+1
2015-01-20 22:32
大鸟库页
Rank: 2
等 级:论坛游民
帖 子:5
专家分:10
注 册:2014-12-12
收藏
得分:0 
贴下边参考,边编写的代码
208行,拖进去直接运行。注意那三个文件放到同名文件夹下,还有三个txt的格式。
程序代码:
// Tonydeng.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include "stdafx.h"
#include <malloc.h>
#include <string.h>
#include <process.h>
struct stuInfo
{
    long num;
    char name[6];
    int grade;
    int sex;
    int score[10];
    stuInfo*pNext;
}*s_pHead=0;

struct sexInfo
{
    int sex_num;
    char sex[2];
    sexInfo*e_pNext;
    
}*sex_Head=0;

struct CodeInfo
{
    int code_num;
    char college[10];
    CodeInfo*c_pNext;
    
}*code_Head=0;

int readtxt(char path[20])
{
    FILE *fp=fopen(path,"r");
    if(!fp)
        return -1;
    int i=1,k;
    while(strcmp(path,"StudentInfo.txt")==0 && i!=-1 &&  k!=-1)
    {
        struct stuInfo*stu1=(stuInfo*)malloc(sizeof(stuInfo));
        /*    i=fscanf(fp,"%ld %s %d %d %d %d %d %d %d %d %d %d %d %d",
        &stu1->num,stu1->name,&stu1->sex,&stu1->grade,&stu1->score[0],&stu1->score[1],&stu1->score[2],&stu1->score[3],
        &stu1->score[4],&stu1->score[5],&stu1->score[6],&stu1->score[7],&stu1->score[8],&stu1->score[9]);*/
        i=fscanf(fp,"%ld %s %d %d",&stu1->num,stu1->name,&stu1->sex,&stu1->grade);
        for(int m=0;m<10;m++)
            k=fscanf(fp,"%d",&stu1->score[m]);
        if(k==-1 || i==-1)
            //if(i==-1)
            free(stu1);
        else
        {
            stu1->pNext=s_pHead;
            s_pHead=stu1;
        }    
    }
    
    while(strcmp(path,"SexInfo.txt")==0 && i!=-1)
    {
        struct sexInfo*sex1=(sexInfo*)malloc(sizeof(sexInfo));
        i=fscanf(fp,"%d %s",&sex1->sex_num,sex1->sex);
        if(i==-1)
            free(sex1);
        else
        {
            sex1->e_pNext=sex_Head;
            sex_Head=sex1;
        }
    }
    
    while(strcmp(path,"CodeInfo.txt")==0 && i!=-1)
    {
        struct CodeInfo*code1=(CodeInfo*)malloc(sizeof(CodeInfo));
        i=fscanf(fp,"%d %s",&code1->code_num,code1->college);
        if(i==-1)
            free(code1);
        else
        {
            code1->c_pNext=code_Head;
            code_Head=code1;
        }
    }
    return 0;
}

void read()
{
    readtxt("StudentInfo.txt");
    readtxt("SexInfo.txt");
    readtxt("CodeInfo.txt");
}

void prf()
{
    struct stuInfo* stu1=s_pHead;
    
    while(stu1)
    {
        printf("num:%ld\tname:%s",stu1->num,stu1->name);
        int i=0;
        float sum=0.0;
        while(i<10)
            sum+=stu1->score[i++];
        sum/=10;
        printf("\tscore:%f",sum);
        struct sexInfo*sex1=sex_Head;
        while(sex1 && sex1->sex_num!=stu1->sex)
            sex1=sex1->e_pNext;
        printf("\tsex:%s",sex1->sex);
        struct CodeInfo*code=code_Head;
        while(code && code->code_num!=(stu1->grade+1))
            code=code->c_pNext;
        printf("\tgrade:%s\n",code->college);
        
        stu1=stu1->pNext;
    }
}

void sort_num()//大的数往0方向推
{
    struct stuInfo* stu1=s_pHead,*p2,*anchor,*now;
    int i=0,j;
    while(stu1)
    {
        i++;
        anchor=stu1;
        stu1=stu1->pNext;    
    }    
    while(i>=0)
    {
        j=0;
        stu1=s_pHead->pNext;
        if(s_pHead->num>stu1->num)
        {
            s_pHead->pNext=stu1->pNext;
            stu1->pNext=s_pHead;
            s_pHead=stu1;
        }
        stu1=s_pHead;
        while(j<i-2)
        {
            
            now=stu1->pNext;
            p2=now->pNext;
            //交换
            if(now->num>p2->num)
            {
                stu1->pNext=p2;
                now->pNext=p2->pNext;
                p2->pNext=now;
            }
            stu1=stu1->pNext;
            j++;
        }
        
        i--;
    }
}
const char*menu[]=
{
    "0.退出",
        "1.打印数据",
        "2.按学生学号排序",
        NULL
};
int menu_dis()
{
    int i=0;
    do
    {
        printf("\t\t%s\n",menu[i++]);
        
    }while(menu[i]!=NULL ||puts("输入选择")==1);
    
    return i;
}


void dis()
{
    int i,j;
    do
    {
        i=menu_dis();
        fflush(stdin);
        scanf("%d",&j);
        switch(j)
        {
        case 1:
            prf();
            break;
        case 2:
            sort_num();
            break;
        default:
            break;
            
        }
    }while( j<0 || j>i || j!=0 );
}
int main(int argc, char* argv[])
{
    read();
    dis();
    return 0;
}

2015-01-25 10:23
wengbin
Rank: 10Rank: 10Rank: 10
来 自:陕西西安
等 级:贵宾
威 望:19
帖 子:370
专家分:1846
注 册:2015-5-8
收藏
得分:0 
标记
2016-03-09 21:02
快速回复:【解剖麻雀】通过一道小型课题解答一些常见问题
数据加载中...
 
   



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

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