C語言第九周作業(指標變數,記憶體訪問,取址,空指標)

初與久歌2020發表於2020-11-03

1.

3.
(a)
#include <stdio.h>

int secs(int,int,int);

int main(){
    int h,m,s;
    printf("enter a time:");
    scanf("%d:%d:%d",&h,&m,&s);
    printf("the seconds are %d",secs(h,m,s));
}
int secs(int h,int m,int s){
    int total;
    total = h*3600+m*60+s;
    return total;
}

在這裡插入圖片描述

(b)
#include <stdio.h>

int total;
int secs(int,int,int,int *);

int main(){
    int h,m,s;
    int totsec;
    int *p=&totsec;

    printf("enter a time:");
    scanf("%d:%d:%d",&h,&m,&s);
    printf("the seconds are %d",secs(h,m,s,p));
}
int secs(int h,int m,int s,int *p){
    *p= h*3600+m*60+s;

    return *p;
}

在這裡插入圖片描述

4.
#include <stdio.h>

void time(int,int*,int*,int*);

int main(){
    int second;
    printf("enter seconds:");
    scanf("%d",&second);

    int hours,min,sec;
    int *h,*m,*s;
    h=&hours,m=&min;s=&sec;
    time(second,h,m,s);
    printf("the time is %02d:%02d:%d",hours,min,sec);

    return 0;
    
}
void time(int second,int *h,int *m,int *s){
    *h = second/3600;
    *m = (second - *h * 3600)/60;
    *s = second - (*m *60 + *h *3600);
}

在這裡插入圖片描述

6.
#include <stdio.h>

void date(int,int*,int*,int*);

int main(){
    int riqi,year,month,day;
    int *y=&year,*m=&month,*d=&day;
    printf("enter a date:");
    scanf("%d",&riqi);

    date(riqi,y,m,d);
    printf("the date is %04d/%02d/%02d\n",year,month,day); 
    printf("the date is %04d/%02d/%02d\n",*y,*m,*d);

}
void date(int riqi,int *y,int *m,int *d){
    *y = riqi/10000;
    riqi %=10000;
    *d = riqi%100;
    *m = riqi/100;
}

在這裡插入圖片描述

2.

1.

(a)(g)

2.

(e) (i)

3.

#include <stdio.h>

void larger_of(double*,double*);

int main(){
    double i,j;
    printf("enter two numbers:");
    scanf("%lf %lf",&i,&j);

    double *a,*b;
    a=&i,b=&j;
    printf("%p %p\n",a,b);
    larger_of(a,b);
    printf("%lf %lf",i,j);

    return 0;
}
void larger_of(double *a,double *b){
    if(*a>*b){
        *b=*a;
    }else{
        *a=*b;
    }
}

在這裡插入圖片描述

4.

5.
void split_time(long total_sec,int *hr, int *min, int *sec){
    *hr = total_sec/3600.0;
    *min = total_sec/60.0;
    *sec = total_sec;
}
7.

相關文章