C语言学习笔记——第七章

7-1.c

#include<stdio.h>
int main(void)
{
	int blank,turn,others;
	char tmp;
	blank=turn=others=0;
	printf("Please input some string.End with #:");
	while((tmp=getchar())!='#')
	{
		if(tmp==' ')
			blank++;
		else if(tmp=='\n')
			turn++;
		else
			others++;
	}
	printf("\nYou have input %d blank, %d return, %d others.\n", blank, turn
	, others);
	return 0;
}

7-11.c

#include<stdio.h>
void printtop(void)
{
	int a;
	for(a=0;a<70;a++)
		putchar('*');
	putchar('\n');
}
int menu(void)
{
	int input;
	printtop();
	printf("What are you going to do?\na)caoxianjing:$1.25/pound\t\tb)tiancai:$0.65/pound\n");
	printf("c)huluobo:$0.89/pound\t\t\nq)Tell me how much I should pay\n");
	printtop();
	input=getchar();
	return input;
}
int main(void)
{
	int input;
	int a,b,c;
	float money;
	float yunshu;
	int zong;
	while((input=menu())!='q')
	{
		if(input<'a' || input>'c')
			continue;
		switch(input)
		{
			case 'a':
				printf("How many?");
				scanf("%d",&a);
				break;
			case 'b':
				printf("How many?");
				scanf("%d",&b);
				break;
			case 'c':
				printf("How many?");
				scanf("%d",&c);
				break;
		}
	}
	printf("caoxianjing:$1.25/pound\t%d pounds\t$%2.2f\n",a,a*1.25);
	printf("tiancai:$0.65/pound\t%d pounds\t$%2.2f\n",b,b*0.65);
	printf("huluobo:$0.89/pound\t%d pounds\t$%2.2f\n",c,c*0.89);
	money=a*1.25+b*0.65+c*0.89;
	if(money<100)
		printf("You need to pay $%2.2f!\n",money);
	else
	{
		money*=0.95;
		printf("You need to pay $%2.2f! Included 5%c off!\n",money,'%');
	}
	zong=a+b+c;
	if(zong<6)
		yunshu=3.5;
	else if(zong<20)
		yunshu=10;
	else 
		yunshu=8+0.1*zong;
	printf("If you need to yunshu,you need $%2.2f for it.\n",yunshu);
	printf("Total to pay $%2.2f\n",money+yunshu);
	return 0;
}

7-2.c

#include<stdio.h>
int main(void)
{
	int num,ch;
	num=ch=0;
	printf("Please input some strings:");
	while((ch=getchar())!='#')
	{
		num++;
		if((num%8)!=0)
			printf("|%c %d", ch, ch);
		else
			printf("\n");
	}
	return 0;
}

7-3.c

#include<stdio.h>
int main(void)
{
	int all_ou,all_ji,num_ou,num_ji,now;
	all_ou=all_ji=num_ou=num_ji=0;
	printf("Input some integers,end with 0:");
	while((scanf("%d",&now)!=-1) && (now!=0))
	{
		if(now%2==0)
		{

			all_ou+=now;
			num_ou++;
		}
		else
		{
			all_ji+=now;
			num_ji++;
		}
	}
	printf("You have input %d ou and ave is %2.2f.\n", num_ou,(float)all_ou/(float)num_ou);
	printf("You have input %d ji and ave is %2.2f.\n", num_ji,(float)all_ji/(float)num_ji);
	return 0;
}

7-4.c

#include<stdio.h>
int main(void)
{
	int times,now;
	times=0;
	while((now=getchar())!='#')
	{
		if(now=='.')
		{
			times++;
			putchar('!');
		}
		else if(now=='!')
		{
			times++;
			printf("!!");
		}
		else 
			putchar(now);
	}
	printf("\nI have repleaced for %d times!\n",times);
	return 0;
}

7-6.c

#include<stdio.h>
int main(void)
{
	char before=0,now;
	int times=0;
	printf("Please input some strings,end with #:");
	while((now=getchar())!='#')
	{
		if(before=='e' && now=='i')
			times++;
		before=now;
	}
	printf("\nYou have input %d ei.\n", times);
	return 0;
}

7-7.c

#include<stdio.h>
#define base 10
#define add 1.5
#define qian300 0.15
#define hou150 0.2
#define yuxia 0.25
int main(void)
{
	int hours,money=0;
	printf("How many hours do you woke a week?");
	scanf("%d",&hours);
	if(hours>40)
		hours+=(hours-40)*add;
	money=hours*base;
	if(money<300 || money==300)
		money=money*(1-qian300);
	else if(money<450 || money==450)
		money=money-300*qian300-(money-300)*hou150;
	else
		money=money-300*qian300-150*hou150-(money-450)*yuxia;
	printf("You will have %d $ a week.", money);
	return 0;
}

7-8.c

#include<stdio.h>
void printtop(void);
int getswitch(void);
int main(void)
{
	int hours,select=0;
	float monperh=0.0;
	while((select=getswitch())!=5)
	{
		if(select<1 || select>5)
			continue;
		switch(select)
		{	
			case 1:
				monperh=8.75;
				break;
			case 2:
				monperh=9.33;
				break;
			case 3:
				monperh=10.00;
				break;
			case 4:
				monperh=11.2;
		}
		printf("How many hours have you worked?");
		scanf("%d",&hours);
		printf("You will have $%2.2f\n",hours*monperh);
	}
	printf("Bye!");
	return 0;
}
void printtop(void)
{
	int a;
	for(a=0;a<70;a++)
		putchar('*');
	putchar('\n');
}
int getswitch(void)
{
	int a;
	printtop();
	printf("Enter the number corresponding to the desired pay rate or action:\n");
	printf("1)$8.75/hr\t\t2)$9.33/hr\n3)$10.00/hr\t\t4)$11.20/hr\n5)quit\n");
	printtop();
	scanf("%d",&a);
	return a;
}

7-9.c

#include<stdio.h>
int ifshu(int a)
{
	int b=0,c=1;
	for(b=2;b<a;b++)
	{
		if(a%b==0)
		{
			c=0;
			break;
		}
	}
	return c;
}
int main(void)
{
	int i,now;
	printf("Please input an integer:");
	while(scanf("%d",&i)==-1)
		continue;
	for(now=2;now<(i+1);now++)
	{
		if(ifshu(now)==1)
			printf("%d ", now);
	}
	printf("\nBye!\n");
	return 0;
}

原创文章,转载请注明: 转载自YXZ’s Blog

本文链接地址: C语言学习笔记——第七章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>