这个c语言如何转化为C++ - 爱问答

(爱问答)

这个c语言如何转化为C++

#include "string.h"
#include <stdio.h>
char *WEEK[7] = { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
struct demand
{
char name[5];
int day[7];
} man[7];
int main()
{
int IsChecked(int p[]);
int t = 0, j, ren[7];
long i, k;
printf("****保安值班系统***** ");
printf("请各位分别输入各自合适的休假日 ");
printf("数字0 1 2 3 4 5 6分别表示星期天到星期六 ");

for (i = 0; i<7; i++)
{
printf("请第%d个人输入 ", i + 1);
for (k = 0; k<7; k++)
{
char c;
scanf("%c", &c);
if (c == ' ')
{
break;//读取到换行符,即回车,退出循环。
}
else if (c >= 48 && c <= 57)
{
man[i].day[k] = (int)(c - '0');
}
else if (c == ' ')
{
k--;
}
}
printf("你输入日期是:");
for (int m = 0; m < k; m++)
{
printf("%d ", man[i].day[m]);
}
for (int m = k; m < 7; ++m)
{
man[i].day[m] = 8;
printf("%d ", man[i].day[m]);
}
printf(" ");
}
printf("############################################");
printf(" # 赵, 钱, 孙, 李, 周 , 吴, 陈 , # ");
printf("*--------------------------------------------------* ");
printf("");
for (i = 0; i < 2097152; ++i)
{
for (j = 0; j<7; ++j)
{
ren[j] = (i >> (3 * j)) & 7; //*通过这个循环,穷尽0-7在数组中所有的排列组合方式*//
}
if (!(ren[0] == man[0].day[0] || ren[0] == man[0].day[1] || ren[0] == man[0].day[2] || ren[0] == man[0].day[3] || ren[0] == man[0].day[4] || ren[0] == man[0].day[5] || ren[0] == man[0].day[6]))
continue;//*如果赵保安不是休周二或周四,就不用循环了。*//
else if (!(ren[1] == man[1].day[0] || ren[1] == man[1].day[1] || ren[1] == man[1].day[2] || ren[1] == man[1].day[3] || ren[1] == man[1].day[4] || ren[1] == man[1].day[5] || ren[1] == man[1].day[6]))
continue; //*如果钱保安不是休周一或周六,就不用循环了。*//
else if (!(ren[2] == man[2].day[0] || ren[2] == man[2].day[1] || ren[2] == man[2].day[2] || ren[2] == man[2].day[3] || ren[2] == man[2].day[4] || ren[2] == man[2].day[5] || ren[2] == man[2].day[6]))
continue; //*如果孙保安不是休周三或周日,就不用循环了。*//
else if (!(ren[3] == man[3].day[0] || ren[3] == man[3].day[1] || ren[3] == man[3].day[2] || ren[3] == man[3].day[3] || ren[3] == man[3].day[4] || ren[3] == man[3].day[5] || ren[3] == man[3].day[6]))
continue; //*如果李保安不是休周五,就不用循环了。*//
else if (!(ren[4] == man[4].day[0] || ren[4] == man[4].day[1] || ren[4] == man[4].day[2] || ren[4] == man[4].day[3] || ren[4] == man[4].day[4] || ren[4] == man[4].day[5] || ren[4] == man[4].day[6]))
continue; //*如果周保安不是休周一或周四或周六,就不用循环了。*//
else if (!(ren[5] == man[5].day[0] || ren[5] == man[5].day[1] || ren[5] == man[5].day[2] || ren[5] == man[5].day[3] || ren[5] == man[5].day[4] || ren[5] == man[5].day[5] || ren[5] == man[5].day[6]))
continue; //*如果吴保安不是休周二或周五,就不用循环了。*//
else if (!(ren[6] == man[6].day[0] || ren[6] == man[6].day[1] || ren[6] == man[6].day[2] || ren[6] == man[6].day[3] || ren[6] == man[6].day[4] || ren[6] == man[6].day[5] || ren[6] == man[6].day[6]))
continue; //*如果陈保安不是休周三或周六或周日,就不用循环了。*//
else if (!IsChecked(ren))
continue; //*至此,所有的保安可按他们的愿望休假,但是此时的方案可能有两个人同休一天的*//
//*况发生,因此用这个函数排除,如果0-6这七个数字(一周七天)任何一个包含在数组中则此次匹配失败。*//
for (j = 0; j<7; ++j)
{
printf("%s ", WEEK[ren[j]]);
}
printf(" *");
printf(" "); //*输出成功匹配方案*//
++t; //*记录成功匹配个数*//
}
printf("*--------------------------------------------*");
printf(" * %d 种 况! *", t); //*输出成功匹配方案个数*//
printf(" #########################################################");
getchar();
return 0;
}
int IsChecked(int p[])
{
int i, j;
for (i = 0; i<7; ++i)
{
for (j = 0; j<7 && p[j] != i; ++j); /*从0到6循环,如果数组中缺少0-6的任何一位数字,则返回0,如果0-6都有,则返回。*/
if (j == 7)
return 0;
}
return 1; //*这个函数的作用是确保0-6这7个数字均包含在该数组中*//
}

一个概念,C是C++的一个子集,也就是说,你上面的程序完全可以作为C++在C++的编译环境中编译运行,不用做任何的修改。很多C++的程序员,就是用C的风格写C++程序的。


相关标签:c语言

下一篇:将字符串“我喜欢学习php”从UTF-8编码转换成GB2312编码,请将代码补充完整。

上一篇:用excel判断一组文本中的数据,部分匹配后,进行统计

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?