卡在 Caesar.c
Got stuck with Caesar.c
我正在尝试 运行 来自 edx Introduction to programming 的程序作业 caesar.c。它需要一个能够使用凯撒加密来加密字符串的程序:因此,用户必须输入一个密钥(命令行);例如,密钥为 2 的 'A' 字符需要用 'C' 字符加密;当您必须输入大于 26 的键时,问题就开始了,这是字母的数量。例如,对于 27 键和 'A' 字符,程序必须 return 'B' 类似于 1.
我尝试将字符的 ASCII 值转换为 0 到 26 之间的字母值,以便在键等于或大于 26 时使用模数运算符。
return 是我的分段错误。任何人都可以帮助我对我的错误原因提出一些建议吗?
程序如下:
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int key;
// function for an alphabetic value with non capital letters
int alpha_low( char c )
{
int alpha_value;
alpha_value = (int) c - 97;
return alpha_value + ( key % 26 );
}
// function to return to ascii valuee for non capital letters
char ascii_low( char c )
{
return (char) alpha_low( c ) + 97;
}
// function for an alphabetic value with capital letters
int alpha_up( char c )
{
int alpha_value;
alpha_value = (int) c - 65;
return alpha_value + ( key % 26 );
}
// function to return to ascii value for capital letters
char ascii_up( char c )
{
return (char) alpha_up( c ) + 65;
}
int main(int argc, string argv[])
{
int result;
string p;
key = atoi( argv[1] );
if( argc != 2 || key < 0 )
{
printf("Usage: ./caesar key(positive integer)\n");
return 1;
}
printf("Please, write a plaintext: ");
p = GetString();
for( int i = 0, n = strlen(p); i < n; i++)
{
if ( isalpha(p[i]) )
{
if ( islower(p[i]) )
{
result = alpha_low( p[i] );
printf("%c", ascii_low( p[i] ));
}
else if( islower( p[i]) )
{
result = alpha_up( p[i] );
printf("%c", ascii_up( p[i]) );
}
}
}
return 0;
}
凯撒字母字符的函数应该是这样的(分解为基本步骤):
int caesar_lower(int c,int key) {
int v = c-'a'; // translate 'a'--'z' to 0--25
v = v+key; // translate 0--25 to key--key+25
v = v%26; // translate key--key+25 to key--25,0--key-1
v = v+'a'; // translate back 0--25 to 'a'--'z'
return v;
}
我正在尝试 运行 来自 edx Introduction to programming 的程序作业 caesar.c。它需要一个能够使用凯撒加密来加密字符串的程序:因此,用户必须输入一个密钥(命令行);例如,密钥为 2 的 'A' 字符需要用 'C' 字符加密;当您必须输入大于 26 的键时,问题就开始了,这是字母的数量。例如,对于 27 键和 'A' 字符,程序必须 return 'B' 类似于 1.
我尝试将字符的 ASCII 值转换为 0 到 26 之间的字母值,以便在键等于或大于 26 时使用模数运算符。 return 是我的分段错误。任何人都可以帮助我对我的错误原因提出一些建议吗?
程序如下:
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int key;
// function for an alphabetic value with non capital letters
int alpha_low( char c )
{
int alpha_value;
alpha_value = (int) c - 97;
return alpha_value + ( key % 26 );
}
// function to return to ascii valuee for non capital letters
char ascii_low( char c )
{
return (char) alpha_low( c ) + 97;
}
// function for an alphabetic value with capital letters
int alpha_up( char c )
{
int alpha_value;
alpha_value = (int) c - 65;
return alpha_value + ( key % 26 );
}
// function to return to ascii value for capital letters
char ascii_up( char c )
{
return (char) alpha_up( c ) + 65;
}
int main(int argc, string argv[])
{
int result;
string p;
key = atoi( argv[1] );
if( argc != 2 || key < 0 )
{
printf("Usage: ./caesar key(positive integer)\n");
return 1;
}
printf("Please, write a plaintext: ");
p = GetString();
for( int i = 0, n = strlen(p); i < n; i++)
{
if ( isalpha(p[i]) )
{
if ( islower(p[i]) )
{
result = alpha_low( p[i] );
printf("%c", ascii_low( p[i] ));
}
else if( islower( p[i]) )
{
result = alpha_up( p[i] );
printf("%c", ascii_up( p[i]) );
}
}
}
return 0;
}
凯撒字母字符的函数应该是这样的(分解为基本步骤):
int caesar_lower(int c,int key) {
int v = c-'a'; // translate 'a'--'z' to 0--25
v = v+key; // translate 0--25 to key--key+25
v = v%26; // translate key--key+25 to key--25,0--key-1
v = v+'a'; // translate back 0--25 to 'a'--'z'
return v;
}