-----------------------------C programming!! ONLY PLEASE------------------------
ID: 648561 • Letter: #
Question
-----------------------------C programming!! ONLY PLEASE-----------------------------------------------------------------
Create a program that prompts a user for 3 strings.
The user will input a number numerically or as text followed by another number numerically or as text followed by a math symbol or text.
The program will determine what the input is and print the result.
This will be a larger program than normal, try to plan out what will you do ahead of time.
// sample output 1
Enter a string: Two
Enter a string: 4
Enter a string: +
Answer = 6
//sample output 2
Enter a string: 5
Enter a string: two
Enter a string: divide
Answer = 2.5
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int getNumber(char str1[]){
int num1;
if (strcmp(str1, "One") == 0 || strcmp(str1, "one") == 0){
num1 = 1;
}
else if (strcmp(str1, "Two") == 0 || strcmp(str1, "two") == 0){
num1 = 2;
}
else if (strcmp(str1, "Three") == 0 || strcmp(str1, "three") == 0){
num1 = 3;
}
else if (strcmp(str1, "Four") == 0 || strcmp(str1, "four") == 0){
num1 = 4;
}
else if (strcmp(str1, "Five") == 0 || strcmp(str1, "five") == 0){
num1 = 5;
}
else if (strcmp(str1, "Six") == 0 || strcmp(str1, "six") == 0){
num1 = 6;
}
else if (strcmp(str1, "Seven") == 0 || strcmp(str1, "seven") == 0){
num1 = 7;
}
else if (strcmp(str1, "Eight") == 0 || strcmp(str1, "eight") == 0){
num1 = 8;
}
else if (strcmp(str1, "Nine") == 0 || strcmp(str1, "nine") == 0){
num1 = 9;
}
else if (strcmp(str1, "Zero") == 0 || strcmp(str1, "zero") == 0){
num1 = 0;
}
else{
num1 = atoi(str1);
}
return num1;
}
int main(){
char str1[100], str2[100], str3[100];
printf("Enter a string: ");
scanf("%s", str1);
printf("Enter a string: ");
scanf("%s", str2);
printf("Enter a string: ");
scanf("%s", str3);
int num1, num2;
double res;
num1 = getNumber(str1);
num2 = getNumber(str2);
if (strcmp(str3, "+") == 0 || strcmp(str3, "plus") == 0){
res = num1 + num2;
}
else if (strcmp(str3, "-") == 0 || strcmp(str3, "minus") == 0){
res = num1 - num2;
}
else if (strcmp(str3, "-") == 0 || strcmp(str3, "divide") == 0){
if (num2 == 0){
printf("Remainder should not be zero ");
return 0;
}
res = num1 / num2;
}
else if (strcmp(str3, "-") == 0 || strcmp(str3, "multiply") == 0){
res = num1 * num2;
}
else{
printf("Input is invalid ");
return 0;
}
printf("Answer = %.2lf ", res);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.