#include<limits>
if( cin.rdstate() ) // 通过cin的状态来判断是否出现异常 异常值为1.一次只能判断一个字符
{
cin.clear(); //清除cin流的错误标志,
cin.ignore(
numeric_limits<streamsize>::max(),'\n'); 第一个参数是限制,第二个参数是遇到将\n 之前的都
}
-----------------------------------------
#include<iostream>
#include<string>
#include<cmath>
#include<limits>
using namespace std;
int main()
{
int s;
double a,b;char s1;
while(1)
{
s=0;
cin>>a;
if(cin.rdstate())
s++;
cin>>s1;
if(cin.rdstate())
s++;
cin>>b;
if(cin.rdstate())
s++;
if(s>=1)
{
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(),'\n');
cout<<"输入出错,重新输入";
}
if(s==0)
{
switch(s1)
{
case'+':cout<<a+b<<endl;break;
case'-':cout<<a-b<<endl;break;
case'*':cout<<a*b<<endl;break;
case'/':cout<<a/b<<endl;break;
case'^':cout<<pow(a,b);
}
}
}
return 0;
}
========================
字符串 操作
-------------------------------------------------------------------
关于 scanf 输入
#include <iostream>
#include <string>
#include <stdlib.h>
#include <windows.h>
using namespace std;
int main()
{
char str[80];
char str1[80];
scanf("%s",str); // 此处输入:language programmiing//scanf 输入时有
空格 就断开了,键盘缓冲区还有,所以下一次scanf
不用输入直接扫描就可以了
printf("%s",str);
Sleep(5000); // 这里等待5秒,告诉你程序运行到什么地方
scanf("%s",str1); // 这句无需你再输入,是对键盘盘缓冲区再扫描
printf("\n%s",str1);
return 0;
}
-----------------------------------
sprintf 处理字符串
int sprintf( char *buffer, const char *format [, argument] ... );
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<windows.h>
#include<string>
using namespace std;
int main()
{
char buffer[128];
char time[32];
scanf("%s",time); //输入时间
sprintf(buffer,"at %s shutdown -s -t 2",time);
// buffer 中存的是 at 10:30 shutdown -s-t 2
system(buffer); //相当于 system("at 10:30 shutdown -s -t 1"); // 分号里面的是 字符数组
return 0;
}
/*int sprintf( char *buffer, const char *format [, argument] ... );
连接字符串
sprintf 的格式控制串中既然可以插入各种东西,并最终把它们“连成一串”,自然也就能够连
接字符串,从而在许多场合可以替代strcat,但sprintf 能够一次连接多个字符串(自然也可以同时
在它们中间插入别的内容,总之非常灵活)。比如:
char* who = "I";
char* whom = "优快云";
sprintf(s, "%s love %s.", who, whom); //产生:"I love 优快云. "存放在s 中
*/
-----------------------------------
#include<iostream>
using namespace std;
int main()
{
char s[12] ="a book!";
printf("%d\n",strlen(s)); //strlen 答案是7 ,求字符数组的长度
printf("%d\n",sizeof(s)); //sizeof(s) 答案是12 ,求字符数组开辟 的空间大小
return 0;
}
-------------------------------------
void main()
{
char aa[10];
printf("%d",strlen(aa)); // 答案是随机的,因为没有初始化。 如果char aa[10] 定义在
主函数外,答案为0
}
=========================================
在com窗口中 输入 “shutdown/?” ,出现系统命令
如
shutdown /s 是关闭计算机
shutdown /s /t 100 在100秒后关闭计算机
shutdown /a 是取消关机
=========================================
文件操作
===========
读写
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp; //文件类型的指针
if( (fp=fopen("E:\\a.txt","r") )==NULL) / /文件不可以打开
{
printf("Cannot open file.\n");
exit(1);
}
while(!feof(fp)) // 如果不是文件尾
{
char ch = getc(fp); // 从文件读一个字符
printf("%c",ch); // 打印字符
}
fclose(fp); // 关闭文件
}
if( cin.rdstate() ) // 通过cin的状态来判断是否出现异常 异常值为1.一次只能判断一个字符
{
cin.clear(); //清除cin流的错误标志,
cin.ignore(
numeric_limits<streamsize>::max(),'\n'); 第一个参数是限制,第二个参数是遇到将\n 之前的都
}
-----------------------------------------
#include<iostream>
#include<string>
#include<cmath>
#include<limits>
using namespace std;
int main()
{
int s;
double a,b;char s1;
while(1)
{
s=0;
cin>>a;
if(cin.rdstate())
s++;
cin>>s1;
if(cin.rdstate())
s++;
cin>>b;
if(cin.rdstate())
s++;
if(s>=1)
{
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(),'\n');
cout<<"输入出错,重新输入";
}
if(s==0)
{
switch(s1)
{
case'+':cout<<a+b<<endl;break;
case'-':cout<<a-b<<endl;break;
case'*':cout<<a*b<<endl;break;
case'/':cout<<a/b<<endl;break;
case'^':cout<<pow(a,b);
}
}
}
return 0;
}
========================
字符串 操作
-------------------------------------------------------------------
关于 scanf 输入
#include <iostream>
#include <string>
#include <stdlib.h>
#include <windows.h>
using namespace std;
int main()
{
char str[80];
char str1[80];
scanf("%s",str); // 此处输入:language programmiing//scanf 输入时有
空格 就断开了,键盘缓冲区还有,所以下一次scanf
不用输入直接扫描就可以了
printf("%s",str);
Sleep(5000); // 这里等待5秒,告诉你程序运行到什么地方
scanf("%s",str1); // 这句无需你再输入,是对键盘盘缓冲区再扫描
printf("\n%s",str1);
return 0;
}
-----------------------------------
sprintf 处理字符串
int sprintf( char *buffer, const char *format [, argument] ... );
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<windows.h>
#include<string>
using namespace std;
int main()
{
char buffer[128];
char time[32];
scanf("%s",time); //输入时间
sprintf(buffer,"at %s shutdown -s -t 2",time);
// buffer 中存的是 at 10:30 shutdown -s-t 2
system(buffer); //相当于 system("at 10:30 shutdown -s -t 1"); // 分号里面的是 字符数组
return 0;
}
/*int sprintf( char *buffer, const char *format [, argument] ... );
连接字符串
sprintf 的格式控制串中既然可以插入各种东西,并最终把它们“连成一串”,自然也就能够连
接字符串,从而在许多场合可以替代strcat,但sprintf 能够一次连接多个字符串(自然也可以同时
在它们中间插入别的内容,总之非常灵活)。比如:
char* who = "I";
char* whom = "优快云";
sprintf(s, "%s love %s.", who, whom); //产生:"I love 优快云. "存放在s 中
*/
-----------------------------------
#include<iostream>
using namespace std;
int main()
{
char s[12] ="a book!";
printf("%d\n",strlen(s)); //strlen 答案是7 ,求字符数组的长度
printf("%d\n",sizeof(s)); //sizeof(s) 答案是12 ,求字符数组开辟 的空间大小
return 0;
}
-------------------------------------
void main()
{
char aa[10];
printf("%d",strlen(aa)); // 答案是随机的,因为没有初始化。 如果char aa[10] 定义在
主函数外,答案为0
}
=========================================
在com窗口中 输入 “shutdown/?” ,出现系统命令
如
shutdown /s 是关闭计算机
shutdown /s /t 100 在100秒后关闭计算机
shutdown /a 是取消关机
=========================================
文件操作
===========
读写
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp; //文件类型的指针
if( (fp=fopen("E:\\a.txt","r") )==NULL) / /文件不可以打开
{
printf("Cannot open file.\n");
exit(1);
}
while(!feof(fp)) // 如果不是文件尾
{
char ch = getc(fp); // 从文件读一个字符
printf("%c",ch); // 打印字符
}
fclose(fp); // 关闭文件
}
9071

被折叠的 条评论
为什么被折叠?



