if,while,std::cin,std::cout用法练习

实例:统计输入值连续出现的次数

#include <iostream>
int main()
{
	int lastVal = 0,inputVal = 0;
	if(std::cin>>lastVal)//第一次输入值赋值给lastVal
	{
		int count = 1;
		while(std::cin>>inputVal)//第二次输入值赋值给inputVal
		{
			if(inputVal == lastVal)//上一次输入的值和这一次输入的值相等时
			{
				++count;
			}else{//不相等先打印之前相等的总共的次数,然后做个终结,最后重新记录
				std::cout<<" input "<<lastVal<<" "<<count<<" times !"<<std::endl;
				lastVal = inputVal;
				count = 1;
			}
		}
		std::cout<<" input "<<lastVal<<" "<<count<<" times !"<<std::endl;//打印最后输入值的统计
	}
	
    return 0;
}


    如果我们输入如下内容:

        <

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <iostream> #include <string> #include <ctime> #include <csignal> #include "Automaton.h" #ifdef _WIN32 #include <windows.h> #else #include <unistd.h> #endif bool isRunning = true; Automaton* automaton; void wait(int ms) { #ifdef _WIN32 Sleep(ms); #else usleep(ms * 1000); #endif } void handleCtrlC(int); int main() { srand(time(NULL)); int lines = -1, columns = -1; std::cout << "Lines?" << std::endl; std::cin >> lines; std::cout << "Columns?" << std::endl; std::cin >> columns; std::cout << "Generate random patterns? [y/N]" << std::endl; std::string yn; std::cin >> yn; automaton = new Automaton(lines, columns); if (yn == "y" || yn == "Y") automaton->init(true); else automaton->init(); std::string s; std::getline(std::cin, s); signal(SIGINT, handleCtrlC);while (true) { if (isRunning) { std::cout << "[RUNNING]" << std::endl; automaton->run(); automaton->display(); } wait(1000); } } void handleCtrlC(int) { isRunning = false; std::cout << "[PAUSED]" << std::endl; while (true) { automaton->display(); std::cout << "Press Enter to continue, S to save, L to load, C to change cell state, R to revert to previous generation, or Q to quit." << std::endl; std::string input; std::getline(std::cin, input); if (input.empty()) { std::cout << "[CONTINUE]" << std::endl; break; } else if (input == "S" || input == "s") { std::cout << "Enter file name: "; std::getline(std::cin, input); automaton->saveToFile(input); } else if (input == "L" || input == "l") { std::cout << "Enter file name: "; std::getline(std::cin, input); automaton->loadFromFile(input); } else if (input == "C" || input == "c") { std::cout << "Enter cell coordinates (x,y): "; std::getline(std::cin, input); int x = std::stoi(input.substr(0, input.find(','))); int y = std::stoi(input.substr(input.find(',') + 1)); automaton->changeCellState(x, y); } else if (input == "R" || input == "r") { automaton->revertToPreviousGeneration(); } else if (input == "Q" || input == "q") { std::cout << "[EXIT]" << std::endl; exit(0); } else { std::cout << "Invalid input." << std::endl; } } isRunning = true; signal(SIGINT, handleCtrlC); } 介绍这段代码思路
05-29

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值