Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Your homework assignments:
- * 1. Initialize every variable!
- * 2. Test every scanf! Handle invalid input!
- * 3. Try to think of any conditions that can crash your program!
- *
- * DON'T LET YOUR ROCKET EXPLODE.
- */
- #include <stdio.h>
- #include <math.h>
- void main()
- {
- int m,y,w,clw,cld;
- float a = 0, b = 0;
- char ch = 0;
- 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");
- fflush(stdin);
- scanf("%c",&ch);
- if (ch=='L')
- {
- printf("Please enter the equation ax+b=0 arguments: a and b\n");
- fflush(stdin);
- if (scanf("%f %f", &a, &b) == 2)
- {
- if (a==0 && b==0)
- printf("There are infinity solutions for the equation\n");
- else if (a!=0)
- printf("There is a single solution for the equation: %f\n", -b/a);
- else if (a==0 && b!=0)
- printf("There is no solution for the equation\n");
- }
- else
- printf("Invalid input!\n");
- }
- else if (ch=='D')
- {
- printf("Please enter the month and the year\n");
- scanf("%d %d", &m,&y);
- if (m==1 || m==3 ||m==5 || m==7 || m==8 || m==10 || m==12)
- printf("The number of days in month 31\n");
- else if (m==4 || m==6 || m==9 || m==11)
- printf("The number of days in month 30\n");
- else if (m==2 && !(y%4) && (y%400))
- printf("The number of days in month 29\n");
- else
- {
- if (m==2)
- printf("The number of days in month 28\n");
- }
- }
- else if (ch=='W')
- {
- printf("Please enter the number of days:\n");
- scanf("%d", &w);
- clw=w/7;
- cld=w%7;
- printf("The number of weeks is: %d weeks and %d days\n", clw,cld);
- }
- else
- printf("Invalid character!\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement