- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
用一些示例问题(我没有解决方案)在字符串操作上为 C++ 考试做准备 - 下面是今天的手工作业。尽管它工作正常 - 如果您遇到任何明显的失误/更好的做事方式,那就太棒了,请给我一个简短的说明,我需要快速学习一些 C++ :)
谢谢!
#include <iostream>
#include <cctype>
#include <cstring>
//#include "words.h"
using namespace std;
void reverse(const char un_rev[], char rev[]);
void clean(const char in_string[], char out_string[]);
//produces 'cleaned' copy of the string and passes on to recursive compare
bool compare(const char input_1[], const char input_2[]);
//the recursive bit
bool compare_recur(char input_1[], char input_2[]);
bool palindrome(const char input[]);
//no mixed capps for func below!
int min_pos(int starting_pos, char input[]);
void sort(char input[]);
bool anagram(const char input_1[], const char input_2[]);
int main() {
/*** QUESTION 1 ***/
char reversed[9];
reverse("lairepmi", reversed);
cout << "'lairepmi' reversed is '" << reversed << "'" << endl;
reverse("desserts", reversed);
cout << "'desserts' reversed is '" << reversed << "'" << endl << endl;
/*** QUESTION 2 **/
cout << "The strings 'this, and THAT......' and 'THIS and THAT!!!' are ";
if (!compare("this, and THAT......", "THIS and THAT!!!"))
cout << "NOT ";
cout << "the same" << endl << " (ignoring punctuation and case)" << endl;
cout << "The strings 'this, and THAT' and 'THIS, but not that' are ";
if (!compare("this, and THAT", "THIS, but not that"))
cout << "NOT ";
cout << "the same" << endl << " (ignoring punctuation and case)" << endl << endl;
/*** QUESTION 3 **/
cout << "The string 'rotor' is ";
if (!palindrome("rotor"))
cout << "NOT ";
cout << "a palindrome." << endl;
cout << "The string 'Madam I'm adam' is ";
if (!palindrome("Madam I'm adam"))
cout << "NOT ";
cout << "a palindrome." << endl;
cout << "The string 'Madam I'm not adam' is ";
if (!palindrome("Madam I'm not adam"))
cout << "NOT ";
cout << "a palindrome." << endl << endl;
/*** QUESTION 4 **/
cout << "The string 'I am a weakish speller!' is ";
if (!anagram("I am a weakish speller!", "William Shakespeare"))
cout << "NOT ";
cout << "an anagram of 'William Shakespeare'" << endl;
cout << "The string 'I am a good speller!' is ";
if (!anagram("I am a good speller!", "William Shakespeare"))
cout << "NOT ";
cout << "an anagram of 'William Shakespeare'" << endl;
return 0;
}
void reverse(const char* un_rev, char rev[]) {
int len = 0;
len = strlen(un_rev);
int i = 0;
rev[len+1] = '\0'; //null terminate string
while (len >= 0) {
rev[i] = un_rev[len -1];
i++;
len--;
}
}
void clean(const char in_string[], char out_string[]) {
int n =0;
for (int i = 0; in_string[i]; i++) {
if (isalpha(in_string[i])) {
out_string[n] = toupper(in_string[i]);
n++;
}
}
out_string[n] = '\0';
// cout << "out: " << out_string;
}
bool compare(const char input_1[], const char input_2[]) {
//cleaned copies of string
int len1 = strlen(input_1);
int len2 = strlen(input_2);
char cinput_1[len1+1];
cinput_1[len1+1] = '\0';
char cinput_2[len2+1];
cinput_2[len2+1] = '\0';
clean(input_1, cinput_1);
clean(input_2, cinput_2);
return(compare_recur(cinput_1, cinput_2));
}
//recursive bit of the compare function
//possibly work into a single func?
bool compare_recur(char input_1[], char input_2[]) {
if (!(*input_1) || !(*input_2)) {
return true;
} else if ( *input_1 != *input_2) {
return false;
}
return compare_recur(++input_1, ++input_2);
}
bool palindrome(const char input[]) {
int len = strlen(input);
char cinput[len+1];
cinput[len+1] = '\0';
reverse(input, cinput);
compare(input, cinput);
}
int min_pos(int starting_pos, char input[]) {
int min = starting_pos;
char min_char = input[starting_pos];
for (int i = starting_pos; input[i]; i++) {
if (input[i] < min_char) {
min = i;
min_char = input[i];
}
}
return min;
}
void sort(char input[]) {
char temp;
int the_min = 0;
for(int i = 0; input[i]; i++) {
the_min = min_pos(i, input);
if ((the_min) != i) {
//swap
temp = input[the_min];
input[the_min] = input[i];
input[i] = temp;
}
}
}
bool anagram(const char input_1[], const char input_2[]) {
int len1 = strlen(input_1);
int len2 = strlen(input_2);
char cinput_1[len1+1];
cinput_1[len1+1] = '\0';
char cinput_2[len2+1];
cinput_2[len2+1] = '\0';
clean(input_1, cinput_1);
clean(input_2, cinput_2);
sort(cinput_1);
sort(cinput_2);
return compare(cinput_1, cinput_2);
}
最佳答案
正如人们所说,使用 std::string 将使事情变得更加简单和安全。如果您必须使用 C 风格的字符串,这里是对您的代码的一些批评:
1.在许多地方,您的代码等同于
buf[len+1] = '\0';
应该是
buf[len] = '\0';
2。 palindrome
函数不返回值。
3。此代码(其变体出现在多个地方)不是标准 C++,因为数组大小不是常量:
char cinput[len+1];
对于可变大小的数组,你需要动态分配它们:
char *cinput = new char[len+1];
//... use the array ...
delete[] cinput;
当然,std::string
甚至 std::vector<char>
将使这里的事情变得更容易。
关于c++ - 为考试而死记硬背——我错过了什么(C++ 字符串操作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1970302/
我正在准备 SQL Server 考试 (70-431)。我有 Sybex 的书 "SQL Server 2005 - Implementation and Maintenance" .我对估计一张
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the proper declaration of main? 我刚刚参加了第一次 C++
我刚刚参加了考试,被问到以下问题: Write the function body of each of the methods GenStrLen, InsertChar and StrRevers
如何通过 {exams} 包创建一个 moodle 问题,该问题至少为多项选择题打分? 我已经试过了: exams2moodle(..., mchoice = list(eval = list(neg
我正在为我们的项目进行 PAX 集成测试,我在类加载方面遇到了一些问题。 我在 PAX(使用的 karaf 容器)中部署了几个包。 karaf 启动后,我可以看到我的包和服务已启动并处于事件状态。但是
我正在尝试使用 PAX Exam 设置测试,如下所示: @ExamReactorStrategy(PerMethod.class) public class AbstractTest { @C
我正在参加过去的 Java 考试,但有一个问题一直困扰着我。 问题是:“任何构造函数显式或自动调用其父类的构造函数,父类调用其父类,依此类推类层次结构。这个过程的名称是什么? 感谢您的回答! 最佳答案
我正在准备 Java 程序员认证 (SCJP) 考试。关于异常的问题,当处理异常时,最好是处理特定的异常,如 NumberFormatException ,还是使用父级 Exception 类捕获所有
我一直在准备 SCJP,现在是 Oracle 认证专业 Java SE 程序员考试。 我很难理解所有不同的集合以及何时使用它们。我也喜欢闪存卡。因此,我尝试创建一组本质上相同的类,除了它们使用的集合之
在 MS Exam 70-536 .Net Foundation ,案例场景 1 中的第 3 章“搜索、修改和编码文本” Your organization, Northwind Traders, i
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
在MS Exam 70-536 .Net Foundation ,第1课Creating Threads中的Chapter 7“Threading”有一段文字: Be aware that becau
我正在与 exams2nops 合作R/exams 包的功能。由于我所在大学的学生注册号只有六个数字,我尝试调整了reglength exams2nops 中的选项(通常最少 7 个)功能以及nops
我的目标是使用 R/exams 和 Moodle 创建一个问题,包括在 Rmd 练习文件中生成的一些图。学生应口头描述情节,然后手动评分练习。 是否可以使用 exams2moodle为 Moodle
大家好,最近一次 MCQ 复习了我做错的答案。 函数 getSum 被定义为计算大小为 a[] 的 double 组中值的总和大小,大于给定的输入值阈值。下面显示了四个原型(prototype)定义,
我从官方 MCTS 考试 70-562 书中得到的小测试程序没有触发事件。我在 Debug模式下跟踪了计算机逻辑,它甚至没有进入我的事件,即使我已经设置好一切来处理它们。所以最大的问题是我做错了什么?
我已经开始使用 PAX-EXAM 和 Karaf 容器来测试我们的应用程序。有时,测试只是在测试方法开始之前挂起,并且始终卡在 cxf 注册 mbean 上: 正在注册 MBean org.apach
在第一版C# 70-483 Exam Ref , 示例 1-12 给出了将子任务附加到父任务的示例。我认为这是错误的,并希望有人在继续这个假设之前检查我的理解。示例中的代码如下: using Syst
我目前正在准备 Microsoft 考试 70-480。在实践测试中,我发现了以下问题,我无法解释其答案。 谁能解释为什么这是正确答案? You are developing a web page b
我正在阅读 MCTS 自定进度培训工具包(考试 70-536):Microsoft .NET Framework — 应用程序开发基础,第二版电子书。 现在我正在完成线程章节(第 7 期)。在第 2
我是一名优秀的程序员,十分优秀!