1.编写一个程序读取输入,读到#字符为止,然后报告读取的空格数、换行符数和所有其他字符的数量。
#include
#define SPACE ' ' //定义空格
#define LINEBREAK '\n' //定义换行符
int main(void)
{
char ch;
int count_space = 0; //记录空格数
int count_linebreak = 0; //记录换行符数
int count_others; //记录其他字符数
printf("请输入一段话,以#字符结尾:\n");
while ((ch = getchar()) != '#')
{
if(ch == SPACE)
{
count_space = count_space + 1;
}
else if (ch == LINEBREAK)
{
count_linebreak = count_linebreak + 1;
}
else
{
count_others = count_others + 1;
}
}
printf("输入的这段话中,有%d个空格,有%d个换行符,有%d个其他字符。\n",count_space,count_linebreak,count_others);
return 0;
}
2./编写一个程序读取输入,读到#字符停止。程序要打印每个输入的字符以及对应的ASCII码(十进制)。每行打印8个“字符-ASCII码”组合。
建议:使用字符计数和球磨运算符(%)在每8个循环周期时打印一个换行符。
#include
int main(void)
{
char ch;
int count = 0;
printf("请输入一段话,以#字符结尾:\n");
while ((ch = getchar()) != '#')
{
printf("%c-%-5d",ch,ch);
count = count +1;
if (count % 8 == 0)
{
printf("\n");
}
}
return 0;
}
3.编写一个程序,读取整数直到用户输入0。
输入结束以后,程序应报告用户输入的偶数(不包括0)个数、这些偶数的平均值、输入的奇数个数及其奇数的平均数。
#include
int main(void)
{
int number;
int count_ji = 0;
int sum_ji = 0;
int count_ou = 0;
int sum_ou = 0;
printf("请输入一串数字,以空格间隔,以数字0结尾:\n");
while (scanf("%d",&number) == 1)
{
if (number == 0)
{
break;
}
else if (number % 2 != 0) //输入奇数
{
count_ji = count_ji + 1;
sum_ji = sum_ji + number;
}
else //输入偶数
{
count_ou = count_ou + 1;
sum_ou = sum_ou + number;
}
}
printf("输入的这串数字中,有%d个偶数(不包括0),这些偶数的平均值为%d\n",count_ou,sum_ou/count_ou);
printf("有%d个奇数,这些奇数的平均值为%d。\n",count_ji,sum_ji/count_ji);
return 0;
}
4.使用if else语句编写一个程序读取输入。读到#停止。用感叹号替换句号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换。
#include
int main(void)
{
char ch;
int count = 0;
printf("请输入一段话(包括句号和感叹号),并以#结尾:\n");
while ((ch = getchar()) != '#')
{
if (ch == '.')
{
putchar('!');
count = count + 1;
}
else if (ch == '!')
{
putchar('!');
putchar('!'); //注意,这里不能是putchar('!!'); 单引号是字符型,不是字符串!
count = count + 1;
}
else
{
putchar(ch);
}
}
printf("\n");
printf("一共替换了%d次。\n",count);
return 0;
}
6.编写程序读取输入,读到#停止,报告ei出现的次数。用“Receive your eieio award”的输入来测试。
#include
int main(void)
{
char former,present;
int count = 0;
printf("请输入一段话,并以#结尾:\n");
while ((present = getchar()) != '#')
{
if (former == 'e' && present == 'i')
{
count = count + 1;
}
former = present;
}
printf("ei出现的次数为%d。\n",count);
return 0;
}
7编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额、税金和净收入。
1.基本工资 = 10美元/小时。2.加班(超过40小时)= 1.5倍时间。3.税率:前300美金为15%,续150美元为20%,余下的为25%。
#include
#define JIBEN 10
int main(void)
{
int hour;
int zonge;
int shuijin;
int jingshouru;
printf("请输入一周工作的小时数:\n");
scanf("%d",&hour);
if (hour <= 40)
{
zonge = JIBEN * hour;
if (zonge <= 300)
{
shuijin = 0;
jingshouru = zonge;
printf("您的工资总为%d,税金为%d,净收入为%d。\n",zonge,shuijin,jingshouru);
}
else
{
shuijin = 300 * 0.15 + (zonge - 300) * 0.2;
jingshouru = zonge - shuijin;
printf("您的工资总为%d,税金为%d,净收入为%d。\n",zonge,shuijin,jingshouru);
}
}
else
{
zonge = 400 + JIBEN * (hour-40) * 1.5;
if (zonge <= 450)
{
shuijin = 300 * 0.15 + (zonge - 300) * 0.2;
jingshouru = zonge - shuijin;
printf("您的工资总为%d,税金为%d,净收入为%d。\n",zonge,shuijin,jingshouru);
}
else
{
shuijin = 300 * 0.15 + 150 * 0.2 + (zonge - 450) * 0.25;
jingshouru = zonge - shuijin;
printf("您的工资总为%d,税金为%d,净收入为%d。\n",zonge,shuijin,jingshouru);
}
}
return 0;
} `
8.使用switch完成工资等级选择。如果选择1~4其中的一个数字,程序应该询问用户工作的小时数。程序要通过循环运行,除非用户输入5。
#include
#define G1 8.75
#define G2 9.33
#define G3 10.00
#define G4 11.20
#define S300 0.15
#define S150 0.2
#define S450 0.25
void work(int G); //函数原型
int main(void)
{
int option;
int k=1;
printf("请选择您的工资等级:\n");
printf("1.$8.75/hr\t2.$9.33/hr\n3.$10.00/hr\t4.$11.20/hr\n5.quit\n");
while (k == 1)
{
scanf("%d",&option);
switch (option)
{
case 1:
work(G1);
break;
case 2:
work(G2);
break;
case 3:
work(G3);
break;
case 4:
work(G4);
break;
case 5:
k = 0;
break;
default:
printf("请输入正确的选项!\n");
printf("请选择您的工资等级:\n");
}
}
return 0;
}
void work(int G) //函数定义
{
int hour;
int zonge;
int shuijin;
int jingshouru;
printf("请输入一周工作的小时数:\n");
scanf("%d",&hour);
if (hour <= 40)
{
zonge = G * hour;
if (zonge <= 300)
{
shuijin = 0;
jingshouru = zonge;
printf("您的工资总为%d,税金为%d,净收入为%d。\n",zonge,shuijin,jingshouru);
}
else
{
shuijin = 300 * S300 + (zonge - 300) * S150;
jingshouru = zonge - shuijin;
printf("您的工资总为%d,税金为%d,净收入为%d。\n",zonge,shuijin,jingshouru);
}
}
else
{
zonge = 400 + G * (hour-40) * S300;
if (zonge <= 450)
{
shuijin = 300 * S300 + (zonge - 300) * S150;
jingshouru = zonge - shuijin;
printf("您的工资总为%d,税金为%d,净收入为%d。\n",zonge,shuijin,jingshouru);
}
else
{
shuijin = 300 * S300 + 150 * S150 + (zonge - 450) * S450;
jingshouru = zonge - shuijin;
printf("您的工资总为%d,税金为%d,净收入为%d。\n",zonge,shuijin,jingshouru);
}
}
printf("\n");
printf("请选择您的工资等级:\n");
printf("1.$8.75/hr\t2.$9.33/hr\n3.$10.00/hr\t4.$11.20/hr\n5.quit\n");
}
9.编写一个程序,只接受正整数输入,然后显示所有小于或等于该数的素数。
#include
int main(void)
{
int number;
int i,j;
int flag = 1; //定义一个标记来表示是否为素数。flag=1是素数,flag=0不是素数。
printf("请输入一个正整数:\n");
while (scanf("%d",&number) == 1 && number > 0)
{
for (i = 2;i <= number;i++)
{
for (j = 2;j <= i;j++)
{
if (i % j == 0 && j != i )
{
flag = 0;
break;
}
}
if (flag == 1)
{
printf("%d ",i);
}
flag = 1;
}
break;
}
return 0;
}