首页 > 代码库 > c语言练习

c语言练习

技术分享

#include<stdio.h>

struct student{
    char name[20];
    int No;
    int sub[4];
    int sum;
};

int strcmp(char *s, char *t)
{
    int i;
    
    for(i = 0; s[i] == t[i]; i ++)
        if(s[i] == \0)
            return 0;
    return s[i] - t[i];
}

int main()
{
    struct student stu[10];
    struct student sort_byname[10], sort_byNo[10], sort_bysum[10];
    int i, j, a, b, c;
    int last_three;
    
    // open file
    FILE *fp;
    fp = fopen("info.dat" , "r");
    
    // operate file
    i = a = b = c = 0;
    while(!feof(fp)){
        fscanf(fp, "%s %d %d %d %d %d", &stu[i].name, &stu[i].No, &stu[i].sub[0], &stu[i].sub[1], &stu[i].sub[2], &stu[i].sub[3]);
        // printf("%s %d %d %d %d %d\n", stu[i].name, stu[i].No, stu[i].sub[0], stu[i].sub[1], stu[i].sub[2], stu[i].sub[3]);
        last_three = stu[i].No % 1000;
        // printf("%d\n", last_three);
        if(last_three%3 == 2)
            sort_byname[a++] = stu[i];
        else if(last_three%3 == 1)
            sort_byNo[b++] = stu[i];
        else if(last_three%3 == 0)
            sort_bysum[c++] = stu[i];
        i++;
    }
    
    for(int k = 0; k < i; k++)
        for(int l = 0; l < 4; l++)
            stu[k].sum += stu[k].sub[l];
        
    //printf("%s\n", sort_byname[1].name);
    // printf("x = %d\n", strcmp("abcd" , "zabcd"));
    // close file
    fclose(fp);
    
    struct student temp;
    for(i = 0; i < a; i++)
        for(j = 0; j < a-i-1; j++)
            if(strcmp(sort_byname[j].name , sort_byname[j+1].name) > 0){
                temp = sort_byname[j];
                sort_byname[j] = sort_byname[j+1];
                sort_byname[j+1] = temp;
            }
            
    for(i = 0; i < b; i++)
        for(j = 0; j < b-i-1; j++)
            if(sort_byNo[j].No > sort_byNo[j].No){
                temp = sort_byname[j];
                sort_byname[j] = sort_byname[j+1];
                sort_byname[j+1] = temp;
            }
    
    for(i = 0; i < c; i++)
        for(j = 0; j < c-i-1; j++)
            if(sort_bysum[j].sum > sort_bysum[j].sum){
                temp = sort_byname[j];
                sort_byname[j] = sort_byname[j+1];
                sort_byname[j+1] = temp;
            }
    
    // open file
    FILE *fp2;
    fp2 = fopen("infosort.dat" , "w");
    
    // operate file
    fprintf(fp2, "sort_byname:\n");
    for(i = 0; i < a; i++)
        fprintf(fp2, "%s %d %d %d %d %d\n", sort_byname[i].name, sort_byname[i].No, sort_byname[i].sub[0], sort_byname[i].sub[1], sort_byname[i].sub[2], sort_byname[i].sub[3]);
    
    fprintf(fp2, "sort_byNo:\n");
    for(i = 0; i < b; i++)
        fprintf(fp2, "%s %d %d %d %d %d\n", sort_byNo[i].name, sort_byNo[i].No, sort_byNo[i].sub[0], sort_byNo[i].sub[1], sort_byNo[i].sub[2], sort_byNo[i].sub[3]);
    
    fprintf(fp2, "sort_bysum:\n");
    for(i = 0; i < c; i++)
        fprintf(fp2, "%s %d %d %d %d %d\n", sort_bysum[i].name, sort_bysum[i].No, sort_bysum[i].sub[0], sort_bysum[i].sub[1], sort_bysum[i].sub[2], sort_bysum[i].sub[3]);
    
    // close file
    fclose(fp2);
    return 0;
}

 注释是在写的中途的测试

 

以下是由于没写测试写崩的代码(没有正确结果):

#include<stdio.h>
#define MAX 100

struct student{
    char name[20];
    int num;
    int score[4];
    int sum;
};

int main()
{
    struct student stu[MAX];
    int i, j, k;
    struct student sort_byname[MAX], sort_bynum[MAX], sort_bysum[MAX];
    int a, b, c; // 统计各类学生的数目
    
    // 1-从文件info.dat读入数据并分类
    FILE *fp;
    fp = fopen("info.dat" , "r");
    
    i = a = b = c = 0;
    while(!feof){
        fscanf(fp , "%s", &stu[i].name);
        fscanf(fp , "%d", &stu[i].num);
        for(k = 0; k < 4; k++){
            fscanf(fp, "%d", &stu[i].score[k]);
            stu[i].sum += stu[i].score[k]; // 统计总分
        }
        // 分类
        if(stu[i].num%3 == 2)
            sort_byname[a++] = stu[i];
        if(stu[i].num%3 == 1)
            sort_bynum[b++] = stu[i];
        if(stu[i].num%3 == 0)
            sort_bysum[c++] = stu[i];
        i++;
    }
    
    fclose(fp);
    
    // 2-对数据进行排序
    int strcmp(char *s, char *t);
    void swap(struct student *s, struct student *t);
    
    for(i = 0; i < a; i ++)
        for(j = 0; j < a-i-1; j ++)
            if(strcmp(sort_byname[j].name , sort_byname[j+1].name) > 0)
                swap(&sort_byname[j] , &sort_byname[j+1]);
            
    for(i = 0; i < b; i ++)
        for(j = 0; j < b-i-1; j ++)
            if(sort_bynum[j].num > sort_bynum[j+1].num)
                swap(&sort_bynum[j] , &sort_bynum[j+1]);
            
    for(i = 0; i < c; i ++)
        for(j = 0; j < c-i-1; j ++)
            if(sort_bynum[j].sum > sort_bynum[j+1].sum)
                swap(&sort_bysum[j] , &sort_bysum[j+1]);
            
    // 3-将数据写入 infosort.dat 以下可以用函数实现。。。。。。
    fp = fopen("infosort.dat" , "w");
    
    fprintf(fp, "sort_byname:\n");
    for(i = 0; i < a; i ++){
        fprintf(fp , "%s ", sort_byname[i].name);
        fprintf(fp , "%d ", sort_byname[i].num);
        for(k = 0; k < 4; k++)
            fprintf(fp, "%d ", sort_byname[i].score[k]);
    }
    fprintf(fp, "sort_bynum:\n");
    for(i = 0; i < b; i ++){
        fprintf(fp , "%s ", sort_bynum[i].name);
        fprintf(fp , "%d ", sort_bynum[i].num);
        for(k = 0; k < 4; k++)
            fprintf(fp, "%d ", sort_bynum[i].score[k]);
    }
    fprintf(fp, "sort_bysum:\n");
    for(i = 0; i < c; i ++){
        fprintf(fp , "%s ", sort_bysum[i].name);
        fprintf(fp , "%d ", sort_bysum[i].num);
        for(k = 0; k < 4; k++)
            fprintf(fp, "%d ", sort_bysum[i].score[k]);
    }
    
    fclose(fp);
    return 0;
}

int strcmp(char *s, char *t)
{
    int i;
    
    for(i = 0; s[i] == t[i]; i ++)
        if(s[i] == \0)
            return 0;
    return s[i] - t[i];
}

void swap(struct student *s, struct student *t)
{
    struct student temp;
    
    temp = *s;
    *s = *t;
    *t = temp;
}

测试文件:

wang 12301 67.5 78.9 88.0 99.2
zhang 12302 89.7 45.6 78.8 85.7
tang 12303 56.6 77.7 89.9 85.7
li 12304 56.6 45.6 56.6 85.7
ren 12305 99.9 77.7 89.9 85.7
fa 12306 44.4 55.5 56.6 85.7
compu 12307 56.6 77.7 89.9 85.7

 

c语言练习