这是我的问题的解决方案,以防万一其他人面临它。学分转到@qPCR4vir
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
main()
{ //This program encourages the user to perform a sum of two fractions.
int N1, D1, N2, D2, N, D;
char divide{};
system("cls");
cout<<"The Format is: 'A/B' & 'C/D'..\n\n";
cout<<"Enter First Fraction: ";
cin>>N1>>divide>>D1;
cout<<"Enter Second Fraction: ";
cin>>N2>>divide>>D2;
if (divide=='/')
{
N=(N1*D2)+(D1*N2); //Numerator
D=D1*D2; //Denominator
cout<<"Sum of Both Fractions is: "<<N<<"/"<<D;
}
else
{
system("cls");
cout<<"The Correct Format is: A/B & C/D\nWhere these alphabets are Integers..\n\n";
cout<<"Example: 4/5";
}
getch();
system("cls");
return(0);
}
cin
这是仅为“ ”语句指定格式的代码部分。
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int main()
{
char divide{}; //iota{};
int x{},y{};
cout<<"Enter Dividion of Two Numbers (A/B): ";
cin>>x>>divide>>y; //>> iota;
if (divide=='/') //&& iota=='i')
{
//x=(N1*D2)+(D1*N2); //Numerator
//y=D1*D2; //Denominator
cout<<"The Fractional Form is: "<<x<<"/"<<y;
}
else
{
system("cls");
cout<<"The Correct Format is: A/B & C/D\nWhere these alphabets are Integers..\n\n";
cout<<"Example: 4/5";
}
getch();
return 0;
}
注意:这是@qPCR4vir 的简化/修改解决方案;用 cin 读取特定格式