C通过解剖第8章练习6 [英] C By Dissection chapter 8 exercise 6

查看:60
本文介绍了C通过解剖第8章练习6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想知道是否有人可以帮助我以下内容?


编写一个以循环方式移动五个字符变量的存储值的函数。您的功能应该以下列方式工作。假设c1,c2,...,c5是char类型的变量,并假设这些变量的值分别是A,B,......E。函数调用shift(& c1,& c2,& c3,& c4,& c5)应该使变量c1,c2,...,c5具有值''B''''' C'','D'',''E'',''A''。你的函数定义应该如下开始:


void shift(char * p1,char * p2,char * p3,char * p4,char * p5)

{

....

}


通过五次调用来测试你的功能并依次打印出来BCDEA,CDEAB,DEABC,EABCD和ABCDE。


--------------------------- -------------------------------------------------- -----------------------------------


我在想为什么练习要求启动上面的功能定义?


任何帮助都将不胜感激。

提前谢谢。

解决方案

Erm,因为有5个字符变量需要更改,因此需要5个输入参数,因为函数必须更改它们传递的变量的值,所以需要通过引用传递(即char *)而不是值(char)。


如果你遇到麻烦帽子问题然后尝试这个更简单的相关问题。


编写一个函数来交换2个字符变量的存储值。假设c1和c2是char类型的变量,并假设这些变量的值分别是''A''和''B''。函数调用swop(& c1,& c2)应该使变量c1和c2分别具有值B和A。你的函数定义应该如下开始:


void swop(char * p1,char * p2)

{

.. ..

}


通过调用两次并依次打印BA和AB来测试你的功能。


< blockquote>对不起,我没跟着你。我有我的程序书,但没有关于切换的例子。我试图想出这个但是...... ????这就是我已经拥有的。


#include< stdio.h>


void shift(char * p1,char * p2,char * p3,char * p4,char * p5)

{

char c1 =''A'',c2 =''B'',c3 =''C'',c4 =''D'',c5 =''E'';

int tmp = 0;

}


我过早地试图编译它并返回错误。


我希望有人能把我放在正确的轨道上。


void shift(char * p1,char * p2,char * p3,char * p4,char * p5)

{

char temp;

temp = * p1; * p1 = * p2; * p2 = * p3; * p3 = * p4; * P4 = P5 *; * p5 = temp;

}


Hello, I was wondering if someone could help me with the following?

Write a function that shifts the stored value of five character variables in a circular fashion. Your function should work in the following way. Suppose that c1, c2,...,c5 are variables of type char, and suppose that the values of these variables are ''A'' , ''B'' , ....'' E'', respectively. The function call shift (&c1, &c2, &c3, &c4, &c5) should cause the variables c1, c2,..., c5 to have the values ''B'', ''C'', ''D'', ''E'', ''A'', respectively. Your function definition should start as follows:

void shift(char *p1, char *p2, char *p3, char *p4, char *p5)
{
....
}

Test your function by calling it five times and printing out, in turn, BCDEA, CDEAB, DEABC, EABCD, and ABCDE.

----------------------------------------------------------------------------------------------------------------

I was wondering why the exercise calls for starting the function definition as above?

Any help would be greatly appreciated.
Thank you in advance.

解决方案

Erm because there are 5 character variables to be changed so 5 input parameters are required and since the function has to change the value of the variables they are passed they need to be passed by reference (i.e. char *) rather than by value (char).

If you are having trouble getting you head round that problem then try this simpler related problem.

Write a function that swops the stored value of 2 character variables. Suppose that c1 and c2 are variables of type char, and suppose that the values of these variables are ''A'' and ''B'' respectively. The function call swop(&c1, &c2) should cause the variables c1 and c2 to have the values ''B'' and ''A'' respectively. Your function definition should start as follows:

void swop(char *p1, char *p2)
{
....
}

Test your function by calling it twice and printing out, in turn, BA and AB.


I am sorry I am not following you. I have my the program book, but there is no examples that talk about switching. I have tried to figure this out but.....???? Here is what I have already.

#include <stdio.h>

void shift(char *p1, char *p2, char *p3, char *p4, char *p5)
{
char c1 = ''A'', c2 = ''B'', c3 = ''C'', c4 = ''D'', c5 = ''E'';
int tmp = 0;
}

I have prematurely tried to compile this and come back with errors.

I am hoping that someone can put me on the right track.


void shift(char *p1, char *p2, char *p3, char *p4, char *p5)
{
char temp;
temp=*p1; *p1 = *p2; *p2 = *p3; *p3= *p4; *p4=*p5; *p5 = temp;
}


这篇关于C通过解剖第8章练习6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆