Trabalhando com Função
Autor: Algoritmo Nota 10 | Publicado: 2011-05-16T16:18:00-07:00



#include&lt; stdio.h&gt;
#include&lt; stdlib.h&gt;

int &nbsp; menu (void); // Estou declarando minha função do tipo Menu
int &nbsp; soma(int a, int b); // Declaro a função Soma
int &nbsp; sub(int a, int b);  // Declaro a função Subtração
float divi(int a, int b);  // Declaro a função Divisão
int &nbsp; multi(int a, int b);  // Declaro a função Multiplicação

int main(){&nbsp;&nbsp; 
&nbsp;&nbsp; &nbsp; 
&nbsp;&nbsp; &nbsp; &nbsp; int a, b, op=0; &nbsp; &nbsp; 
&nbsp;&nbsp; &nbsp; &nbsp; while(op != 5){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;op = menu();
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((op&gt;=1)&amp;&amp;(op&lt;=4))
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("Digite 1 Valores Inteiros : &nbsp;");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf("%d", &amp;a);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("Digite 1 Valores Inteiros : &nbsp;");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf("%d", &amp;b);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;switch(op){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 1:
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("A Soma de %d + %d e igual a &nbsp;= %d\n", a, b, soma (a, b));
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 2:
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("A Subtracao de %d - %d e igual a &nbsp;= %d\n", a, b, sub (a, b));
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 3:
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("A Divisao de %d / %d e igual a &nbsp;= %d\n", a, b, divi (a, b));
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 4:
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("A Multiplicacao de %d * %d e igual a &nbsp;= %d\n", a, b, multi (a, b));
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 5:
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("Tchau ");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break; &nbsp; 
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; 
&nbsp;&nbsp; &nbsp; &nbsp; 
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;int menu(void){ &nbsp; 
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; int op;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("****************************************************\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MENU &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("****************************************************\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("[1] - Soma \n"); // Se o usuario digitar a opção 1 ele vai chamar a função Soma
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("[2] - Subtracao \n");  // Se o usuario digitar a opção 2 ele vai chamar a função Subtração
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("[3] - Divisao \n"); // Se o usuario digitar a opção 3 ele vai chamar a função Divisão
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("[4] - Multiplicacao \n");  // Se o usuario digitar a opção 4 ele vai chamar a função Multiplicação
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("[5] - Sair \n");  // Se o usuario digitar a opção 5 ele vai Sai do programa
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("Digite uma Funcao &nbsp; == ");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; scanf("%d", &amp;op);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return op;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; 
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;int soma(int a , int b){ // Função Soma
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return a+b;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;system("pause\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;int sub (int a, int b){ // Função Subtração
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return a-b;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;system ("pause\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;int multi (int a, int b){ // Função Multiplicação
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return a*b;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;system ("pause\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;float divi (int a, int b){ // Função Divisão
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return a / b;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;system ("pause\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}