2014年8月24日 星期日

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

==============================================
 /* 程式檔名:test_16-27-1*/
/* 程式目的:溫度度量衡的轉換 請以一新的自訂資料型別來宣告*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400820*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void Pause(const char* message);

union data
{
    int cha;
    float C;
    float F;      
      
}mydata;

int main(int argc, char *argv[])
{
    printf("度量衡轉換 (1) C->F (2) F-C :");
    scanf("%d",&mydata.cha);
    switch(mydata.cha)
    {
     case 1:
            {
             printf("請輸入攝氏溫度:");
             scanf("%f",&mydata.C); 
             printf("華氏溫度:%.2f\n",mydata.F=mydata.C*9/5+32);
             break;         
            }                  
     case 2:
            {
             printf("請輸入華氏溫度:");
             scanf("%f",&mydata.cha);                  
             printf("攝氏溫度:%.2f\n",mydata.C = (mydata.F-32)*5/9);
             break;         
            } 
     default:
            {
             printf("輸入錯誤,重新輸入");               
            }                 
    }
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


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

}
==============================================
 /* 程式檔名:test_16-27-2*/
/* 程式目的:根據輸入的成績來決定輸出的標語,請用enum宣告三個等級*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400820*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum ans {OK=60,Good=80,Excellent=90};

void Pause(const char* message);

int main(int argc, char *argv[])
{
    int a,flag;
    char *namea[]={"OK","Good","Excellent"};
    printf("請輸入成績 60 80 90:");
    scanf("%d",&a);
    
    if(a==OK)
    {
              printf("%s\n",namea[0]);
    }
    else if(a==Good)
    {
              printf("%s\n",namea[1]);
    }
    else if(a==Excellent)
    {
              printf("%s\n",namea[2]);
    }
    else
    {
              printf("輸入錯誤\n");
    }
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


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

}

==============================================
 /* 程式檔名:test_16-28-3*/
/* 程式目的:請利用typedef定義結構體data 成員有 年月日 請用此結構體宣告變數 並輸出今天日期*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400821*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct data
{
    int year;
    int mon;
    int mday;
} myday;

void Pause(const char* message);

int main(int argc, char *argv[])
{
    myday today={2014,8,24};
    printf("%d年/%d月/%d日\n",today.year,today.mon,today.mday);
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


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

}

==============================================
 /* 程式檔名:test_16-28-4*/
/* 程式目的:請利用typedef定義結構體student 成員有 name ID age 設定所有成員初始值*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400821*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct studentmenu
{
    char name[10];
    int ID;
    int age;
}student[3];

void Pause(const char* message);

int main(int argc, char *argv[])
{
    int i;
    student menu={"王小明",123456,21,
                 "陳阿華",456789,22,
                 "吳小敏",789123,23};
    printf("   名子   編號  年齡\n");
    for(i=0;i<3;i++)
    {
     printf("%s  %d    %d\n",menu[i].name,menu[i].ID,menu[i].age);
    }
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


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

}

==============================================
 /* 程式檔名:test_16-28-5*/
/* 程式目的:試用一年中的12個月份的英文縮寫定義列舉成員,利用列舉型別,從螢幕輸出12個月的英文單子*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400821*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);
enum week{Mon=1,Tue,Wed,Thu,Fri,Sat,Sun}weekday;

int main(int argc, char *argv[])
{
    char *ewname[]={"Monday","Tuesday","Wednessday","Thrusday","Friday","Saturday","Sunday"};
    printf("  英文\n");
    for(weekday=Mon;weekday<=Sun;weekday++)
    {
     printf("%6s\n",ewname[weekday-1]);                                       
    } 
    

    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


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

}

==============================================
 /* 程式檔名:test_16-28-6*/
/* 程式目的:根據輸入的年齡來判斷可看哪一級的電影*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400821*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Pause(const char* message);
enum agele{PG=6,G=12,R=18}ageha;

int main(int argc, char *argv[])
{
    int age;
    printf("您的年齡:"); 
    scanf("%d",&age);
    if(age<PG)
    {
     printf("未滿6歲只可以看普級\n");          
    }
    else if(age>=PG && age<G)
    {
     printf("6~11歲只可以看保護級\n");          
    }
    else if(age>=G && age<R)
    {
     printf("12~18歲只可以看輔導級\n");          
    }
    else
    {
     printf("輸入錯誤\n");   
    }

    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


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

}

==============================================
 /* 程式檔名:test_16-28-7*/
/* 程式目的:請利用typedef定義結構體 成員為三科成績 並用程式計算出平均分數*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400821*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct FP
{
    int math1;
    int math2;
    int math3;
}agemath;

void Pause(const char* message);

int main(int argc, char *argv[])
{
    agemath math={100,50,40};
    printf("%.2f\n",(math.math1+math.math2+math.math3)/3.0);
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


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

}

==============================================
 /* 程式檔名:test_16-28-8*/
/* 程式目的:利用enum宣告5個等級ABCDE 90分以上為A 80分以上為B 70分以上為C 以下為D 請從螢幕輸出相對等級*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400823*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum matheng {A=90,B=80,C=70,D=60}mathengname;

void Pause(const char* message);

int main(int argc, char *argv[])
{
    int math;
    printf("請輸入成績:");
    scanf("%d",&math);
    
    mathengname=math;
    printf("您的成績是:");
    
    if(math>=A) 
    {
     printf("A\n");            
    }
    else if(math<A && math>=B) 
    {
     printf("B\n");            
    }
    else if(math<B && math>=C) 
    {
     printf("C\n");            
    }
    else if(math<C && math>=D) 
    {
     printf("D\n");            
    }
    else if(math<D) 
    {
     printf("E\n");            
    }
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


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

}

==============================================
 /* 程式檔名:test_16-28-9*/
/* 程式目的:請用最節省記憶體空間的方式,宣告結構體與共同空間儲存以下資料,從鍵盤輸入個數值後從螢幕輸出結果*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400823*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct company
{
      char workname[20];
      char IDnum[10];
      char address[30];
      char telphone[13];
      int page;  
};

union data
{
      struct company group;    
}mydata;

void Pause(const char* message);

int main(int argc, char *argv[])
{
    printf("請輸入公司名稱:");
    scanf("%s",mydata.group.workname);
    printf("請輸入身分證字號:");
    scanf("%s",mydata.group.IDnum);
    printf("請輸入地址:");
    scanf("%s",mydata.group.address);
    printf("請輸入電話:");
    scanf("%s",mydata.group.telphone);
    printf("請輸入方案:");
    scanf("%d",&mydata.group.page);
    
    printf("%s  %s  %s  %s  %d",mydata.group.workname,mydata.group.IDnum,mydata.group.address,mydata.group.telphone,mydata.group.page);
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


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

}

==============================================
 /* 程式檔名:test_16-28-10*/
/* 程式目的:請將方案已enum宣告,並以ABC代替123*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400823*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum eng {A=1,B,C}engpage;
struct company
{
      char workname[20];
      char IDnum[10];
      char address[30];
      char telphone[13];
      int page;
};

union data
{
      struct company group;    
}mydata;

void Pause(const char* message);

int main(int argc, char *argv[])
{
    
    printf("請輸入公司名稱:");
    scanf("%s",mydata.group.workname);
    printf("請輸入身分證字號:");
    scanf("%s",mydata.group.IDnum);
    printf("請輸入地址:");
    scanf("%s",mydata.group.address);
    printf("請輸入電話:");
    scanf("%s",mydata.group.telphone);
    printf("請輸入方案:");
    scanf("%d",&mydata.group.page);
    printf("%s  %s  %s  %s  ",mydata.group.workname,mydata.group.IDnum,mydata.group.address,mydata.group.telphone);
    
    switch(mydata.group.page)
    {
     case A:
              {
               printf("A方案\n");
               break;
              }  
     case B:
              {
               printf("B方案\n");
               break;
              }
     case C:
              {
               printf("C方案\n");
               break;
              } 
                   
    }                        

    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


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

}

==============================================

沒有留言:

張貼留言