1.判断一个数是素数还是合数,如果是合数则打印其约数
/*判断一个数是素数还是合数,如果是合数则打印其约数*/
#include <stdio.h>
#define SIZE 100
int main(void)
{
int i, j, m, n, temp, num, divisor[SIZE];
printf("enter integer (enter interger<=1 and q to quit):");
while (scanf("%d", &num) == 1 && num > 1)
{
j = 0;/*每次循环开始初始化j*/
for (i = 1; i*i <= num; i++)
{
if (num%i == 0)
{
if (num / i == i)/*判断是否是a*a==num,防止多统计相同的约数*/
{
divisor[j] = i;/*将约数放入数组divisor*/
j++;
}
else
{
divisor[j] = i;
j++;
divisor[j] = num / i;
j++;
}
}
}
if (j > 2)/*通过判断约数的个数,来确定是素数(约数只有1和它本身,此时j=2(0.1.2,j最后自增了1))还是合数(j>2)*/
{
printf("%d is composite number\n", num);
printf("%d divisor is:\n", num);
for (m = j - 1; m >= 0; m--)/*对约数排序*/
{
for (n = 0; n < m; n++)
{
if (divisor[n] > divisor[m])
{
temp = divisor[m];
divisor[m] = divisor[n];
divisor[n] = temp;
}
}
}
for (i = 0; i < j; i++)/*输出约数*/
printf("%d ", divisor[i]);
printf("\n\n");
}
else
printf("%d is prime\n\n", num);
printf("enter integer (enter interger<=1 and q to quit):");
}
return 0;
}
/*VS2017输入输出结果:
enter integer (enter interger<=1 and q to quit):8
8 is composite number
8 divisor is:
1 2 4 8
enter integer (enter interger<=1 and q to quit):2
2 is prime
enter integer (enter interger<=1 and q to quit):5
5 is prime
enter integer (enter interger<=1 and q to quit):1000
1000 is composite number
1000 divisor is:
1 2 4 5 8 10 20 25 40 50 100 125 200 250 500 1000
enter integer (enter interger<=1 and q to quit):
*/
2.菜单选项程序实例(11程序中跳过输入有用)
7.编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额、税金和净收入。做如下假设:
a.基本工资 = 1000美元/小时
b.加班(超过40小时) = 1.5倍的时间
c.税率: 前300美元为15% 续150美元为20% 余下的为25%
用#define定义符号常量。不用在意是否符合当前的税法。
8.修改练习7的假设a,让程序可以给出一个供选择的工资等级菜单。使用switch完成工资等级选择。运行程序后,显示的菜单应该类似这样:
Enter the number corresponding to the desired pay rate or action:
- $8.75/hr 2) $9.33/hr
- $10.00/hr 4) $11.20/hr
- quit
如果选择 1~4 其中的一个数字,程序应该询问用户工作的小时数。程序要通过循环运行,除非用户输入 5。如果输入 1~5 以外的数字,程序应提醒用户输入正确的选项,然后再重复显示菜单提示用户输入。使用#define创建符号常量表示各工资等级和税率。
#include <stdio.h>
#include<stdbool.h>
#define TAX_TATE1 0.15
#define TAX_TATE2 0.20
#define TAX_TATE3 0.25
#define PAY_RATE1 8.75
#define PAY_RATE2 9.33
#define PAY_RATE3 10.00
#define PAY_RATE4 11.20
int main(void)
{
float taxes, wages, net_income, work_hours, pay_rate;
int option;
bool sign=0;/*用来判断pay_rate是否获取成功*/
printf("*****************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf(" 1)$8.75/hr 2)$9.33/hr\n");
printf(" 3)$10.00/hr 4)$11.20/hr\n");
printf(" 5)quit\n");
printf("*****************************************************************\n");
while (scanf("%d", &option) == 1)/*获取工资等级pay_rate*/
{
if (option == 5)
break;
switch (option)
{
case 1:sign = 1, pay_rate = PAY_RATE1;
break;
case 2:sign = 1, pay_rate = PAY_RATE2;
break;
case 3:sign = 1, pay_rate = PAY_RATE3;
break;
case 4:sign = 1, pay_rate = PAY_RATE4;
break;
default:printf("Please enter the correct option\n");
break;
}
if (sign)
{
printf("enter working time of a week(q to quit)\n");
break;
}
printf("*****************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf(" 1)$8.75/hr 2)$9.33/hr\n");
printf(" 3)$10.00/hr 4)$11.20/hr\n");
printf(" 5)quit\n");
printf("*****************************************************************\n");
}
while (sign&&scanf("%f", &work_hours) == 1)/*计算工资、税金、净收入*/
{
if (work_hours > 40)/*看是否加班,计算工作时间*/
work_hours = 40 + 1.5*(work_hours - 40);
wages = pay_rate * work_hours;/*工资*/
if (!(wages > 300))/*分段计算税金*/
taxes = wages * TAX_TATE1;
else if (wages > 300 && !(wages > 450))
taxes = 300 * TAX_TATE1 + (wages-300) * TAX_TATE2;
else
taxes = 300 * TAX_TATE1 + 150 * TAX_TATE2 + (wages - 300 - 150)*TAX_TATE3;
net_income = wages - taxes;/*净收入*/
printf("wages is %.2f\n", wages);
printf("taxes is %.2f\n", taxes);
printf("net income is %.2f\n", net_income);
printf("enter working time of a week(q to quit)\n");
}
return 0;
}
/*
vs2017输入输出结果:
*****************************************************************
Enter the number corresponding to the desired pay rate or action:
1)$8.75/hr 2)$9.33/hr
3)$10.00/hr 4)$11.20/hr
5)quit
*****************************************************************
6
Please enter the correct option
*****************************************************************
Enter the number corresponding to the desired pay rate or action:
1)$8.75/hr 2)$9.33/hr
3)$10.00/hr 4)$11.20/hr
5)quit
*****************************************************************
2
enter working time of a week(q to quit)
55
wages is 583.13
taxes is 108.28
net income is 474.84
enter working time of a week(q to quit)
15
wages is 139.95
taxes is 20.99
net income is 118.96
enter working time of a week(q to quit)
*/
11.ABC 邮购杂货店出售的洋蓟售价为 2.05 美元/磅,甜菜售价为 1.15美元/磅,胡萝卜售价为 1.09美元/磅。在添加运费之前,100美元的订单有5%的打折优惠。少于或等于5磅的订单收取6.5美元的运费和包装费,5磅~20磅的订单收取14美元的运费和包装费,超过20磅的订单在14美元的基础上每续重1磅增加0.5美元。编写一个程序,在循环中用switch语句实现用户输入不同的字母时有不同的响应,即输入a的响应是让用户输入洋蓟的磅数,b是甜菜的磅数,c是胡萝卜的磅数,q 是退出订购。程序要记录累计的重量。即,如果用户输入 4 磅的甜菜,然后输入 5磅的甜菜,程序应报告9磅的甜菜。然后,该程序要计算货物总价、折扣(如果有的话)、运费和包装费。随后,程序应显示所有的购买信息:物品售价、订购的重量(单位:磅)、订购的蔬菜费用、订单的总费用、折扣(如果有的话)、运费和包装费,以及所有的费用总额。
#include <stdio.h>
#define DISCOUNT 0.05/*折扣*/
#define ARTICHOKE_PRICE 2.05
#define BEET_PRICE 1.15
#define CARROT_PRICE 1.09
#define POSTAGE5 6.5
#define POSTAGE20 14
#define POSTAGE21 (14+(weight-20)*0.5)
int main(void)
{
float weight, artichoke_weight,beet_weight,carrot_weight,postage,cost,discount,artichoke_cost,beet_cost,carrot_cost,veg_cost;
char option, ch;
weight = artichoke_weight = carrot_weight = beet_weight = 0;
cost = veg_cost = postage = artichoke_cost = beet_cost = carrot_cost = discount = 0;
printf("*****************************************************************\n");
printf("Choose the vegetables you want to buy:\n");
printf(" a)artichoke $2.05/lb \n");
printf(" b)beet $1.15/lb\n");
printf(" c)carrot $1.09/lb\n");
printf(" q)quit\n");
printf("*****************************************************************\n");
while (scanf("%c", &option) == 1)/*选择商品*/
{
if (option == '\n')/*这样写是为了跳过后面一次输入weight后剩下的'\n'*/
continue;
while((ch=getchar()) != '\n')/*跳过输入行的剩余部分包括'\n'*/
continue;
if (option == 'q')
break;
switch (option)
{
case 'a':
printf("Please enter artichoke weight:\n");
scanf("%f", &artichoke_weight);/*多次选择会覆盖,不能累计*/
weight += artichoke_weight;
artichoke_cost = artichoke_weight * ARTICHOKE_PRICE;
break;
case 'b':
printf("Please enter beet weight:\n");
scanf("%f", &beet_weight);
weight += beet_weight;
beet_cost = beet_weight * BEET_PRICE;
break;
case 'c':
printf("Please enter carrot weight:\n");
scanf("%f", &carrot_weight);
weight += carrot_weight;
carrot_cost = carrot_weight * CARROT_PRICE;
break;
default:printf("Please enter the correct option\n");
break;
}
printf("*****************************************************************\n");
printf("Choose the vegetables you want to buy:\n");
printf(" a)artichoke $2.05/lb \n");
printf(" b)beet $1.15/lb\n");
printf(" c)carrot $1.09/lb\n");
printf(" q)quit\n");
printf("*****************************************************************\n");
}
veg_cost = artichoke_cost + beet_cost + carrot_cost;/*蔬菜总价*/
if (veg_cost >= 100)
discount = veg_cost * DISCOUNT;/*折扣*/
if (!(weight > 5))/*运费和包装费*/
postage = POSTAGE5;
else if (weight > 5 && weight <= 20)
postage = POSTAGE20;
else
postage = POSTAGE21;
cost = veg_cost - discount + postage;/*总价格*/
if (weight > 0)
{ printf("*****************************************************************\n");
printf("Purchase information:\n");
printf("vegetables unit-price weight cost\n");
if(artichoke_weight>0)
printf("a)artichoke %.2f %.2f %.2f\n",ARTICHOKE_PRICE,artichoke_weight,artichoke_cost);
if(beet_weight>0)
printf("b)beet %.2f %.2f %.2f\n",BEET_PRICE,beet_weight,beet_cost);
if(carrot_weight>0)
printf("c)carrot %.2f %.2f %.2f\n",CARROT_PRICE,carrot_weight,carrot_cost);
printf("total weight is %.2f\n", weight);
printf("vegetables cost is:%.2f\n", veg_cost);
printf("dicount is %.2f\n", discount);
printf("postage is %.2f\n", postage);
printf("total cost is %.2f\n", cost);
printf("*****************************************************************\n");
}
else
printf("You didn't buy anything.\n");
return 0;
}
/*
vs2017输入输出结果:
*****************************************************************
Choose the vegetables you want to buy:
a)artichoke $2.05/lb
b)beet $1.15/lb
c)carrot $1.09/lb
q)quit
*****************************************************************
a
Please enter artichoke weight:
56.2
*****************************************************************
Choose the vegetables you want to buy:
a)artichoke $2.05/lb
b)beet $1.15/lb
c)carrot $1.09/lb
q)quit
*****************************************************************
b
Please enter beet weight:
64.3
*****************************************************************
Choose the vegetables you want to buy:
a)artichoke $2.05/lb
b)beet $1.15/lb
c)carrot $1.09/lb
q)quit
*****************************************************************
c
Please enter carrot weight:
34.6
*****************************************************************
Choose the vegetables you want to buy:
a)artichoke $2.05/lb
b)beet $1.15/lb
c)carrot $1.09/lb
q)quit
*****************************************************************
q
*****************************************************************
Purchase information:
vegetables unit-price weight cost
a)artichoke 2.05 56.20 115.21
b)beet 1.15 64.30 73.95
c)carrot 1.09 34.60 37.71
total weight is 155.10
vegetables cost is:226.87
dicount is 11.34
postage is 81.55
total cost is 297.08
*****************************************************************
*/