如何将逻辑转换为指针逻辑,所以下面我按照我理解的方式编写代码,但我需要使用指针,就像它在TODO中所说的那样,请指教,TY:)
这就是TODO中没有帮助我理解的内容,(提示:避免做双指针算术,保存一个char *指针 //到外循环中的活动块[?]但在内循环之前。 //然后内循环只关注单个数组 //字符而不是字符串数组。)
循环遍历数组中的每个字符串 //消息块(字符串)并打印出来。不要打印第一个 //每个字符串中的字符,因为它仅用于存储顺序 //块(提示:使用指针算法跳过一个字符。)
////////////////////////////////////////////////////////////////////////////////
//INCLUDES
#include <stdio.h>
#include <string.h>
//macros: constants
#define CHUNK_LENGTH (20+1) //each chunk has twenty characters, we add 1 so
//there is space for the null terminator.
#define NUMBER_OF_CHUNKS 4 //the message is spread across 4 chunks.
#define DECRYPTION_SHIFT 5 //this is the ASCII table shift used for decryption.
//forward declarations
void sort_chunks();
void decrypt_chunks();
void display_chunks();
char chunks[NUMBER_OF_CHUNKS][CHUNK_LENGTH];
int main() {
//copy message into memory.
strcpy(chunks[0], "2i1%fsi%fs%jstwrtzx%");
strcpy(chunks[1], "1'H%nx%vznwp~1%kqf|j");
strcpy(chunks[2], "4R3%Wnyhmnj%%%%%%%%%");
strcpy(chunks[3], "3xzhhjxx3'%2%Ijssnx%");
//the format of a chunk is a single number indicating its order in overall
//message followed by twenty encrypted characters.
//reorder chunks in message by sorting them based on the first digital
//they contain. looking above, one can see they are currently in the order
//2, 1, 4, 3 but should be ordered 1, 2, 3, 4.
sort_chunks();
//shift the characters in the message to produce the original characters.
decrypt_chunks();
//display the decrypted message.
display_chunks();
return 0;
}
//given two strings, swaps their contents in memory.
void swap_strings(char* x, char* y) {
//create a temporary holding place for the data so we don't lose it.
char temp[CHUNK_LENGTH];
strcpy(temp, x);
strcpy(x, y);
strcpy(y, temp);
}
//sorts the strings the global chunks variable by the first character they contain.
void sort_chunks() {
//TODO: Implement sort_chunks(). Using your favorite sorting algorithm (we
// suggest selection sort), sort the array containing the message chunks.
// Sort based on the first character in the chunk - it will always be a
// number. We provide a swap_strings function that you may use. Example
// usage: swap_strings(chunks[0], chunks[1]) will swap the contents of
// the first and second string.
int i, j;
int lowest;
for (i = 0; i < NUM_OF_CHUNKS - 1; i++)
{
lowest = i;
for (j = i + 1; j < NUM_OF_CHUNKS; j++)
{
if (chunks[j][0] < chunks[lowest][0])
lowest = j;
}
swap_strings(chunks[i], chunks[lowest]);
}
}
//for each string in the global chunks variable, shifts the characters in it by
//DECRYPTION_SHIFT.
void decrypt_chunks() {
//TODO: Implement decrypt_chunks(). Loop over each string in the array
// and shift the characters in it by subtracting DECRYPTION_SHIFT value
// from them. Use pointer arithmetic to access individual characters but
// array access to the strings. Remember that C-style strings have a null
// terminator at the end. Do not apply the shift to the terminator.
// (Hint: to avoid doing double pointer arithmetic, save a char* pointer
// to the active chunk[?] in the outer loop but before the inner loop.
// Then the inner loop is only concerned with a single array of
// characters rather than an array of strings.)
int row, col;
for (row = 0; row < NUM_OF_CHUNKS; row++)
{
for (col = 0; col < CHUNK_LENGTH - 1; col++)
{
if (chunks[row][col] != '\0')
chunks[row][col] -= DECRYPTION_SHIFT;
}
}
}
//displays the strings in the global chunks variable
void display_chunks() {
//TODO: Implement display_chunks(). Loop over each string in the array of
// message chunks (strings) and print it. Do not print the first
// character in each string since it is only used to store the order of
// the chunks. (Hint: use pointer arithmetic to skip a character.)
int row, col;
for (row = 0; row < NUM_OF_CHUNKS; row++)
{
for (col = 1; col < CHUNK_LENGTH - 1; col++)
{
if (chunks[row][col] == '\0')
{
printf("\n");
return;
}
printf("%c", chunks[row][col]);
}
}
printf("\n");
}
答案 0 :(得分:0)
让我们说明这一点。让我们使用一个简单的预处理器定义WPOINTERS
来允许您的代码被编译为使用指针或使用原始代码中的两个循环:
//displays the strings in the global chunks variable
void display_chunks() {
//TODO: Implement display_chunks(). Loop over each string in the array of
// message chunks (strings) and print it. Do not print the first
// character in each string since it is only used to store the order of
// the chunks. (Hint: use pointer arithmetic to skip a character.)
int row;
for (row = 0; row < NUMBER_OF_CHUNKS; row++)
{
#ifdef WPOINTERS
char *p = chunks[row];
printf ("%s", p + 1);
#else
int col;
for (col = 1; col < CHUNK_LENGTH - 1; col++)
{
if (chunks[row][col] == '\0')
{
putchar ('\n');
return;
}
putchar (chunks[row][col]);
}
#endif
}
putchar ('\n');
}
如果在编译期间在命令行中指定-DWPOINTERS
,则执行以下代码:
#ifdef WPOINTERS
char *p = chunks[row];
printf ("%s", p + 1);
否则,您的原始代码将被执行:
#else
int col;
for (col = 1; col < CHUNK_LENGTH - 1; col++)
{
if (chunks[row][col] == '\0')
{
putchar ('\n');
return;
}
putchar (chunks[row][col]);
}
在这两种情况下,您都会获得:
$ ./bin/prnwptrsnew
"C is quirky, flawed, and an enormous success." - Dennis M. Ritchie
示例编译字符串将是:
$ gcc -Wall -Wextra -pedantic -std=gnu11 -Ofast -DWPOINTERS -o bin/prnwptrsnew prnwptrs.c
或原始代码,只需省略-DWPOINTERS
:
$ gcc -Wall -Wextra -pedantic -std=gnu11 -Ofast -o bin/prnwptrsnew prnwptrs.c
如果您使用cl.exe
,有/无-DWPOINTERS
的工作方式相同。