C语言 数组 结构体 typedef

来源:互联网 发布:私募排排数据库 编辑:程序博客网 时间:2024/06/09 14:23

//

//  main.c

//  C数组一维数组、二维数组、结构体  typedef:重命名

//

//  Created by  on 12-12-5.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#include <stdio.h>

#include <stdlib.h>

#include <time.h>


//定义结构体取结构体元素用 “.”   花括号后面 “;”不能省略

 //结构体在内存中所占的字节数:结构体在内存中所占字节数是所有成员字节数相加,若结果不为四的倍数,自动增加到四的倍数

 // 

//struct CGPoint {

//

//    float x;      

//    float y;

//    char a;

//

//}point2,point3;               //分号结束 定义结构体同时 声明 结构体变量 、、、、


int main(int argc, const char * argv[])

{


    // insert code here...


    

    //注意:

   //一维数组************************************

    

//    char str[9] = "IPHONE4s";

//    printf("%s\n",str);

//    

//    char str2[4] = {"abc"};

//    

//    printf("%lu\n",sizeof(str));

//    printf("%s",str2);

//    printf("输入五个数:\n");

//

//    int num[5];

//    int sum=0;

//    for (int i=0; i<5; i++) {

//        scanf("%d",&num[i]);

//    }

//    

//    for (int i=0; i<5; i++) {

//        sum += num[i];   

//    }

//    printf("sum == %d\n",sum);

//    for(int i=0; i<5; i++){

//        printf("%d  ",num[i]);

//        

//    }

//    num[4] = num[0]+num[1];

//    printf("%d",num[4]);

    

   //二维数组**************************

    

//    int array[3][4] ={{1,2,3},{4,5,6},{7,8,9}};

//    //for (int i=0; i<3;i++) {

//        for (int j=0; j<4;j++) {

//            //printf("array[%d][%d] === %d\n",i,j,array[i][j]);

//            array[2][j] = array[0][j]+array[1][j];

//            printf("array[2][%d] == %d\n",j,array[2][j]);

//        }

//    //}

//      

//    char str[3][255] = {"iPhone","android","symbin"};

//    for (int i=0; i<3; i++) {

//        printf("%s \n",str[i]);

//    }

//    //printf("%s",str);

//    

//    

//    //结构体******************************

//    struct CGPoint point = {19.3,11.1};  //定义结构体变量初始化 成员 

//    printf("x=%.2f,y=%.2f\n",point.x,point.y);

//    

//    struct CGPoint point1;              //先定义在初始化

//    point1.x = 1.3;

//    point1.y = 3.5;

//    printf("x=%.2f,y=%.2f\n",point1.x,point1.y);

//    

//    point2.x = 22.2;

//    point2.y = 888;

//    

//    point3.x = 33.3;

//    point3.y = 66.6;

//    

//    printf("x=%.2f,y=%.2f\n",point2.x,point2.y);

//    printf("x=%.2f,y=%.2f\n",point3.x,point3.y);

//    

//    //****************************** typedef重命名 ***************************

//    // typedef:重命名typedef struct CGPoint起名字为:newPoint 相当于 宏定义

//    

//    typedef struct CGPoint newPoint;  //重命名

//    newPoint point4 = {99.9,100.1};   //定义变量初始化 

//    printf("x=%.2f,y=%.2f\n",point4.x,point4.y);

//    

//    printf("%ld",sizeof(newPoint));  //result :12 实际为:12 4419不是4的倍数自动添加到12

//    

//    //结构体 数组

//  //  newPoint arr[3] = 

//    //strcpy()函数 可以解决 结构体 数组 初始化问题

    

    

    //数组练习

    

   //定义一个结构体,输出学生信息

    /*

    struct Student{

        int num;

        char *name;

        char sex;

        float score;

    }student1,student2={110,"Lisi",'M',89};

    

    student1.num = 100;

    student1.name = "Zhangsan";

    printf("input sex and score\n");

    scanf("%c %f",&student1.sex,&student1.score);

    //student2 = student1;

    printf("Number = %d\nName= %s\n",student1.num,student1.name);

    printf("Sex =%c\nScore=%f\n",student1.sex,student1.score);

    printf("Number = %d\nName= %s\n",student2.num,student2.name);

    printf("Sex =%c\nScore=%f\n",student2.sex,student2.score);

    

    //student1={110,"Lisi",'M',89};

    */

    

//    //作业

//    1(必做)、将一个数组中的值按逆序重新存放,例如原来的顺序为:86, 5, 4,1.要求改为:1,4,5,6,8;并将数组中的值输出。

//    2 (必做)、定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天?注意闰年问题。

//    3 (必做)、编写一个函数用冒泡法10个字符 reputation 按由小到大顺序排序。

//    4 (必做)、随机产生20[1050]的正整数存放到数组中,并求数组中的所有元素最大值、最小值、平均值及各元素之和。

//    5 (选做)、编写子函数:(1)用冒泡法将一个数组排成升序的函数---SUB1;(2)在升序数组中插入一个数,并且保持该数组仍为升序数组的函数---SUB2主函数:①输入任意10个正整数给数组;②调用SUB1对数组进行排序;③从键盘输入一个正整数,调用SUB2将其插入该数组。

   // 1、将一个数组中的按逆序重新排放,例如原来顺序为:86541.要求改为:14568并将数组中的值输出

    

    /*

    int a[5] = {8,6,5,4,1};

    int b[5] = {8,6,5,4,1};

   

    printf("变化之前:\n");

    for (int i=0; i<5; i++) {

        printf("a[%d]==%d\n",i,a[i]);

    }

    printf("变化之后:\n");

    for (int j=0,i=4; j<5; j++,i--) {

        a[j] = b[i];

        printf("a[%d] == %d\n",j,a[j]);

    }

    */

    

    /*

    

    //2、定义一个结构体变量(包括年、月、日)。计算改日在本年中是第几天?注意闰年问题

    

    struct Oneday

    {

        int year;

        int month;

        int day;

    }oneday;

    int flag = 0; //表示是否是闰年 1 表示是闰年 0 不是闰年

    int count = 0,m;    //表示该天在一年中是第几天 

    m = oneday.month - 1;

    printf("输入年月日:\n");

    scanf("%d%d%d",&oneday.year,&oneday.month,&oneday.day);

    //判断是否是闰年

    if (oneday.year %100 ==0) 

    {

        if(oneday.year % 4 == 0)

        {

            flag = 1;

        }

    }else {

        if(oneday.year % 4 == 0)

        {

            flag = 1;

        }

    }

    

    count = m*30+oneday.day+(m/2+m%2); //二月当做 30计算count

    if(m>=2)

    {

        if (flag) //闰年时,二月为29天,所有减去一天

        {

            count--;

        }else {

            count-=2; //不是闰年二月为 28天,减去两天

        }

    

    }

    

    printf("count == %d",count);

    */

    

    // 3 (必做)、编写一个函数用冒泡法10个字符 reputation 按由小到大顺序排序。

    /*

    char array[]={"reputation"};

    for (int i=0; i<10; i++) {

        printf("%c ",array[i]);

    }

    //排序从小到大

    for (int i=0; i<=8; i++) {         //循环九次

        for (int j=i+1; j<=9-i; j++) {

            char temp;

            if (array[i]>array[j]) {

                temp = array[i];

                array[i] = array[j];

                array[j] = temp;

            }

        }

    }

    printf("从小到大排序后结果:\n");

    for (int i=0; i<10; i++) {

        printf("%c ",array[i]);         //result: a e i r t u t p o n 

    }

     */

   //4 (必做)、随机产生20[1050]的正整数存放到数组中,并求数组中的所有元素最大值、最小值、平均值及各元素之和。

    /*

    int array[20];

    for (int i=0; i<20; i++) 

    {

        array[i] = rand()%41+10;  //产生随机数 511010不包括51

    }

    int max = array[0],min = array[0],sum = 0,avg = 0;

    for (int i=0; i<20; i++) {

        if(array[i]>=max)

        {

            max = array[i];

        }

        if (array[i]<=min) 

        {

            min = array[i];

        }

        sum += array[i];

    }

    avg = sum/20;

    

    printf("The max= %d\nThe min = %d\nThe sum=%d\nThe avg=%d",max,min,sum,avg);

    */

    

   //5、编写子函数:(1)用冒泡法将一个数组排成升序的函数---SUB1;(2)在升序数组中插入一个数,并且保持该数组仍为升序数组的函数---SUB2主函数:①输入任意10个正整数给数组;②调用SUB1对数组进行排序;③从键盘输入一个正整数,调用SUB2将其插入该数组。

    /*

    int array[10];

    printf("输入10个数初始化数组:\n");

    for (int i=0; i<10; i++) {

        scanf("%d",&array[i]);

    }

    void Sub1(int a[]); //函数声明

    Sub1(array);        //函数调用

     */

    

    return 0;

    

}


/*

void Sub1(int a[]){

    int temp ;

    for (int i=0; i<=8; i++) 

    {

        for (int j=0; j<9-i; j++) 

        {

            if (a[j]<a[j+1]) 

            {

                temp = a[j];

                a[j] = a[j+1];

                a[j+1] = temp;

            }

        }

        

    }

    printf("升序之后的数组为:\n");

    for (int i=0; i<10; i++)

    {

        printf("a[%d] == %d\n",i,a[i]);

    }

}


void Sub2(int a[],int num)

{

    int temp ;

    for (int i=0; i<=8; i++) 

    {

        for (int j=0; j<9-i; j++) 

        {

            if (a[j]<a[j+1]) 

            {

                temp = a[j];

                a[j] = a[j+1];

                a[j+1] = temp;

            }

        }

        

    }

    

    for (int i=0; i<10; i++) {

        if (num>a[i]) {

            a[i] = num;

        }

    }


}

*/



原创粉丝点击