VerySource

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
12
返回列表 发新帖
楼主: iskiller

C++ 高手帮忙看个问题,急~~~

[复制链接]

1

主题

39

帖子

27.00

积分

新手上路

Rank: 1

积分
27.00
发表于 2020-7-12 18:15:01 | 显示全部楼层
main 函数不要放那么多细节.
看代码从main函数开始, 首先给人一个总体的概况.
将代码分到几个函数里面,然后main调用他们,给每一个函数注释.那么感觉会清爽很多.
回复

使用道具 举报

0

主题

9

帖子

7.00

积分

新手上路

Rank: 1

积分
7.00
发表于 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";
        }
    }
}

回复

使用道具 举报

2

主题

12

帖子

10.00

积分

新手上路

Rank: 1

积分
10.00
 楼主| 发表于 2020-7-16 10:00:01 | 显示全部楼层
我是想麻烦人帮我改一下这段:

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>



void days31();

void days();

int main () {
//declare and initialise variables
int month = 1;

//obtian the month in list
cout<<"\n\n January (1)";
cout<<"\n\n February (2)";
cout<<"\n\n March (3)";
cout<<"\n\n April (4)";
cout<<"\n\n May (5)";
cout<<"\n\n June (6)";
cout<<"\n\n July (7)";
cout<<"\n\n August (8)";
cout<<"\n\n September (9)";
cout<<"\n\n October (10)";
cout<<"\n\n November (11)";
cout<<"\n\n December (12)";
cout<<"\n Please select the month: ";
//user input a month
cin>>month;

switch (month){
//in those cases the days will be display 31 days
case 1: case 3: case 5:
    case 7: case 8: case 10:
    case 12:
days31();
break;
//in this case the days will be display 28 days
case 2:
cout<<"\n 28"; //days28();
break;
//in those cases the days will be display 30 days
case 4: case 6: case 9: case 11:
cout<<"\n 30";//days30();
break;
//other input will be show a error message
default:
cout<<"\n error messages!!! it is not correct input, please try again";
}

getch ();
return 0;
}
//this function will display 31 days in "days31"function
void days31()
{

//obtian and initialise variables
int day=1;
int maxrow=6;
int maxcol=6;
int date=0;


//obtain the sharp of calendar from Sun to Sat
cout <<"\n 1. Sunday";
    cout <<"\n 2. Monday";
    cout <<"\n 3. Tuesday";
    cout <<"\n 4. Wednsday";
    cout <<"\n 5. Thusday";
    cout <<"\n 6. Friday";
    cout <<"\n 7. Saturday";
//user input the day

cout<<" \n\n Please Insert the day you wanted: ";
cin>>date;

days(); //display the dates from Sun to Sat

if (date > 0 && date <= 7)
{
for (int row = 1; row <= maxrow && day <= 31; row++)
{
for(int  col = 0; col <= maxcol && day <= 31; col++)
{
if (day <=31)
{//month have 31 day
   cout<<setw(6) <<day++;//print out the number from 1 - 31 by 6 spaces

                    }

}
cout<<"\n";
}
}
else
{
cout<<"\n\nError!!!! Please try again";
}


  }


void days() {
  

   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";
   cout     << "\n";
   
}


回复

使用道具 举报

2

主题

12

帖子

10.00

积分

新手上路

Rank: 1

积分
10.00
 楼主| 发表于 2020-7-18 18:45:02 | 显示全部楼层
帮帮忙
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

快速回复 返回顶部 返回列表