0

是否可以显示如下输入消息?

Enter First Fraction:_/_
Enter Second Fraction: _/_

_输入空间在哪里?

使用某种以下代码?

cout<<"Enter First Fraction: ";
cin>>N1>>"/">>D1;
cout<<"Enter Second Fraction: ";
cin>>N2>>"/">>D2;

或者

cout<<"Enter First Fraction: ";
cin>>N1>>/>>D1;
cout<<"Enter Second Fraction: ";
cin>>N2>>/>>D2;
4

2 回答 2

0

这是我的问题的解决方案,以防万一其他人面临它。学分转到@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 读取特定格式

于 2020-10-30T16:27:09.297 回答
-1

我在另一个堆栈问题中看到了这段代码..

#include <iostream>
using namespace std;

int main() 
{
    char plus{},img{};
    double x{},y{};
    cin>> x >> plus >> y >> img;
    if (plus!='+' || img!='i') 
        cout << "\nError: "<< "x=" << x <<", plus="  << plus <<", y="  << y <<", img="  << img;
    else
        cout << "\nComplex: " << x << plus << y << img;


    return 0;
}

似乎要求用户在中间输入一个“+”和一个“!” 最后......但我试过了,它仍然不起作用。

于 2020-10-30T15:58:04.203 回答