【C语言求助】

飞禽走兽卍一2b 2021-03-06 09:37:29
(本代码目的是将输入的字符换成后面第四个字母,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'); } } 请大家帮忙看看是哪的问题,谢谢!
...全文
252 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
源代码大师 2021-05-03
  • 打赏
  • 举报
回复
C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html C和C++算法完整教程:https://blog.csdn.net/it_xiangqiang/category_10768339.html
自信男孩 2021-03-08
  • 打赏
  • 举报
回复
#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;
}

供参考~
paena 2021-03-08
  • 打赏
  • 举报
回复
再读密码循环前,再加个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'); } } 请大家帮忙看看是哪的问题,谢谢!
自信男孩 2021-03-08
  • 打赏
  • 举报
回复
引用 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]

只看加密和解密部分吧,其他的不用考虑~
  • 打赏
  • 举报
回复
引用 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;
}

供参考~
看不懂不过还是谢谢啦!
  • 打赏
  • 举报
回复
引用 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] 谢谢!现在知道了
qzjhjxj 2021-03-06
  • 打赏
  • 举报
回复
引用 楼主 飞禽走兽卍一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'); } } 请大家帮忙看看是哪的问题,谢谢!
一.C语言基础 1.C语言特点(识记); 2.C语言程序基本组成(识记): 3.基本数据类型: 3.1 标识符与基本数据类型(识记), 3.2 常量与变量(领会) 3.3 内存的概念(识记) 4.基本输入、输出函数(领会): 5.运算符与表达式(简单应用): 5.1 运算符的优先级与结合性 二.程序控制结构 1.C语言的语句(识记): 2.顺序结构(领会): 3.分支结构(简单应用): 4.循环结构(综合应用): 5 算法特点 6 流程图 三.构造型数据 1.数组(综合应用): 1.1 定义和引用 1.2 字符数组 1.3 指针和数组 2.结构类型: 2.1 结构类型的概念 2.2 结构类型定义及结构变量说明 2.3 结构变量的初始化 2.4 结构数组的初始化 3.联合类型(识记): 3.1 联合类型的概念 3.2 联合类型定义和联合变量说明 3.3 联合类型的使用 3.4 Struct 和 Union区别 4.枚举型(识记): 4.1 枚举型的定义 4.2 使用枚举型变量 5.typedef的用途(识记): 四.指针 1.指针与指针变量(识记): 2.指针运算符(领会): 3. 指针与函数 4.指针数组与指向指针的指针(识记): 5.指针与结构(领会): 6. 难点和易混淆 五.函数 1.常见的系统库函数(识记): 2.用户自定义函数(简单应用): 2.1函数定义 2.2 函数调用 2.3 函数声明 2.4 函数返回值 2.5 函数参数 3.函数之间的数据传递(领会): 4.函数的嵌套调用及递归调用(领会): 5.局部变量与全局变量(识记): 6.变量的存储类型与变量的初始化(领会): 7.编译预处理(领会): 六.文件 1.文件的基本概念,C语言中的两种文件(识记) 2.文件的打开、关闭和文件结束测试,文件的读写,文件的定位(识记) 2.1文件操作函数 2.2 文件权限 七.算法与编程(综合应用) 1 使用Turbo C集成开发环境调试程序 1.1.源程序的编写、编辑与改错(领会); 1.2.集成环境下的求助Help(识记); 1.3.程序的编译与目标代码的生成(识记); 1.4.程序的调试(综合应用): 1.5.了解Turbo C程序的常见错误提示(识记)。 2 重点编程题 八 位运算 1. & 2. | 3. ^ 4. ~ 5. << 6. >> ———————————————— 版权声明:本文为CSDN博主「kaikai_sk」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/kaikai_sk/article/details/106061539

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧