鱼C论坛

 找回密码
 立即注册
查看: 2889|回复: 9

[已解决]c++小甲鱼老师课后作业 答案在哪啊

[复制链接]
发表于 2018-3-19 17:25:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如题
最佳答案
2018-4-1 17:13:00
视频06,函数重载,写了两种,1.是自己输入参数个数;2是程序计算输入参数个数,大于3个重新输入。
1.
  1. void main()
  2. {
  3.         void calc(int a);
  4.         void calc(int a,int b);
  5.         void calc(int a,int b,int c);
  6.         int a,b,c,number;
  7.         cout<<"请输入参数个数number=";
  8.         cin>>number;
  9.         switch(number)
  10.         {
  11.        
  12.         case 1:
  13.                 cout<<"请输入a的值:";
  14.                 cin>>a;
  15.                 calc(a);
  16.                 break;
  17.         case 2:
  18.                 cout<<"请输入a和b的值:";
  19.                 cin>>a>>b;
  20.                 calc(a,b);
  21.                 break;
  22.         case 3:
  23.                 cout<<"请输入a、b和c的值:";
  24.                 cin>>a>>b>>c;
  25.                 calc(a,b,c);
  26.                 break;
  27.         default:
  28.                 cout<<"您的输入有错误!!!!!";
  29.         }
  30.         system("pause");
  31. }
  32. void calc(int a)
  33. {
  34.         cout<<a<<"的平方是"<<a*a<<endl;
  35. }
  36. void calc(int a,int b)
  37. {
  38.         cout<<a<<"*"<<b<<"="<<a*b<<endl;
  39. }
  40. void calc(int a,int b,int c)
  41. {
  42.         cout<<a<<"+"<<b<<"+"<<c<<"="<<a+b+c<<endl;
  43. }
复制代码


2.
  1. void main()
  2. {
  3.         int count(int q[]);
  4.         void input(int r[]);
  5.         void calc(int a);
  6.         void calc(int a,int b);
  7.         void calc(int a,int b,int c);
  8.         int a[100]={0},number,i;
  9.         cout<<"请输入需要计算的参数:";
  10.         for(i=0;i<100;i++)
  11.         {
  12.                 cin>>a[i];
  13.                 if(cin.peek()=='\n')
  14.                         break;
  15.         }
  16.         number=i+1;
  17.         cout<<"number="<<number<<endl;
  18.         while(1)
  19.         {
  20.                 if(number==1)
  21.                 {
  22.                         calc(a[0]);
  23.                     break;
  24.                 }
  25.                 if(number==2)
  26.                 {
  27.                         calc(a[0],a[1]);
  28.                     break;
  29.                 }
  30.                 if(number==3)
  31.                 {
  32.                         calc(a[0],a[1],a[2]);
  33.                     break;
  34.                 }
  35.                 if((number<1)||(number>3))
  36.                 {
  37.                         cout<<"输入参数有误,请重新输入参数元素:";
  38.                         for(i=0;i<100;i++)
  39.                 {
  40.                                 cin>>a[i];
  41.                         if(cin.peek()=='\n')
  42.                             break;
  43.                 }
  44.                 number=i+1;
  45.                 cout<<"number="<<number<<endl;
  46.                 }
  47.         }
  48.         system("pause");
  49. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-3-20 16:20:33 | 显示全部楼层
自己瞎写的,我们互相交流啊
视频02
  1. void main()
  2. {
  3.         cout<<"请输入任意长度的整数和空格数:";
  4.         int sum,c;
  5.         sum=0;
  6.         while(1)
  7.         {
  8.                 cin>>c;
  9.                 sum=sum+c;
  10.                 if(getchar()=='\n')
  11.                 {
  12.                         break;       
  13.                 }
  14.                
  15.         }
  16.         cout<<"输入所有数据之和sum="<<sum<<endl;
  17.         system("pause");
  18. }
复制代码

应该是可以自动识别空格,弄了一下午,我刚学到这里
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-21 11:05:36 | 显示全部楼层
答案是回复可见的      必须回复那个帖子才可以看到
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-22 09:21:32 | 显示全部楼层
夏露咩咩咩 发表于 2018-3-20 16:20
自己瞎写的,我们互相交流啊
视频02

好啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-22 09:23:06 | 显示全部楼层
踏雪失踪 发表于 2018-3-21 11:05
答案是回复可见的      必须回复那个帖子才可以看到

那个代码我知道 那个课后作业在哪啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-22 09:23:37 | 显示全部楼层
夏露咩咩咩 发表于 2018-3-20 16:20
自己瞎写的,我们互相交流啊
视频02

我在那个指针那里
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-1 15:23:46 | 显示全部楼层
视频04,复制文件
  1. void main()
  2. {
  3.         ifstream in;
  4.         ofstream out;
  5.         string a;
  6.         char s;
  7.         in.open("D:\text1.txt");
  8.         out.open("D:\text2.txt");
  9.         while(!in)
  10.         {
  11.                 cout<<"源文件打开失败,请重新输入路径:";
  12.                 while(1)
  13.                 {
  14.                         cin>>a;
  15.                         if(cin.peek()=='\n')
  16.                         {
  17.                                 break;
  18.                         }
  19.                 }
  20.                 in.open(a);
  21.         }
  22.         while(!out)
  23.         {
  24.                 cout<<"目标文件失败,请重新输入路径:";
  25.                 while(1)
  26.                 {
  27.                         cin>>a;
  28.                         if(cin.peek()=='\n')
  29.                         {
  30.                                 break;
  31.                         }
  32.                 }
  33.                 out.open(a);
  34.         }
  35.         while(in>>s)
  36.         {
  37.                 out<<s;
  38.         }
  39.         out<<endl;
  40.         in.close();
  41.         out.close();
  42.         system("pause");
  43. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-1 17:13:00 | 显示全部楼层    本楼为最佳答案   
视频06,函数重载,写了两种,1.是自己输入参数个数;2是程序计算输入参数个数,大于3个重新输入。
1.
  1. void main()
  2. {
  3.         void calc(int a);
  4.         void calc(int a,int b);
  5.         void calc(int a,int b,int c);
  6.         int a,b,c,number;
  7.         cout<<"请输入参数个数number=";
  8.         cin>>number;
  9.         switch(number)
  10.         {
  11.        
  12.         case 1:
  13.                 cout<<"请输入a的值:";
  14.                 cin>>a;
  15.                 calc(a);
  16.                 break;
  17.         case 2:
  18.                 cout<<"请输入a和b的值:";
  19.                 cin>>a>>b;
  20.                 calc(a,b);
  21.                 break;
  22.         case 3:
  23.                 cout<<"请输入a、b和c的值:";
  24.                 cin>>a>>b>>c;
  25.                 calc(a,b,c);
  26.                 break;
  27.         default:
  28.                 cout<<"您的输入有错误!!!!!";
  29.         }
  30.         system("pause");
  31. }
  32. void calc(int a)
  33. {
  34.         cout<<a<<"的平方是"<<a*a<<endl;
  35. }
  36. void calc(int a,int b)
  37. {
  38.         cout<<a<<"*"<<b<<"="<<a*b<<endl;
  39. }
  40. void calc(int a,int b,int c)
  41. {
  42.         cout<<a<<"+"<<b<<"+"<<c<<"="<<a+b+c<<endl;
  43. }
复制代码


2.
  1. void main()
  2. {
  3.         int count(int q[]);
  4.         void input(int r[]);
  5.         void calc(int a);
  6.         void calc(int a,int b);
  7.         void calc(int a,int b,int c);
  8.         int a[100]={0},number,i;
  9.         cout<<"请输入需要计算的参数:";
  10.         for(i=0;i<100;i++)
  11.         {
  12.                 cin>>a[i];
  13.                 if(cin.peek()=='\n')
  14.                         break;
  15.         }
  16.         number=i+1;
  17.         cout<<"number="<<number<<endl;
  18.         while(1)
  19.         {
  20.                 if(number==1)
  21.                 {
  22.                         calc(a[0]);
  23.                     break;
  24.                 }
  25.                 if(number==2)
  26.                 {
  27.                         calc(a[0],a[1]);
  28.                     break;
  29.                 }
  30.                 if(number==3)
  31.                 {
  32.                         calc(a[0],a[1],a[2]);
  33.                     break;
  34.                 }
  35.                 if((number<1)||(number>3))
  36.                 {
  37.                         cout<<"输入参数有误,请重新输入参数元素:";
  38.                         for(i=0;i<100;i++)
  39.                 {
  40.                                 cin>>a[i];
  41.                         if(cin.peek()=='\n')
  42.                             break;
  43.                 }
  44.                 number=i+1;
  45.                 cout<<"number="<<number<<endl;
  46.                 }
  47.         }
  48.         system("pause");
  49. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-3 21:03:57 | 显示全部楼层
视频07
  1. void main()
  2. {  
  3.         //提取子字符串,标准库的string有一个substr函数用来截取子字符串。
  4.         //一般使用时传入两个参数,第一个是开始的坐标(第一个字符是0),第二个是截取的长度。
  5.         string str1("Nice to meet you!");
  6.         string newstr1(str1.substr(2,5));
  7.         cout<<newstr1<<endl;

  8.         //如果要比较的对象是两个string,则利用函数compare()。
  9.         //若要比较string s1和s2则写为:s1.compare(s2),若返回值为0,则两者相等。
  10.         string str2("Nice To meet you!");
  11.         cout<<str1.compare(str2)<<endl;

  12.         //string &insert(int p0,const string &s, int pos, int n);——在p0位置插入字符串s从pos开始的连续n个字符
  13.         cout<<str1.insert(5,str2,5,2)<<endl;

  14.         //find(string, int):第一个参数用来指示要查找的字符,第二个参数用来表示从字符串的何处开始查找子串(默认的查找位置是0)。
  15.         //如果找到,就返回首次匹配的开始位置;如果没有查找到匹配的内容,就返回string::npos。
  16.         string str3("Nice");
  17.         string str4("MEET");
  18.         string str5("123");
  19.         cout<<str1.find(str3)<<endl;
  20.         cout<<str1.find(str4)<<endl;
  21.         cout<<str1.find(str5)<<endl;
  22.         cout<<string::npos;//我发现npos在vs2010中是4294967295
  23.        
  24.         system("pause");
  25. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-15 14:35:41 | 显示全部楼层
视频11 结构
需要添加的头文件
  1. #include "iostream"
  2. using namespace std;
  3. #include <fstream>
  4. #include <string>
  5. #include <windows.h>
  6. #include <ctype.h>
复制代码

代码
  1. struct fishoil
  2.         {
  3.                 string name;
  4.                 string id;
  5.                 char sex;
  6.         };

  7. bool InitFishC();
  8. bool ReadFishC();
  9. void RecordFishC();
  10. bool WriteFishC(fishoil *p );

  11. void main()
  12. {
  13.         InitFishC();
  14.         int flag=0,bye=0;
  15.         while(1)
  16.         {
  17.                 if(bye==1)
  18.                 {
  19.                         break;
  20.                 }
  21.                 cout<<"请根据提示输入你需要的操作:"<<endl<<"打印文件内容到屏幕请按1:"<<endl
  22.                         <<"录入成员信息请按2:"<<endl<<"退出程序请按:3"<<endl;
  23.                 cin>>flag;
  24.                
  25.                 switch(flag)
  26.                 {
  27.                 case 1:
  28.                         {
  29.                                 if(ReadFishC())
  30.                                 {
  31.                                    cout<<"打印文件成功^ _ ^";
  32.                                 }
  33.                                 else
  34.                                 {
  35.                                         cout<<"打印文件失败";
  36.                                
  37.                                 }
  38.                                 break;
  39.                         }
  40.                 case 2:
  41.                         {
  42.                                 RecordFishC();
  43.                                 break;
  44.                         }
  45.                 case 3:
  46.                         {
  47.                                 cout<<"拜拜~~"<<endl;
  48.                                 bye=1;
  49.                                 break;
  50.                         }
  51.                 default:
  52.                         {
  53.                                 cout<<"请根据提示输入你需要的操作:"<<endl<<"打印文件内容到屏幕请按1:"
  54.                                 <<"录入成员信息请按2:"<<endl<<"退出程序请按3";
  55.                         cin>>flag;
  56.                                 break;
  57.                         }
  58.                 }
  59.         }
  60.         system("pause");
  61. }
  62. bool InitFishC()
  63. {
  64.         //string name,id;
  65.         //char sex;
  66.         fishoil information;
  67.         information.name="xiaojiayu";
  68.         information.id="Fishoil00001";
  69.         information.sex='M';
  70.         ofstream out;
  71.         out.open("D:\\text.txt");
  72.         while(!out)
  73.         {
  74.                 cout<<"打开文件失败,请重新输入文件路径:";
  75.             string str;
  76.                 while(1)
  77.                 {
  78.                        
  79.                         cin>>str;
  80.                         if(cin.peek()=='\n')
  81.                         {break;}
  82.                 }
  83.                 out.open(str);
  84.                 cout<<endl;
  85.         }
  86.                 out<<information.name<<"     ";
  87.                 out<<information.id<<"    ";
  88.                 out<<information.sex;
  89.                 out.close();
  90.                 return 0;
  91. }
  92. bool ReadFishC()
  93. {
  94.         cout<<"请等待,正在全力读取数据············";
  95.         for(int count=0;count<101;count++)
  96.         {
  97.                 if(count<10)
  98.                 {
  99.                         cout<<count<<"%"<<"\b\b";
  100.                 }
  101.                 if(count>=10)
  102.                 {
  103.                         cout<<count<<"%"<<"\b\b\b";
  104.                 }
  105.                 Sleep(100);
  106.         }
  107.         cout<<endl;
  108.         char x;
  109.         ifstream in;
  110.         in.open("D:\\text.txt");
  111.         while(!in)
  112.         {
  113.                 cout<<"打开文件失败,请重新输入文件路径:";
  114.             string str;
  115.                 while(1)
  116.                 {
  117.                        
  118.                         cin>>str;
  119.                         if(cin.peek()=='\n')
  120.                         {break;}
  121.                 }
  122.                 in.open(str);
  123.                 cout<<endl;
  124.         }
  125.         cout<<"姓名"<<"          "<<"身份证"<<"          "<<"性别"<<endl;
  126.         in>>noskipws;
  127.         while(in>>x)
  128.         {
  129.           cout<<x;
  130.         }
  131.         cout<<endl<<endl;
  132.         in.close();
  133.         return in.eof();
  134. }
  135. void RecordFishC()
  136. {
  137.         fishoil *pinformation;
  138.         fishoil information;
  139.         information.name;
  140.         information.id;
  141.         information.sex;
  142.         pinformation=&information;
  143.         char J;
  144.         J='C';
  145.         while(1)
  146.         {
  147.                 if((J=='N')||(J=='n'))
  148.                 {
  149.                         break;
  150.                 }
  151.                 cout<<"请输入会员姓名,中英文均可:";
  152.             cin>>information.name;
  153.                 cout<<endl;
  154.                 cout<<"请输入会员id,格式为【Fishoilxxxxx】:";
  155.                 cin>>information.id;
  156.                 while((information.id.length()!=12))
  157.                 {
  158.                         cout<<"您输入的id长度有问题,一共有12个字符哦~"<<endl;
  159.                         cout<<"请输入会员id,格式为【Fishoilxxxxx】:";
  160.                         cin>>information.id;
  161.                 }
  162.                 while(((information.id.at(0)!='F')&&(information.id.at(0)!='f'))||((information.id.at(1)!='i')&&(information.id.at(1)!='I'))
  163.                         ||((information.id.at(2)!='s')&&(information.id.at(2)!='S'))||((information.id.at(3)!='h')&&(information.id.at(3)!='H'))
  164.                         ||((information.id.at(4)!='o')&&(information.id.at(4)!='O'))||((information.id.at(5)!='i')&&(information.id.at(5)!='I'))
  165.                         ||((information.id.at(6)!='l')&&(information.id.at(6)!='L'))||(!isdigit(information.id.at(7)))||(!isdigit(information.id.at(8)))
  166.                         ||(!isdigit(information.id.at(9)))||(!isdigit(information.id.at(10)))||(!isdigit(information.id.at(11))))
  167.                 {
  168.                         cout<<"您的格式有错误,请输入会员id,格式为【Fishoilxxxxx】:";
  169.                         cin>>information.id;
  170.                 }//判断是否满足Fishoilxxxxx模式,不满足,重新输入,information.id.at提取某一位,isdigit(),检查是否为数字,是返回1
  171.                 cout<<endl;
  172.                 cout<<"请输入会员性别,F代表Female,M代表Mame:";
  173.                 cin>>information.sex;
  174.                 while(((information.sex)!='F')&&((information.sex)!='f')&&((information.sex)!='M')&&((information.sex)!='m'))
  175.                 {
  176.                         cout<<"您的格式有错误,请输入会员性别,F代表Female,M代表Mame:";
  177.                     cin>>information.sex;
  178.                 }
  179.                 while(!WriteFishC(pinformation))
  180.                 {
  181.                        
  182.                         cout<<"录入信息成功^—^"<<endl;
  183.                         cout<<"请问需要再次录入吗?Y/N?";
  184.                         cin>>J;
  185.                         while((J!='Y')&&(J!='y')&&(J!='N')&&(J!='n'))
  186.                         {
  187.                                 cout<<"您输入的格式有错误,请输入Y或N:";
  188.                                 cin>>J;
  189.                         }
  190.                         break;
  191.                 }
  192.         }
  193.         cout<<endl;
  194. }
  195. bool WriteFishC(fishoil *p )
  196. {
  197.         ofstream out;
  198.         out.open("D:\\text.txt",ios::app);
  199.         while(!out)
  200.         {
  201.                 cout<<"打开文件失败,请重新输入文件路径:";
  202.             string str;
  203.                 while(1)
  204.                 {
  205.                        
  206.                         cin>>str;
  207.                         if(cin.peek()=='\n')
  208.                         {break;}
  209.                 }
  210.                 out.open(str,ios::app);
  211.                 cout<<endl;
  212.         }
  213.         out<<endl;
  214.         out<<(*p).name<<"        ";
  215.         if(((*p).id.at(0)=='f'))
  216.         {
  217.                 (*p).id.at(0)='F';
  218.         }
  219.         if(((*p).id.at(1)=='I'))
  220.         {
  221.                 (*p).id.at(1)='i';
  222.         }
  223.         if(((*p).id.at(2)=='S'))
  224.         {
  225.                 (*p).id.at(2)='s';
  226.         }
  227.         if(((*p).id.at(3)=='H'))
  228.         {
  229.                 (*p).id.at(3)='h';
  230.         }
  231.         if(((*p).id.at(4)=='O'))
  232.         {
  233.                 (*p).id.at(4)='o';
  234.         }
  235.         if(((*p).id.at(5)=='I'))
  236.         {
  237.                 (*p).id.at(5)='i';
  238.         }
  239.         if(((*p).id.at(6)=='L'))
  240.         {
  241.                 (*p).id.at(6)='l';
  242.         }
  243.         out<<(*p).id<<"        ";
  244.         if(((*p).sex=='f'))
  245.         {
  246.                 (*p).sex='F';
  247.        
  248.         }
  249.         if(((*p).sex=='m'))
  250.         {
  251.                 (*p).sex='M';
  252.         }
  253.         out<<(*p).sex;
  254.         out.close();
  255.         return 0;
  256. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 07:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表