注:我寫的代碼有些要求超出題目需求,可適當減去部分功能
一、實驗目的與要求
-
理解C語言表示邏輯量的方法,學會正確使用邏輯運算符和邏輯表達式。
-
掌握利用if結構實現選擇結構的方法。
-
掌握利用switch結構實現多分支選擇結構。
-
結合程序掌握一些簡單的算法,學習調試程序。
-
實驗4學時。
二、實驗內容(基礎實驗)
編程題1
輸入任意三個整數a,b,c,求3個數中的最大值和最小值。
code:
#include <stdio.h>
int main(int argc, char *argv[])
{
int a, b, c, tmp_max, tmp_min;
printf("請輸入三個數:\n");
scanf("%d%d%d", &a, &b, &c);
tmp_max=(a >= b ? a : b);
tmp_min = a + b - tmp_max;
tmp_min=(tmp_min <= c ? tmp_min : c);
tmp_max=(tmp_max >= c ? tmp_max : c);
printf("最大值為:%d,最小值為:%d\n", tmp_max, tmp_min);
return 0;
}
console:
編程題2
輸入x,計算並輸出下面分段函數f(x)的值(保留2位小數)。請調用sqrt函數求平方根,調用pow函數求冪。
*輸入輸出示例*(共運行3次)
Enter x:10
f(10.00) = 3.16
Enter x:-0.5
f(-0.50) = -2.75
Enter x:0
f(0.00) = 0.00
code:
#include <stdio.h>
#include <math.h> //包含pow();和sqrt();函數
int main(int argc, char* argv[])
{
double x, y; //使用高精度
for (int i = 1; i <= 3; i++)
{
printf("Enter x:\t"); //\t為了美觀
scanf("%lf", &x);
if (x < 0)
{
y = pow((x + 1), 2) + 2 * x + (1.0 / x);
}
else
{
y = sqrt(x);
}
printf("f(%.2f)=\t%.2f\n", x, y); //\t為了美觀
}
return 0;
}
console:
編程題3
有一函數:
編寫程序,要求用scanf函數輸入x的值,輸出y的值。
具體要求如下:
(1)用if語句實現分支,自變量和函數值均用實數類型。
(2)自變量用scanf函數輸入,給一些輸入提示。
(3)運行程序,輸入x的值(分別為-5<x<0,x=0,0<x<10)三種情況,檢查輸出的y值是否正確。
code:
#include <stdio.h>
#include <conio.h> //包含getche();函數
int main(int argc, char* argv[])
{
double x, y;
char control; //設置控制器,用於控制函數計算的進行
while (1)
{
printf("Please Enter the Number of x: ");
scanf("%lf", &x);
if (x > -5 && x < 0)
{
y = x;
printf("y= %.2f\n", y);
}
else if (x == 0)
{
y = x - 1;
printf("y= %.2f\n", y);
}
else if (x > 0 && x < 10)
{
y = x + 1;
printf("y= %.2f\n", y);
}
else
{
printf("N/A\n");
}
//進入循環后更改控制器的值,用於存放下次判斷條件
printf("Enter q to quit or other character to continue: \n");
control=getche(); //使用getche();(帶回顯)或getch();(不帶回顯)
printf ("\n");
if (control =='q' || control=='Q') break;
}
return 0;
}
console:
編程題4
從鍵盤上輸入一個字符,如果它是大寫字母,則把它轉換成小寫字母輸出;否則,直接輸出。
code:
#include <stdio.h>
int main(int argc, char* argv[])
{
char ch;
printf("Please enter a letter:\n");
ch=getchar();
/******************************
0x20表示16進制的20,'A'和'a'的ascii碼相差十六進制的20,用0x20
使用十六進制記憶英文字母的ascii碼比較方便,'A'=0x41,'a'=0x61
******************************/
if(ch>=65 && ch <=90) printf("%c\n",ch+0x20); //判斷大小寫並轉換
else printf("%c\n",ch);
//該if-else可改寫為:?語句,使代碼更簡潔
//示例:ch >= 65 && ch <= 90 ? printf("%c\n",ch+0x20) : printf("%c\n",ch);
//示例未進行驗證,僅供參考
return 0;
}
console:
編程題5
編寫程序實現如下功能:輸入二個運算數(exp1,exp2)和一個運算符(op),計算表達式exp1 op exp2的值,其中op(格式)可以為+、-、*、/、%五個運算符中的任一種(提示:用switch語句實現)。
code:
#include <stdio.h>
int main(int argc, char* argv[])
{
double exp1,exp2,tot;
char op;
scanf("%lf%c%lf",&exp1,&op,&exp2);
//注意此處輸入需要緊貼,示例:2+3,三個字符間無空格,如有格式需要,可在scanf();中修改
switch (op)
{
case '+':
tot=exp1+exp2;
printf("%.2f",tot);
break;
case '-':
tot=exp1-exp2;
printf("%.2f",tot);
break;
case '*':
tot=exp1*exp2;
printf("%.2f",tot);
break;
case '/':
tot=exp1/exp2;
printf("%.2f",tot);
break;
case '%':
tot=(int)exp1%(int)exp2;
printf("%d",(int)tot);
break;
default:
printf("Can not match!\n");
break;
}
return 0;
}
console:
三、實驗內容(提高實驗)
編程題1
某服裝店經營成套服裝的買賣,同時也允許單件銷售。若一次購買不少於70套,則每套90元;若不足70套,則每套110元;只買上衣每件70元;只買褲子每條60元。請編寫程序假設用戶買入x件上衣和y條褲子,請編寫程序計算相應的應付款是多少?
code:
#include <stdio.h>
int main(int argc, char* argv[]);
{
double x,y,z;
printf("請輸入上衣和褲子的件數:\n");
printf("上衣:\t\t件\b\b\b\b\b\b\b\b"); //\t和\b是為了美觀
scanf("%lf",&x);
printf("褲子:\t\t件\b\b\b\b\b\b\b\b");
scanf("%lf",&y);
if (x>=y&&y>=70)
{
z=90*y+70*(x-y);
}else
if (y>=x&&x>=70)
{
z=90*x+60*(y-x);
}else
if (x>y&&y<70)
{
z=110*y+70*(x-y);
}else
if (y>x&&x<70)
{
z=110*x+60*(y-x);
}
printf("應付:%.2f(元)\n",z);
return 0;
}
console:
編程題2
編寫程序實現:輸入一個整數,判斷它能否被3 、5 、7整除,同時輸出如下信息。
(1)能同時被3 、5 、7整除;
(2)能同時被3、5、7中的任二個數整除;
(3)能被3、5、7中的任一個數整除;
(4)不能被3 、5 、7任一個數整除。
code:
//本例使用了自定義函數
#include <stdio.h>
int judge(int x, int y); //judge為自定義函數,int judge(int x, int y),返回值為1或0
int main(int argc, char* argv[])
{
int Num_integer, totl;
int a[3] = {0}; //定義長度為3的數組,並賦初值為0
printf("請輸入一個整數:\n");
scanf("%d", &Num_integer);
a[0] = judge(Num_integer, 3);
a[1] = judge(Num_integer, 5);
a[2] = judge(Num_integer, 7);
totl = a[0] + a[1] + a[2];
if (totl == 0)
printf("不能被3 、5 、7任一個數整除。\n");
else if (totl == 1)
printf("能被3、5、7中的任一個數整除。\n");
else if (totl == 2)
printf("能同時被3、5、7中的任二個數整除。\n");
else
printf("能同時被3 、5 、7整除。\n");
return 0;
}
//judge函數用於提高實驗的編程題2中,判斷x是否能被y整除,能整除返回1,否則返回0
int judge(int x, int y)
{
if (x % y == 0)
return 1;
else
return 0;
}
console:
編程題3
請用switch語句編程計算員工每月的薪水。
已知某公司員工的保底薪水為500,某月所接工程的利潤profit(整數)與提成的關系如下(計量單位:元):
profit≤1000 \ 沒有提成;
1000<profit≤2000 提成10%;
2000<profit≤5000 提成15%;
5000<profit≤10000 \ 提成20%;
10000<profit \ 提成25%。
分析:為使用switch語句,必須將利潤profit與提成的關系,轉換成某些整數與提成的關系。分析本題可知,提成的變化點都是1000的整數倍(1000、2000、5000、……),如果將利潤profit整除1000,則相鄰兩個區間的重疊。為解決相鄰兩個區間的重疊問題,最簡單的方法就是:利潤profit先減1(最小增量),然后再整除1000即可
profit≤1000 \ 對應0
1000<profit≤2000 對應1
2000<profit≤5000 對應2、3、4
5000<profit≤10000 \ 對應5、6、7、8、9
10000<profit \ 對應10、11、12、……
不聽不聽我不聽
code:
#include <stdio.h>
int main(int argc, char* argv[])
{
long int profit;
int flag; //設標記區分薪水檔次
double salary;
printf("請輸入該員工該月所接工程的利潤:\n");
scanf("%ld",&profit);
if (profit<=1000) flag=0;
else if(profit>1000&&profit<=2000) flag=1;
else if(profit>1000&&profit<=2000) flag=2;
else if(profit>5000&&profit<=10000) flag=3;
else if(profit>10000) flag=4;
switch (flag) //根據不同檔次計算薪水
{
case 1:
salary=500+10*profit/100.0;
printf("薪水為:%.2f\n",salary);
break;
case 2:
salary=500+15*profit/100.0;
printf("薪水為:%.2f\n",salary);
break;
case 3:
salary=500+20*profit/100.0;
printf("薪水為:%.2f\n",salary);
break;
case 4:
salary=500+25*profit/100.0;
printf("薪水為:%.2f\n",salary);
break;
default:
salary=500;
printf("薪水為:%.2f",salary);
break;
}
return 0;
}
console:
編程題4
求一元二次方程ax2+bx+c=0的根。
這憨批題目也不說明白要求,怎么輸入怎么輸出,求得值的精確度,復數是否要求通通沒有
code:
//利用了求根公式,可以輸出復數根
#include <stdio.h>
#include <math.h>
int main(int argc, char* argv[])
{
double a, b, c, delta, x1, x2, m, n, i, j;
printf("請輸入一元二次函數ax^2+bx+c=0中常數a,b,c的值:\n");
scanf("%lf%lf%lf", &a, &b, &c);
//1e-6(0.000001)叫做epslon,用來抵消浮點運算中因為誤差造成的相等無法判斷的情況,在此處配合fabs()可用於判定是否為零
if (fabs(a) <= 1e-6)
{
if (fabs(b) <= 1e-6) //到此則可判定a==0,b==0, 因此構不成方程式
puts("不是方程式"); //或者printf("不是方程式");理論可行,博主未驗證
else
printf("%.2lf", -c / b);
}
else
{
delta = b * b - 4 * a * c; //根的判別式
m = -b / (2 * a);
n = sqrt(fabs(delta)) / (2 * fabs(a)); //求根公式分解計算
i = m + n;
j = m - n;
if (delta < 0) //判斷是否為復根
printf("x1 = %.2lf+%.2lfi\tx2 = %.2lf-%.2lfi\n", m, n, m, n); //輸出復數根
else
{
if (i == j) //判斷兩根是否相等
printf("x1 = %.2lf\tx2 = %.2lf\n", i, i); //直接輸出相等的兩根x1,x2
else //兩根不相等的情況
{
x1 = (i > j) ? i : j; //將較大根放x1輸出
x2 = (i > j) ? j : i; //將較小根放x2輸出
printf("x1 = %.2lf\tx2 = %.2lf\n", x1, x2);
}
}
}
return 0;
}
console:
*編程題5 (NBU OJ題目)
1206 水費問題
為了加強居民的節水意識,某地區制定了以下生活用水收費標准:每戶每月用水未超過7噸時,每噸收費1.0元,並加收0.2元每噸的城市污水處理費;用水若超過7噸(含7噸),則每噸收費1.5元,並加收0.4元每噸的城市污水處理費。輸入用水量並計算應交水費。(注:非分段計費)
code:
#include <stdio.h>
int main(int argc, char* argv[])
{
double water_use, fare;
printf("請輸入用水量(噸):\n"); //提交oj時刪除該提示性輸出語句
scanf("%lf", &water_use);
if (water_use < 7)
fare = 1.0 * water_use + 0.2 * water_use;
else if (water_use >= 7)
fare = 1.5 * water_use + 0.4 * water_use;
printf("應交水費:%.2f 元\n", fare); //提交oj時刪除提示性字符
return 0;
}
console:
*編程題6 (NBU OJ題目)
1060 五級制成績評級
在學生成績管理中,成績經常需要在百分制與等級制之間進行轉換。輸入一個表示成績的百分制分數,將其轉換為對應的等級制並輸出。 A:[90-100] B:[80-89] C:[70-79] D:[60-69] E:[0-59]
code:
#include <stdio.h>
int main(int argc, char* argv[])
{
int grades;
scanf("%d", &grades);
if (grades >= 90 && grades <= 100)
printf("A\n");
else if (grades >= 80 && grades <= 89)
printf("B\n");
else if (grades >= 70 && grades <= 79)
printf("C\n");
else if (grades >= 60 && grades <= 69)
printf("D\n");
else
printf("E\n");
return 0;
}
console:
*編程題7 (NBU OJ題目)
1218 正方形還是圓形
首先從鍵盤讀入一個浮點數x,然后再讀入一個小寫字母(s或c),如果讀入的字母是s,則計算並輸出正方形面積(此時x作為邊長);如果讀入的字母是c,則計算並輸出圓面積(此時x作為半徑)。
code:
#include <stdio.h>
int main(int argc, char* argv)
{
double r;
char ch;
double area;
scanf("%lf%c", &r, &ch);
if (ch == 's')
area = r * r;
else if (ch == 'c')
area = pi * r * r;
printf("%.2f\n", area);
return 0;
}
console:
感謝收看!請多多支持!點個關注吧!!