| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 699 人关注过本帖
标题:C++中投票问题遇到的困难,大侠们帮忙啊
只看楼主 加入收藏
hong4781798
Rank: 1
来 自:中国河北
等 级:新手上路
帖 子:28
专家分:0
注 册:2010-4-17
结帖率:90%
收藏
已结贴  问题点数:20 回复次数:4 
C++中投票问题遇到的困难,大侠们帮忙啊
题目要求:投票管理需求
预先指定若干名侯选人(最多12人)
预先指定当选标准(给定百分数)
提供投票功能(不可以选非侯选人)
支持选举结果的输出(当选人和票数)
下面是我写的程序:
demo.h
struct Candidate
{
  char name;//候选人名字
  int amount;//所得的票数
};
class Demo
{
public:   
    Candidate can[3];
public:
    void demo();
    int Iseffvote(char s,Candidate *x);
    int Result();
};
demo.cpp
#include<iostream.h>
#include <stdio.h>
#include "demo.h"
void demo()
{
  Candidate can[3]={{'a',0},{'b',0},{'c',0}};
}
int Iseffvote(char s,Candidate *x)//*判断输入的候选人是否在指定的当中,是的话相应的票数增加*//
{     Demo d=new demo();   
    *x=d.can[0];
    int flag=0;   
      for(int i=0;i<3;i++)
    {
      if(s==d.can[i].name)      {d.can[i].amount++;flag=1;}
    }   
   if(flag==0)
   cout<< "Input wrong!Please vote one of the candidate again!" <<endl;
   return 0;
   
}
 int Result()//*得出结果   如何将上面已经进行加操作的各个的票数带进来,进行下一步的操作???
 { Demo d;
  int flag=0;
  int n;double m[3];
  for(int i=0;i<3;i++)
      n+=d.can[i].amount;  
  for(int j=0;j<3;j++)
      m[j]=d.can[j].amount/n;
  for(int r=0;r<3;r++)
  {
      if(m[r]>0.5)  
      {
          cout <<"The winner is" <<d.can[r].name<<"and the amount of vote is " <<d.can[r].amount <<endl;
          flag=1;
      }
  }
  if(flag==0)  cout << "The result is none,because the percent of every candidate is below 50%."<<endl;
  return 0;
}
 void main()
 {
   Demo t=new demo();
  cout << "The candidates are " << t.can[0].name <<endl<<t.can[1].name<<endl <<t.can[2].name <<endl;
   cout <<"Please choose one of them:"<<endl;
   char c;   
   cin >> c;
   while(c=='a'||c=='b'||c=='c')  {n=t.Iseffvote(c,t.can);}
   t.Result();
 }
 但是运行后出现的错误信息提示是:
F:\OOP\demo.cpp(9) : error C2061: syntax error : identifier 'demo'
F:\OOP\demo.cpp(12) : error C2143: syntax error : missing ')' before ';'
F:\OOP\demo.cpp(12) : error C2143: syntax error : missing ';' before ')'
F:\OOP\demo.cpp(12) : error C2143: syntax error : missing ';' before ')'
F:\OOP\demo.cpp(12) : warning C4552: '<' : operator has no effect; expected operator with side-effect
F:\OOP\demo.cpp(12) : error C2143: syntax error : missing ';' before ')'
F:\OOP\demo.cpp(13) : error C2143: syntax error : missing ';' before '{'
执行 cl.exe 时出错.

demo.obj - 1 error(s), 0 warning(s)
希望大侠们帮帮忙啊
搜索更多相关主题的帖子: 困难 投票 
2010-09-19 18:07
minghan0313
Rank: 2
等 级:论坛游民
帖 子:15
专家分:31
注 册:2007-5-21
收藏
得分:10 
大致看了一下  我也是初学者   你可以忽略我所说的  因为我也不确定我自己说的是不是完全正确

1 你的demo函数是想作为构造函数 还是只想作为一个初始Candidate数组的函数?
   
   如果作为构造函数,那格式肯定是错的
   
    如果作为一个初始化Candidate数组的函数 我感觉没什么必要来写这个函数吧.....

2 struct类型的数组貌似不能那样初始化(目前没有找到确凿证据....)   
     不过肯定的一点的是
     can[0].amount=0;
    can[1].amount=0;
    can[2].amount=0;
    can[0].name='a';
    can[1].name='b';
    can[2].name='b';
    这样写是完全没有错误的....

3  main函数中     
    while(c=='a'||c=='b'||c=='c')  {n=t.Iseffvote(c,t.can);}
    这里的n没有初始化   你只在result函数中初始化n了   而这句执行的时候你还没有调用result函数   再退一步说   就算你调用了result函数  你在下面也不一定能使用n....这涉及到变量的生命  周期问题  我也得好好看书才能回答你....
2010-09-20 15:39
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:10 
你写的太乱了!我自己帮你写了一下!希望可以!呵呵!
  #include <iostream>
using namespace std;
class  Candidate
{
public:
  char name;//候选人名字
  int amount;//所得的票数
    Candidate(char n='0',int am=0):name(n),amount(am)
    {}
};

void  Iseffvote(char s,Candidate a[])
{
    int flag=0;   
    for(int i=0;i<3;i++)
    {
        if(s==a[i].name)
        {
            a[i].amount++;
            flag=1;
        }
    }   
    if(flag==0)
        cout<< "Input wrong!Please vote one of the candidate again!" <<endl;


}
void  Result(Candidate a[])
{
   
  int flag=0;
  double s=0;
  double m[3];
  for(int i=0;i<3;i++)
      s+=a[i].amount;  
  for(int j=0;j<3;j++)
      m[j]=a[j].amount/s;
  for(int r=0;r<3;r++)
  {
      if(m[r]>0.5)  
      {
          cout <<"The winner is \t" <<a[r].name<<"\t and the amount of vote is " <<a[r].amount <<endl;
          flag=1;
      }
  }
  if(flag==0)
      cout << "The result is none,because the percent of every candidate is below 50%."<<endl;

}
int main()
{
   Candidate a[3];
   a[0]=Candidate('a',0);
   a[1]=Candidate('b',0);
   a[2]=Candidate('c',0);
   cout << "The candidates are " << a[0].name <<"\t"<<a[1].name<<"\t"<<a[2].name <<endl;
   cout <<"Please choose one of them:(put 0 can break)"<<endl;
   char c;   
   while((cin>>c)&&(c!='0'))
   {
       if(c=='a'||c=='b'||c=='c')
            Iseffvote(c,a);
       else
           cout<<"您输入的有误!"<<endl;
       cout <<"Please choose one of them:(put 0 can break)"<<endl;
   }
   Result(a);
}

If You Want Something, Go Get It, Period.
2010-09-24 20:55
无缘今生
Rank: 2
等 级:新手上路
威 望:3
帖 子:523
专家分:7
注 册:2007-6-25
收藏
得分:0 
楼上写的代码思路清晰了许多,不过还是有一点小问题,
程序代码:
while((cin>>c)&&(c!='0'))
   {
       if(c=='a'||c=='b'||c=='c')
            Iseffvote(c,a);
       else
           cout<<"您输入的有误!"<<endl;
       cout <<"Please choose one of them:(put 0 can break)"<<endl;
   }
这里只有三个候选人,代码可胜任,可要是候选人有几十个,几百个,不能可去一个一个的比较吧,
所有我觉得再加入数据结构的应用会比较好一点,比如顺序表或者链表。


时不再来!!!
2010-09-25 09:27
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:0 
这我知道啊!我是按照她的思路在写啊!她的程序只有三个人啦!

If You Want Something, Go Get It, Period.
2010-09-25 11:55
快速回复:C++中投票问题遇到的困难,大侠们帮忙啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017253 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved