我的计划有什么问题? [英] What is wrong with my program?

查看:80
本文介绍了我的计划有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,需要确定两个辅音之间包含的元音数量。

例如:

输入:水沸腾百度。 br />
输出:5



说明:唯一遵守规则的元音是:

'a'和' e'来自水,'u'和'e'来自百度,第一个'e'来自度数。



我尝试过:



I have a string and need to determine the number of vowels that are included between two consonants.
Eg:
Input:Water boils at one hundred degrees.
Output:5

Explication:The only vowels that respect the rule are:
'a' and 'e' from water,'u' and 'e' from hundred and the first 'e' from degrees.

What I have tried:

#include<stdio.h>
bool vowel(char c)
{
	if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
		return true;
	else
		return false;

}
bool consonant(char c)
{
	if (c >= 'a'&&c <= 'z'&&vowel(c) == false)
		return true;
	else
		return false;
}
int main()
{
	char S[256];
	int i;
	int nr = 0;
	scanf_s("%s", S);
	for (i = 1; S[i]!=EOF; i++)
		if (vowel(S[i]) == true && consonant(S[i - 1]) == true && consonant(S[i + 1]) == true)
			nr++;
	printf("%d", nr);
	
	system("pause");
	return 0;
}

推荐答案

如果输入大写字母会怎样?



此外,EOF定义为-1。你实际上想在for循环中寻找S [i]!= 0,因为在C字符串中以空字符终止,0



另一件事, bool类型未在C语言中定义,因此添加typedef int bool;其中的语句以及true和false的宏定义分别为1和0.
What happens if you enter a capital letter?

Also, EOF is defined as -1. You actually want to look for S[i] != 0 in the for loop because in C strings are terminated with a null character, 0.

Another thing, the bool type is not defined in the C language so add a "typedef int bool;" statement in there along with macros definitions for true and false as 1 and 0 respectively.
typedef int bool;

#define true  1
#define false 0

最后,如果你想读一行含有多个单词的文字得到(S);是个不错的选择。它会读取字符,直到收到换行符或EOF。

Lastly, if you want to read a line of text containing multiple words gets( S ); is a good option. It reads characters until a newline or EOF is received.


您的代码行为不符合您的预期,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

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

调试器中没有魔法,它不知道你的cpde应该做什么,它没有找到bug,它只是通过显示帮助你你发生了什么事。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



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



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

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试你的程序(踩踏和断点)|学习C ++ [ ^ ]

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

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
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 know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
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.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
引用:

但是当我在c ++上创建这个程序时,我发现当我用w作为大写字母写Water时, a'不尊重规则。

However when i made this programm on c++,i found out that when for example i write "Water" with w as capital letter,'a' doesn't respect the rule.



因为你的代码只是小写,你需要在你的代码中添加大写字母。


Because your code is only lowercase, you need to add uppercase to your code.

引用:

其次,在c ++中可以将EOF定义为空字符吗?

Secondly,in c++ can EOF be defined as null character?



是的,您可以将EOF定义为0 char ,只需将其包含在您的代码中。

C语言:#define指令(宏定义) [ ^ ]

#define Directive(C-C ++) [ ^ ]


几个问题:

1.你的标签是C;你用bool,true,false; C.不存在。

2.正如里克所说,你不使用EOF; EOF =文件结束。你需要检查0

3.重要的是,scanf只读取你的第一个单词并忽略第一个空格后的所有其他单词。你可以使用fgets,获得。

4.另外,正如里克所说;你应该写大写字母;
few issues:
1. Your tag says C; you used bool, true, false; does not exists in C.
2. As Rick mentioned you do not use EOF; EOF=end of file. You need to check 0
3. Importantly scanf only read your first word and ignore every other words after the first space. You can use fgets, gets.
4. Also, as Rick mentioned; you should address capital letters;


这篇关于我的计划有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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