Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.70 KB | None | 0 0
  1. /*
  2.  * Your homework assignments:
  3.  * 1. Initialize every variable!
  4.  * 2. Test every scanf! Handle invalid input!
  5.  * 3. Try to think of any conditions that can crash your program!
  6.  *
  7.  *    DON'T LET YOUR ROCKET EXPLODE.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <math.h>
  12.  
  13. void main()
  14. {      
  15.     int m,y,w,clw,cld;
  16.     float a = 0, b = 0;
  17.     char ch = 0;
  18.        
  19.     printf("Please enter:\nL to solve a linear equation\nD to calculate the number of days in month\nW to calculate the number of weeks and days\n");
  20.     fflush(stdin);
  21.     scanf("%c",&ch);
  22.    
  23.     if (ch=='L')
  24.     {
  25.         printf("Please enter the equation ax+b=0 arguments: a and b\n");
  26.        
  27.         fflush(stdin);
  28.         if (scanf("%f %f", &a, &b) == 2)
  29.         {
  30.             if (a==0 && b==0)
  31.                 printf("There are infinity solutions for the equation\n");
  32.                
  33.             else if (a!=0)
  34.                 printf("There is a single solution for the equation: %f\n", -b/a);
  35.                
  36.             else if (a==0 && b!=0)
  37.                 printf("There is no solution for the equation\n");
  38.         }
  39.         else
  40.             printf("Invalid input!\n");
  41.     }
  42.    
  43.     else if (ch=='D')
  44.     {
  45.         printf("Please enter the month and the year\n");
  46.         scanf("%d %d", &m,&y);
  47.            
  48.         if (m==1 || m==3 ||m==5 || m==7 || m==8 || m==10 || m==12)
  49.             printf("The number of days in month 31\n");
  50.  
  51.         else if (m==4 || m==6 || m==9 || m==11)
  52.                 printf("The number of days in month 30\n");
  53.    
  54.         else if (m==2 && !(y%4) && (y%400))
  55.             printf("The number of days in month 29\n");
  56.  
  57.         else
  58.         {
  59.             if (m==2)
  60.                 printf("The number of days in month 28\n");
  61.         }
  62.     }
  63.    
  64.     else if (ch=='W')
  65.     {
  66.         printf("Please enter the number of days:\n");
  67.         scanf("%d", &w);
  68.         clw=w/7;
  69.         cld=w%7;
  70.         printf("The number of weeks is: %d weeks and %d days\n", clw,cld);
  71.     }
  72.  
  73.     else
  74.         printf("Invalid character!\n");
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement