在C++中将If Else语句更改为循环

vx6bjr1n  于 2022-11-27  发布在  其他
关注(0)|答案(1)|浏览(156)

我对编程很陌生。有没有办法把这个If else语句改成一个循环,把这个书的列表按升序和降序或者(每个主题的书)的数量排序。
基本上,它模拟了一个图书馆系统,在那里你选择一个主题,然后你会看到未排序的书籍与数量之前的书籍名称,最后你会选择你想如何看到列表无论是升序或降序列表。

#include<iostream>
using namespace std;

int main()
{
    int subject;
    cout<<"What subject? \n" ;
    cout<<"[1] English \n" ;
    cout<<"[2] Mathematics \n" ;
    cout<<"[3] Science \n" ;
    cout<<"[4] Filipino \n\n" ;
    cin>>subject;
    switch(subject) {
        case 1 : cout<<"How do you want to see the books on this subject? \n\n";
                 cout<<"3 Spelling\n" ;
                 cout<<"5 Alphabet\n" ;
                 cout<<"6 Punctuations\n" ;
                 cout<<"2 Grammar\n" ;
                 cout<<"5 Alphabet\n" ;
                 cout<<"1 Literature\n\n" ;
        {

        int a = 1;
        int ans;

        ans=a;

        int inp;
        cout<<"type 1 for ascending and 2 for descending\n \n"<<endl;
        cin>>inp;

        if (inp==ans) {
        cout<<"Ascending\n\n" ;
        cout<<"1 Literature\n" ;
        cout<<"2 Grammar\n" ;
        cout<<"3 Spelling\n" ;
        cout<<"4 Short Stories\n" ;
        cout<<"5 Alphabet\n" ;
        cout<<"6 Punctuations\n" ;}
        else{
        cout<<"Descending\n\n" ;
        cout<<"6 Punctuations \n" ;
        cout<<"5 Alphabet \n" ;
        cout<<"4 Short Stories \n" ;
        cout<<"3 Spelling \n" ;
        cout<<"2 Grammar\n" ;
        cout<<"1 Literature\n" ;
       
         } 
         } 
        break;
        case 2 : cout<<"How do you want to see the books on this subject? \n";
                 cout<<"3 Calculus\n" ;
                 cout<<"1 Algebra\n" ;
                 cout<<"6 Trigonometry\n" ;
                 cout<<"4 Graphs\n" ;
                 cout<<"2 Functiosn\n" ;
                 cout<<"5 Geometry\n\n" ;
        {

        int a = 1;
        int ans;

        ans=a;

        int inp;
        cout<<"type 1 for ascending and 2 for descending\n \n"<<endl;
        cin>>inp;

        if (inp==ans) {
        cout<<"Ascending\n\n" ;
        cout<<"1 Algebra\n" ;
        cout<<"2 Functiosn\n" ;
        cout<<"3 Calculus\n" ;
        cout<<"4 Graphs\n" ;
        cout<<"5 Geometry\n" ;
        cout<<"6 Trigonometry\n" ;}
        else{
        cout<<"Descending\n\n" ;
        cout<<"6 Trigonometry \n" ;
        cout<<"5 Geometry \n" ;
        cout<<"4 Graphs \n" ;
        cout<<"3 Calculus \n" ;
        cout<<"2 Functions \n" ;
        cout<<"1 Algebra \n" ;
       
         } 
         }
        break;
        case 3 : cout<<"How do you want to see the books on this subject? \n";
                 cout<<"3 Solar system\n" ;
                 cout<<"1 Living things\n" ;
                 cout<<"6 Newton's Theory\n" ;
                 cout<<"4 Energy\n" ;
                 cout<<"2 Non-living things\n" ;
                 cout<<"5 Elements\n\n" ;
        {

        int a = 1;
        int ans;

        ans=a;

        int inp;
        cout<<"type 1 for ascending and 2 for descending\n \n"<<endl;
        cin>>inp;

        if (inp==ans) {
        cout<<"Ascending\n\n" ;
        cout<<"1 Living things\n" ;
        cout<<"2 Non-living things\n" ;
        cout<<"3 Solar system\n" ;
        cout<<"4 Energy\n" ;
        cout<<"5 Elements\n" ;
        cout<<"6 Newton's Theory\n" ;}
        else{
        cout<<"Descending\n\n" ;
        cout<<"6 Newton's Theory\n" ;
        cout<<"5 Elements \n" ;
        cout<<"4 Energy \n" ;
        cout<<"3 Solar system \n" ;
        cout<<"2 Non-living things\n" ;
        cout<<"1 Living things\n" ;
       
         } 
         }
        break;
        case 4 : cout<<"How do you want to see the books on this subject? \n";
                 cout<<"3 Gramatika\n" ;
                 cout<<"1 Pandiwa\n" ;
                 cout<<"6 Tamang Pagbigkas\n" ;
                 cout<<"4 Bantas\n" ;
                 cout<<"5 Mga kilalang manunulat\n" ;
                 cout<<"2 Maikling Kwento\n\n" ;
        {

        int a = 1;
        int ans;

        ans=a;

        int inp;
        cout<<"type 1 for ascending and 2 for descending\n \n"<<endl;
        cin>>inp;

        if (inp==ans) {
        cout<<"Ascending\n\n" ;
        cout<<"1 Pandiwa\n" ;
        cout<<"2 Maikling Kwento\n" ;
        cout<<"3 Gramatika\n" ;
        cout<<"4 Bantas\n" ;
        cout<<"5 Mga kilalang manunulat\n" ;
        cout<<"6 Tamang Pagbigkas\n" ;}
        else{
        cout<<"Descending\n\n" ;
        cout<<"6 Tamang Pagbigkas \n" ;
        cout<<"5 Mga kilalang manunulat \n" ;
        cout<<"4 Bantas \n" ;
        cout<<"3 Gramatika \n" ;
        cout<<"2 Maikling kwento\n" ;
        cout<<"1 Pandiwa\n" ;
       
         } 
         }
        break; 
        default : cout<<"Choose between 1,2,3,and 4";
        }
    
}
vhmi4jdf

vhmi4jdf1#

你需要在你的编码以及设计这个应用程序中改进很多。首先你想把你的if else语句转换成循环,你可以使用while循环,并为用户添加一个退出菜单的选项。所有的菜单(你要求用户输入选择的地方)都应该有退出选项。例如下面的例子

case 1 : cout<<"How do you want to see the books on this subject? \n\n";
                 cout<<"3 Spelling\n" ;
                 cout<<"5 Alphabet\n" ;
                 cout<<"6 Punctuations\n" ;
                 cout<<"2 Grammar\n" ;
                 cout<<"5 Alphabet\n" ;
                 cout<<"1 Literature\n\n" ;
                 cout<<"0 Exit Menu/Application\n\n";

要按名称重新排列图书,您可以比较图书名称的第一个字符ASCII值,并根据菜单选择在循环内按排序顺序重新排列图书。
希望这对你有帮助。我在这里并不是建议使用更好的数据结构,因为记住你是编码新手。

相关问题