复习题很轻松的通过了,不赘述了。
最近工作开始忙了,第七章看的效果很慢,但是兴趣还在上了24小时的班之后居然能兴致勃勃的写代码,当做休息吧。
瞎扯结束,以上。
编程练习
1.
/很简单,看会书上例题就可以。/
#include
2.
#include <stdio.h>
int main(void)
{
char ch;
int n;
for( n = 1;ch = getchar() != '#';n++)
{
printf("%c %d", ch, ch);
if(n%8==0)
printf("\n");
}
return 0;
}
3.
#include <stdio.h>
int main(void)
{
int a = 0, b = 0, c = 0, d = 0, i;
printf("Please enter the number:\n");
while(1)
{
scanf("%d", & i);
if(i == 0)
break;
if(i%2==0)
{
a++;
b += i;
}
else
{
c++;
d += i;
}
}
printf("%d %d %d %d", a, b/a, c, d/c);
return 0;
}
变量名依然取不好。while(1)可以无限循环,直到输入0,break这个循环。做错的原因是用了getchar…….用顺手了………
4.
#include <stdio.h>
int main(void)
{
char ch;
int a,b;
while((ch = getchar()) != '#')
{
if(ch == '.')
{
putchar('!');
a++;
}
else if(ch == '!')
{
putchar('!');
b++;
}
else
putchar(ch);
}
printf("%d times . to !", a);
printf("%d times ! to !!", b);
return 0;
}
putchar(”)需要注意。
5.
#include <stdio.h>
int main(void)
{
int num, odd = 0, even = 0, sum_odd = 0, sum_even = 0;
printf("Plese enter the number:\n");
while(1)
{
scanf("%d", &num);
if(num == 0)
break;
switch(num % 2 != 0)
{
case 1: sum_odd += num;
odd++;
break;
case 0: sum_even += num;
even++;
}
}
printf("odd count is %d.\n", sum_odd);
printf("odd average is %d.\n", sum_odd / odd);
printf("even count is %d.\n", sum_even);
printf("even count is %d.\n", sum_even / even);
return 0;
}
6.
#include <stdio.h>
int main(void)
{
int count = 0;
char first = 0,second ;
while((second = getchar()) != '#')
{
if((first == 'e') && (second == 'i'))
count++;
first = second;
}
printf("'ei' appeared %d times.\n", count);
return 0;
}
7.
#include <stdio.h>
#define HOURPER 100
#define TAX1 0.15
#define TAX2 0.2
#define TAX3 0.25
int main(void)
{
float pay, hours, tax;
printf("Please enter the hours:\n");
scanf("%f", &hours);
if(hours > 40.0 )
{
hours = 40 + (hours - 40) *1.5;
pay = hours * HOURPER;
printf("Pay is %f.\n", pay);
}
else
pay = hours * HOURPER;
if(pay <= 300)
tax = pay * TAX1;
else if(pay <= 450)
tax = 300 * TAX1 + (pay - 300) * TAX2;
else
tax = 300 * TAX1 + 150 * TAX2 + (pay - 450) * TAX3;
printf("pay is %f,tax is %f, income is %f\n", pay, tax, pay - tax);
return 0;
}
8.
#include <stdio.h>
#define TAX1 0.15
#define TAX2 0.2
#define TAX3 0.25
int main(void)
{
float pay, hours, tax, HOURPER;
while(1)
{
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");
printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");
printf("5) quit\n");
switch(get_int())
{
case 1: HOURPER = 8.75;
break;
case 2: HOURPER = 9.33;
break;
case 3: HOURPER = 10.00;
break;
case 4: HOURPER = 11.20;
break;
case 5: printf("quit.\n");
return 0;
}
printf("Pleas enter the hours:\n");
scanf("%f", &hours);
if(hours > 40.0 )
{
hours = 40 + (hours - 40) *1.5;
pay = hours * HOURPER;
printf("Pay is %f.\n", pay);
}
else
pay = hours * HOURPER;
if(pay <= 300)
tax = pay * TAX1;
else if(pay <= 450)
tax = 300 * TAX1 + (pay - 300) * TAX2;
else
tax = 300 * TAX1 + 150 * TAX2 + (pay - 450) * TAX3;
printf("pay is %f,tax is %f, income is %f\n", pay, tax, pay - tax);
return 0;
}
}
int get_int(void)
{
int num;
char str[40];
while(scanf("%d", &num) != 1)
{
gets(str);
printf("Please enter the NUMBER!\n");
}
while(getchar() != '\n');
return num;
}
定义了一个get_int函数,用来过滤非法选择的数字,得到1到5的选择整数。
9.
#include <stdio.h>
int isprimer(int);
int main(void)
{
int num,i;
printf("please enter a positive number:\n");
scanf("%d", &num);
for(i=2;i<=num;i++)
if(isprimer(i))
printf("%d\t", i);
printf("\n");
return 0;
}
int isprimer(int n)
{
int div;
for(div =2; div * div <=n; div++)
if(n % div == 0)
return 0;
return 1;
}
isprimer()是判断素数的函数(记不住就多看,多记几遍),素数返回1,非素数返回0。