ctime to cstring

1.cstring to time
CString str = "2000-1-1 23:01:22" ;
COleDateTime tm;
tm.ParseDateTime(str);
SYSTEMTIME st;
tm.GetAsSystemTime(st);
CTime ct(st);
2.ctime to cstring
 
CString strTime; 
CTime tTime = CTime::GetCurrentTime(); 
strTime = tTime.Format("%Y-%m-%d %H:%M:%S");

#include <iostream>#include <cstdlib>#include <ctime>#include <cstring>using namespace std;const int MAX_WRONG = 6; //最大错误次数string WORDS[] = {"HELLO", "WORLD", "COMPUTER", "PROGRAMMING", "LANGUAGE"}; //单词库int main(){ srand(time(0)); //用当前时间作为随机数种子 char play; //玩家选择是否继续游戏 do { string word = WORDS[rand() % 5]; //从单词库中随机选择一个单词 int wrong = 0; //错误次数 string soFar(word.size(), '_'); //初始时,所有字母都用"_"代替 string used = ""; //已经猜过的字母 cout << "Welcome to Hangman. Good luck!\n"; //循环直到猜出单词或者错误次数达到最大值 while (wrong < MAX_WRONG && soFar != word) { cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n"; cout << "\nYou've used the following letters:\n" << used << endl; cout << "\nSo far, the word is:\n" << soFar << endl; char guess; //玩家猜测的字母 cout << "\n\nEnter your guess: "; cin >> guess; guess = toupper(guess); //将小写字母转换成大写字母 while (used.find(guess) != string::npos) { cout << "\nYou've already guessed " << guess << endl; cout << "Enter your guess: "; cin >> guess; guess = toupper(guess); } used += guess; if (word.find(guess) != string::npos) { cout << "That's right! " << guess << " is in the word.\n"; for (int i = 0; i < word.size(); ++i) { if (word[i] == guess) { soFar[i] = guess; } } } else { cout << "Sorry, " << guess << " isn't in the word.\n"; ++wrong; } } if (wrong == MAX_WRONG) { cout << "\nYou've been hanged!"; } else { cout << "\nYou guessed it!"; } cout << "\nThe word was " << word << endl; cout << "\nDo you want to play again? (y/n): "; cin >> play; play = toupper(play); } while (play == 'Y'); return 0;}优化这段代码
最新发布
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值