| 
 | 
 
 
发表于 2020-7-15 14:45:02
|
显示全部楼层
 
 
 
变短了一部分,,没有改你的算法. 
 
#include <iostream> 
#include <iomanip> 
 
using namespace std; 
 
void date(); 
void OutputDays(); 
void printDays(int maxrow, int maxcol, int daysOfMonth); 
 
int main () { 
 
    int optionM; 
    bool done = false; 
    optionM = 0; 
 
    while (!done) { 
        //clrscr();//Clear the screen 
        cout<<"\n\nWelcome to programming"; 
        cout<<"\n\nThis program is print out the calendar"; 
        cout<<"\n\nPlease select the month"; 
        cout<<"\n 1. Janruary"; 
        cout<<"\n 2. February"; 
        cout<<"\n 3. March"; 
        cout<<"\n 4. April"; 
        cout<<"\n 5. May"; 
        cout<<"\n 6. June"; 
        cout<<"\n 7. July"; 
        cout<<"\n 8. August"; 
        cout<<"\n 9. September"; 
        cout<<"\n 10. October"; 
        cout<<"\n 11. November"; 
        cout<<"\n 12. December"; 
        cout<<"\n\nPlease select one month: "; 
        cin>>optionM; 
 
       if (optionM == 1 || optionM ==3 || optionM == 5 || optionM == 7 || optionM == 8 || optionM ==10 || optionM == 12)  
       { 
            printDays (6,6,31);done = true; 
        } 
        else if (optionM == 4 || optionM == 6 || optionM == 9 || optionM ==11) 
        { 
            printDays (6,6,30); done = true; 
        } 
        else if (optionM == 2) { 
            printDays (6,6,28); done = true; 
        } 
        else  
        { 
            cout<<"\n\nError!!!! Please try again"; 
        } 
    } 
 
    system("pause"); 
    return 0; 
} 
 
void date() { 
    //clrscr(); 
     cout <<setw(6) << "Sun"; 
    cout <<setw(6) << "Mon"; 
    cout <<setw(6) << "Tue"; 
    cout <<setw(6) << "Wed"; 
    cout <<setw(6) << "Thu"; 
    cout <<setw(6) << "Fri"; 
    cout <<setw(6) << "Sat"; 
} 
 
void OutputDays() 
{ 
    cout<<"\n\nThis month have 30 days"; 
    cout<<"\n\n 1. Sunday"; 
    cout<<"\n\n 2. Monday"; 
    cout<<"\n\n 3. Tuesday"; 
    cout<<"\n\n 4. Wednesday"; 
    cout<<"\n\n 5. Thursday"; 
    cout<<"\n\n 6. Friday"; 
    cout<<"\n\n 7. Saturday"; 
    cout<<"\n\nPlease select one day: "; 
} 
 
void printDays(int maxrow, int maxcol, int daysOfMonth) 
{ 
    int num = 1; 
    bool done = false; 
    while (!done) { 
        OutputDays(); 
        int optionD = 0; 
        cin>>optionD; 
        date(); 
        cout<<"\n"; 
        if (optionD > 0 && optionD <= 7){ 
            for (int row = 1; row <= maxrow && num <= daysOfMonth; row++)  
            { 
                for(int col = 0; col <= maxcol && num <= daysOfMonth; col++) 
                { 
                    if (col + 1 < optionD && !done)  
                    { 
                        cout<<"      "; 
                    } 
                    else  
                    { 
                        if (num <=daysOfMonth)  
                        { 
                            cout<<setw(6) <<num++;  
                        } 
                        done = true; 
                    } 
                } 
                cout<<"\n"; 
            } 
        } 
        else 
        { 
            cout<<"\n\nError!!!! Please try again"; 
        } 
    } 
} 
 
 |   
 
 
 
 |