1、编写一个程序。该程序输入知道遇到#字符,然后报告读取的空额数目、读取的换行字符数以及读取的所有其他字符数目。
#include <stdio.h>
int main(void){
int space_num=0;
int enter_num=0;
int other=0;
char ch;
printf("Please enter some letter(# to quit):\n");
while((ch=getchar())!='#'){
switch(ch){
case ' ':space_num++;
break;
case '\n':enter_num++;
break;
default:other++;
}
}
printf("The space num is %d,the enter num is %d,the other char num is %d.",space_num,enter_num,other);
return 0;
}
**********因为期末考试所以好久没编程了。感觉手都生了。明天正式开始继续编程*************************
2、编写一个程序。该程序读取输入直到遇到#字符。使程序打印每个输入的字符以及它的十进制ASCII码。
#include <stdio.h>
#include <ctype.h>
#define STOP '#'
int main(void){
char ch;
int count=0;
int test;
printf("Please enter some letters without SPACE(# to quit):\n");
while((ch=getchar())!=STOP){
count++;
test = isalpha(ch);
if(count%8==0){
printf("%5d\n",ch);
}
else
{
switch (test)
{
case 1:printf("%5d",ch);
break;
case 0:printf("Please try something else.\n");
break;
case 2:printf("%5d ",ch);
break;
default:printf("Unknow error");
break;
}
}
}
return 0;
}
注:isalpha()返回非零字符(不一定是1)。实际上,经测试大写字母返回1,小写字母返回2.
3、编写一个程序。该程序读取整数,直到输入0。输入终止后,程序应该报告输入的偶数(不包括0),总个数、偶数的平均值,输入的奇数总个数以及奇数的平均值。
#include <stdio.h>
int main(void){
int count_all=0;
int count_all_sum=0;
int count_odd=0;
int count_odd_sum=0;
int count_even=0;
int count_even_sum=0;
unsigned int num;
//while((scanf("%d",&num))!=0){
a:scanf("%d",&num);
if(num!=0){
count_all++;
if(num%2==0){
count_even_sum+=num;
count_even++;
}
else{
count_odd_sum+=num;
count_odd++;
}
goto a;
}
//}
printf("The odd num is %3d and the odd average is %.2f\n",count_odd,(float)count_odd_sum/(float)count_odd);
printf("The even num is %3d and the even average is %.2f\n",count_even,(float)(count_even_sum)/(float)(count_even));
printf("The all num is %3d",count_all);
return 0;
}
注:在大型程序中应避免使用goto.
4、用if else语句编写程序读取输入,直到#。用一个感叹号代替每个句号,原有的每个!用!!代替,最后报告进行了多少次替代。
#include <stdio.h>
int main(void){
char ch;
char ch2[2];
int count;
while((ch=getchar())!='#'){
if(ch=='.'){
ch='!';
count++;
}
else if(ch=='!'){
ch='!';
count++;}
}
putchar(ch);
printf("Total change %3d",count);
return 0;
}
!处理有问题。找时间重新写。
5、编写一个程序读取输入,直到#,并报告序列ei出现的次数。
#include <stdio.h>
#include <ctype.h>
#define STOP '#'
#define MARK 'e'
#define MARK_NEXT 'i'
int main(void){
char cur;
char cur_low;
char next;
int count=0;
while((cur=getchar())!=STOP){
cur_low = tolower(cur);
if(cur_low==MARK&&((next=getchar())==MARK_NEXT)){
count++;
}
}
if (count==0||count==1)
printf("'ei' has appearanced %d time.",count);
else
printf("'ei' has appearanced %d times.",count);
return 0;
}
6、编写程序,要求输入一周中的工作小时数,然后打印工资总额,税金以及净工资。作如下假设:
a.基本工资等级=10.00美元/小时
b.加班(超过40小时)=1.5倍工作时间
c.税率 前300美元为15%
下一个150美元为20%
余下的为25%
#include <stdio.h>
#define SALARY_PER_HOUR 10.00
#define FIRST 0.15
#define SECOND 0.20
#define ELSE 0.25
int main(void)
{
int salary,hour;
float tax=0;
printf("Please enter hours you worked:\n");
scanf("%d",&hour);
if(hour>40){
hour=(hour-40)*1.5+40;
printf("The salary is %.2f.\n",hour*SALARY_PER_HOUR);
}else{
printf("The salary is %.2f.\n",hour*SALARY_PER_HOUR);
}
salary=hour*SALARY_PER_HOUR;
if(salary<=300)
tax = salary*FIRST;
else if(salary>300&&salary<=450)
tax = 300*FIRST+(salary-300)*SECOND;
else
tax = 300*FIRST+150*SECOND+(salary-450)*ELSE;
printf("The tax fine is %.2f.\n",tax);
printf("The rest salary is %.2f.\n",salary-tax);
return 0;
}
float常量需要格式化输出。不然输出会出现意外。
7、修改6中的条件,使之更人性化。
#include <stdio.h>
#define FIRST 0.15
#define SECOND 0.20
#define ELSE 0.25
int main(void)
{
float salary=0;
int hour=0;
double salary_per_hour;
int size_of_puts;
int index;
int index_next;
char ch;
float tax=0;
size_of_puts=sizeof("Enter the number corresponding to the desired pay rate or action");
for(index=0;index<=size_of_puts;index++)
printf("*");
printf("\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("a.$8.75/hr b.$9.33/hr\n");
printf("c.$10.00/hr d.$11.20/hr\n");
printf("e.quit\n");
for(index_next=0;index_next<=size_of_puts;index_next++)
printf("*");
printf("\n");
switch (ch=getchar())
{
case 'a':salary_per_hour=8.75;
break;
case 'b':salary_per_hour=9.33;
break;
case 'c':salary_per_hour=10.00;
break;
case 'd':salary_per_hour=11.20;
break;
default:printf("Thank you for using.\n");
}
if(ch>='a'&&ch<='d'){
printf("Please enter hours you had worked:\n");
scanf("%d",&hour);
printf("The hours you had workd is %d.\n",hour);
if(hour>40){
hour=(hour-40)*1.5+40;
printf("The salary is %.2f.\n",hour*salary_per_hour);
}else{
printf("The salary is %.2f.\n",hour*salary_per_hour);
}
salary=hour*salary_per_hour;
if(salary<=300)
tax = salary*FIRST;
else if(salary>300&&salary<=450)
tax = 300*FIRST+(salary-300)*SECOND;
else
tax = 300*FIRST+150*SECOND+(salary-450)*ELSE;
}
if(salary>0){
printf("The tax fine is %.2f.\n",tax);
printf("The rest salary is %.2f.\n",salary-tax);
}
return 0;
}
8、编写一个程序,接受一个整数输入,然后显示所有小于或等于该数的素数。
#include <stdio.h>
#include <math.h>
#include <ctype.h>
int find(float n);
int main(void)
{
int i;
int num;
printf("Please enter a number(q to quit):\n");
while((scanf("%d",&num))==1){
for(i=num;i>1;i--){
if(find(i)==0)
printf("%d is a prime.\n",i);
}
printf("Please enter a number(q to quit):\n");
}
return 0;
}
int find(float n){
float f=n;
int i=0;
int count=0;
if(f==2||f==3)
return count=0;
else if(f>=4){
for(i=2;i<=(int)sqrt(f);i++){
if((int)f%i==0)
count++;
}
}
return count;
}
————————————————————————后记————————————————————
第七章终于结束了。!!!!!
有一个阿姨来我们这边看牙齿。回家不方便就住在我家。顺便带来了一个熊孩子。。经常吵。我又不能去跟一个5岁的小孩较真。。搞得我也没什么时间可以安静下来写程序。。白天只能在纸上写个框架。等他睡着之后把框架码成代码..明天开始跑图书馆。代码的事情只能慢慢地跟进了。。一天写一题。。这进度真是。。。好想哭。