C++ 指针算法和冒泡排序(C+;+;)
我似乎不知道我做错了什么。我今天在课堂上做了笔记,但不幸的是我的OneNote崩溃并删除了我今天的所有笔记。我知道我遗漏了一些东西,但我无法理解。我一直在我的课本上搜索,甚至在网上搜索了一个多小时。我似乎找不到任何有效的方法 我知道我的错误可能是因为我在if或set变量语句中使用了charPtr++,但我不记得该怎么做。如何检查一个元素是否小于另一个元素 我们应该使用指针算法来编写10个元素的降序冒泡排序。有人能解释我做错了什么,为什么我的程序只输出原始数组而不输出任何东西吗?谢谢强> 我们也不能使用括号或偏移符号。C++ 指针算法和冒泡排序(C+;+;),c++,pointers,math,C++,Pointers,Math,我似乎不知道我做错了什么。我今天在课堂上做了笔记,但不幸的是我的OneNote崩溃并删除了我今天的所有笔记。我知道我遗漏了一些东西,但我无法理解。我一直在我的课本上搜索,甚至在网上搜索了一个多小时。我似乎找不到任何有效的方法 我知道我的错误可能是因为我在if或set变量语句中使用了charPtr++,但我不记得该怎么做。如何检查一个元素是否小于另一个元素 我们应该使用指针算法来编写10个元素的降序冒泡排序。有人能解释我做错了什么,为什么我的程序只输出原始数组而不输出任何东西吗?谢谢 我们也不能使
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// DRIVER PROGRAM
char *characters, *charPtr = nullptr; // Array or undefined size.
fstream fs; // file stream for file arithmetic
fs.open("array.txt", ios::in); // Open the file
char currentChar; // Used to check if the file can still be read.
int counter = 0; // Counter to check how many elements are in the array.
while (fs >> currentChar) { // While data can be put into counter, continue...
counter++; // Add one to counter
}
characters = new char[counter]; // Sets size of array.
charPtr = characters;
fs.clear(); // Clears eof flag.
fs.seekg(0, ios::beg); // Sets pointer back to the beginning. CHECK IF YOU CAN REMOVE THIS LINE AND THE ONE ABOVE.
for (int i = 0; i < counter; i++) { // While less than the size of array.
fs >> charPtr; // Write to charPtr
cout << *charPtr << " "; // Output array.
charPtr++; // Move to next element
}
fs.close(); // Close file
putchar('\n'); // Output newline efficiently.
charPtr = characters;
// BUBBLE SORT
bool swapChar;
char temp;
do {
swapChar = false;
for(int count = 0; count < (counter - 1); count++) {
if (*(charPtr) < *(charPtr++)) { // If character at 0 is less than the character at 1
temp = *(charPtr); // Set temp to character at 0
*(charPtr) = *(charPtr++); // set character at 0 to character at 1
*(charPtr++) = temp; // set character at 1 to temp
swapChar = true; // set swap to true
cout << *charPtr << " "; // output current swap
charPtr++; // add 1 to charPtr
}
}
} while (swapChar == true);
}
#包括
#包括
使用名称空间std;
int main(){
//驱动程序
char*characters,*charPtr=nullptr;//数组或未定义的大小。
fstream fs;//用于文件算法的文件流
open(“array.txt”,ios::in);//打开文件
char currentChar;//用于检查文件是否仍然可以读取。
int counter=0;//检查数组中有多少个元素的计数器。
虽然(fs>>currentChar){//虽然数据可以放入计数器,但继续。。。
计数器+++;//向计数器添加一个
}
characters=new char[counter];//设置数组的大小。
charPtr=字符;
fs.clear();//清除eof标志。
fs.seekg(0,ios::beg);//将指针设置回开头。检查是否可以删除此行和上面的一行。
对于(int i=0;i>charPtr;//写入charPtr
请记住,charPtr++
本身是递增的。在下面的代码块中,您做了4次递增,并且您只希望得到一次。您应该将其替换为*(charPtr+1)
if (*(charPtr) < *(charPtr++)) { // If character at 0 is less than the character at 1
temp = *(charPtr); // Set temp to character at 0
*(charPtr) = *(charPtr++); // set character at 0 to character at 1
*(charPtr++) = temp; // set character at 1 to temp
swapChar = true; // set swap to true
cout << *charPtr << " "; // output current swap
charPtr++; // add 1 to charPtr
}
if(*(charPtr)<*(charPtr++){//如果0处的字符小于1处的字符
temp=*(charPtr);//将temp设置为0处的字符
*(charPtr)=*(charPtr++);//将0处的字符设置为1处的字符
*(charPtr++)=temp;//将字符1设置为temp
swapChar=true;//将swap设置为true
cout首先,有太多的++
,其次,您应该忽略一些基本功能,如交换(以便您可以独立测试),第三,您没有在每次运行后正确设置charPtr
if (*(charPtr) < *(charPtr++)) { // If character at 0 is less than the character at 1
// swap
temp = *(charPtr); // Set temp to character at 0
*(charPtr) = *(charPtr++); // set character at 0 to character at 1
*(charPtr++) = temp; // set character at 1 to temp
swapChar = true; // set swap to true
cout << *charPtr << " "; // output current swap
charPtr++; // add 1 to charPtr
}
}
以及交换功能
void swap(char *firstPtr, char *secondPtr) {
char temp = *(charPtr); // Set temp to character at 0
*(firstPtr) = *(secondPtr++); // set character at 0 to character at 1
*(secondPtr++) = temp; // set character at 1 to temp
}
这使它更好一些,但是++
仍然是错误的,它们不应该出现在交换中,因为我们只想交换第一个和第二个
void swap(char *firstPtr, char *secondPtr) {
char temp = *(charPtr); // Set temp to character at 0
*(firstPtr) = *(secondPtr); // set character at 0 to character at 1
*(secondPtr) = temp; // set character at 1 to temp
}
程序仍然是错误的
char *nextPtr = charPtr; // moved this out of the condition as we need it later.
if (*(charPtr) < *(++nextPtr)) { // If character at 0 is less than the character at 1
// swap
swap(charPtr, nextPtr);
swapChar = true; // set swap to true
cout << *charPtr << " "; // output current swap
charPtr++; // add 1 to charPtr
}
只有当你交换的时候你才会这样做,否则你会再次检查完全相同的
char *nextPtr = charPtr;
if (*(charPtr) > *(++nextPtr)) { // If character at 0 is greater than the character at 1
// swap
swap(charPtr, nextPtr);
swapChar = true; // set swap to true
}
cout << *charPtr << " "; // output current position
charPtr = nextPtr; // check the next char
}
cout << endl; // change line after each pass through
char*nextPtr=charPtr;
如果(*(charPtr)>*(++nextPtr)){//如果0处的字符大于1处的字符
//交换
掉期(charPtr、nextPtr);
swapChar=true;//将swap设置为true
}
CUTTr+++/<代码>,你可能不应该。试着逐行地在代码中调试代码,以确保它能做它应该做的事情。@有些程序员很遗憾我使用了代码块,它不允许我调试一个文件。我不知道该怎么做。C++学习的第一步是LEA。rn关于标准库,特别是,std::string
。不要将其视为花哨的C。如果您确实需要原始字符进行排序,您可以使用C_str()
将其转储。这不被视为偏移符号吗?另外,charPtr[1]
在符号方面没有那么难看。每次都是*(charPtr++)
您实际上阅读了*(charPtr)
并执行了charPtr=charPtr+1
我将所有*(charPtr++)更改为*(charPtr+1),但仍然不起作用。我建议放入调试器或打印调试日志以查找原因。但是,我有两个问题,为什么您在*(charPtr+1)时不中断循环
为空。if检查是否应该反转?if(*(charPtr)>*(charPtr+1))
用于升序?
charPtr++; // add 1 to charPtr
char *nextPtr = charPtr;
if (*(charPtr) > *(++nextPtr)) { // If character at 0 is greater than the character at 1
// swap
swap(charPtr, nextPtr);
swapChar = true; // set swap to true
}
cout << *charPtr << " "; // output current position
charPtr = nextPtr; // check the next char
}
cout << endl; // change line after each pass through