所以这段代码显然不完整并且混乱,但是当我尝试格式化我的代码时,我在这里收到一个我没有预料到的错误(在
int getNumberFromSingleLine(char * singleLine)
之后的括号上)
int getNumberFromSingleLine(char * singleLine)
{
char number[10];
int i = 0;
while (isDigit(singleLine[i]))
{
number[i] = singleLine[i];
i++
}
number[i] = '\0'; // null term character
return atoi(number);
}
我的完整代码是
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//first, read text opening the file
//then, sort the numbers in order
//then find out how high/how many rows there will be given the highest number
//then, chronologically isolate the numbers before /n
//then print the words that correspond to the numbers
void merge(int arr[], int l, int m, int r);
void mergeSort(int arr[], int l, int r);
int main(int argc, char *argv[])
{
// Check for command line args
if (argc != 2)
{
printf("Usage: ./pyramidcode filename.txt\n");
return 1;
}
typedef struct
{
int buffer;
char phrase;
}
nameandnum;
//give array a size
int b = 1;
nameandnum nameandnumber[b];
FILE * fPointer = fopen(argv[1], "r");
char singleLine[150];
//read through lines until end
while(!feof(fPointer))
{
//extract text from line
fgets(singleLine, 150, fPointer);
//get the number from the text
int getNumberFromSingleLine(char * singleLine)
{
char number[10];
int i = 0;
while (isDigit(singleLine[i]))
{
number[i] = singleLine[i];
i++
}
number[i] = '\0'; // null term character
return atoi(number);
}
char getPhraseFromSingleLine(char * singleLine)
{
char string[50];
while (singleLine[i] != '\n')
{
if(!isDigit(singleLine[i]))
{
i++
}
else
{
singleLine[i] = string[i]
i++
}
}
//puts number into buffer array
number = nameandnumber[b].buffer;
//puts string into phrase array
string = nameandnumber[b].phrase;
//expands size of all arrays
b++;
}
}
//merge sorts the buffer so that it's in chronological order
merge_sort(nameandnumber[z].buffer)
//Pyramidsortation
int z = 0;
while (z < b);
{
int x = 1 + z;
z + x = z;
printf("%s, string[z])
}
fclose(fPointer);
return 0;
}
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
// Create temp arrays
int L[n1], R[n2];
// Copy data to temp arrays
// L[] and R[]
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
// Merge the temp arrays back
// into arr[l..r]
// Initial index of first subarray
i = 0;
// Initial index of second subarray
j = 0;
// Initial index of merged subarray
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
// Copy the remaining elements
// of L[], if there are any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
// Copy the remaining elements of
// R[], if there are any
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// l is for left index and r is
// right index of the sub-array
// of arr to be sorted
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
// Same as (l+r)/2, but avoids
// overflow for large l and h
int m = l + (r - l) / 2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
好吧,显然我知道除此之外还有很多问题,但我正在专门寻找为什么我会在那条线上遇到该错误。
这段代码的要点是打开一个文本文件,每行左侧有数字,后面跟着一个短语,并通过合并排序将数字按升序排序。然后我只想按以下顺序打印与粗体数字对应的短语: 1, 23, 4 5 6, 7 8 9 10, ...ETC 我知道我的代码在合并排序中“链接”结构的两个元素方面并不完整,并且在指定数组大小方面存在问题。如果您愿意就此提供建议,我将非常感激,但我并不期待。我只是想了解为什么我收到函数定义错误。
抱歉,如果这是一个基本问题,我已经浏览了论坛并尝试确保我正确地标记了括号并将其与人们遇到的其他问题进行了比较,但我找不到解决方案。
提前感谢并道歉 - 我仍然是一个初学者!
而不是:
int main()
{
hello_world();
/* */
int hello_world(void)
{
prinitf("hellow");
}
}
你应该写:
int hello_world(void)
{
prinitf("hellow");
}
int main()
{
hello_world();
}
while(!feof(fPointer))
是总是错的我没有进一步分析。