user-0 提问: 【C语言求助】

回复:
user: user-1
content: C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html C和C++算法完整教程:https://blog.csdn.net/it_xiangqiang/category_10768339.html

user: user-2
content: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h>   #define FILE_NAME_LEN    32 #define MAX_KEY_NUM     26 enum { 	ENCRYPT_DATA = 1, 	DECRYPT_DATA, 	FORCE_DECRYPT_DATA,     EXIT_PROCESS };   /*加密函数，把字符向右循环移位n*/ char encrypt(char ch, int n) {     while(ch >= 'A' && ch <= 'Z') {         return ('A'+(ch-'A'+n)%26);     }     while(ch >= 'a' && ch <= 'z') {         return ('a'+(ch-'a'+n)%26);     }     return ch; }  /*菜单，1.加密，2.解密，3.暴力破解，密码只能是数字*/ static void menu(void) {     printf("\n=========================================================");     printf("\n1.Encrypt the file");     printf("\n2.Decrypt the file");     printf("\n3.Force decrypt file");     printf("\n4.Quit\n");     printf("=========================================================\n");      return; }  static int get_opt(void) {     int option, ret;      printf("Please input your choice: ");     ret = scanf("%d", &option);     while (ret != 1) {         printf("Please input your choice: ");         ret = scanf("%d", &option);     }      return option; }  /* Open a file, then return file decription. */ static FILE *open_file(const char *filename, const char *openmode) { 	FILE *fp = NULL;  	fp = fopen(filename, openmode); 	if(!fp) {        	printf("Can not open the outfile: %s, %s!\n", filename, strerror(errno)); 		exit(0);     }  	return fp; }  /* Copy data from source file to destination file. */ int copy_data(FILE *src_fp, FILE *dst_fp, int key) { 	int data, cnt = 0;  	while(!feof(src_fp)) {	/* 加密 */         data = encrypt(fgetc(src_fp), key);         if (data != EOF) {             putchar(data);             fputc(data, dst_fp); 		    cnt++;         } 	}   	return cnt; }  int main(int argc, const char *argv[]) {     char src_file[FILE_NAME_LEN];     char dst_file[FILE_NAME_LEN];     FILE *src_fp, *dst_fp;     int option, key, ret;     int total_num, i;      while (1) {         menu();         option = get_opt();         if (option == EXIT_PROCESS) {             printf("Goodbye!\n");             break;         }         printf("Please input src file: ");         scanf("%s", src_file);         src_fp = open_file(src_file, "r");         if (!src_fp)             return 1;         printf("Please input dst file: ");         scanf("%s", dst_file);         dst_fp = open_file(dst_file, "w");         if (!dst_fp) {             fclose(src_fp);             return 1;         }         if (option != FORCE_DECRYPT_DATA) {             printf("Please input the key: ");             ret = scanf("%d", &key);             while (ret != 1) {                 printf("Please input the key: ");                 ret = scanf("%d", &key);             }         }         switch (option) {         case ENCRYPT_DATA:             total_num = copy_data(src_fp, dst_fp, key);             printf("Encrypt %d character(s)\n", total_num); 	        fclose(src_fp); 	        fclose(dst_fp);             break;          case DECRYPT_DATA:             key = 26 - key;             total_num = copy_data(src_fp, dst_fp, key);             printf("Encrypt %d character(s)\n", total_num); 	        fclose(src_fp); 	        fclose(dst_fp);             break;          case FORCE_DECRYPT_DATA:             for (i = 1; i < MAX_KEY_NUM; i++) {                 fseek(src_fp, 0l, SEEK_SET);                 fseek(dst_fp, 0l, SEEK_SET);                 total_num = copy_data(src_fp, dst_fp, 26-i);                 printf("Current encrypt's key is %d\n", i);                 printf("Encrypt %d character(s)\n", total_num);                 printf("Press 'Q' to quit and other key to continue......\n");                 option = getchar();                 if (option == 'Q' || option == 'q') {                     break;                 }             } 	        fclose(src_fp); 	        fclose(dst_fp);             break;         }         printf("\nEncrypt is over!\n");      }     return 0; }  供参考～

user: user-3
content: 再读密码循环前，再加个getchar()，把前面的回车读掉，用getchar()都要注意这个问题。  引用 楼主 飞禽走兽卍一2b 的回复:（本代码目的是将输入的字符换成后面第四个字母，China——>Glmre，前面那段代码是并列执行的，便于让各位看到结果的差异，即口Glmre）  问题描述： 当独立执行第二段代码时输出：Glmre 当和第一段代码并列执行时输出：口Glmr   #include <math.h> #include <stdio.h> int main(void) {     //以下为并列的代码，不是主要问题    {                   printf("hw 3.2\n");            float way,temp;             printf("请输入金额：\n");             scanf("%f",&temp);            printf("请输入存款方式(1-5)：\n");                         scanf("%f",&way);              if(way==1)                  {                   temp*=(1+5*0.03);                  printf("result= %f \n\n",temp);            }            else if(way==2)          {                 temp*=(1+2*0.021);                                                 temp*=(1+3*0.0275);                printf("result= %f \n\n",temp);          }           else if(way==3)           {                   temp*=(1+3*0.0275);                                       temp*=(1+2*0.021);                printf("result= %f \n\n",temp);          }           else if(way==4)         {                  temp*=pow(1.015,5);                printf("result= %f \n\n",temp);         }          else         {                temp*=pow(1+0.0035/4,20);                               printf("result=%f \n\n",temp);          }       }    //以下为提问的代码     {          printf("hw 3.6\n");                 printf("请输入五位数密码：\n");                 char c[5];          int i;    getchar(); //这里要加个getchar()把前面的回车读掉，才是你这个问题的根本原因          for(i=0;i<5;i++)      //for循环输入多个字符        {                   c[i]=getchar();   //将输入的字符赋给变量        }               for(i=0;i<5;i++)         {                     c[i]=c[i]+4;         //将输入的字符换成后面第四个                              //c[5]='\0';           这里越界了，但不是根本问题             putchar(c[i]);         }                 putchar('\n');      }  } 请大家帮忙看看是哪的问题，谢谢！

user: user-2
content: 引用 5 楼 飞禽走兽卍一2b 的回复:[quote=引用 3 楼 自信男孩的回复:]#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h>   #define FILE_NAME_LEN    32 #define MAX_KEY_NUM     26 enum { 	ENCRYPT_DATA = 1, 	DECRYPT_DATA, 	FORCE_DECRYPT_DATA,     EXIT_PROCESS };   /*加密函数，把字符向右循环移位n*/ char encrypt(char ch, int n) {     while(ch >= 'A' && ch <= 'Z') {         return ('A'+(ch-'A'+n)%26);     }     while(ch >= 'a' && ch <= 'z') {         return ('a'+(ch-'a'+n)%26);     }     return ch; }  /*菜单，1.加密，2.解密，3.暴力破解，密码只能是数字*/ static void menu(void) {     printf("\n=========================================================");     printf("\n1.Encrypt the file");     printf("\n2.Decrypt the file");     printf("\n3.Force decrypt file");     printf("\n4.Quit\n");     printf("=========================================================\n");      return; }  static int get_opt(void) {     int option, ret;      printf("Please input your choice: ");     ret = scanf("%d", &option);     while (ret != 1) {         printf("Please input your choice: ");         ret = scanf("%d", &option);     }      return option; }  /* Open a file, then return file decription. */ static FILE *open_file(const char *filename, const char *openmode) { 	FILE *fp = NULL;  	fp = fopen(filename, openmode); 	if(!fp) {        	printf("Can not open the outfile: %s, %s!\n", filename, strerror(errno)); 		exit(0);     }  	return fp; }  /* Copy data from source file to destination file. */ int copy_data(FILE *src_fp, FILE *dst_fp, int key) { 	int data, cnt = 0;  	while(!feof(src_fp)) {	/* 加密 */         data = encrypt(fgetc(src_fp), key);         if (data != EOF) {             putchar(data);             fputc(data, dst_fp); 		    cnt++;         } 	}   	return cnt; }  int main(int argc, const char *argv[]) {     char src_file[FILE_NAME_LEN];     char dst_file[FILE_NAME_LEN];     FILE *src_fp, *dst_fp;     int option, key, ret;     int total_num, i;      while (1) {         menu();         option = get_opt();         if (option == EXIT_PROCESS) {             printf("Goodbye!\n");             break;         }         printf("Please input src file: ");         scanf("%s", src_file);         src_fp = open_file(src_file, "r");         if (!src_fp)             return 1;         printf("Please input dst file: ");         scanf("%s", dst_file);         dst_fp = open_file(dst_file, "w");         if (!dst_fp) {             fclose(src_fp);             return 1;         }         if (option != FORCE_DECRYPT_DATA) {             printf("Please input the key: ");             ret = scanf("%d", &key);             while (ret != 1) {                 printf("Please input the key: ");                 ret = scanf("%d", &key);             }         }         switch (option) {         case ENCRYPT_DATA:             total_num = copy_data(src_fp, dst_fp, key);             printf("Encrypt %d character(s)\n", total_num); 	        fclose(src_fp); 	        fclose(dst_fp);             break;          case DECRYPT_DATA:             key = 26 - key;             total_num = copy_data(src_fp, dst_fp, key);             printf("Encrypt %d character(s)\n", total_num); 	        fclose(src_fp); 	        fclose(dst_fp);             break;          case FORCE_DECRYPT_DATA:             for (i = 1; i < MAX_KEY_NUM; i++) {                 fseek(src_fp, 0l, SEEK_SET);                 fseek(dst_fp, 0l, SEEK_SET);                 total_num = copy_data(src_fp, dst_fp, 26-i);                 printf("Current encrypt's key is %d\n", i);                 printf("Encrypt %d character(s)\n", total_num);                 printf("Press 'Q' to quit and other key to continue......\n");                 option = getchar();                 if (option == 'Q' || option == 'q') {                     break;                 }             } 	        fclose(src_fp); 	        fclose(dst_fp);             break;         }         printf("\nEncrypt is over!\n");      }     return 0; }  供参考～ 看不懂不过还是谢谢啦！[/quote]  只看加密和解密部分吧，其他的不用考虑～

user: user-0
content: 引用 3 楼 自信男孩的回复:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h>   #define FILE_NAME_LEN    32 #define MAX_KEY_NUM     26 enum { 	ENCRYPT_DATA = 1, 	DECRYPT_DATA, 	FORCE_DECRYPT_DATA,     EXIT_PROCESS };   /*加密函数，把字符向右循环移位n*/ char encrypt(char ch, int n) {     while(ch >= 'A' && ch <= 'Z') {         return ('A'+(ch-'A'+n)%26);     }     while(ch >= 'a' && ch <= 'z') {         return ('a'+(ch-'a'+n)%26);     }     return ch; }  /*菜单，1.加密，2.解密，3.暴力破解，密码只能是数字*/ static void menu(void) {     printf("\n=========================================================");     printf("\n1.Encrypt the file");     printf("\n2.Decrypt the file");     printf("\n3.Force decrypt file");     printf("\n4.Quit\n");     printf("=========================================================\n");      return; }  static int get_opt(void) {     int option, ret;      printf("Please input your choice: ");     ret = scanf("%d", &option);     while (ret != 1) {         printf("Please input your choice: ");         ret = scanf("%d", &option);     }      return option; }  /* Open a file, then return file decription. */ static FILE *open_file(const char *filename, const char *openmode) { 	FILE *fp = NULL;  	fp = fopen(filename, openmode); 	if(!fp) {        	printf("Can not open the outfile: %s, %s!\n", filename, strerror(errno)); 		exit(0);     }  	return fp; }  /* Copy data from source file to destination file. */ int copy_data(FILE *src_fp, FILE *dst_fp, int key) { 	int data, cnt = 0;  	while(!feof(src_fp)) {	/* 加密 */         data = encrypt(fgetc(src_fp), key);         if (data != EOF) {             putchar(data);             fputc(data, dst_fp); 		    cnt++;         } 	}   	return cnt; }  int main(int argc, const char *argv[]) {     char src_file[FILE_NAME_LEN];     char dst_file[FILE_NAME_LEN];     FILE *src_fp, *dst_fp;     int option, key, ret;     int total_num, i;      while (1) {         menu();         option = get_opt();         if (option == EXIT_PROCESS) {             printf("Goodbye!\n");             break;         }         printf("Please input src file: ");         scanf("%s", src_file);         src_fp = open_file(src_file, "r");         if (!src_fp)             return 1;         printf("Please input dst file: ");         scanf("%s", dst_file);         dst_fp = open_file(dst_file, "w");         if (!dst_fp) {             fclose(src_fp);             return 1;         }         if (option != FORCE_DECRYPT_DATA) {             printf("Please input the key: ");             ret = scanf("%d", &key);             while (ret != 1) {                 printf("Please input the key: ");                 ret = scanf("%d", &key);             }         }         switch (option) {         case ENCRYPT_DATA:             total_num = copy_data(src_fp, dst_fp, key);             printf("Encrypt %d character(s)\n", total_num); 	        fclose(src_fp); 	        fclose(dst_fp);             break;          case DECRYPT_DATA:             key = 26 - key;             total_num = copy_data(src_fp, dst_fp, key);             printf("Encrypt %d character(s)\n", total_num); 	        fclose(src_fp); 	        fclose(dst_fp);             break;          case FORCE_DECRYPT_DATA:             for (i = 1; i < MAX_KEY_NUM; i++) {                 fseek(src_fp, 0l, SEEK_SET);                 fseek(dst_fp, 0l, SEEK_SET);                 total_num = copy_data(src_fp, dst_fp, 26-i);                 printf("Current encrypt's key is %d\n", i);                 printf("Encrypt %d character(s)\n", total_num);                 printf("Press 'Q' to quit and other key to continue......\n");                 option = getchar();                 if (option == 'Q' || option == 'q') {                     break;                 }             } 	        fclose(src_fp); 	        fclose(dst_fp);             break;         }         printf("\nEncrypt is over!\n");      }     return 0; }  供参考～ 看不懂不过还是谢谢啦！

user: user-0
content: 引用 2 楼 paena的回复:再读密码循环前，再加个getchar()，把前面的回车读掉，用getchar()都要注意这个问题。  [quote=引用 楼主 飞禽走兽卍一2b 的回复:]（本代码目的是将输入的字符换成后面第四个字母，China——>Glmre，前面那段代码是并列执行的，便于让各位看到结果的差异，即口Glmre）  问题描述： 当独立执行第二段代码时输出：Glmre 当和第一段代码并列执行时输出：口Glmr   #include <math.h> #include <stdio.h> int main(void) {     //以下为并列的代码，不是主要问题    {                   printf("hw 3.2\n");            float way,temp;             printf("请输入金额：\n");             scanf("%f",&temp);            printf("请输入存款方式(1-5)：\n");                         scanf("%f",&way);              if(way==1)                  {                   temp*=(1+5*0.03);                  printf("result= %f \n\n",temp);            }            else if(way==2)          {                 temp*=(1+2*0.021);                                                 temp*=(1+3*0.0275);                printf("result= %f \n\n",temp);          }           else if(way==3)           {                   temp*=(1+3*0.0275);                                       temp*=(1+2*0.021);                printf("result= %f \n\n",temp);          }           else if(way==4)         {                  temp*=pow(1.015,5);                printf("result= %f \n\n",temp);         }          else         {                temp*=pow(1+0.0035/4,20);                               printf("result=%f \n\n",temp);          }       }    //以下为提问的代码     {          printf("hw 3.6\n");                 printf("请输入五位数密码：\n");                 char c[5];          int i;    getchar(); //这里要加个getchar()把前面的回车读掉，才是你这个问题的根本原因          for(i=0;i<5;i++)      //for循环输入多个字符        {                   c[i]=getchar();   //将输入的字符赋给变量        }               for(i=0;i<5;i++)         {                     c[i]=c[i]+4;         //将输入的字符换成后面第四个                              //c[5]='\0';           这里越界了，但不是根本问题             putchar(c[i]);         }                 putchar('\n');      }  } 请大家帮忙看看是哪的问题，谢谢！   [/quote] 谢谢！现在知道了

user: user-4
content: 引用 楼主 飞禽走兽卍一2b 的回复:（本代码目的是将输入的字符换成后面第四个字母，China——>Glmre，前面那段代码是并列执行的，便于让各位看到结果的差异，即口Glmre）  问题描述： 当独立执行第二段代码时输出：Glmre 当和第一段代码并列执行时输出：口Glmr   #include <math.h> #include <stdio.h> int main(void) {     //以下为并列的代码，不是主要问题    {                   printf("hw 3.2\n");            float way,temp;             printf("请输入金额：\n");             scanf("%f",&temp);            printf("请输入存款方式(1-5)：\n");                         scanf("%f",&way);              if(way==1)                  {                   temp*=(1+5*0.03);                  printf("result= %f \n\n",temp);            }            else if(way==2)          {                 temp*=(1+2*0.021);                                                 temp*=(1+3*0.0275);                printf("result= %f \n\n",temp);          }           else if(way==3)           {                   temp*=(1+3*0.0275);                                       temp*=(1+2*0.021);                printf("result= %f \n\n",temp);          }           else if(way==4)         {                  temp*=pow(1.015,5);                printf("result= %f \n\n",temp);         }          else         {                temp*=pow(1+0.0035/4,20);                               printf("result=%f \n\n",temp);          }       }    //以下为提问的代码     {          printf("hw 3.6\n");                 printf("请输入五位数密码：\n");                 char c[5];          int i;                 for(i=0;i<5;i++)      //for循环输入多个字符        {                   c[i]=getchar();   //将输入的字符赋给变量        }               for(i=0;i<5;i++)         {                     c[i]=c[i]+4;         //将输入的字符换成后面第四个                              c[5]='\0'; 这里对数组越界操作，这句也是多余的，删除就好了                     putchar(c[i]);         }                 putchar('\n');      }  } 请大家帮忙看看是哪的问题，谢谢！