#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int main (int argc, string argv[])
{
if (argc != 2)
{
printf ("Usage: ./caesar k\n");
return 1;
}
int k = atoi (argv[1]);
printf ("plaintext: ");
string p = get_string ();
printf ("ciphertext: ");
for (int i = 0, n = strlen (p); i < n; i++)
{
if (isupper (p[i]))
{
int alpha = p[i];
alpha = alpha - 65;
alpha = (alpha + k) % 26;
char cipher = alpha + 65 ;
printf ("%c", cipher);
}
else if (islower (p[i]))
{
int alpha = p[i];
alpha = alpha - 97;
alpha = (alpha + k) % 26;
char cipher = alpha + 97 ;
printf ("%c", cipher);
}
else
{
printf ("%c", p[i]);
}
}
printf ("\n");
return 0;
}
该程序完美运行,没有&#39;返回0&#39;最后但是cs50 pset 具体说明程序应该从main返回0退出。什么 我做错了吗?
答案 0 :(得分:0)
代码对我来说很好。你得到的确切错误是什么?
没有环绕的测试:
$./stack_question 5
plaintext: Hello
ciphertext: Mjqqt
测试环绕:
$./stack_question 36
plaintext: Hello
ciphertext: Rovvy
另外,你可以尝试在工作区上运行一个简单的hello world程序吗?如果没有,我建议您创建一个新工作区(CS50 IDE - &gt; Dashboard - &gt;创建新工作区)有时候c9工作区会让我感到烦恼,我甚至无法运行一个简单的hello world程序
答案 1 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int main (int argc, string argv[])
{
if (argc != 2)
{
printf ("Usage: ./caesar k\n");
return 1;
}
int k = atoi (argv[1]);
printf ("plaintext: ");
string p = get_string ();
printf ("ciphertext: ");
for (int i = 0, n = strlen (p); i < n; i++)
{
if (isupper (p[i]))
{
int alpha = p[i];
alpha = alpha - 65;
alpha = (alpha + k) % 26;
char cipher = alpha + 65 ;
printf ("%c", cipher);
}
else if (islower (p[i]))
{
int alpha = p[i];
alpha = alpha - 97;
alpha = (alpha + k) % 26;
char cipher = alpha + 97 ;
printf ("%c", cipher);
}
else
{
printf ("%c", p[i]);
}
}
printf ("\n");
return 0;
}