c++小甲鱼老师课后作业 答案在哪啊
如题 自己瞎写的,我们互相交流啊{:10_254:}视频02
void main()
{
cout<<"请输入任意长度的整数和空格数:";
int sum,c;
sum=0;
while(1)
{
cin>>c;
sum=sum+c;
if(getchar()=='\n')
{
break;
}
}
cout<<"输入所有数据之和sum="<<sum<<endl;
system("pause");
}
应该是可以自动识别空格,弄了一下午{:10_247:},我刚学到这里{:10_266:} 答案是回复可见的 必须回复那个帖子才可以看到 夏露咩咩咩 发表于 2018-3-20 16:20
自己瞎写的,我们互相交流啊
视频02
好啊
踏雪失踪 发表于 2018-3-21 11:05
答案是回复可见的 必须回复那个帖子才可以看到
那个代码我知道 那个课后作业在哪啊
夏露咩咩咩 发表于 2018-3-20 16:20
自己瞎写的,我们互相交流啊
视频02
我在那个指针那里
视频04,复制文件
void main()
{
ifstream in;
ofstream out;
string a;
char s;
in.open("D:\text1.txt");
out.open("D:\text2.txt");
while(!in)
{
cout<<"源文件打开失败,请重新输入路径:";
while(1)
{
cin>>a;
if(cin.peek()=='\n')
{
break;
}
}
in.open(a);
}
while(!out)
{
cout<<"目标文件失败,请重新输入路径:";
while(1)
{
cin>>a;
if(cin.peek()=='\n')
{
break;
}
}
out.open(a);
}
while(in>>s)
{
out<<s;
}
out<<endl;
in.close();
out.close();
system("pause");
} 视频06,函数重载,写了两种,1.是自己输入参数个数;2是程序计算输入参数个数,大于3个重新输入。
1.
void main()
{
void calc(int a);
void calc(int a,int b);
void calc(int a,int b,int c);
int a,b,c,number;
cout<<"请输入参数个数number=";
cin>>number;
switch(number)
{
case 1:
cout<<"请输入a的值:";
cin>>a;
calc(a);
break;
case 2:
cout<<"请输入a和b的值:";
cin>>a>>b;
calc(a,b);
break;
case 3:
cout<<"请输入a、b和c的值:";
cin>>a>>b>>c;
calc(a,b,c);
break;
default:
cout<<"您的输入有错误!!!!!";
}
system("pause");
}
void calc(int a)
{
cout<<a<<"的平方是"<<a*a<<endl;
}
void calc(int a,int b)
{
cout<<a<<"*"<<b<<"="<<a*b<<endl;
}
void calc(int a,int b,int c)
{
cout<<a<<"+"<<b<<"+"<<c<<"="<<a+b+c<<endl;
}
2.
void main()
{
int count(int q[]);
void input(int r[]);
void calc(int a);
void calc(int a,int b);
void calc(int a,int b,int c);
int a={0},number,i;
cout<<"请输入需要计算的参数:";
for(i=0;i<100;i++)
{
cin>>a;
if(cin.peek()=='\n')
break;
}
number=i+1;
cout<<"number="<<number<<endl;
while(1)
{
if(number==1)
{
calc(a);
break;
}
if(number==2)
{
calc(a,a);
break;
}
if(number==3)
{
calc(a,a,a);
break;
}
if((number<1)||(number>3))
{
cout<<"输入参数有误,请重新输入参数元素:";
for(i=0;i<100;i++)
{
cin>>a;
if(cin.peek()=='\n')
break;
}
number=i+1;
cout<<"number="<<number<<endl;
}
}
system("pause");
} 视频07
void main()
{
//提取子字符串,标准库的string有一个substr函数用来截取子字符串。
//一般使用时传入两个参数,第一个是开始的坐标(第一个字符是0),第二个是截取的长度。
string str1("Nice to meet you!");
string newstr1(str1.substr(2,5));
cout<<newstr1<<endl;
//如果要比较的对象是两个string,则利用函数compare()。
//若要比较string s1和s2则写为:s1.compare(s2),若返回值为0,则两者相等。
string str2("Nice To meet you!");
cout<<str1.compare(str2)<<endl;
//string &insert(int p0,const string &s, int pos, int n);——在p0位置插入字符串s从pos开始的连续n个字符
cout<<str1.insert(5,str2,5,2)<<endl;
//find(string, int):第一个参数用来指示要查找的字符,第二个参数用来表示从字符串的何处开始查找子串(默认的查找位置是0)。
//如果找到,就返回首次匹配的开始位置;如果没有查找到匹配的内容,就返回string::npos。
string str3("Nice");
string str4("MEET");
string str5("123");
cout<<str1.find(str3)<<endl;
cout<<str1.find(str4)<<endl;
cout<<str1.find(str5)<<endl;
cout<<string::npos;//我发现npos在vs2010中是4294967295
system("pause");
} 视频11 结构
需要添加的头文件
#include "iostream"
using namespace std;
#include <fstream>
#include <string>
#include <windows.h>
#include <ctype.h>
代码
struct fishoil
{
string name;
string id;
char sex;
};
bool InitFishC();
bool ReadFishC();
void RecordFishC();
bool WriteFishC(fishoil *p );
void main()
{
InitFishC();
int flag=0,bye=0;
while(1)
{
if(bye==1)
{
break;
}
cout<<"请根据提示输入你需要的操作:"<<endl<<"打印文件内容到屏幕请按1:"<<endl
<<"录入成员信息请按2:"<<endl<<"退出程序请按:3"<<endl;
cin>>flag;
switch(flag)
{
case 1:
{
if(ReadFishC())
{
cout<<"打印文件成功^ _ ^";
}
else
{
cout<<"打印文件失败";
}
break;
}
case 2:
{
RecordFishC();
break;
}
case 3:
{
cout<<"拜拜~~"<<endl;
bye=1;
break;
}
default:
{
cout<<"请根据提示输入你需要的操作:"<<endl<<"打印文件内容到屏幕请按1:"
<<"录入成员信息请按2:"<<endl<<"退出程序请按3";
cin>>flag;
break;
}
}
}
system("pause");
}
bool InitFishC()
{
//string name,id;
//char sex;
fishoil information;
information.name="xiaojiayu";
information.id="Fishoil00001";
information.sex='M';
ofstream out;
out.open("D:\\text.txt");
while(!out)
{
cout<<"打开文件失败,请重新输入文件路径:";
string str;
while(1)
{
cin>>str;
if(cin.peek()=='\n')
{break;}
}
out.open(str);
cout<<endl;
}
out<<information.name<<" ";
out<<information.id<<" ";
out<<information.sex;
out.close();
return 0;
}
bool ReadFishC()
{
cout<<"请等待,正在全力读取数据············";
for(int count=0;count<101;count++)
{
if(count<10)
{
cout<<count<<"%"<<"\b\b";
}
if(count>=10)
{
cout<<count<<"%"<<"\b\b\b";
}
Sleep(100);
}
cout<<endl;
char x;
ifstream in;
in.open("D:\\text.txt");
while(!in)
{
cout<<"打开文件失败,请重新输入文件路径:";
string str;
while(1)
{
cin>>str;
if(cin.peek()=='\n')
{break;}
}
in.open(str);
cout<<endl;
}
cout<<"姓名"<<" "<<"身份证"<<" "<<"性别"<<endl;
in>>noskipws;
while(in>>x)
{
cout<<x;
}
cout<<endl<<endl;
in.close();
return in.eof();
}
void RecordFishC()
{
fishoil *pinformation;
fishoil information;
information.name;
information.id;
information.sex;
pinformation=&information;
char J;
J='C';
while(1)
{
if((J=='N')||(J=='n'))
{
break;
}
cout<<"请输入会员姓名,中英文均可:";
cin>>information.name;
cout<<endl;
cout<<"请输入会员id,格式为【Fishoilxxxxx】:";
cin>>information.id;
while((information.id.length()!=12))
{
cout<<"您输入的id长度有问题,一共有12个字符哦~"<<endl;
cout<<"请输入会员id,格式为【Fishoilxxxxx】:";
cin>>information.id;
}
while(((information.id.at(0)!='F')&&(information.id.at(0)!='f'))||((information.id.at(1)!='i')&&(information.id.at(1)!='I'))
||((information.id.at(2)!='s')&&(information.id.at(2)!='S'))||((information.id.at(3)!='h')&&(information.id.at(3)!='H'))
||((information.id.at(4)!='o')&&(information.id.at(4)!='O'))||((information.id.at(5)!='i')&&(information.id.at(5)!='I'))
||((information.id.at(6)!='l')&&(information.id.at(6)!='L'))||(!isdigit(information.id.at(7)))||(!isdigit(information.id.at(8)))
||(!isdigit(information.id.at(9)))||(!isdigit(information.id.at(10)))||(!isdigit(information.id.at(11))))
{
cout<<"您的格式有错误,请输入会员id,格式为【Fishoilxxxxx】:";
cin>>information.id;
}//判断是否满足Fishoilxxxxx模式,不满足,重新输入,information.id.at提取某一位,isdigit(),检查是否为数字,是返回1
cout<<endl;
cout<<"请输入会员性别,F代表Female,M代表Mame:";
cin>>information.sex;
while(((information.sex)!='F')&&((information.sex)!='f')&&((information.sex)!='M')&&((information.sex)!='m'))
{
cout<<"您的格式有错误,请输入会员性别,F代表Female,M代表Mame:";
cin>>information.sex;
}
while(!WriteFishC(pinformation))
{
cout<<"录入信息成功^—^"<<endl;
cout<<"请问需要再次录入吗?Y/N?";
cin>>J;
while((J!='Y')&&(J!='y')&&(J!='N')&&(J!='n'))
{
cout<<"您输入的格式有错误,请输入Y或N:";
cin>>J;
}
break;
}
}
cout<<endl;
}
bool WriteFishC(fishoil *p )
{
ofstream out;
out.open("D:\\text.txt",ios::app);
while(!out)
{
cout<<"打开文件失败,请重新输入文件路径:";
string str;
while(1)
{
cin>>str;
if(cin.peek()=='\n')
{break;}
}
out.open(str,ios::app);
cout<<endl;
}
out<<endl;
out<<(*p).name<<" ";
if(((*p).id.at(0)=='f'))
{
(*p).id.at(0)='F';
}
if(((*p).id.at(1)=='I'))
{
(*p).id.at(1)='i';
}
if(((*p).id.at(2)=='S'))
{
(*p).id.at(2)='s';
}
if(((*p).id.at(3)=='H'))
{
(*p).id.at(3)='h';
}
if(((*p).id.at(4)=='O'))
{
(*p).id.at(4)='o';
}
if(((*p).id.at(5)=='I'))
{
(*p).id.at(5)='i';
}
if(((*p).id.at(6)=='L'))
{
(*p).id.at(6)='l';
}
out<<(*p).id<<" ";
if(((*p).sex=='f'))
{
(*p).sex='F';
}
if(((*p).sex=='m'))
{
(*p).sex='M';
}
out<<(*p).sex;
out.close();
return 0;
}
页:
[1]