在这个程序中,p [2]不与str [j]进行比较,我需要澄清这个问题 [英] In this program p[2] is not compared to str[j], I need clarification of this problem

查看:71
本文介绍了在这个程序中,p [2]不与str [j]进行比较,我需要澄清这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
int main()
{
char plaintext[10];
printf("Enter your plain text\n");
int i,j;
for(i=0;i<3;i++)
scanf("%c",&plaintext[i]);
int n;
printf("Enter the row & column for key matrix");
scanf("%d",&n);
int k[n][n],m[0][n];
char str[]="abcdefghijklmnopqrstuvwxyz";
printf("Enter your key");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&k[i][j]);
printf("\n Your key is %d x %d matrix \n",n,n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",k[i][j]);
printf("\n");
}
for(i=0;i<3;i++)
{
printf("%d",i);
printf("%c",plaintext[i]);
for(j=0;j<25;j++)
{
if(plaintext[2]==str[j])
{
printf("\n %c%c \n",plaintext[i],str[j]);
m[0][i]=j;
}
}
}
printf("Your plaintext equivalent value is");
for(i=0;i<3;i++)
printf("\n%d\n",m[0][i]);
}





我的尝试:



在这个程序中p [2]不与str [j]进行比较,我需要澄清这个问题



What I have tried:

In this program p[2] is not compared to str[j],i need clarification of this problem

推荐答案

正如CPallini所说,那里你的代码中没有p [2] - 所以我们甚至无法为你解决这个问题。说实话,即使我们想要,但我们无论如何都无法提供帮助,因为我们不知道您的输入应该是什么,您期望的输出,甚至应用程序应该做什么!



因此,请使用调试器,并在运行时查看代码。

在函数的第一行放置一个断点,然后通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!



自己做一个青睐:缩进你的代码。在它目前的从左到右边距格式中,很难弄清楚发生了什么。缩进它,并且变得更容易看到与以下内容相关:

As CPallini says, there is no "p[2]" in your code - so we can't even begin to fix this for you. And to be honest, even if we wanted to, we couldn't do much to help anyway, as we have no idea what your inputs should be, what outputs you expect, or even what the app is supposed to do!

So use the debugger, and look at your code while it runs.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

And do yourself a favour: indent your code. in it's current "flat-to-the-left-margin" format it's very difficult to work out what is going on. Indent it, and it becomes a lot easier to see that is related to what:
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",k[i][j]);
printf("\n");
}

与以下相比:

Compared with:

for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
        printf("%d\t",k[i][j]);
    printf("\n");
    }

例如。



当你开始总是使用花括号时,这也是一个非常好的主意,即使你只想要一行代码:

for example.

It's also a very good idea when you are starting out to always use curly braces, even if you only want a single line of code:

for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
        {
        printf("%d\t",k[i][j]);
        }
    printf("\n");
    }

因为它在修改代码时会更加安全。

as it makes it a lot safer when you modify the code.


如果你想比较字符串,请使用strcmp



并重新考虑修复的策略 plaintext [2]字符与运行str [j]进行比较。你测试str中的出现明文[2]。



提示:用缩进和所有大括号编写更清晰的代码,使其更容易理解。最多为其他人...
If you want to compare strings use strcmp.

And rethink the strategy that the fix plaintext[2] character is compared with the running str[j]. You test of the occurance plaintext[2] of in str.

Tip: write cleaner code with indents and all braces to make it more understandable. At most for others...


首先,使用缩进,它有助于阅读代码

First of all, use indentation, it help reading the code
#include<stdio.h>
int main()
{
	char plaintext[10];
	printf("Enter your plain text\n");
	int i,j;
	for(i=0;i<3;i++)
		scanf("%c",&plaintext[i]);
	int n;
	printf("Enter the row & column for key matrix");
	scanf("%d",&n);
	int k[n][n],m[0][n];
	char str[]="abcdefghijklmnopqrstuvwxyz";
	printf("Enter your key");
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			scanf("%d",&k[i][j]);
	printf("\n Your key is %d x %d matrix \n",n,n);
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
			printf("%d\t",k[i][j]);
		printf("\n");
	}
	for(i=0;i<3;i++)
	{
		printf("%d",i);
		printf("%c",plaintext[i]);
		for(j=0;j<25;j++)
		{
			if(plaintext[2]==str[j])
			{
				printf("\n %c%c \n",plaintext[i],str[j]);
				m[0][i]=j;
			}
		}
	}
	printf("Your plaintext equivalent value is");
	for(i=0;i<3;i++)
		printf("\n%d\n",m[0][i]);
}




Quote:

在这个程序中p [2]是与str [j]相比,我需要澄清这个问题

In this program p[2] is not compared to str[j], I need clarification of this problem



你怎么知道比较没有完成?问题可能是没有匹配!

唯一知道的方法是使用调试器来确保。

注意:你没有解释应该是什么做代码。



当你不明白你的代码在做什么或为什么它做它做的时候,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


How do you know that the comparison is not done ? The problem can be that there is no match !
The only way to know is to use the debugger to make sure.
Note: you did not explained what is supposed to do the code.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于在这个程序中,p [2]不与str [j]进行比较,我需要澄清这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆