花了几天的时间修改完善的问卷调查的程序 用C++写的

Code:
  1. #include<iostream>   
  2. using namespace std;   
  3. #include<fstream>   
  4. class questionaire   
  5. {   
  6.       char answer[24][6];// 每题的答案临时变量      
  7.       int finalanswer[24][6];//储存最后的答案,6位代表a-f,选了就为1    
  8.       int flag[24];//题目完成与否的标记    
  9.       public:   
  10.       void showthanks();//显示感谢词   
  11.       int checkall();//检查问卷完成的完整性,完成则返回0,否则返回题号    
  12.       int getanswer(int form,int number);//检查合法的答案输入,并返回可能的选择,L为1,N为2,否则为0   
  13.       int getanswerln(char ch[]);//判断是否是功能选项   
  14.       int legal1(int form,char ch[]);//判断选项是否合法    
  15.       void showinformation();   
  16.       void getfinalanswer(int i);//将获得的数据进行处理的部分,i题号    
  17.       void showresult();//显示测试结果    
  18.       void change(int form,char ch[]);//大写转换成小写    
  19.       void showtopic(int &i);//显示题目    
  20.       questionaire();   
  21.       void outputfinal();//输出最后结果    
  22. };   
  23. questionaire::questionaire()   
  24. {   
  25.    for(int i=0;i<24;i++)   
  26.    {    
  27.    flag[i]=0; //必须进行初始化,否则出错    
  28.    for(int j=0;j<6;j++)   
  29.    finalanswer[i][j]=0;   
  30.    }    
  31. }   
  32. void questionaire::showtopic(int &i)   
  33. {   
  34.      int form;   
  35.      switch(i)   
  36.           {   
  37.                    case 1:   
  38.                    {   
  39.                    form=3;   
  40.                    cout<<"您的性别:"<<endl;   
  41.                    cout<<"A.男  B.女"<<endl;   
  42.                    if(getanswer(form,i)==1);   
  43.                    else    
  44.                    i++;   
  45.                    }   
  46.                    break;   
  47.                    case 2:   
  48.                    {   
  49.                    form=1;   
  50.                    cout<<"您现在所处年级:"<<endl;   
  51.                    cout<<"A.大一 B.大二 C.大三 D.大四"<<endl;   
  52.                    if(getanswer(form,i)==1)   
  53.                    i=i-1;   
  54.                    else    
  55.                    i++;   
  56.                    }   
  57.                    break;   
  58.                    case 3:   
  59.                    {   
  60.                    form=1;   
  61.                    cout<<"请问您每周的课外阅读时间一般是多少?"<<endl;   
  62.                    cout<<"A.一小时以及一小时以下   B.一到两小时    C.两到三小时 D.三小时以上"<<endl;   
  63.                    if(getanswer(form,i)==1)   
  64.                    i=i-1;   
  65.                    else    
  66.                    i++;   
  67.                    }   
  68.                    break;    
  69.                     case 4:   
  70.                    {   
  71.                    form=2;   
  72.                    cout<<"请问您每天课外阅读时间是____"<<endl;   
  73.                    cout<<"A.固定的   B.有时间就看看   C.不确定"<<endl;   
  74.                    if(getanswer(form,i)==1)   
  75.                    i=i-1;   
  76.                    else    
  77.                    i++;   
  78.                    }   
  79.                    break;    
  80.                     case 5:   
  81.                    {   
  82.                    form=2;   
  83.                    cout<<"如果一本很好的课外阅读读物你会多久读完?"<<endl;   
  84.                    cout<<"A.一口气读完(能尽快读完) B.每天花段时间阅读(慢慢的品味)  C.经常反复的看(细细回味)"<<endl;   
  85.                    if(getanswer(form,i)==1)   
  86.                    i=i-1;   
  87.                    else    
  88.                    i++;   
  89.                    }   
  90.                    break;    
  91.                     case 6:   
  92.                    {   
  93.                    form=3;   
  94.                    cout<<"您觉得大学生应该每天花时间进行课外阅读吗?"<<endl;   
  95.                    cout<<"A.应该 B.不应该"<<endl;   
  96.                    if(getanswer(form,i)==1)   
  97.                    i=i-1;   
  98.                    else    
  99.                    i++;   
  100.                    }   
  101.                    break;    
  102.                     case 7:   
  103.                    {   
  104.                    form=1;   
  105.                    cout<<"请问您一年一般能阅读几本课外书籍?"<<endl;   
  106.                    cout<<"A.一本以及一本以下    B.一本到三本  C.三本到四本 D.四本以上"<<endl;   
  107.                    if(getanswer(form,i)==1)   
  108.                    i=i-1;   
  109.                    else    
  110.                    i++;   
  111.                    }   
  112.                    break;    
  113.                     case 8:   
  114.                    {   
  115.                    form=1;   
  116.                    cout<<"请问您一年课外阅读的书数量占您一年中阅读书籍数量的比例是:"<<endl;   
  117.                    cout<<"A.25%及以下   B.25%~50%   C.50~75%  D.75%及以上"<<endl;   
  118.                    if(getanswer(form,i)==1)   
  119.                    i=i-1;   
  120.                    else    
  121.                    i++;   
  122.                    }   
  123.                    break;    
  124.                     case 9:   
  125.                    {   
  126.                    form=3;   
  127.                    cout<<"您的阅读数量会受外界因素的影响吗?"<<endl;   
  128.                    cout<<"A.会 B.不会"<<endl;   
  129.                    if(getanswer(form,i)==1)   
  130.                    i=i-1;   
  131.                    else    
  132.                    i++;   
  133.                    }   
  134.                    break;    
  135.                     case 10:   
  136.                    {   
  137.                    form=3;   
  138.                    cout<<"您的阅读是否存在自己的计划?"<<endl;   
  139.                    cout<<"A.存在 B.不存在"<<endl;   
  140.                    if(getanswer(form,i)==1)   
  141.                    i=i-1;   
  142.                    else    
  143.                    i++;   
  144.                    }   
  145.                    break;    
  146.                     case 11:   
  147.                    {   
  148.                    form=4;   
  149.                    cout<<"请问您一般阅读的内容是什么?(多选)"<<endl;   
  150.                    cout<<"A.文学类 B.娱乐消遣类 C.应用技术类 D.专业书籍 E.科普读物 F.其他"<<endl;   
  151.                    if(getanswer(form,i)==1)   
  152.                    i=i-1;   
  153.                    else    
  154.                    i++;   
  155.                    }   
  156.                    break;    
  157.                     case 12:   
  158.                    {   
  159.                    form=2;   
  160.                    cout<<"您对您自己阅读的内容看法"<<endl;   
  161.                    cout<<"A.范围不广,仅限于自己喜欢的    B.范围广,但是不精  C.挺满意的"<<endl;   
  162.                    if(getanswer(form,i)==1)   
  163.                    i=i-1;   
  164.                    else    
  165.                    i++;   
  166.                    }   
  167.                    break;    
  168.                     case 13:   
  169.                    {   
  170.                    form=5;   
  171.                    cout<<"请问什么是影响您课外阅读内容的因素(多选)"<<endl;   
  172.                    cout<<"A.情感以及个人困惑   B.专业知识装备   C.休闲娱乐   D.个人兴趣   E.其他"<<endl;   
  173.                    if(getanswer(form,i)==1)   
  174.                    i=i-1;   
  175.                    else    
  176.                    i++;   
  177.                    }   
  178.                    break;    
  179.                     case 14:   
  180.                    {   
  181.                    form=1;   
  182.                    cout<<"你所读的课外书多来自:"<<endl;   
  183.                    cout<<"A. 图书馆借的 B. 书店买的 C.电子版的 D.其他"<<endl;   
  184.                    if(getanswer(form,i)==1)   
  185.                    i=i-1;   
  186.                    else    
  187.                    i++;   
  188.                    }   
  189.                    break;    
  190.                     case 15:   
  191.                    {   
  192.                    form=2;   
  193.                    cout<<"传统阅读与网络在线阅读,您更喜欢:"<<endl;   
  194.                    cout<<"A.传统阅读   B.网络在线阅读   C.不确定"<<endl;   
  195.                    if(getanswer(form,i)==1)   
  196.                    i=i-1;   
  197.                    else    
  198.                    i++;   
  199.                    }   
  200.                    break;    
  201.                     case 16:   
  202.                    {   
  203.                    form=4;   
  204.                    cout<<"您喜欢的书籍类型有:(多选)"<<endl;   
  205.                    cout<<"A.文学  B.生活  C.科学 D.娱乐 E.理财 F.其他"<<endl;   
  206.                    if(getanswer(form,i)==1)   
  207.                    i=i-1;   
  208.                    else    
  209.                    i++;   
  210.                    }   
  211.                    break;    
  212.                     case 17:   
  213.                    {   
  214.                    form=2;   
  215.                    cout<<"您平时喜欢阅读什么样的书籍:"<<endl;   
  216.                    cout<<"A.正版书籍(即求质量,又保证内容) B.便宜的盗版书籍(只求内容)C.无所谓(只要是自己要的书就行)"<<endl;   
  217.                    if(getanswer(form,i)==1)   
  218.                    i=i-1;   
  219.                    else    
  220.                    i++;   
  221.                    }   
  222.                    break;    
  223.                     case 18:   
  224.                    {   
  225.                    form=5;   
  226.                    cout<<"在业余生活中,你对哪一种形式的活动更感兴趣?(多选)"<<endl;   
  227.                    cout<<"A.上网 B.读课外书 C.看电视,听广播 D.参加体育活动 E.其他"<<endl;   
  228.                    if(getanswer(form,i)==1)   
  229.                    i=i-1;   
  230.                    else    
  231.                    i++;   
  232.                    }   
  233.                    break;    
  234.                     case 19:   
  235.                    {   
  236.                    form=1;   
  237.                    cout<<"请问您阅读的书一般来自于:"<<endl;   
  238.                    cout<<"A.朋友馈赠   B.自己购买   C.家长购买  D.从不买书,只是借阅"<<endl;   
  239.                    if(getanswer(form,i)==1)   
  240.                    i=i-1;   
  241.                    else    
  242.                    i++;   
  243.                    }   
  244.                    break;    
  245.                     case 20:   
  246.                    {   
  247.                    form=1;   
  248.                    cout<<"你平均每学期用于购买课外书的消费:"<<endl;   
  249.                    cout<<"A.20元以上 B.10-20元 C.5-10元 D.5元以下"<<endl;   
  250.                    if(getanswer(form,i)==1)   
  251.                    i=i-1;   
  252.                    else    
  253.                    i++;   
  254.                    }   
  255.                    break;    
  256.                     case 21:   
  257.                    {   
  258.                    form=3;   
  259.                    cout<<".假使考试之前书店有两本书很紧销,但受条件限制你只能买一本,这两本书分别是:第一本关注当今热门现象,你很感兴趣,第二本与考试内容有关指导你考一个好成绩.在这种情况下,你通常回选择:"<<endl;   
  260.                    cout<<"A.第一本 B. 第二本"<<endl;   
  261.                    if(getanswer(form,i)==1)   
  262.                    i=i-1;   
  263.                    else    
  264.                    i++;   
  265.                    }   
  266.                    break;    
  267.                     case 22:   
  268.                    {   
  269.                    form=4;   
  270.                    cout<<"在寒暑假你更愿意读什么(多选)"<<endl;   
  271.                    cout<<"A.娱乐类书籍 B.专业书籍 C.应用技术类(如计算机,英语) D.文学类 E.科普读物"<<endl;   
  272.                    if(getanswer(form,i)==1)   
  273.                    i=i-1;   
  274.                    else    
  275.                    i++;   
  276.                    }   
  277.                    break;    
  278.                     case 23:   
  279.                    {   
  280.                    form=3;   
  281.                    cout<<"你在考虑课阅读内容时,是从何种角度出发"<<endl;   
  282.                    cout<<"A.满足社会的需要 B.满足个人的需要"<<endl;   
  283.                    if(getanswer(form,i)==1)   
  284.                    i=i-1;   
  285.                    else    
  286.                    i++;   
  287.                    }   
  288.                    break;    
  289.                    case 24:   
  290.                    {   
  291.                    form=1;   
  292.                    cout<<"当前你读课外书的主要目的"<<endl;   
  293.                    cout<<"A.备考 B.扩大知识面 C.充实自己 D.兴趣"<<endl;   
  294.                    if(getanswer(form,i)==1)   
  295.                    i=i-1;   
  296.                    else  
  297.                    i++;   
  298.                    }   
  299.                    break;    
  300.                       
  301.           }       
  302. }   
  303. int questionaire::checkall()   
  304. {   
  305.     for(int i=0;i<24;i++)   
  306.     {   
  307.        if(!flag[i])   
  308.        return i+1;        
  309.     }   
  310.     return 0;   
  311. }   
  312. void questionaire::showthanks()   
  313. {   
  314.      cout<<"本次调查采用选择和填空的形式进行,请您抽出您宝贵的一点时间来配合我们完成此次调查,"  
  315.      "本次调查涉及到的个人信息均只用于此次调查,不会另作它用,请您根据您的实际情况,如实作答,谢谢您的合作。"<<endl<<   
  316.      "按确认键继续……";   
  317. }   
  318. void questionaire::change(int form,char ch[])   
  319. {   
  320.      int length=strlen(ch);   
  321.      switch(form)   
  322.      {   
  323.             case 1:   
  324.                  case 2:   
  325.                       case 3:   
  326.                            {   
  327.                             if(strcmp(ch,"A")>=0&&strcmp(ch,"D")<=0)   
  328.                             ch[0]=ch[0]+32;   
  329.                            }   
  330.                       break;   
  331.                            case 4:   
  332.                                 case 5:     
  333.                                     {   
  334.                                          for(int i=0;i<length;i++)   
  335.                                          if(strcmp(ch,"A")>=0&&strcmp(ch,"F")<=0)   
  336.                                          ch[i]=ch[i]+32;   
  337.                                     }    
  338.                                 break;     
  339.      }   
  340. }   
  341. int questionaire::getanswer(int form,int number)   
  342. {   
  343.      number=number-1;   
  344.      if(flag[number]==1)   
  345.      {   
  346.          cout<<"您之前的选择是:"<<answer[number]<<"如需更改请重新键入您的选择"<<endl;    
  347.      }   
  348.      cout<<"您的选择是:";   
  349.      char ch[6];   
  350.      cin>>ch;   
  351.      //cin>>answer[number];   
  352.      //change(form,answer[number]);   
  353.      if(!getanswerln(ch))   
  354.      {   
  355.          while(legal1(form,ch))   
  356.          {   
  357.            cout<<"输入有误,没有这个选项,请重新输入:";   
  358.            cin>>ch;   
  359.            if(getanswerln(ch))   
  360.            break;   
  361.            //cin>>answer[number];   
  362.            //int length=strlen(ch);               
  363.          }   
  364.          int returnnumber=getanswerln(ch);   
  365.          if(returnnumber!=0)   
  366.          return returnnumber;   
  367.          else  
  368.          {   
  369.              flag[number]=1;   
  370.              strcpy(answer[number],ch);   
  371.              change(form,answer[number]);   
  372.              cout<<"ok"<<endl;   
  373.              return 0;   
  374.          }   
  375.             
  376.      }   
  377.      else    
  378.      return getanswerln(ch);   
  379. }   
  380. int questionaire::getanswerln(char ch[])   
  381. {   
  382.      if(strlen(ch)>1)//多选了,直接否认选择前后    
  383.      return 0;   
  384.      if(!strcmp(ch,"l")|!strcmp(ch,"L"))//前一题的指令    
  385.      return 1;   
  386.      else if(!strcmp(ch,"n")|!strcmp(ch,"N"))//后一题的指令    
  387.      return 2;   
  388.      return 0;     
  389. }   
  390. int questionaire::legal1(int form,char ch[])   
  391. {   
  392.     int length=strlen(ch);   
  393.     switch(form)   
  394.     {   
  395.     //前面三种代表检查单选三种题目   后面是检查多选的    
  396.     case 1:   
  397.     {   
  398.     if(strcmp(ch,"A")<0||(strcmp(ch,"D")>0&&strcmp(ch,"a")<0)||strcmp(ch,"d")>0||length>1)   
  399.     return 1;   
  400.     else    
  401.     return 0;   
  402.     }   
  403.     break;   
  404.     case 2:   
  405.     {   
  406.     if(strcmp(ch,"A")<0||(strcmp(ch,"C")>0&&strcmp(ch,"a")<0)||strcmp(ch,"c")>0||length>1)   
  407.     return 1;   
  408.     else    
  409.     return 0;   
  410.     }   
  411.     break;   
  412.     case 3:   
  413.     {   
  414.     if(strcmp(ch,"A")<0||(strcmp(ch,"B")>0&&strcmp(ch,"a")<0)||strcmp(ch,"b")>0||length>1)   
  415.     return 1;   
  416.     else    
  417.     return 0;   
  418.     }   
  419.     break;   
  420.     case 4:   
  421.     {   
  422.        for(int i=0;i<length;i++)   
  423.        {   
  424.         if(strcmp(&ch[i],"A")<0||(strcmp(&ch[i],"F")>0&&strcmp(&ch[i],"a")<0)||strcmp(&ch[i],"f")>0)   
  425.         return 1;   
  426.        }     
  427.        return 0;   
  428.     }   
  429.     break;   
  430.     case 5:   
  431.     {   
  432.         for(int i=0;i<length;i++)   
  433.        {   
  434.         if(strcmp(&ch[i],"A")<0||(strcmp(&ch[i],"E")>0&&strcmp(&ch[i],"a")<0)||strcmp(&ch[i],"e")>0)   
  435.         return 1;   
  436.        }     
  437.        return 0;    
  438.     }   
  439.     break;        
  440.     }   
  441. }   
  442. void questionaire::showinformation()   
  443. {   
  444.      cout<<"输入l,返回上一题,输入n跳至下一题(大小写不限)"<<endl;   
  445. }   
  446. void questionaire::getfinalanswer(int i)   
  447. {   
  448.    int length=strlen(answer[i]);   
  449.    switch(length)   
  450.    {   
  451.       case 1:   
  452.            {   
  453.            int number;   
  454.            number=answer[i][0]%'a';   
  455.            //cout<<number<<endl;   
  456.            finalanswer[i][number]=1;   
  457.            }   
  458.            break;   
  459.            default:   
  460.                    {   
  461.                     for(int j=0;j<length;j++)   
  462.                     finalanswer[i][answer[i][j]%'a']=1;   
  463.                    }   
  464.            break;   
  465.    }   
  466. }    
  467. void questionaire::showresult()   
  468. {   
  469.      for(int i=0;i<24;i++)   
  470.      {   
  471.      cout<<i<<"      "<<answer[i]<<endl;   
  472.      }   
  473. }   
  474. void questionaire::outputfinal()   
  475. {   
  476.      char filename[]="调查问卷结果.dat";    
  477.    fstream outfile(filename,ios::out);   
  478.    if(!outfile)   
  479.    {   
  480.    cerr<<"open error"<<endl;   
  481.    }   
  482.    else  
  483.    {   
  484.        for(int topicnumber=0;topicnumber<24;topicnumber++)   
  485.        {   
  486.        for(int line=0;line<6;line++)   
  487.        outfile<<finalanswer[topicnumber][line]<<"  ";   
  488.        outfile<<endl;   
  489.        }   
  490.    }   
  491. }   
  492. main()   
  493. {   
  494.       questionaire test;   
  495.       test.showthanks();   
  496.       cin.get();   
  497.       for(int i=1;i<=24;)   
  498.       {   
  499.           system("cls");   
  500.           test.showinformation();   
  501.           cout<<i<<". ";   
  502.           test.showtopic(i);   
  503.       }   
  504.       int number;   
  505.        while(number=test.checkall())   
  506.       {   
  507.           system("cls");   
  508.           cout<<"您未完成所有题目,请继续完成:"<<endl;   
  509.           test.showtopic(number);                     
  510.       }   
  511.       system("cls");   
  512.       test.showresult();   
  513.       for(int i=0;i<24;i++)   
  514.       test.getfinalanswer(i);   
  515.       test.outputfinal();   
  516.       system("pause");   
  517. }   

几天的成果…与大家分享下…本人技艺不精…暂时还只能做DOS版的

希望有人点拨啊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值