C Primer Plus 第2章编程练习

作者在 2017-08-09 15:26:03 发布以下内容

1. 编写一个程序,用printf () 函数在一行上输出你的姓和名,再调用一次printf () 函数在两行上输出你的姓和名。然后再调用一对printf () 函数在一行上输出你的姓和名。输出的效果应如下所示(当然里面可以换成你自己的姓和名):

Anton  Bruckner    第一个输出语句。

Anton                   第二个输出语句。

Bruckner              仍然是第二个输了语句。

Anton Bruckner    第三个和第四个输出语句。


答:


# include <stdio.h>

int main (void)
{
   printf ("Anton Bruckner\n");
   printf ("Anton\nBruckner\n");
   printf ("Anton ");
   printf ("Bruckner");
   return 0;   
   
}



作者在 2017-08-09 16:30:49 补充以下内容

2. 编写一个程序输出你的姓名和地址。


答:



# include <stdio.h>
 
int main (void)
{
   printf ("野花豹\n\n");
   printf ("中国野花省花豹市野豹县\n");
//   printf ("Anton\nBruckner\n");
//   printf ("Anton ");
//  printf ("Bruckner\n");
   getchar ();
   return 0;   
    
}


作者在 2017-08-09 16:33:16 补充以下内容

3.  编写一个程序,把你的年龄换算成天数并显示二者的值。不用考虑平年( fractional )和闰年( leap  year )的问题。



# include <stdio.h>
int main (void)
{
	int days, years;
	years = 38;
	days  = 365*years ;
	printf ("I am %d years old.\nAnd it means %d days !\n",years ,days );
	getchar ();
	return 0;

}


作者在 2017-08-09 17:53:19 补充以下内容
4. 编写一个能产生下面输出的程序:


   For  he's a jolly good fellow !
   For  he's a jolly good fellow !
   For  he's a jolly good fellow !
   Which nobady can denny !


   程序中出了main () 函数外,要使用两个用户定义的函数:一个用于把上面的夸奖消息输出一次,另一个用于把最后一行输出一次。



# include <stdio.h>
   void shuchu01 (void);  //声明第一个函数。
   void shuchu02 (void);  //声明第二个函数。
	int main (void)
	{
		printf ("For he's a jolly good fellow !\n");
		shuchu01 ();  //调用第一个函数。
		printf ("For he's a jolly good fellow !\n");
		shuchu02 ();  //调用第二个函数。
		getchar ();
		return 0;

	}

	void shuchu01 (void)//定义第一个函数。
	{
		printf ("For he's a jolly good fellow !\n");
	
	}

	void shuchu02 (void)//定义第二个函数。
	{
		printf ("Which nobady can doney !\n");
	}


作者在 2017-08-09 21:43:36 补充以下内容

  5.编写一个程序,创建一个toes的整数变量。让程序把toes设置为10。
  然后现让程序计算两个toes的和以及toes的平方。程序应该输出所有的
  3个值,并分别标识它们。


  答:

# include <stdio.h>
int main (void)
{
int toes ;     //创建一个叫toes的整数变量。
toes = 10 ;    //把toes的值设置为10。
printf ("toes = %d\n",toes);  //输出toes本身的值。
printf ("toes + toes = %d\n",toes + toes); //输出两个toes的和。
printf ("toes的平方 = %d\n",toes*toes);    //输出toes的平方数。
getchar ();
return 0;
}

作者在 2017-08-09 22:15:41 补充以下内容
6.编写一个能产生下列输出的程序:
  
  Smile!Smile!Smile!
  Smile!Smile!
  Smile!

  

  在程序中定义一个能显示字符串Smile!一次的函数,并在需要时使用它。

答:


# include <stdio.h>
void zifuchuang (void);
int main (void)
{
zifuchuang (); //第一次调用函数zifuchuang。
zifuchuang (); //第二次调用函数zifuchuang。
zifuchuang (); //第三次调用函数zifuchuang。
printf ("\n");
zifuchuang (); //第四次调用函数zifuchuang。
zifuchuang (); //第五次调用函数zifuchuang。
printf ("\n");
zifuchuang (); //第六次调用函数zifuchuang。
getchar ();
return 0;
}


void zifuchuang (void)
{
printf ("Smile!");
//  return 0;  //不知道为什么这里不能用 return 0; 这一个语句。先记录下来。20170809。
}

作者在 2017-08-10 00:52:23 补充以下内容
7.编写一个程序,程序中要调用名为 one_three () 的函数。该函数要在一行中显示单词“one”。
  再调用 two () 函数,然后在另一行显示单词“three”。函数 two () 应该能显示单词“two”。
  main () 函数应该在调用 One_three () 函数之前显示短语“starting now :”,函数调用之后
  显示“done!”。这样,最后的输出结果应如下所示:


  starting now:
  one
  two
  three

  done!


# include <stdio.h>
void one_three (void);
void two (void);
int main (void)
{
	printf ("starting now: \n");
	one_three ();
	printf ("done!");
	getchar ();
	return 0;

}


void one_three (void)
{
	printf ("one\n");
	two ();
	printf ("three\n");
}

void two (void)
{
	printf ("two\n");
}


学习杂记 | 阅读 1425 次
文章评论,共0条
游客请输入验证码
浏览14734次