VerySource

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2156|回复: 13

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

[复制链接]

2

主题

12

帖子

10.00

积分

新手上路

Rank: 1

积分
10.00
发表于 2020-2-20 15:30:01 | 显示全部楼层 |阅读模式
一样的运行结果, 但是可以用其他方法来代替不? 总觉得这个CODE有点过长!!


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

//Fuction for print the function
void print31();//print function for the month that have 31 days
void print30();//print function for the month that have 30 days
void print28();//print function for the month that have 28 days
void date();//print function for the header for each date

int main () {

        int optionM;
        bool done = false;
       
        //Initialise variable
        optionM = 0;
       
        while (!done) {
                clrscr();//Clear the screen
                //Obtain the user to select the month
                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;//Input option for month
               
                //check if the user select the option month that are on month 1 and 3 and 5 and 7 and 8 and 10 and 12 which is 31 days month
                if (optionM == 1 || optionM ==3 || optionM == 5 || optionM == 7 || optionM == 8 || optionM ==10 || optionM == 12) {
                        print31 ();        done = true;                               
                }//check if the user select the option month that are on month 4 and 6 and 9 and 11 which is 30 days month
                else if (optionM == 4 || optionM == 6 || optionM == 9 || optionM ==11){
                        print30 (); done = true;
                }//check if the user select the option month that are on month 2 that have 28 days month
                else if (optionM == 2) {
                        print28 (); done = true;
                }
                else {
                        cout        <<"\n\nError!!!! Please try again";
                }
        }               
       
               
getch ();
return 0;
}


                        //functiion for print the function for user select the month that have 31 days
                        void print31 () {

                                        //Initialise variable
                                        int maxrow;
                                        int maxcol;                                                               
                                        int num = 1;
                                        maxrow = 6;
                                        maxcol = 6;
                                        bool done = false;

                        while (!done){
                                //obtain user to select the start day in the week
                                cout        <<"\n\nThis month have 31 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: ";
                                int optionD = 0;//intput option day
                                cin                >>optionD;
                                date();//print the header for sunday to saturday buy setting in print date.
                                cout        <<"\n";
                                //check user select option day between 1 and 7 otherwise error and redo again
                                if (optionD > 0 && optionD <= 7){                                                               
                                                for (int row = 1; row <= maxrow && num <= 31; row++) {//set up row to have 31 number in row
                                                        for        (int  col = 0; col <= maxcol && num <= 31; col++){// set up column to have only 7 column
                                                                if (col +1 < optionD && !done) {//to set the start date of the week
                                                                        cout        <<"      ";//space for the start date
                                                                }
                                                                else {
                                                                        if (num <=31) {//month have 31 day
                                                                        cout        <<setw(6) <<num++;//print out the number from 1 - 31 by 6 spaces
                                                                        }                                               
                                                                        done = true;
                                                                }
                                                        }
                                                        cout        <<"\n";//enter new line
                                                }
                                }
                                else {
                                        cout        <<"\n\nError!!!! Please try again";
                                }
                        }
                        }
                        //functiion for print the function for user select the month that have 30 days
                        void print30 () {

                                        //Initialise variable
                                        int maxrow;
                                        int maxcol;                                                               
                                        int num = 1;
                                        maxrow = 6;
                                        maxcol = 6;
                                        bool done = false;
                        while (!done) {
                                //obtain user to select the start day in the week       
                                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: ";
                                int optionD = 0;//intput option day
                                cin                >>optionD;
                                date();//print the header for sunday to saturday buy setting in print date.
                                cout        <<"\n";
                                //check user select option day between 1 and 7 otherwise error and redo again
                                if (optionD > 0 && optionD <= 7){                                                               
                                                for (int row = 1; row <= maxrow && num <= 30; row++) {//set up row to have 30 number in row
                                                        for        (int  col = 0; col <= maxcol && num <= 30; col++){// set up column to have only 7 column
                                                                if (col +1 < optionD && !done) {//to set the start date of the week
                                                                        cout        <<"      ";//space for the start date
                                                                }
                                                                else {
                                                                        if (num <=30) {//month have 30 day
                                                                        cout        <<setw(6) <<num++; //print out the number from 1 - 30 by 6 spaces
                                                                        }
                                                                        done = true;
                                                                }
                                                        }
                                                        cout        <<"\n";//enter new line
                                                }
                                }
                                else {
                                        cout        <<"\n\nError!!!! Please try again";
                                }
                               
                        }
                        }

                        //functiion for print the function for user select the month that have 31 days
                        void print28 () {

                                        //Initialise variable
                                        int maxrow;
                                        int maxcol;                                                               
                                        int num = 1;
                                        maxrow = 6;
                                        maxcol = 6;
                                        bool done = false;

                        while (!done){
                                //obtain user to select the start day in the week       
                                cout        <<"\n\nThis month have 28 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: ";
                                int optionD = 0;//intput option day
                                cin                >>optionD;
                                date();//print the header for sunday to saturday buy setting in print date.
                                cout        <<"\n";

                                //check user select option day between 1 and 7 otherwise error and redo again
                                if (optionD > 0 && optionD <= 7){                                                               
                                                for (int row = 1; row <= maxrow && num <= 28; row++) {//set up row to have 28 number in row
                                                        for        (int  col = 0; col <= maxcol && num <= 28; col++){// set up column to have only 7 column
                                                                if (col +1 < optionD && !done) {//to set the start date of the week
                                                                        cout        <<"      ";//space for the start date
                                                                }
                                                                else {
                                                                        if (num <=28) {//month have 28 day
                                                                        cout        <<setw(6) <<num++; //print out the number from 1 - 28 by 6 spaces
                                                                        }
                                                                        done = true;
                                                                }
                                                        }
                                                        cout        <<"\n";//enter new line
                                                }
                                }
                                else {
                                        cout        <<"\n\nError!!!! Please try again";
                                }
                        }
                        }

                        void date() {
                           clrscr();//clear the screen
                        //fuction to print the header for date sun to friday       
                           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";
                        }


回复

使用道具 举报

2

主题

12

帖子

10.00

积分

新手上路

Rank: 1

积分
10.00
 楼主| 发表于 2020-4-27 19:00:02 | 显示全部楼层
其实就是输入月份和星期几, 然后就会显示日历从星期几排到最后, 按月份的天数来排~~
回复

使用道具 举报

2

主题

12

帖子

10.00

积分

新手上路

Rank: 1

积分
10.00
 楼主| 发表于 2020-4-27 19:15:01 | 显示全部楼层
我用同样的SWITCH来搞, 但是显示错误: 帮忙解决一下::


#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
                                                                 
                                }
                                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";
                          
                }
回复

使用道具 举报

0

主题

3

帖子

4.00

积分

新手上路

Rank: 1

积分
4.00
发表于 2020-4-29 16: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-5-10 17:15:01 | 显示全部楼层
楼上,你给的那个程序,我输入3月份,然后输入星期1开始排31天, 还是从星期天开始排啊,没有变化啊~
回复

使用道具 举报

2

主题

12

帖子

10.00

积分

新手上路

Rank: 1

积分
10.00
 楼主| 发表于 2020-5-11 19:30:01 | 显示全部楼层
我要的结果是, 如果是选择3月份(31天), 再选择星期4. 然后出来的日历的1号就从第一周星期4开始排31天一直到最后一天完.

就好像:
SUN  MON  TUE  WED  THU  FRI  SAT
                     1    2    3
4    5    6    7    8    9    10
11    ..    .   ..   ..


像上面那样排到31~~
回复

使用道具 举报

2

主题

12

帖子

10.00

积分

新手上路

Rank: 1

积分
10.00
 楼主| 发表于 2020-6-11 19:30:01 | 显示全部楼层
没有人知道啊? 帮帮忙啊
回复

使用道具 举报

0

主题

55

帖子

44.00

积分

新手上路

Rank: 1

积分
44.00
发表于 2020-6-14 09:00:01 | 显示全部楼层
你自己找找,代码里有哪些是重复的,把它们封装到函数即可。如果你找不出重复,那么也就蛮好了,不用太在意代码长短。
回复

使用道具 举报

0

主题

4

帖子

3.00

积分

新手上路

Rank: 1

积分
3.00
发表于 2020-6-18 10:15:01 | 显示全部楼层
各位大哥高手:
        帮个忙,小弟有个问题是关于SPI拦截网络封包技术的,我现在已经拦截到了数据包,放到了文件中,但不知道如何过滤数据包了,我是用c++写的!请各位专家指点!!!非常感谢!!留个联系方式也好啊,象qq什么的?
回复

使用道具 举报

2

主题

12

帖子

10.00

积分

新手上路

Rank: 1

积分
10.00
 楼主| 发表于 2020-7-7 02:00:01 | 显示全部楼层
我要的结果是, 如果是选择3月份(31天), 再选择星期4. 然后出来的日历的1号就从第一周星期4开始排31天一直到最后一天完.

就好像:
SUN  MON  TUE  WED  THU  FRI  SAT
                     1    2    3
4    5    6    7    8    9    10
11    ..    .   ..   ..


像上面那样排到31~~


高手帮帮忙啊~~
回复

使用道具 举报

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

本版积分规则

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

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