对于Ceasar',用C包装;s代码

对于Ceasar',用C包装;s代码,c,arrays,file,word-wrap,C,Arrays,File,Word Wrap,嘿,伙计,上次我发帖子的时候我有点邋遢。希望这次它看起来会好很多。如果你决定帮助我,谢谢你抽出时间。我真的需要它。无论如何,问题来了。我需要对我的代码进行总结,我听说你可以用模来做,但我不确定我做得是否正确,因为我没有得到正确的结果 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int main () { char s[200]; /

嘿,伙计,上次我发帖子的时候我有点邋遢。希望这次它看起来会好很多。如果你决定帮助我,谢谢你抽出时间。我真的需要它。无论如何,问题来了。我需要对我的代码进行总结,我听说你可以用模来做,但我不确定我做得是否正确,因为我没有得到正确的结果

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main () {
   char s[200]; //blank array//
   int mess;
   printf("Generations have wondered how the Ceasar Code works\n");
   printf("Please choose a number to mess up (encode) the current file\n");
   scanf("%d", &mess);
   mess = mess % 26;
   FILE *ptof = fopen("Rock.txt", "r");
   char a[200];//fill array with characters from file//
   int i=0;
   while( (a[i++]=fgetc(ptof)) != EOF && i < 89) { //get character from file//
   }
   a[i] = '\0'; /* null-terminate the string */
   i = 0;

   do{
      printf("%c",a[i++]);
   } while  (a[i] != '\0'); /* print until hit \0 */
   int j = 0;
   for (j = 0; j < 89; j++){
      s[j] = a[j] + mess;
   }
   printf("%s\n", s);

   fclose(ptof);
   return 0;
}
#包括
#包括
#包括
#包括
int main(){
char s[200];//空白数组//
int mess;
printf(“几代人都想知道Ceasar代码是如何工作的\n”);
printf(“请选择一个数字将当前文件弄乱(编码)”;
scanf(“%d”、&mess);
混乱=混乱%26;
文件*ptof=fopen(“Rock.txt”,“r”);
char a[200];//用文件中的字符填充数组//
int i=0;
而((a[i++]=fgetc(ptof))!=EOF&&i<89){//从文件中获取字符//
}
a[i]='\0';/*null终止字符串*/
i=0;
做{
printf(“%c”,a[i++]);
}while(a[i]!='\0');/*打印直到点击\0*/
int j=0;
对于(j=0;j<89;j++){
s[j]=a[j]+mess;
}
printf(“%s\n”,s);
fclose(ptof);
返回0;
}

s[j]=a[j]+mess
也需要模运算

s[j]=a[j]+mess
也需要模运算

这里有很大的改进空间。是否确实要将可打印字符映射为(可能)不可打印字符?或者你只是想对字母表中的字母做一个凯瑟变换?为什么任意限制90个字符的输入?使用scanf从来都不是一个好主意(在写代码的20多年中,我离开学校后就从未使用过它)。将移位传递给stdin而不是作为参数,这会使您的程序很难用作过滤器。例如,如果你能把一个字符串移动4,然后移动13,再移动9,然后看到你得到了原来的文本,那就太好了。(例如
不应报告差异)

以下是一些想法:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

FILE *
xfopen( const char *path, const char *mode )
{
        FILE *ifp = fopen( path, mode );
        if( ifp == NULL ) {
                perror( path );
                exit( 1 );
        }
        return ifp;
}

int
main( int argc, char **argv )
{
        int mess = argc > 1 ? strtol( argv[1], NULL, 10 ) % 26 : 13;
        FILE *ptof = argc > 2 ? xfopen( argv[2], "r" ) : stdin;
        int c;

        while( ( c = fgetc( ptof )) != EOF ) {
                if( isupper( c ))
                        c = 'A' + ( c - 'A' + mess ) % 26;
                if( islower( c ))
                        c = 'a' + ( c - 'a' + mess ) % 26;
                putchar( c );
        }
        return 0;
}
#包括
#包括
#包括
文件*
xfopen(常量字符*路径,常量字符*模式)
{
文件*ifp=fopen(路径、模式);
如果(ifp==NULL){
佩罗尔(路径);
出口(1);
}
返回ifp;
}
int
主(内部argc,字符**argv)
{
int mess=argc>1?strtol(argv[1],NULL,10)%26:13;
文件*ptof=argc>2?xfopen(argv[2],“r”):stdin;
INTC;
而((c=fgetc(ptof))!=EOF){
如果(上(c))
c='A'+(c-'A'+混乱)%26;
if(岛下(c))
c='a'+(c-'a'+混乱)%26;
普查尔(c);
}
返回0;
}

这方面还有很大的改进空间。是否确实要将可打印字符映射为(可能)不可打印字符?或者你只是想对字母表中的字母做一个凯瑟变换?为什么任意限制90个字符的输入?使用scanf从来都不是一个好主意(在写代码的20多年中,我离开学校后就从未使用过它)。将移位传递给stdin而不是作为参数,这会使您的程序很难用作过滤器。例如,如果你能把一个字符串移动4,然后移动13,再移动9,然后看到你得到了原来的文本,那就太好了。(例如
不应报告差异)

以下是一些想法:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

FILE *
xfopen( const char *path, const char *mode )
{
        FILE *ifp = fopen( path, mode );
        if( ifp == NULL ) {
                perror( path );
                exit( 1 );
        }
        return ifp;
}

int
main( int argc, char **argv )
{
        int mess = argc > 1 ? strtol( argv[1], NULL, 10 ) % 26 : 13;
        FILE *ptof = argc > 2 ? xfopen( argv[2], "r" ) : stdin;
        int c;

        while( ( c = fgetc( ptof )) != EOF ) {
                if( isupper( c ))
                        c = 'A' + ( c - 'A' + mess ) % 26;
                if( islower( c ))
                        c = 'a' + ( c - 'a' + mess ) % 26;
                putchar( c );
        }
        return 0;
}
#包括
#包括
#包括
文件*
xfopen(常量字符*路径,常量字符*模式)
{
文件*ifp=fopen(路径、模式);
如果(ifp==NULL){
佩罗尔(路径);
出口(1);
}
返回ifp;
}
int
主(内部argc,字符**argv)
{
int mess=argc>1?strtol(argv[1],NULL,10)%26:13;
文件*ptof=argc>2?xfopen(argv[2],“r”):stdin;
INTC;
而((c=fgetc(ptof))!=EOF){
如果(上(c))
c='A'+(c-'A'+混乱)%26;
if(岛下(c))
c='a'+(c-'a'+混乱)%26;
普查尔(c);
}
返回0;
}

嗯?即使是应用于问题的“包装”标记也没有意义。有一个很好的论点是,您应该始终关闭输出流并报告任何错误,但关闭输入没有多大意义。如果您没有检查错误,那么关闭输入就毫无意义。(严格来说这不是真的,关闭文件是个好主意,但是如果你不检查错误,你就会错过这样做的大部分好处。)哈?即使是应用于问题的“包装”标记也没有意义。有一个很好的论点是,您应该始终关闭输出流并报告任何错误,但关闭输入没有多大意义。如果您没有检查错误,那么关闭输入就毫无意义。(严格来说,这并不是真的,关闭文件是个好主意,但如果不检查错误,则会错过这样做的大部分好处。)