gpt4 book ai didi

c - 如何用C语言打印二维数组

转载 作者:行者123 更新时间:2023-11-30 15:38:43 28 4
gpt4 key购买 nike

我一直在尝试让我的程序打印条形图。

问题出在底部,我创建了一个二维数组来保存值,然后尝试打印数组。问题是没有打印任何内容。我尝试解决这个问题几个小时但没有成功。有什么建议吗?

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

#define DELIM " " /* the delimiter */
#define MAX_CHANGE (10.0/86400.0) /* 10kg/day */

/* seconds in a day is 24 hours * 60 minutes * 60 seconds */
/* return 0 if the passed strings don't math, 1 otherwise */

/* defines the structure of Node */
struct Node{
char * id;
float weight;
int time;
int count;
struct Node * next;
} *head, *p, *t, *last;

/* Constructor which returns a pointer to a new node*/
struct Node *newNode(int *time, char * id, float *w)
{ /*note malloc returns a pointer */
struct Node *r = (struct Node *)malloc( sizeof(struct Node) );
r->time = *time;
r->id = strdup(id); //a duplicate id is made to prevent all the nodes from using the same userID
r->weight = *w;
r->count = 1;
r->next = NULL;
return r;
}

/* prints the list starting with head */
printList(struct Node * head)
{
while(head != NULL)
{
printf("%d %s %f\n",head->time,head->id,head->weight);
head = head->next;
}

return 0;
}


int main() {

char line[1024];
int lasttime = 0;
int success;
int timestamp;
int duration;
char userID[1000] = "";
char *token;
char temp[1000];
float weight;
float lastweight;
float change;
float changePerTime;

head = (struct Node*)malloc(sizeof(struct Node));
head->id = "";
head->weight = 0.0;
head->time = 0;
head->next = NULL;

last = head;

/*FILE * f = fopen("C:\\Users\\Chris\\Documents\\School\\York\\Computer Science\\2031 Software Tools\\Labs\\lab3\\testcases\\01.in","r"); */

/* last points to the last node in the list
head is always the same node
p is used to travers the list
t is a pointer the most recent occurrense of a user record
*/

while (fgets(line,1024,stdin) != NULL) {
userID[0] ='\0'; // resets userID
token = strtok(line, DELIM);
success = sscanf(token,"%d",&timestamp);

if (success < 1 || timestamp == 0)
{
printf("Invalid time\n");
continue;
}

while((token = strtok(NULL,DELIM) ) != NULL && token[0] != '.' && ! isdigit(token[0]) )
{
strcpy(temp,token); //
strcat(temp,DELIM ); // adds space between each token
strcat(userID, temp); // src temp must be a const string, not a pointer
temp[0] = '\0';
}

userID[strlen(userID)-1] = '\0'; //erases the tailing space.

if(strlen(userID) > 179 || !strlen(userID) )
{printf("Illegal userID\n"); continue; }
else if(token == NULL || sscanf(token,"%f", &weight) < 1 || weight < 30.0 || weight > 300.0)
{printf("Illegal weight\n"); continue; }
else if (lasttime >= timestamp)
{printf("Nonmonotonic timestamps\n"); continue; }
else {
/* sets t to last found user record and sets "last" to the last record*/
for(p = head, t = NULL; p != NULL; p = p->next)
{
if(strcmp(userID,p->id) == 0)
{
t=p;
}

last = p; // set last to last p.
}

if(t == NULL)
{
printf("OK newuser\n");
}
else if(t != NULL)
{
/* increments count of id's for this user */
(t->count)++;
duration = timestamp - t->time;
change = weight - t->weight;
changePerTime = change / duration;
if(changePerTime < -MAX_CHANGE || changePerTime > MAX_CHANGE)
printf("Suspiciously large weight change\n");
else
printf("OK\n");
}

/* adds node to end of list */
last->next = newNode(&timestamp,userID,&weight);
last = last->next;

/* adds time to last time */
lasttime = timestamp;
}
}

//fclose(f);
char bc[10][last->count];

int j, i, k, bh;
for(p = head; p != NULL, j <= last->count; p=p->next)
{
if(strcmp(last->id,p->id) == 0)
{
for(i = 11, k=0, bh = (int)(p->weight / 30);i >= 0; i--)
{
if(k < bh)
{
bc[i][j] = '*';
k++;
}
else bc[i][j] = ' ';
}
j++;
}
}

//printf("%c", bc[9][1]);

int m=0, n=0;
for(m < 10; m++;)
{
for(n=0 ;n < last->count; n++)
{
printf("%c",bc[m][n]);
}
printf("%c",'\n');
}
}

最佳答案

您的外部 for 循环部分放置不正确。而不是:

for(m < 10; m++;)

你想要:

for(m=0;m < 10; m++)

条件,m<10 ,是 for 循环的第二部分,而您错误地将其放在循环的初始化部分中。类似地,增量语句 i++ ,在您的条件部分中,因此您没有增加 m变数正在发生。

关于c - 如何用C语言打印二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21666277/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com