2016年10月29日 星期六

C語言 選擇敍述

1.除錯題

#include <stdio.h>
#include <stdlib.h>
int main(){
    int select,result=77; 
    printf("猜猜今晚樂透彩號碼(二位數):");
    scanf("%d",&select);
    if(select!=result){
         printf("猜錯了~~");
         printf("答案是%d\n",result);
         }
    printf("好棒棒!! 你中獎了!\n");
    system("pause");
    return 0;
}

2.除錯題

#include <stdio.h>
#include <stdlib.h>
int main(){
    int value;
    printf("請任意輸入一個整數:");
    scanf("%d",&value);
    if(value%2==0||value%3==0){ 
      if(value%6!=0)
      printf("符合條件\n");
        else 
        printf("不符合條件\n");
    } 
    else printf("不符合條件\n");
    system("pause");
    return 0;
}

3.除錯題

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main(){
    int score = 40;
    if (score <= 60) 
    score +=10;
    printf("score =%d\n",score);
    system("pause");
    return 0;
    }

4.除錯題

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main(){
    int score = 90;
    if (score <= 60)
    score += 10;
    else
    score +=5;
    printf("score = %d\n",score);
    system("pause");
    return 0;
}

5.除錯題

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main(){
    int num = 50;
    if (num == 100)
    printf("此數等於100\n");
    else  
    printf("此數不等於100\n");
    system("pause");
    return 0;
    }

6.設計一程式 ,輸入打電話時間及長度 ,輸入打電話時間及長度 (length), 

計算其毛成本 (gsum) 及淨成本 (nsum)  ,並輸出淨成本。計算方式如下 :

(1) gsum=0.4 *lengthgsum=0.4 

(2) 如果時間在 AM 8:00以前或 PM 6:00以後 , 則 nsum=0.5* gsum; 否則 nsum=gsum; 

(3) 若長度超過 60 分鐘 , 則nsum=0.85* nsum(打 85 折)

(4) 最後 nsum=1.04* nsum;

#include<stdio.h>
#include<stdlib.h>
int main(){
    double hour,length,gsum,nsum;  //毛成本,淨成本
    printf("請輸入打電話的時間(1-24):");
    scanf("%lf",&hour);
    printf("請輸入打電話的長度(分鐘):");
    scanf("%lf",&length);
    /*-----------*計算成本----------*/
    gsum=0.4*length;
    if(hour<8||hour>18) nsum=0.5*gsum;
    else nsum=gsum;
    if(length>=60) {
    nsum=0.85*gsum;
    printf("\n通話費為:%.2f",nsum);
}
    else printf("\n通話費為:%.2f",nsum*1.04);            
    system("pause");
    return 0;
}

7.設計一程式 ,輸入某一整數 , 判斷它是否為 2的倍數或 3的倍數或 5的倍數

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main(){
    int value;
    printf("請任意輸入一個整數:");
    scanf("%d",&value);
    if(value%2==0||value%3==0||value%5==0)
    printf("符合條件\n");
    else printf("不符合條件\n");
    system("pause");
    return 0;
}

8.除錯題

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main(){
    int a = 80,b = 50;
    printf("變數a比變數b大\n",a > b);
    system("pause");
    return 0;
}

9.除錯題

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main(){
    int num = 40;
    if(num > 0)
    printf("此數為正整數\n");
    else if(num = 0)
    printf("此數為0\n");
    else
    printf("此數為負整數\n");
    system("pause");
    return 0;
}

10.輸入年份 , 並判斷此年份是否為閏或平年

(提示 : 閏年的條件為 

(1) 不能被 100 整除 、但能被 4整除 ; 

(2) 可被 100 整 除, 而且也可被 400 整除;

如:西元 2000 、2020 是閏年 ,而西元 2010是平年)

#include<stdio.h>
#include<stdlib.h>
int main(){
    int year=0;
    printf("請輸入西元年:");
    scanf("%d",&year);
    if(year%4==0 && year%100!=0 || year%100==0 && year%400==0)
    printf("%d 是閏年",year);
    else printf("%d 是平年",year);
    system("pause");
    return 0;
}
11.除錯題

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main(){
    int choice = 1;
    switch(choice){
    case 1:
    printf("你是1號學生\n");
    break;
    case 2:
    printf("你是2號學生\n");
    break;
    case 3:
    printf("你是3號學生\n");
    break;
    default :
    printf("你不是1~3號學生\n");
}
    system("pause");
    return 0;
}

12.輸入某一同學的GPA ,當

GPA=4 時,印出 時,印出 excellent student 

GPA=3 時,印出 時,印出 good student 

GPA=2 時,印出 時,印出 satisfactory 

GPA=1 時,則直接印出時,則直接印出 score=50 

其它則印出 Are youa fool or a genius? 

請利用 else ……if  選擇敍述執行

#include <stdio.h>
#include <stdlib.h>
int main(){
    int gpa;
    printf("Please input your classmate's gpa(1 to 4)");
    scanf("%d",&gpa);
    if(gpa==4)
       printf("excellent student\n");
    else if(gpa==3) 
           printf("good student\n");
    else if(gpa==2)
         printf("satification student\n");
    else if(gpa==1)
         printf("score=50\n");
    else   
  printf("Are you a fool or genius \n");
    system("pause"); 
    return 0;
}

13.同上一題目,將它改為 switch……case 的形式

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main(){
    int GPA;
    printf("Please input your classmate's GPA(1 to 4)");
    scanf("%d",&GPA);
    switch(GPA){
    case 4:
    printf("excellent student\n");
    break;
    case 3:
    printf("good student\n");
    break;
    case 2:
    printf("satification student\n");
    break;
    case 1:
    printf("score=50\n");
    break;
    default :
    printf("Are you a fool or genius?\n");
}
    system("pause");
    return 0;
}

14.請設計一C程式,利用 switch 敘述來完成簡單的計算機功能。敘述來完成簡單的計算機功能。例如只要由使用者輸入兩個數字 ,再鍵入 +, -,*,/ 任一鍵就可以進行運算。

#include <stdio.h>
#include <stdlib.h>
int main(){
 float a,b;
 char op_key;
 printf("請輸入兩個數字(以空白鍵區隔):\n");
 scanf("%f %f", &a, &b);
 printf("請輸入+ - * /鍵:\n");
 op_key=getche(); /*輸入字元並存入變數 op_key*/
 switch(op_key){  //請在此輸入程式碼以完成本題要求
     case '+':
          printf("\n%.2f %c %.2f=%.2f\n",a,op_key,b,a+b);
          break;
     case '-':
          printf("%.2f %c %.2f=%.2f\n",a,op_key,b,a-b);
           break;
     case '*':     
          printf("%.2f %c %.2f=%.2f\n",a,op_key,b,a*b);
           break;
     case '/':     
          printf("%.2f %c %.2f=%.2f\n",a,op_key,b,a/b);
           break;
     default:
          printf("運算式有誤\n");
 }
 system("pause");
 return 0;
 }

沒有留言:

張貼留言