小编Mik*_*ike的帖子

对结构中的多个项进行qsort

我的任务是用户输入包含月份,日期和年份的n行,格式为"1月12日99".

我必须按时间顺序排序日期列表,首先使用qsort逐年,然后按天,然后按月.

我的问题是我不知道如何对多个索引进行qsort.我已经完成了今年的工作,但是在那之后我不知道如何在当天进行调整,因为它肯定会在白天对它进行排序,但这些年将再次混乱?

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

typedef int (*compfn)(const void*, const void*);

struct date
{
    char month[9]; //Maximum of 9 characters in a month
    int day; //The day of the month (e.g. 18)
    int year; //The year of the date    
};

int sortDates(struct date *elem1, struct date *elem2)
{

    if (elem1 -> year < elem2 -> year)
    {
        return -1;
    }
    else 

    if (elem1->year > elem2->year)
        {
        return 1;
    }
    else
        return 0;

}


main()
{
    int …
Run Code Online (Sandbox Code Playgroud)

c qsort

4
推荐指数
1
解决办法
2856
查看次数

C - 删除字符串中的空格

我正在使用fgets输入一个字符串,例如"Hello World".我想尝试删除单词之间的空白区域,但是我正在尝试不断返回hello @ world(其中@是一个随机字符).

void sortString(char phrase[])
{
    int i, j;
    char temp[200];


    for(i = 0; i < 200; i++)
    {
        if(!(isspace(phrase[i])))
        {
            temp[i] = phrase[i];
        }
    }

    printf("%s", temp);
}
Run Code Online (Sandbox Code Playgroud)

所以我基本上将字符[i]从短语复制到临时数组,如果它不是空格,但我不确定为什么我得到一个随机字符而不仅仅是,例如,helloworld .

c

0
推荐指数
1
解决办法
139
查看次数

数字的产品,不打印正确的数字

我有一个二维数组(矩阵),我试图计算相邻数字的最大乘积.它与Project Euler Problem 11非常相似,只是用户在计算中输入了他们想要的相邻数字.我觉得我没事.问题是如果我使用整数来计算5个整数99的乘积(例如99*99*99*99*99)它将无法正确显示.可以检查的相邻数字的最大数量是99.我已经尝试过更改为长双打(正如您在代码中看到的那样)但是它打印出荒谬的数字,例如我在所有矩阵位置输入99的3个相邻数字并且应该得到返回970299(我在maxProduct为int时执行),但我得到:

-497917511184158537131936752181264370659584929560826523880745083032965215342755650440802286656251727430041200624372430370294634536699364412350122489510814753628581807006780156992324264734484592980976635224618682514265787653963930812412392499329499188301075222828863209569131692032
Run Code Online (Sandbox Code Playgroud)

我觉得这是显而易见的,但我看不到它.

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

long double calcProduct(int n, int m, int ** matrix)
{
    int i, x, y; //Loop counters
    long double maxProduct; //Used to hold the maximum product of numbers found so far
    long double temp; //Used to hold the current product of numbers

    //Searching left to right
    for(y = 0; y < n; y++)
    {
        for(x = 0; x <= n - m; x++)
        {
            for(i = 0; i < m; …
Run Code Online (Sandbox Code Playgroud)

c arrays product

-1
推荐指数
1
解决办法
278
查看次数

标签 统计

c ×3

arrays ×1

product ×1

qsort ×1