trust Posted January 7, 2010 at 01:02 AM Report Share #304565 Posted January 7, 2010 at 01:02 AM Boas pessoal, será que me sabem dizer o que esta mal neste programa?? #include <stdio.h> int g; /*variavel global*/ int ler_horas (int h); int ler_minutos (int min); int media_dur (int TI[], int TF[]); int custo_dur (int TI[], int TF []); int main () { int h, min, TI[100], TF[100], media, custo, op, n, CI[100], CM[100], Dur[100], i; printf ("\n MENU \n\n"); printf("\n 1 - Calcular a duracao de cada manutencao."); printf("\n 2 - Calcular a media da duracao de cada manutencao."); printf("\n 3 - Listagem do custo de cada manutencao."); printf("\n 4 - Terminar programa.\n"); printf("\n Por favor, seleccione uma opcao:"); scanf("%d", &op); i=0; printf("Introduza o numero de assistencias realizadas:"); scanf("%d", &n); g=n; for (i=0; i<n; ++i) { printf("\n %d manutencao:\n", i+1); printf("Insira o codigo da manutencao:"); scanf("%d", &CM[i]); printf("Insira o codigo do instrumento utilizado:"); scanf("%d", &CI[i]); printf("Hora inicial:"); h=ler_horas(h); printf("Minutos iniciais:"); min=ler_minutos(min); TI[i]=h+min; printf("Hora final:"); h=ler_horas(h); printf("Minutos finais:"); min=ler_minutos(min); TF[i]=h+min; } switch (op) { case 1: { for(i=0;i<n; ++i) { Dur[i]=TF[i]-TI[i]; } for (i=0;i<n;++i) { printf("A duracao da %d manutencao e de %d minutos.", i+1, Dur[i]); } break; } case 2: { media=media_dur (TF, TI); printf("A media dos tempos de manutencao e de %d minutos.", media); break; } case 3: { custo=custo_dur(TI,TF); } { printf("Opcao invalida! Por favor, tente novamente inserindo uma opcao correcta!!"); } default: { if (op==4) { printf("\n PROGRAMA TERMINADO!!"); break; } else { printf("\n Por favor, seleccione uma opcao válida."); break; } } } return 0; } int ler_horas (int h) { scanf("%d", &h); if ((h<0)||(h>23)) { printf("HORAS INVALIDAS!!! Por favor, insira as horas correctas!"); scanf("%d", &h); } h=h*60; return h; } int ler_minutos (int min) { scanf("%d", &min); if((min<0)||(min>59)) { printf("MINUTOS INVALIDOS!!! Por favor, insira os minutos correctos!"); scanf("%d", &min); } return min; } int custo_dur ( int TI[], int TF[] ) { float custo, Dur[100]; int i; for (i=0; i<g; i++) { Dur[i] = TF[i] - TI[i]; if (TF[i]<TI[i]) { Dur[i]=1440 - (-Dur[i]); } } for (i=0; i<g; i++) { custo = (Dur[i]/60) * 10; printf("\n \n O custo da %d reparacao e %.2f euros.\n", i+1, custo); } } int media_dur(int TI[], int TF[]) { int media, d, i, Dur[100]; for(i=0;i<g;++i) { Dur[i]=TF[i]-TI[i]; } d=0; for (i=0; i<g;++i) { d = d + Dur[i]; } media=d/g; return media; } Nao me deixa inserir as horas! Ajudem me se puderem! Link to comment Share on other sites More sharing options...
IceBrain Posted January 7, 2010 at 02:01 AM Report Share #304573 Posted January 7, 2010 at 02:01 AM Dica: põe o código inicial entre tags [code=c] e [/code] para ser mais fácil de ler. Ah, e dá um título melhor ao tópico, sff. Quanto ao código, tens alguns problemas: Na linha 34, estás a usar o TI, que é um vector de ints, como se fosse um número. TI=h+min; Suponho que o que queiras é adicionar o valor (h+min) a uma nova posição do TI. Sendo assim, devias fazer: TI[i] = h+min Ou seja, adicionar "h+m" à posição "i" do vector "TI". O mesmo se passa na linha 39. Na linha 47, tens um erro parecido. Estás a somar dois vectores como se fossem ints, e a guardar isso num vector. O que tens que fazer é aceder a cada posição dos vectores: for(i=0;i<n; ++i) { Dur[i] =TF[i] - TI[i]; } Assim, vai somar a posição "i" dos vectores TF e TI, e guardar na posição "i" do vector Dur. Já agora, só por curiosidade: o compilador deixou-te compilar isso, e criar um executável? Que compilador usas? ❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents A list of command line apps Link to comment Share on other sites More sharing options...
mogers Posted January 7, 2010 at 02:08 AM Report Share #304574 Posted January 7, 2010 at 02:08 AM O problema não deve ser esse. Isso aconteceu porque como ele não usou as tags para postar código, o [ i] é o atalho para escrever em itálico :\ "What we do for ourselves dies with us. What we do for others and the world, remains and is immortal.", Albert Pine Blog pessoal : contém alguns puzzles, algoritmos e problemas para se resolver com programação. Link to comment Share on other sites More sharing options...
IceBrain Posted January 7, 2010 at 02:11 AM Report Share #304575 Posted January 7, 2010 at 02:11 AM Tens razão, nem me ocorreu. ❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents A list of command line apps Link to comment Share on other sites More sharing options...
trust Posted January 7, 2010 at 11:43 AM Author Report Share #304605 Posted January 7, 2010 at 11:43 AM entao o que pode estar mal?? Link to comment Share on other sites More sharing options...
IceBrain Posted January 7, 2010 at 04:08 PM Report Share #304637 Posted January 7, 2010 at 04:08 PM A mim deixa: MENU 1 - Calcular a duracao de cada manutencao. 2 - Calcular a media da duracao de cada manutencao. 3 - Listagem do custo de cada manutencao. 4 - Terminar programa. Por favor, seleccione uma opcao:2 Introduza o numero de assistencias realizadas:2 1 manutencao: Insira o codigo da manutencao:1 Insira o codigo do instrumento utilizado:54 Hora inicial:8 Minutos iniciais:30 Hora final:11 Minutos finais:0 2 manutencao: Insira o codigo da manutencao:2 Insira o codigo do instrumento utilizado:11 Hora inicial:16 Minutos iniciais: 0 Hora final:18 Minutos finais:00 A media dos tempos de manutencao e de -135 minutos. Tens é o cálculo mal feito: não devias somar as horas com os minutos diretamente, porque senão ficas com: 8:30 -> 38 11:00 -> 11 E depois ao fazer as contas do tempo decorrido, é feito 11-38, o que dá -27 minutos 😕 Devias fazer: TI[i] = (h*60)+min Assim já fica: 8:30 -> 510 11:00 -> 660 E o tempo decorrido é 660-510 = 150 minutos 😁 ❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents A list of command line apps Link to comment Share on other sites More sharing options...
trust Posted January 7, 2010 at 07:16 PM Author Report Share #304664 Posted January 7, 2010 at 07:16 PM eu ja passo as horas para min na função que lê as horas! ? a mim so deixa inserir horas se usar h=0 e min=0 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now