2014年8月16日 星期六

(綠色皮) 最新C程式語言, 施威銘研究室 著, 旗標 第十五章 '參考' 答案

==============================================
/* 程式檔名:test_15-44-1*/
/* 程式目的:宣告一個包含年月日三個成員的結構體,並設定今天日期為初值,以年/月/日的格式從螢幕輸出*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400813*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    time_t timep;
    struct tm *p;
    time(&timep);
    p=localtime(&timep);
    printf("%d年/%d月/%d日\n",p->tm_year-10,p->tm_mon+1,p->tm_mday);
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_15-44-2*/
/* 程式目的:請將年月日三個結構體成員的數值改由鍵盤輸入*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400813*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

struct timer
{
    int year;
    int mon;
    int mday;
};

int main(int argc, char *argv[])
{
    struct timer tt;
    do
    {
        printf("輸入年:");
        scanf("%d",&tt.year);
        printf("輸入月:");
        scanf("%d",&tt.mon);
        printf("輸入日:");
        scanf("%d",&tt.mday);
    }while(tt.mon > 12 || tt.mday > 31);
    printf("%d年/%d月/%d日\n",tt.year,tt.mon,tt.mday);
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_15-44-3*/
/* 程式目的:請加入一新成員表示星期*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400813*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

struct timer
{
    int year;
    int mon;
    int mday;
    char weekday[7];
};

int main(int argc, char *argv[])
{
    struct timer tt;
    do
    {
        printf("輸入年:");
        scanf("%d",&tt.year);
        printf("輸入月:");
        scanf("%d",&tt.mon);
        printf("輸入日:");
        scanf("%d",&tt.mday);
        printf("輸入星期:");
        scanf("%s",&tt.weekday);
    }while(tt.mon > 12 || tt.mday > 31);
    printf("%d年/%d月/%d日/星期%s\n",tt.year,tt.mon,tt.mday,tt.weekday);
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_15-44-4*/
/* 程式目的:儲存5台電視資料,從螢幕輸出這些資料*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400813*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

struct Screen
{
    char Screen_name[10];
    int Screen_size;
    int Screen_year;
    int Screen_Price;
}TV[5]={"AmTRAN",48,104,10000,
        "  BENQ",52,103,20000,
        "CHIMEI",60,104,30000,
        " HERAN",38, 99, 6000,
        " KOLIN",32, 95, 2000};


int main(int argc, char *argv[])
{
    int i;
    printf("\n");
    printf("螢幕品牌   螢幕大小   螢幕年分   螢幕售價\n");
    for(i=0;i<5;i++)
    {
        printf("  %s        %d         %3d      %5d\n",TV[i].Screen_name,TV[i].Screen_size,TV[i].Screen_year,TV[i].Screen_Price);
    }
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s\n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_15-44-5*/
/* 程式目的:宣告一個除法的結構體,從鍵盤輸入除數與被除數 輸出商與魚數*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400813*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

struct Screen
{
    int Divisor;
    int Divisorend;
    int Trader;
    int Remainder;
};

int main(int argc, char *argv[])
{
    struct Screen dd;
    printf("輸入除數:");
    scanf("%d",&dd.Divisor);
    printf("輸入被除數:");
    scanf("%d",&dd.Divisorend);
    
    printf("商:%d\n",dd.Trader=dd.Divisorend / dd.Divisor);
    printf("餘:%d\n",dd.Remainder = dd.Divisorend % dd.Divisor);
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s\n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_15-44-6*/
/* 程式目的:輸入員工資料結果螢幕輸出*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400814*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

struct staff
{
    char staff_Name[10];
    int staff_Num;
    int staff_Salary;
    int staff_Work;
};


int main(int argc, char *argv[])
{
    struct staff Employees[10];
    
    int i;
    for(i=0;i<2;i++)
    {
        printf("員工編號:");
        scanf("%d",&Employees[i].staff_Num);    
        printf("員工名子:");
        scanf("%s",&Employees[i].staff_Name); 
        printf("基本時薪:");
        scanf("%d",&Employees[i].staff_Salary);
        printf("工作時數:");
        scanf("%d",&Employees[i].staff_Work);
    }
    
    for(i=0;i<2;i++)
    {
        printf("\n");
        printf("員工編號:%5d 員工名子:%s 基本時薪:%5d 工作時數:%5d 月收入:%5d",Employees[i].staff_Num,Employees[i].staff_Name,
                                                                  Employees[i].staff_Salary,Employees[i].staff_Work,Employees[i].staff_Salary*Employees[i].staff_Work);
    }
    printf("\n");
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s\n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_15-44-7*/
/* 程式目的:預先輸入10筆資料,並讓程式提供以編號查詢菜名與售價的功能*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400814*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

struct Menu
{
    int Menu_num;
    char Menu_name[10];
    int Menu_Price;

}aa[11]={0,"離開",0,
         1,"烏龍麵",40,
         2,"炒飯",60,
         3,"蛋炒飯",50,
         4,"拉麵",40,
         5,"滷肉飯",30,
         6,"控肉飯",40,
         7,"火鍋",100,
         8,"蛋花湯",30,
         9,"水餃",4,
         10,"牛肉麵",100};


int main(int argc, char *argv[])
{
    int i,j;
    while(1)
    {
        printf("請輸入查詢菜單編號1~10(輸入0為離開)(輸入99顯示全部菜單):");
        scanf("%d",&i);
        if(i==0)
        {
                break;
        }
        else if(i==99)
        {
                for(j=1;j<=10;j++)
                {
                 printf("編號:%d 菜名:%s 價錢:%d \n",aa[j].Menu_num,aa[j].Menu_name,aa[j].Menu_Price);
                }
        }
        else if(i>10)
        {
                printf("輸入錯誤\n");
        }
        else
        {
                printf("編號:%d 菜名:%s 價錢:%d \n",aa[i].Menu_num,aa[i].Menu_name,aa[i].Menu_Price);
        }
    }
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s\n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_15-44-8*/
/* 程式目的:請加入一輸入所有菜單的功能,輸出的菜單需要根據售價由高到低排序*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400815*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);
struct Menu
{
    int Menu_num;
    char Menu_name[10];
    int Menu_Price;

}aa[11]={0,"離開",0,
         1,"烏龍麵",40,
         2,"炒飯",60,
         3,"蛋炒飯",50,
         4,"拉麵",40,
         5,"滷肉飯",30,
         6,"控肉飯",40,
         7,"火鍋",100,
         8,"蛋花湯",30,
         9,"水餃",4,
         10,"牛肉麵",100};


int main(int argc, char *argv[])
{
    int i,j,k;
    while(1)
    {
        printf("請輸入查詢菜單編號1~10(輸入0為離開)(輸入99顯示全部菜單):");
        scanf("%d",&i);
        if(i==0)
        {
                break;
        }
        else if(i==99)
        {
                int temp;
                char tempname[10];
                for(j=1;j<=10;j++)
                {
                 for(k=1;k<=10;k++)
                 {
                  if(aa[j].Menu_Price > aa[k].Menu_Price)
                  {
                   temp=aa[j].Menu_Price;
                   aa[j].Menu_Price=aa[k].Menu_Price;
                   aa[k].Menu_Price=temp;
                   
                   strcpy(tempname,aa[j].Menu_name);
                   strcpy(aa[j].Menu_name,aa[k].Menu_name);
                   strcpy(aa[k].Menu_name,tempname);
                  }
                 }                 
                }
                for(j=1;j<=10;j++)
                {
                 printf("編號:%d 菜名:%s 價錢:%d \n",aa[j].Menu_num,aa[j].Menu_name,aa[j].Menu_Price);
                }
        }
        else if(i>10)
        {
                printf("輸入錯誤\n");
        }
        else
        {
                printf("編號:%d 菜名:%s 價錢:%d \n",aa[i].Menu_num,aa[i].Menu_name,aa[i].Menu_Price);
        }
    }
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}

void Pause(const char* message)
{
    printf("%s\n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_15-44-9*/
/* 程式目的:結構體變數的初始值,並將結果顯示在螢幕上*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400816*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

struct DVD
{
    int DVD_num;
    char DVD_name[10];
    int DVD_data;
    int DVD_redata;
};

struct Menu
{
    struct DVD DD;
    char Menu_name[10];
    int Menu_phone;

}ss[3]={1,"恐怖片",20140802,20140809,"王小明",02-12345678,
        2,"驚悚片",20140809,20140816,"陳小美",02-23456789,
        3,"動作片",20140816,20140823,"吳阿明",02-34567890};


int main(int argc, char *argv[])
{
    
    int i;
    char name[10];
    printf("請輸入想查詢的姓名:");
    scanf("%s",&name);
    for(i=0;i<3;i++)
    {
        if(strcmp(name,ss[i].Menu_name)==0)
        {
                printf("%d %s %d %d %s %d",ss[i].DD.DVD_num,ss[i].DD.DVD_name,ss[i].DD.DVD_data,ss[i].DD.DVD_redata,ss[i].Menu_name,ss[i].Menu_phone);
                printf("\n");
        }
        else
        {
                printf("輸入錯誤\n");
                break;
        }
    }
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}

void Pause(const char* message)
{
    printf("%s\n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_15-44-10*/
/* 程式目的:結構體變數的初始值,並將結果顯示在螢幕上*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400816*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);

struct Transport
{
    char sport1[10];
    char sport2[10];
};

struct Menu
{
    char Menu_name[10];
    struct Transport Tt;

}ss[3]={"車","汽車","火車",
        "船","輪船","帆船",
        "飛機","直升機","客機"};


int main(int argc, char *argv[])
{
    int i;
    printf("------交通工具------\n");
    for(i=0;i<3;i++)
    {
        printf("%6s %6s %6s\n",ss[i].Menu_name,ss[i].Tt.sport1,ss[i].Tt.sport2);
    }
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}

void Pause(const char* message)
{
    printf("%s\n", message);
    rewind(stdin);
    getchar(); 

}
==============================================

沒有留言:

張貼留言