第一题
#include<stdio.h>
int main(void)
{
printf("Enter something(# to quit):\n");
int ch;
int kong = 0, huan = 0, letter = 0;
while ((ch = (getchar())) != '#')
{
if (ch == ' ')
{
kong++;
continue;
}
else if (ch == '\n')
{
huan++;
continue;
}
else
{
letter++;
continue;
}
}
printf("There are %d spaces.\nThere are %d enters.\nThere are %d letters.\n", kong, huan, letter);
return 0;
}
第二题
#include<stdio.h>
int main(void)
{
printf("Enter a few letters(# to quit):\n");
char ch;
int i = 0;
while ((ch = getchar()) != '#')
{
if (i % 8 == 0)
printf("\n");
printf("%3d_%c ", ch, ch);
i++;
}
return 0;
}
第三题
#include<stdio.h>
int main(void)
{
int num;
int ou = 0, ji = 0;
float sumou = 0, sumji = 0;
printf("Enter some numbers(0 to quit):\n");
while (scanf("%d", &num) == 1)
{
if (num % 2 == 1)
{
ji++;
sumji += num;
}
else if (num <= 0)
break;
else
{
ou++;
sumou += num;
}
}
printf("There are %d ou.\nThere are %d ji.\n", ou, ji);
printf("The average of ou is %f\nThe average of ji is %f\n", sumou / ou, sumji / ji);
return 0;
}
第四题
#include<stdio.h>
int main(void)
{
char ch;
int change = 0;
printf("Enter some sentences(# to quit):\n");
while ((ch = (getchar())) != '#')
{
if (ch == '.')
{
printf("!");
change++;
}
else if (ch == '!')
{
printf("!!");
change++;
}
else
{
printf("%c", ch);
}
}
printf("\n");
printf("There are %d times change.", change);
return 0;
}
第五题
#include<stdio.h>
int main(void)
{
char ch;
int change = 0;
printf("Enter some sentences(# to quit):\n");
while ((ch = (getchar())) != '#')
{
switch (ch)
{
case '.': printf("!");
change++;
continue;
case '!': printf("!!");
change++;
continue;
default: printf("%c", ch);
continue;
}
}
printf("\n");
printf("There are %d times change.\n", change);
return 0;
}
第六题
#include<stdio.h>
int main(void)
{
char ch, num[50];
int times = 0;
int i = 0;
printf("Enter a sentence(# to quit):\n");
while ((ch = (getchar())) != '#')
{
num[i] = ch;
if (num[i - 1] == 'e' && num[i] == 'i')
{
times += 1;
}
i++;
}
printf("It appears %d times.\n", times);
return 0;
}
第七题
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define BASIC 1000
#include<stdio.h>
int main(void)
{
float tax, hour, basic;
printf("Enter the time(hour) you work in a week:\n");
scanf("%f", &hour);
if (hour > 40)
{
hour = 30 + (hour - 30) * 1.5;
}
basic = hour * BASIC;
if (basic <= 300)
{
tax = basic * RATE1;
}
else if (basic > 300 && basic <= 450)
{
tax = 45 + (basic - 300) * RATE2;
}
else
{
tax = 75 + (basic - 450) * RATE3;
}
printf("Your basic salary is $%.2f.\n", basic);
printf("The amount of tax is $%.2f.\n", tax);
printf("Your final income is $%.2f.\n", basic - tax);
return 0;
}
第八题
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#include<stdio.h>
int choice(void);
void print(void);
int main(void)
{
int num;
float hour, basic, tax;
print();
num = choice();
while (1 > 0)
{
if (num == 1 || num == 2 || num == 3 || num == 4)
{
printf("How many hours you work a week?\n");
scanf("%f", &hour);
if (hour > 40)
{
hour = 30 + (hour - 30) * 1.5;
}
switch (num)
{
case 1: basic = hour * 8.75;
break;
case 2: basic = hour * 9.33;
break;
case 3: basic = hour * 10.00;
break;
case 4: basic = hour * 11.20;
break;
}
if (basic <= 300)
{
tax = basic * RATE1;
}
else if (basic > 300 && basic <= 450)
{
tax = 45 + (basic - 300) * RATE2;
}
else
{
tax = 75 + (basic - 450) * RATE3;
}
printf("Your basic salary is $%.2f.\n", basic);
printf("The amount of tax is $%.2f.\n", tax);
printf("Your final income is $%.2f.\n", basic - tax);
print();
num = choice();
continue;
}
else if (num == 5)
{
break;
}
else
{
printf("Enter from 1 to 5 only: \n");
getchar();
scanf("%d", &num);
continue;
}
}
printf("Done!\n");
return 0;
}
int choice(void)
{
int n;
scanf("%d", &n);
return n;
}
void print(void)
{
int i = 0;
for (i = 0; i < 65; i++)
{
printf("*");
}
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");
for (i = 0; i < 65; i++)
{
printf("*");
}
printf("\n");
printf("Choose a number from 1 ~ 4(# to quit): ");
}
第九题
#include<stdio.h>
int main(void)
{
int num, i, j, n;
printf("Enter an integer: ");
scanf("%d", &num);
for (i = 2; i <= num; i++)
{
n = 0;
for (j = 2; j * j <= i; j++)
{
if (i % j == 0)
{
n++;
}
}
if (n == 0)
{
printf("%d ", i);
}
}
return 0;
}
第十题
#include<stdio.h>
int type(void);
float calculate(int, int);
int main(void)
{
int ty;
int salary;
float tax;
while(1 > 0)
{
ty = type();
printf("Enter your salary: ");
scanf("%d", &salary);
printf("The tax of you is $%.2f.\n", calculate(salary, ty));
}
return 0;
}
int type(void)
{
int t;
printf("Category Tax\n");
printf("Single 15%% of first $17,850 plus 28%% of excess\n");
printf("Head of Household 15%% of first $23,900 plus 28%% of excess\n");
printf("Married, Joint 15%% of first $29,750 plus 28%% of excess\n");
printf("Married, Separate 15%% of first $14,875 plus 28%% of excess\n");
printf("Choose the category of you (else to quit): ");
scanf("%d", &t);
return t;
}
float calculate(int s, int t)
{
float tax;
switch (t)
{
case 1:
if (s <= 17850)
{
tax = s * 0.15;
break;
}
else
{
tax = 2677.5 + 0.28 * (s - 17850);
break;
}
case 2:
if (s <= 23900)
{
tax = s * 0.15;
break;
}
else
{
tax = 3585 + 0.28 * (s - 23900);
break;
}
case 3:
if (s <= 29750)
{
tax = s * 0.15;
break;
}
else
{
tax = 4462.5 + 0.28 * (s - 29750);
break;
}
case 4:
if (s <= 14875)
{
tax = s * 0.15;
break;
}
else
{
tax = 2231.25 + 0.28 * (s - 14875);
break;
}
}
return tax;
}
第十一题
#include<stdio.h>
int main(void)
{
char letter, amount;
float moa, mob, moc, mo, yun;
int suma = 0, sumb = 0, sumc = 0, sum, d = 0;
printf("Enter the item you want to buy (a for artichokes, b for beets, c for carrots, q to quit): ");
while ((letter = getchar()) != 'q')
{
printf("Enter the amount you want: ");
scanf("%d", &amount);
switch (letter)
{
case 'a': suma += amount;
break;
case 'b': sumb += amount;
break;
case 'c': sumc += amount;
break;
}
printf("Enter the item you want to buy (a for artichokes, b for beets, c for carrots, q to quit): ");
letter = getchar();
}
printf("The total amount of artichokes is %d pound.\n", suma);
printf("The total amount of beets is %d pound.\n", sumb);
printf("The total amount of carrots is %d pound.\n", sumc);
moa = suma * 2.05;
mob = sumb * 1.15;
moc = sumc * 1.09;
sum = suma + sumb + sumc;
mo = moa + mob + moc;
if (mo >= 100)
{
mo *= 0.95;
d += 1;
}
if (sum <= 5)
yun = 6.5;
else if (sum > 5 && sum <= 20)
yun = 14;
else
yun = 14 + 0.5 * (sum - 20);
printf("Articokes: $2.05 per pound.\nYou buy %d pounds.\nThe charges is $%.2f.\n", suma, suma * 2.05);
printf("Beets : $1.15 per pound.\nYou buy %d pounds.\nThe charges is $%.2f.\n", sumb, sumb * 1.15);
printf("Carrots : $1.09 per pound.\nYou buy %d pounds.\nThe charges is $%.2f.\n", sumc, sumc * 1.09);
printf("The total cost of the order is $%.2f.\n", suma * 2.05 + sumb * 1.15 + sumc * 1.09);
if (d == 0)
printf("There's no discount.\n");
else
printf("There's a discount.\n");
printf("The shipping charge is $%.2f.\n", yun);
printf("The grand total of all the charge is $%.2f.\n", yun + mo);
return 0;
}