帮助理解结构

扫码查看
本文介绍了帮助理解结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我将在即将到来的星期五进行考试,我的教授告诉我们,这将是基于这个项目的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 itwill base upon this program. i am having troubles understanding thisprogram, for example what if i want to add all the total calories thatthe user input together. determine which food has the largest calories.how do i start to modifiy the program inorder to do the things i listedabove. 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 dumpint main( ) {int num; // number of foods enteredint 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 SCANFgetFood( foods, i );} // end fordump( foods, num);return 0;} // end main推荐答案 [太多我觉得我不得不评论的东西......] 1.不要使用获取。这很危险。 GIYF。 2.使用scanf的交互式输入很棘手。搜索档案, 的东西 喜欢scanf + string + input + pete。 (皮特碰巧发布了一些 代码我非常喜欢) 3.了解qsort()。这里有一些提示:你需要类似 .... int calcmp(const void * l,const void * r) { 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 somethinglike "scanf + string + input + pete ". (pete happens to post somecode 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 youpasted.If you understand the code you pasted, the homework should be easy:)See ya'' on friday. 这种使用scanf的方式主要由Dan Pop推广。 Dan Pop倾向于非常准确,但是...... 如果我要说他有一个磨砺的个性, 它不会至少让我感到惊讶, 如果他回答逻辑上的反驳 他的性格如何没有磨损 我必须如何精神上有缺陷 甚至想到这样的事情。 - peteThat 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 rebuttalof how his personality was not abrasiveand how I must be mentally deficientfor even thinking something like that.--pete 这篇关于帮助理解结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 03:57
查看更多