7.1
//编写一个程序读取输入,读到#字符停止,然后报告读取的空格数
//换行符数和所有其他字符的数量
#include
#define STOP '#'
#define SPACE ' '
int main(void)
{
char ch;
int lines=1;
int spaces=0;
int others=0;
while ((ch = getchar()) != STOP)
{
if (ch == SPACE)
spaces++;
if (ch == '\n')
lines++;
else
others++;
}
printf("There are %d lines, %d spaces, %d others.", lines, spaces, others);
return 0;
}
7.2
//编写一个程序读取输入,遇到#停止
//程序要打印每个输入的字符以及对应的ASCII码(十进制),一行8个
#include
#define NUM 8
#define STOP '#'
int main(void)
{
char ch;
int count=0;
while ((ch = getchar()) != 'STOP')
{
count++;
putchar(ch);
printf(":%d ", ch);
if (count % 8 == 0)
printf("\n");
}
printf("Job Down!");
return 0;
}
7.3
程序错误,不知道哪里有问题。。。
先记录以后查找。。。
//编写一个程序,读取整数知道用户输入0
//输入结束后,程序应报告用户输入的偶数、平均数、奇数、平均数个数
#include
int main(void)
{
char num;;
int count_double = 0;
int count_single = 0;
int sum_double = 0;
int sum_single = 0;
//float ave_double, ave_single;
while ((num = getchar()) != 0)
{
if (num % 2 == 0)
{
count_double++;
sum_double = sum_double + num;
}
if (num % 2 == 1)
{
count_single++;
sum_single = sum_single + num;
}
}
printf("偶数个数:%d 平均值:%f\n奇数个数:%d 平均值:%f",
count_double, sum_double / count_double, count_single, sum_single / count_single);
return 0;
}
7.4
//使用if else语句编写一个程序读取输入,读到#停止
//用感叹号替换句号,用两个感叹号替换原来的感叹号,报告进行了多少次替换
#include
#define STOP '#'
int main(void)
{
char ch;
int count = 0;
while ((ch = getchar()) != STOP)
{
if (ch == '.')
{
putchar('!');
count++;
}
else if (ch == '!')
{
putchar('!'); //putchar('!!')错误
putchar('!');
count++;
}
else
putchar(ch);
}
printf("The sentense have replaced %d times.", count);
return 0;
}
7.5
#include
#define STOP '#'
int main(void)
{
char ch;
int count = 0;
while ((ch = getchar()) != STOP)
{
switch (ch)
{
case '.':
putchar('!');
count++;
break;
case '!':
putchar('!'); //putchar('!!')错误
putchar('!');
count++;
default:
putchar(ch);
}
}
printf("The sentense have replaced %d times.", count);
return 0;
}
7.6
//编写程序读取输入,读到#停止,报告ei出现的次数
#include
#define STOP '#'
int main(void)
{
char ch, prev;
int count=0;
while ((ch = getchar()) != STOP)
{
if (ch == 'i' && prev == 'e')
count++;
prev = ch;
}
printf("There are %d times 'ei' in the sentense.", count);
return 0;
}
7.7
太长,参考
//编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额税金和净收入
#include
#define BASE 1000 //一小时的工资
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define TIME_BREAK 40
#define BREAK1 300
#define BREAK2 450
#define BASE1 BREAK1 * RATE1
#define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
int main(void)
{
float time;
float sum_money;
float tax;
float get_money;
printf("Please enter your work time: \n");
scanf("%f", &time);
while (time <= 0)
{
printf("invalid value please enter a positive number: \n");
scanf("%f", &time);
}
if (time <= TIME_BREAK)
{
sum_money = time * BASE;
}
else
{
sum_money = 40 * BASE + (time - TIME_BREAK) * 1.5 * BASE;
}
if (sum_money <= BREAK1)
{
tax = sum_money * RATE1;
}
else if (sum_money <= BREAK2)
{
tax = BASE1 + (sum_money - BREAK1) * RATE2;
}
else
tax = BASE2 + (sum_money - BREAK2) *RATE3;
get_money = sum_money - tax;
printf("Your salary is %.3f, and your tax is %.3f, finally you can get %.3f ", sum_money, tax, get_money);
return 0;
}
7.8
太长,参考
#include
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define TIME_BREAK 40
#define BREAK1 300
#define BREAK2 450
#define BASE1 BREAK1 * RATE1
#define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
int main(void)
{
float time; //工作时间
float sum_money; //薪水
float tax; //税
float get_money; //最终得到的钱
int tag = 2; //初始化为2来控制第一次不打印 ERROR :Please enter a number 1~5
float BASE; //每小时的工资
do
{
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");
if (tag < 1 || tag > 5)
{
printf("ERROR :Please enter a number 1~5: ");
}
scanf("%d", &tag);
} while (tag < 1 || tag > 5);
while (tag != 5)
{
switch (tag) //根据输入来初始化每小时的工资
{
case 1 : BASE = 8.75;
break;
case 2 : BASE = 9.33;
break;
case 3 : BASE = 10.00;
break;
case 4 : BASE = 11.2;
}
printf("What is your working time? \n");
scanf("%f", &time);
while (time <= 0)
{
printf("invalid value please enter a positive number: \n");
scanf("%f", &time);
}
if (time > 0 && time <= TIME_BREAK)
{
sum_money = time * BASE;
}
else
{
sum_money = 40 * BASE + (time - TIME_BREAK) * 1.5 * BASE;
}
if (sum_money <= BREAK1)
{
tax = sum_money * RATE1;
}
else if (sum_money <= BREAK2)
{
tax = BASE1 + (sum_money - BREAK1) * RATE2;
}
else if (sum_money > 450)
tax = BASE2 + (sum_money - BREAK2) *RATE3;
get_money = sum_money - tax;
printf("Your salary is %.3f, and your tax is %.3f, finally you can get %.3f ", sum_money, tax, get_money);
printf("Choose another par rate tag now. \n");
scanf("%d", &tag);
}
printf("Done! \n");
return 0;
}
先暂时发布