南師大複試編程訓練八

71.定義一個結構體變量(包括年、月、日),輸入年、月、日,計算該日是本年中第幾天

#include <stdio.h>

struct y_m_d  //年月日結構體
{
    int year;
    int month;
    int day;
};

int days(int year,int month,int day)  //求天數
{
    int day_sum=0,i;
    int day_tab[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
    for(i=1; i<month; i++)
        day_sum+=day_tab[i];
    day_sum+=day;
    if(((year%4==0&&year%100!=0)||year%400==0)&&month>=3)  //判斷閏年
        day_sum+=1;
    return day_sum;
}

int main()
{
    struct y_m_d date;
    int day_sum;
    scanf("%d %d %d",&date.year,&date.month,&date.day);
    day_sum=days(date.year,date.month,date.day);
    printf("該日是第%d天\n",day_sum);
    return 0;
}

運行結果:

72.有3個學生,每個學生的數據包括學號、姓名和3門課成績,打印3門課平均成績,以及最高分的學生的成績(包括學號、姓名、3門課成績、平均分數)

#include <stdio.h>
#define n 3
struct Student   //學生結構體
{
    long num;
    char name[8];
    int score[3];
    float avg;
} stu[n];
int main()
{
    int i,j,maxi=0;
    float sum,max=0;
    for(i=0; i<n; i++)   //對n個學生成績處理
    {
        sum=0;
        scanf("%ld %s ",&stu[i].num,stu[i].name);
        for(j=0; j<3; j++)
        {
            scanf("%d",&stu[i].score[j]);
            sum+=stu[i].score[j];
        }
        if(sum>max)
        {
            max=sum;
            maxi=i;
        }
        stu[i].avg=sum/3.0;
    }

    printf("打印學生信息:\n");
    for(i=0; i<n; i++)
    {
        printf("%ld %s ",stu[i].num,stu[i].name);
        for(j=0; j<3; j++)
            printf("%d ",stu[i].score[j]);
        printf("%.2f\n",stu[i].avg);
    }

    printf("打印平均成績最高的學生信息:\n");
    printf("%ld %s ",stu[maxi].num,stu[maxi].name);
    for(j=0; j<3; j++)
        printf("%d ",stu[maxi].score[j]);
    printf("%.2f\n",stu[maxi].avg);
    return 0;
}

運行結果:

73.交換(指針)

#include <stdio.h>

int main()
{
    int *p1,*p2,a,b,*p;
    scanf("%d %d",&a,&b);
    p1=&a;
    p2=&b;
    if(a<b)   //a,b的值未變;p1,p2中保存的地址變了,所以指向的值自然就變了
    {
        p=p1;
        p1=p2;
        p2=p;
    }
    printf("a=%d,b=%d\n",a,b);
    printf("max=%d,min=%d\n",*p1,*p2);
    return 0;
}

運行結果:

74.交換二(指針)

#include <stdio.h>

void swap(int *p1,int *p2)   //交換的是地址中的值,注意和上面a,b值的區別
{
    int temp;
    temp=*p1;
    *p1=*p2;
    *p2=temp;
}
int main()
{
    int *p1,*p2,a,b;
    scanf("%d %d",&a,&b);
    p1=&a;
    p2=&b;
    swap(p1,p2);
    printf("a=%d,b=%d\n",a,b); 
    printf("max=%d,min=%d\n",*p1,*p2);
    return 0;
}

運行結果:

75.輸入三個數啊a,b,c,按由大到小的順序將它們輸出。

#include <stdio.h>

void swap(int *p1,int *p2)  //交換地址中的值
{
    int temp;
    temp=*p1;
    *p1=*p2;
    *p2=temp;
}

void exchange(int *p1,int *p2,int *p3)
{
    if(*p1<*p2)
        swap(p1,p2);
    if(*p1<*p3)
        swap(p1,p3);
    if(*p2<*p3)
        swap(p2,p3);
}

int main()
{
    int *p1,*p2,*p3,a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    p1=&a;
    p2=&b;
    p3=&c;
    exchange(p1,p2,p3);  //傳址
    printf("a=%d,b=%d,c=%d\n",a,b,c);
    printf("*p1=%d,*p2=%d,*p3=%d\n",*p1,*p2,*p3);
    return 0;
}

運行結果:

76.數組指針

#include <stdio.h>

int main()
{
    int a[10]= {0,1,2,3,4,5,6,7,8,9};
    int *p;
    printf("一維數組中的值:\n");
    for(p=a; p<a+10; p++)    //一維數組指針應用
        printf("%d ",*p);
    printf("\n");

    int b[3][4]= {1,3,5,7,9,11,13,15,17,19,21,23};
    int *q;
    printf("二維數組中的值:");
    for(q=b[0]; q<b[0]+12; q++)  //二維數組,線性存儲
    {
        if((q-b[0])%4==0)
            printf("\n");
        printf("%4d ",*q);
    }
    printf("\n");

    return 0;
}

運行結果:

77.將3*3矩陣轉置(指針)

#include <stdio.h>

void moved(int *pointer)
{
    int i,j,t;
    for(i=0; i<3; i++)    //對矩陣進行轉置
        for(j=0; j<i; j++)
        {
            t=*(pointer+3*i+j);    //二維數組的指針表示法
            *(pointer+3*i+j)=*(pointer+3*j+i);
            *(pointer+3*j+i)=t;
        }
}
int main()
{
    int a[3][3],*p,i,j;
    for(i=0; i<3; i++)       //輸出原矩陣
        for(j=0; j<3; j++)
            scanf("%d",&a[i][j]);
    p=&a[0][0];    //二維數組的首地址
    moved(p);   //轉置
    printf("轉置之後的矩陣爲:\n");  //輸出轉置矩陣
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            printf("%d ",a[i][j]);
        printf("\n");
    }
    return 0;
}

運行結果:

78.將n個數按輸入時順序的逆序排列(指針)

#include <stdio.h>

void sort(char *p,int m)
{
    int i;
    char temp,*p1,*p2;
    for(i=0; i<m/2; i++)
    {
        p1=p+i;
        p2=p+(m-1-i);
        temp=*p1;
        *p1=*p2;
        *p2=temp;
    }
}
int main()
{
    int i,n;
    char *p,num[20];
    scanf("%d",&n);
    getchar();    //char數組用scanf輸入能接收回車
    for(i=0; i<n; i++)
        scanf("%c",&num[i]);
    p=&num[0];
    sort(p,n);
    for(i=0; i<n; i++)
        printf("%c",num[i]);
    printf("\n");
    return 0;
}

運行結果:

79.自己寫個strcmp函數,實現兩個字符串的比較(指針)

#include <stdio.h>

int strcmp(char *p1,char *p2)
{
    int i;
    i=0;
    while(*(p1+i)==*(p2+i))  //進行比較。若相等則繼續
        if(*(p1+i++)=='\0')
            return 0;
    return (*(p1+i)-*(p2+i));  //若兩字符不相等,則返回結果
}
int main()
{
    char str1[20],str2[20],*p1,*p2;
    gets(str1);
    gets(str2);
    p1=&str1[0];
    p2=&str2[0];
    printf("Result:%d",strcmp(p1,p2));
    printf("\n");
    return 0;
}

運行結果:

80.輸入月份號,輸出該月的英文名字(指針)

#include <stdio.h>

int main()
{   //month_name[13][20]相同作用
    char *month_name[13]= {"illegal month","January","February","March","April","May","June","July","August","September","October","November","December"};
    int n;
    scanf("%d",&n);
    if(n>=1&&n<=12)
        printf("It is %s",*(month_name+n));
    printf("\n");
    return 0;
}

運行結果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章