1.

1
#include<stdio.h>

int main()
{
    int a, b, c, d, e;
    // 红0 黄1 蓝2 白3 紫4
    for (a = 0; a < 5; a++)
    for (b = 0; b < 5; b++)
    for (c = 0; c < 5; c++)
    for (d = 0; d < 5; d++)
    for (e = 0; e < 5; e++)
    if (
        ((b == 4 )+ (d == 1)==1)
        && ((a == 0) + (e == 3) == 1)
        && ((b == 1) + (e == 4) ==1)
        && ((c == 3) + (d == 2) == 1)
        )printf("%d %d %d %d %d", a, b, c, d, e);
    return 0;
}

2
#include<stdio.h>
#include<string.h>
#define N 10
void sortString(char str[][50])
{
    char temp[];

    for(int i = 0; i < N; i ++)
        for(int j = 0; j < N-i-1; j ++)
            if(strcmp(str[j] , str[j+1]) < 0){
                strcpy(temp , str[j]);    
                strcpy(str[j] , str[j+1]);
                strcpy(str[j] , temp);
            }
}

void main()
{
    int i, k;
    char max[50];
    char str[N][50];
    
    for(i = 0; i < N; i ++)
        gets(str[i]);    
    
    strcpy(max , str[0]);
    for(i = 0; i < N; i ++)
        if(strcmp(max , str[i]) < 0){
            strcpy(max , str[i]);
            k = i;
        }
    
    sortString(str);
    
    printf("the position of maxstrin is%d\n", k);
}

3
#include<stdio.h>
#define MAX 100

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

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

void file_write(FILE *fp, struct student *s[], int n)
{
    int i;
    
    for(i = 0; i < n; i ++){
        fprintf(fp, "%s", s[i]->name);
        fprintf(fp, "%d", s[i]->num);
        for(k = 0; k < 4; k++)
            fprintf(fp, "%d ", s[i]->score[k]);
    }
}

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

 

posted @ 2016-12-29 23:46  xkfx  阅读(265)  评论(0编辑  收藏  举报