| |

VerySource

 Forgot password?
 Register
Search
View: 1608|Reply: 13

C ++ experts help to see a problem, anxious ~~~

[Copy link]

2

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 New Zealand

Post time: 2020-2-20 15:30:01
| Show all posts |Read mode
The same results, but can be replaced by other methods? I always feel that this CODE is a bit too long !!


#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;
Ranch
// Initialise variable
optionM = 0;
Ranch
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
Ranch
// 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";
}
}
Ranch
Ranch
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";
}
Ranch
}
}

// 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";
}
Reply

Use magic Report

2

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 New Zealand

 Author| Post time: 2020-4-27 19:00:02
| Show all posts
In fact, just enter the month and day of the week, and then the calendar will be displayed from the day of the week to the end, according to the number of days in the month ~~
Reply

Use magic Report

2

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 New Zealand

 Author| Post time: 2020-4-27 19:15:01
| Show all posts
I use the same SWITCH to do it, but it shows an error: help solve it ::


#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";
}
The
getch ();
return 0;
}
// this function will display 31 days in "days31" function
void days31 () {
The
// obtian and initialise variables
int day = 1;
int maxrow = 6;
int maxcol = 6;
int date = 0;
The
The
// 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
The
cout << "\n\n Please Insert the day you wanted:";
cin >> date;
The
days (); // display the dates from Sun to Sat
The
if (date> 0&&date <= 7) {
for (int row = 1; row <= maxrow&&day <= 31; row ++)
for (int col = 0; col <= maxcol&&day <= 31; col ++)
The
if (day <= 31) {// month have 31 day
cout << setw (6) << day ++; // print out the number from 1-31 by 6 spaces
The
}
else {
cout << "\n\nError !!!! Please try again";
}
The
The
The
The
The
void days () {
The
The
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";
The
}
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-4-29 16:00:01
| Show all posts
#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";
}
The
getch ();
return 0;
}
// this function will display 31 days in "days31" function
void days31 ()
{
The
// obtian and initialise variables
int day = 1;
int maxrow = 6;
int maxcol = 6;
int date = 0;
The
The
// 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
The
cout << "\n\n Please Insert the day you wanted:";
cin >> date;
The
days (); // display the dates from Sun to Sat
The
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
The
}
The
}
cout << "\n";
}
}
else
{
cout << "\n\nError !!!! Please try again";
}
The
The
}
The
The
void days () {
The
The
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";
The
}
Reply

Use magic Report

2

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 New Zealand

 Author| Post time: 2020-5-10 17:15:01
| Show all posts
Upstairs, for the program you gave, I enter March, and then enter the week 1 to start 31 days, or start from Sunday, no change ~
Reply

Use magic Report

2

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 New Zealand

 Author| Post time: 2020-5-11 19:30:01
| Show all posts
The result I want is, if you choose March (31 days), then select week 4. Then the number 1 of the calendar that comes out will be arranged from the first week of Thursday 4 days until the end of the last day.

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


Arranged to 31 ~ as above
Reply

Use magic Report

2

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 New Zealand

 Author| Post time: 2020-6-11 19:30:01
| Show all posts
No one knows? Help me
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-6-14 09:00:01
| Show all posts
You can find out for yourself, what are the duplicates in the code, just encapsulate them into the function. If you can't find duplicates, then it's pretty good, don't care too much about the code length.
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-18 10:15:01
| Show all posts
Brothers master:
        Help me, my brother has a question about the SPI interception network packet technology. I have intercepted the packet and put it in the file, but I don’t know how to filter the packet. I wrote it in C++! Experts please give pointers! ! ! thank you very much! ! It’s okay to leave a contact, like qq?
Reply

Use magic Report

2

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 New Zealand

 Author| Post time: 2020-7-7 02:00:01
| Show all posts
The result I want is, if you choose March (31 days), then select week 4. Then the number 1 of the calendar that comes out will be arranged from the first week of week 4 to 31 days until the last day.

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


Arrange to 31~ as above


Master help~~
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list