【c語言】c程式設計--譚浩強--(第四章)--1

來源:互聯網
上載者:User

標籤:選擇結構   c程式設計   

// 輸入兩個實數,按代數值由小到大的順序輸出這兩個數#include <stdio.h>int main(){int a,b;printf("請輸入兩個數:");scanf("%d%d",&a,&b);if(a > b)printf("由小到大:%d  %d\n",b,a);elseprintf("由小到大:%d  %d\n",a,b);return 0;}




// 輸入3個數,要求按從小到大順序輸出#include <stdio.h>int main(){int a,b,c,t;printf("請輸入三個數:");scanf("%d%d%d",&a,&b,&c);if(a > b){t = a;a = b;b = t;}if(a > c){t = a;a = c;c = t;}if(b > c){t = b;b = c;c = t;}printf("從小到大的順序是:%d  %d  %d\n",a,b,c);return 0;}


// 輸入一個字元,判斷它是否為大寫字母,如果是,將它轉換成小寫字母,如果不是不轉換#include <stdio.h>int main(){char ch;printf("請輸入一個字元:");scanf("%c",&ch);if(ch >= 'A' && ch <= 'Z')ch = ch + 32;printf("轉換成小寫字母是:%c\n",ch);return 0;}



// 有一函數:x < 0 ,y = -1;x = 0,y = 0;x > 0,y = 1,編程輸入一個x值,要求輸出對應的y#include  <stdio.h>int main(){int x,y;printf("請輸入x:");scanf("%d",&x);if(x > 0)y = 1;else if(x < 0)y = -1;else y = 0;printf("對應的y值是:%d\n",y);return 0;}




// 要求按照考試成績的等級輸出百分制分數段,A等為85分以上,B等為70~84分,// C等為60~69分,D等為60分以下。成績的等級由鍵盤輸入#include <stdio.h>int main(){char ch;printf("請輸入成績的等級:");scanf("%c",&ch);switch(ch){case 'A':printf("分數段是:85分以上\n");break;case 'B':printf("分數段是:70~84分\n");break;case 'C':printf("分數段是:60~69分\n");break;case 'D':printf("分數段是:60分以下\n");break;default:break;}return 0;}


// 編寫一個簡單的計算機,實現加減乘除#include <stdio.h>int add( int a,int b ){int c;c = a + b;return c;}int fabs( int a,int b ){int c;c = a - b;return c;}int mul( int a,int b ){int c;c = a * b;return c;}int div( int a,int b ){int c;c = a / b;return c;}int main(){int input = 1;int a,b;printf("***********************************************\n");printf("*  1、加法                      2、減法       *\n");printf("*  3、乘法                      4、除法       *\n");printf("***********************************************\n");while(input){printf("請輸入你要選擇的操作:");scanf("%d",&input);if(input > 4){printf("沒有你想要的操作,請重新輸入\n");return 0;}printf("請輸入你要操作的兩個數:");scanf("%d%d",&a,&b);switch(input){case 1:printf("%d + %d = %d\n",a,b,add(a,b));break;case 2:printf("%d - %d = %d\n",a,b,fabs(a,b));break;case 3:printf("%d * %d = %d\n",a,b,mul(a,b));break;case 4:printf("%d / %d = %d\n",a,b,div(a,b));break;default:break;}}return 0;}


// 運輸公司對使用者計算運輸費用。路程越遠,每噸*千米運費越低。標準如下:// s<250---沒有折扣     250<=s<500---2%折扣    500<=s<1000---5%折扣// 1000<=s<2000---8%折扣   2000<=s<3000---10%折扣   3000<=s---15%折扣#include <stdio.h>int main(){float p,w,zp,z;int x,s;printf("請輸入單價,重量,距離:");scanf("%f%f%d",&p,&w,&s);if( s >= 3000 )x = 12;elsex = s / 250;switch(x){case 0:  z = 0; break;case 1:   z = 2; break;case 2:          break;case 3:   z = 5; break;case 4:     break;case 5:          break;case 6:          break;case 7:   z = 8; break;case 8:          break;case 9:          break;case 10:         break;case 11:  z = 10;break;case 12:  z = 15;break;default:break;}zp = p * w * s * (1 - z / 100);printf("總運費是:%10.2f\n",zp);return 0;}


【c語言】c程式設計--譚浩強--(第四章)--1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.