结构安排在C [英] Structure arrange in c

查看:105
本文介绍了结构安排在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了什么样的程序进入扫描学生和相关信息,并从中结构,使。我需要安排这些学生进入出生年份和姓。我不知道从哪里开始,也许你可以给我一些建议。

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&CONIO.H GT;
周一结构
{
 焦炭名[15]; / * *vārds/
    焦炭LNAME [15]; / * *uzvārds/
 INT日,月,年; / * *基准/
 焦炭性别[15]; / * * dzimums /
};
INT主要(无效)
{
 结构问毫米[5];
 INT N; / * elementu skaitsmasīvā* /
 INT I,J;
 为(n = 0的; N 2; N ++)
 {
  的printf(%d。输入:姓名,性别,日期,性别>中,N + 1);
  scanf函数(%S,毫米[N]。名称);
      如果(!STRCMP(毫米[N] .name和***))打破;
        scanf函数(%S,毫米[N] .lname);
  如果(!STRCMP(毫米[N] .lname,?!))破;
  scanf函数(%D /%D /%D,&安培;毫米[N]·天,和放大器;毫米[N] .month,&安培;毫米[N] .year);
        scanf函数(%S,毫米[N] .gender);
  如果(!STRCMP(毫米[N] .gender,***))打破;
 }
 的printf(----------------------------------------------- -------------- \\ n);
 的printf(|学生名单| \\ n);
 的printf(| ---------------------------------------------- --------------- | \\ n);
 的printf(|名称|姓|日期|性别| \\ n);
 的printf(| --------- | --------- | ------------- | --------- | \\ n );
 对于(i = 0; I< N;我++)
 的printf(|%7S |%7S |%D-%D-%d个|%7S | \\ n,毫米[I] .name和毫米[I] .lname,毫米[I]·天,毫米[我] .month,毫米[I] .year,毫米[I] .gender);
 残培();
 返回0;
}


解决方案

最好的选择,阅读的qsort() 并使用它。

第二选择:排序

  INT排序;
做{
  排序= 1;
  对于(i = 1; I< N;我++){
    如果(毫米[I-1] .year>毫米[I] .year){
      周一结构温度;
      临时=毫米[I-1];
      毫米[I-1] =毫米[I];
      毫米[I] =温度;
      排序= 0;
    }
  }
}而(!排序);

这只是排序按年,左为OP添加姓氏的一部分。

I have made program what scans entered students and information about them and make from it structure. And i need to arrange these entered students by birth year and surname. I dont know from where to start maybe you can give me some advice.

#include <stdio.h>
#include <string.h>
#include <conio.h>
struct mon 
{
 char name[15];           /* vārds*/
    char lname[15];          /* uzvārds*/
 int day,month,year;      /* datums*/
 char gender[15];         /* dzimums*/
};
int main(void) 
{
 struct mon mm[5];
 int n;                   /* elementu skaits masīvā*/
 int i, j;
 for (n=0; n<2; n++)
 {
  printf("%d. Enter: name, gender, date, gender >", n+1);
  scanf("%s",mm[n].name);
      if (!strcmp(mm[n].name,"***")) break;
        scanf("%s",mm[n].lname);
  if (!strcmp(mm[n].lname," ,?!")) break;
  scanf("%d/%d/%d",  &mm[n].day, &mm[n].month, &mm[n].year);
        scanf("%s",mm[n].gender);
  if (!strcmp(mm[n].gender,"***")) break;
 }
 printf("-------------------------------------------------------------\n");
 printf("| Student  List |\n");
 printf("|-------------------------------------------------------------|\n");
 printf("|  Name  | Surname |    Date   | Gender |\n");
 printf("|---------|---------|-------------|---------|\n");
 for (i=0; i<n; i++)
 printf("| %7s | %7s | %d-%d-%d  | %7s |\n", mm[i].name,mm[i].lname,mm[i].day, mm[i].month, mm[i].year,mm[i].gender);
 getch();
 return 0;
}

解决方案

Best choice, read about qsort() and use it.

2nd choice: sort by

int sorted;
do {
  sorted = 1;
  for (i=1; i<n; i++) {
    if (mm[i-1].year > mm[i].year) {
      struct mon temp;
      temp = mm[i-1];
      mm[i-1] = mm[i];
      mm[i] = temp;
      sorted = 0;
    }
  }
} while (!sorted);

This only sorts by year, left the surname part for OP to add.

这篇关于结构安排在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆