帮助理解结构 [英] Help on understanding Structs
问题描述
我将在即将到来的星期五进行考试,我的教授告诉我们,这将是基于这个项目的b $ b。我很难理解这个
计划,例如,如果我想将用户输入的所有总热量添加到用户输入中。确定哪种食物含有最大的卡路里。
我如何开始修改程序以便完成我列出的内容
以上。谢谢
#include< stdio.h>
#include< stdlib.h>
typedef struct {
char * name;
int calories;
int group;
} FOOD;
void getFood(FOOD foods [],int i){
FOOD newFood;
void * characters = malloc(81 * sizeof( char));
printf(&\\; \ n请输入所要求的信息:\ n");
printf(" for food item% d,什么是\ n,i + 1);
printf(" name?");
得到(字符); //注意fgets更好!
newFood.name = characters;
printf(" calories / serving?");
scanf("%d",&(newFood.calories));
printf(" food group?");
scanf("%d",&(newFood.group));
printf(" \ n");
foods [i] = newFood ;
} //结束getFood //注意:结构是和左值!!
void dump(FOOD foods [],int n){
int i;
for(i = 0; i< n; i ++){
printf(" Food%d是:\ n",i + 1);
printf(" name:%s \ n",foods [i] .name);
printf (卡路里:%d \ n,食物[i] .calories);
printf(" group#:%d \ n",foods [i] .group);
printf(" \ n");
} //结束
} //结束转储
int main(){
int num; //输入的食物数量
int i;
char dum [81];
printf(" \\\
How你会输入很多食物吗?");
scanf("%d",& num);
食物食品[num];
for(i = 0; i< num; i ++){
得到(dum); //注意:这就是因为SCANF
getFood(食品,我);
} //结束
dump(foods,num);
返回0;
} //结束主
I will have a exam on the oncoming friday, my professor told us that it
will base upon this program. i am having troubles understanding this
program, for example what if i want to add all the total calories that
the user input together. determine which food has the largest calories.
how do i start to modifiy the program inorder to do the things i listed
above. thanks
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *name;
int calories;
int group;
} FOOD;
void getFood(FOOD foods[ ], int i ) {
FOOD newFood;
void *characters = malloc(81*sizeof(char));
printf("\nPlease enter the requested information:\n");
printf(" for food item %d, what is\n", i+1);
printf(" the name? ");
gets( characters); // NOTE fgets IS BETTER!
newFood.name = characters;
printf(" calories/serving? ");
scanf("%d", &(newFood.calories));
printf(" food group? ");
scanf("%d", &(newFood.group));
printf("\n");
foods[i] = newFood;
} // end getFood // note:a struct is and lvalue !!
void dump( FOOD foods[ ] , int n ) {
int i;
for(i=0; i<n; i++ ) {
printf("Food %d is: \n", i+1);
printf(" name : %s\n", foods[i].name);
printf(" calories: %d\n", foods[i].calories);
printf(" group # : %d\n", foods[i].group);
printf("\n");
} // end for
} // end dump
int main( ) {
int num; // number of foods entered
int i;
char dum[81];
printf("\nHow many foods will you enter? ");
scanf("%d", &num);
FOOD foods[num];
for( i=0; i<num; i++ ) {
gets(dum); //NOTE: THIS IS HERE BECAUSE OF SCANF
getFood( foods, i );
} // end for
dump( foods, num);
return 0;
} // end main
推荐答案
是这样的:
// ......
int total_calories;
int maxcalories ;
char maxcaloriesname [81]
// ......
total_calories = 0;
maxcalories = 0;
strcpy(maxcaloriesname," no");
// ......
食物食品[num];
for(i = 0; i< num ++){
getFood(食物,i);
// .......
total_calories + =食物[i] .calories;
if(食物[i] .calories> maxcalories)< br $>
{
maxcalories = foods [i] .calories;
strcpy(maxcaloriesname,foods [i] .name);
}
} //结束
// .....
like this:
//......
int total_calories;
int maxcalories;
char maxcaloriesname[81]
//......
total_calories=0;
maxcalories=0;
strcpy(maxcaloriesname,"no");
//......
FOOD foods[num];
for( i=0; i<num; i++ ) {
getFood( foods, i );
// .......
total_calories+=foods[i].calories;
if(foods[i].calories>maxcalories)
{
maxcalories=foods[i].calories;
strcpy(maxcaloriesname,foods[i].name);
}
} // end for
//.....
Bail写道:
我将在即将到来的星期五进行考试,我的教授告诉我们,这将基于这个计划。
我已经掀起了一些代码,这些代码可以正常工作,并且仅将其粘贴到
发现,它是星期四!b $ b星期四! />
i我很难理解这个
程序,例如,如果我想添加用户输入的所有总卡路里,该怎么办。确定哪种食物含有最大的卡路里。
我如何开始修改程序,以便完成上面列出的内容。谢谢
I will have a exam on the oncoming friday, my professor told us that it
will base upon this program.
I had whipped up some code, which works, and pasted it only to
discover, its
thursday!
i am having troubles understanding this
program, for example what if i want to add all the total calories that
the user input together. determine which food has the largest calories.
how do i start to modifiy the program inorder to do the things i listed
above. thanks
[太多我觉得我不得不评论的东西......]
1.不要使用获取。这很危险。 GIYF。
2.使用scanf的交互式输入很棘手。搜索档案,
的东西
喜欢scanf + string + input + pete。 (皮特碰巧发布了一些
代码我非常喜欢)
3.了解qsort()。这里有一些提示:你需要类似
....
int calcmp(const void * l,const void * r)< br $>
{
const FOOD * left = l;
const FOOD * right = r;
if(left- left- >卡路里>右 - &>卡路里)
返回1;
/ *填空白... * /
}
4.你说你遇到了麻烦。大概,不是代码你
粘贴。
如果你理解你粘贴的代码,家庭作业应该很容易
:)
星期五见ya''。
[just too many things I felt i had to comment on so ... ]
1. Don''t use gets. Its dangerous. GIYF.
2. Interactive input with scanf is tricky stuff. Search the archive,
with something
like "scanf + string + input + pete ". (pete happens to post some
code I really like)
3. Learn about qsort(). Here''s some hint: you will need something like
....
int calcmp( const void *l, const void *r )
{
const FOOD *left = l;
const FOOD *right = r;
if ( left->calories > right->calories )
return 1;
/* fill in the blanks ... */
}
4. You said you''re having trouble. Presumably, not with the code you
pasted.
If you understand the code you pasted, the homework should be easy
:)
See ya'' on friday.
Suman写道:
Suman wrote:
2.互动使用scanf输入是棘手的事情。搜索存档,
像
scanf + string + input + pete。 (皮特碰巧发布一些我非常喜欢的代码)
2. Interactive input with scanf is tricky stuff. Search the archive,
with something
like "scanf + string + input + pete ". (pete happens to post some
code I really like)
这种使用scanf的方式主要由Dan Pop推广。
Dan Pop倾向于非常准确,但是......
如果我要说他有一个磨砺的个性,
它不会至少让我感到惊讶,
如果他回答逻辑上的反驳
他的性格如何没有磨损
我必须如何精神上有缺陷
甚至想到这样的事情。
-
pete
That way of using scanf, was primarily promoted by Dan Pop.
Dan Pop tended to be very accuarate, but was ...
If I were to say that he had an abrasive personality,
it would not surprise me in the least,
if he were to reply with a logical rebuttal
of how his personality was not abrasive
and how I must be mentally deficient
for even thinking something like that.
--
pete
这篇关于帮助理解结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!