C - 数组、排序和修改

发布于 2024-12-23 11:47:59 字数 2842 浏览 1 评论 0原文

一直在从事一项任务,该任务已成为 SO 上各种其他问题的主题! 我已经到了再次尝试将数字附加到数组的阶段,尽管这一次,代码在参数中传递信息的方式更加复杂。

编辑:代码也在下面

对此感到抱歉,但请修改代码: http://pastebin.com/8SUjRyZQ

论坛帖子上的内容有点太多了。

问题是,它不是附加数字,而只是垃圾,尽管这一次,我真的不明白为什么,因为我已经考虑了之前关于该主题的问题中的所有建议。

它应该在 void AppendInt 函数内将数字 int val 附加到数组,但它只是附加垃圾。

好吧,在 Notepad++ 中,它打印出 Wo​​rd - PasswordBOH - BOH 是一些随机垃圾,它就像一个图像,BOH 为黑色背景的白色文本。

任何人的帮助将不胜感激!

PS>如果需要,我可以在这里发布代码,但它是一个很大的块。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAXLINES 5000 /* max no. lines to be stored */
#define MAXLEN 1000 /* max length of single line */

char *lineptr[MAXLINES];

void capitalise(char *line);
void decision(int i, char *line, int val);
void writelines(char *lineptr[], int nlines);
void qksort(char *v[], int left, int right);
void swap(char *v[], int i, int j);
void AppendInt(char *line, int val, int length);

int main(int argc, char *argv[]) {

int nlines = 0, j, k, i = 0;
char line[MAXLEN];
FILE *fpIn;

printf("WLO v1.0 Alpha Release - Coded in C - Big Shout to StackOverFlow!\n");
printf("1.QuickSort List.\n");
printf("2.QuickSort - Capitalise Word[0]\n");
printf("3.QuickSort - Capitalise Word[0] - Append X\n");
scanf("%d", &k);

if(k == 3) {
    printf("Enter the value you wish to append (a single Integer): ");
    scanf("%d", &i);
}

fpIn = fopen(argv[1], "rb");
while((fgets(line, 65, fpIn)) != NULL) {
    j = strlen(line);
    if (j > 0 && (line[j-1] == '\n')) {
        line[j-1] = '\0';
    }
    if (j > 8) {
        if(k != 1)
            decision(k, line, i);
        lineptr[nlines++] = strdup(line);
    }
}
qksort(lineptr, 0, nlines - 1);
writelines(lineptr, nlines);
return 0;    
}

void decision(int i, char *line, int val) {
 if(i == 2)
    capitalise(line);
 else if(i == 3)
     AppendInt(line, val, strlen(line));
}

void capitalise(char *line) {
 line[0] = toupper((line[0]));
}

void AppendInt(char *line, int val, int length){
 capitalise(line);
 line[length] = val;
 line[length + 1] = '\0';    
}

void writelines(char *lineptr[], int nlines) {
FILE *fpOut;
int i;
fpOut = fopen("tmp.out", "wb");
for(i = 0; i < nlines; i++)
    fprintf(fpOut, "%s\n", lineptr[i]);   
}

void qksort(char *v[], int left, int right) {     
int i, last;
void swap(char *v[], int i, int j);
if (left >= right) 
        return; 
swap(v, left, (left + right)/2);
last = left;
for (i = left+1; i <= right; i++)
        if (strcmp(v[i], v[left]) < 0)
               swap(v, ++last, i);
        swap(v, left, last);
        qksort(v, left, last-1);
        qksort(v, last+1, right);
}

void swap(char *v[], int i, int j) {
 char *temp;  
 temp = v[i];
 v[i] = v[j];
 v[j] = temp;
}

Been working on a task that has been the subject of a whole variety of other question on SO!
I've gotten to the stage where again, I am trying to append a number to an array, though this time, the code is more complex in the way info is passed around in parameters.

EDIT: Code is also below

Sorry about this, but please fine the code: http://pastebin.com/8SUjRyZQ

There is a little too much to place on a forum post.

The issue is, it isn't appending the number, but just garbage, although this time, I really can't see why as I have taken into account all the suggestions from my previous questions on this topic.

It should append the number, int val, to the array, within the void AppendInt function, thought its just appending garbage.

Well, in Notepad++, it prints the Word - PasswordBOH - BOH being some random garbage, its like an image, BOH in white text with a black background.

Anyone help is appreciated!

PS> If needed, I can post the code here, but it's a substantial chunk.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAXLINES 5000 /* max no. lines to be stored */
#define MAXLEN 1000 /* max length of single line */

char *lineptr[MAXLINES];

void capitalise(char *line);
void decision(int i, char *line, int val);
void writelines(char *lineptr[], int nlines);
void qksort(char *v[], int left, int right);
void swap(char *v[], int i, int j);
void AppendInt(char *line, int val, int length);

int main(int argc, char *argv[]) {

int nlines = 0, j, k, i = 0;
char line[MAXLEN];
FILE *fpIn;

printf("WLO v1.0 Alpha Release - Coded in C - Big Shout to StackOverFlow!\n");
printf("1.QuickSort List.\n");
printf("2.QuickSort - Capitalise Word[0]\n");
printf("3.QuickSort - Capitalise Word[0] - Append X\n");
scanf("%d", &k);

if(k == 3) {
    printf("Enter the value you wish to append (a single Integer): ");
    scanf("%d", &i);
}

fpIn = fopen(argv[1], "rb");
while((fgets(line, 65, fpIn)) != NULL) {
    j = strlen(line);
    if (j > 0 && (line[j-1] == '\n')) {
        line[j-1] = '\0';
    }
    if (j > 8) {
        if(k != 1)
            decision(k, line, i);
        lineptr[nlines++] = strdup(line);
    }
}
qksort(lineptr, 0, nlines - 1);
writelines(lineptr, nlines);
return 0;    
}

void decision(int i, char *line, int val) {
 if(i == 2)
    capitalise(line);
 else if(i == 3)
     AppendInt(line, val, strlen(line));
}

void capitalise(char *line) {
 line[0] = toupper((line[0]));
}

void AppendInt(char *line, int val, int length){
 capitalise(line);
 line[length] = val;
 line[length + 1] = '\0';    
}

void writelines(char *lineptr[], int nlines) {
FILE *fpOut;
int i;
fpOut = fopen("tmp.out", "wb");
for(i = 0; i < nlines; i++)
    fprintf(fpOut, "%s\n", lineptr[i]);   
}

void qksort(char *v[], int left, int right) {     
int i, last;
void swap(char *v[], int i, int j);
if (left >= right) 
        return; 
swap(v, left, (left + right)/2);
last = left;
for (i = left+1; i <= right; i++)
        if (strcmp(v[i], v[left]) < 0)
               swap(v, ++last, i);
        swap(v, left, last);
        qksort(v, left, last-1);
        qksort(v, last+1, right);
}

void swap(char *v[], int i, int j) {
 char *temp;  
 temp = v[i];
 v[i] = v[j];
 v[j] = temp;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望笑 2024-12-30 11:47:59

您可以使用 sprintf:

void AppendInt(char *line, int val, int length) {
    capitalise(line);
    sprintf(line+length, "%d", val);
}

You can use sprintf:

void AppendInt(char *line, int val, int length) {
    capitalise(line);
    sprintf(line+length, "%d", val);
}
删除→记忆 2024-12-30 11:47:59

您是否认为 val 不是字符,因此它不可打印...

要解决这个问题: sprintf(&line[length],"%d",val);

您必须确保为行分配足够的内存来包含 val。

do you consider that val is not a char, so it is not printable...

to solve that : sprintf(&line[length],"%d",val);

you have to be sure that you allocate enough memory for line to contain val.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文