什么时候应该使用与scanf函数的地址'和;'符号键? [英] When should I use scanf with the address of '&' ampersand key?

查看:159
本文介绍了什么时候应该使用与scanf函数的地址'和;'符号键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与姓名和电话号码的文本文件阅读。名字重新在(总共7)一个虚拟的选举present考生人数再present选民。如果选举人号不在7候选人的范围它被抛出,但仍保存。最后,我要打印出来谁在大选中获胜,多少宠坏票有结果。

这是我的文本文件:

 罗伯特·布卢姆
约翰·布朗
米歇尔黎明
迈克尔·霍尔
肖恩O'Rielly
亚瑟·史密斯
卡尔·怀特3 8 1 3 1 6 12 9 6 5 0 2 8 4
6 6 8 3 2 8 0 12 6 1 8 3 2 2
3 2 5 7 4 8 11 6 11 12 11 7 5 5
8 9 10 12 1 3 12 12 11 9 9 7 3 1
2 10 12 7 11 9 6 6 0 1 10 7 11 2
8 0 12 8 10 11 2 2 8 4 2 12 3 2
9 1 4 8 8 7 7 4 12 2 10 10 9 4
12 9 3 12 0 4 8 0 6 5 9 0 5 3
11 6 0 3 0

这是我在哪里卡住有关如何扫描这些在正常

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;FILE *的数据;
INT撒娇= 0;typedef结构
{
 INT票;
 焦炭名[20];
}候选人;void初始化(候选人* electionCandidates,FILE *数据)
{
    INT I;
    对于(I = 0; I&下; 7;我+ +)
    {
        的fscanf(数据,%[^ \\ n]的%* C,electionCandidates [我]。名称);
        ([I]。名称%S \\ n,electionCandidates)printf的;
        electionCandidates [I] .votes = 0;
    }}INT processVotes(候选人* electionCandidates,FILE *数据)
{
    INT I; //计算票数
    INT选民;
    对于(i = 0; I< 365;我++)
    {
       的fscanf(数据,%D,投票);
       如果(选举人&下; = 7和;&放大器;选举人大于0)
        electionCandidates [选举人-1] .votes ++;
       其他
        撒娇++;
    }                                                    //捕手抢赢家
    INT maxValue(最大值),胜者= 0;    包括maxValue = electionCandidates [0] .votes;
    对于(I = 0; I&下; 7;我+ +)
    {
        如果(包括maxValue< electionCandidates [I] .votes)
        {
            包括maxValue = electionCandidates [I] .votes;
            electionCandidates [赢家] = electionCandidates [I]
        }    }    返回electionCandidates [赢家],包括maxValue;
}无效printResults(候选人* electionCandidates)
{
    的printf(%S在大选中获胜,共%d个票。\\ n有总共%d个娇生惯养
            electionCandidates [赢家] .name和maxValue(最大值),被宠坏的);}
诠释主(){
    数据= FOPEN(elections.txt,R);
    候选electionCandidates [7];    初始化(electionCandidates,数据);
    processVotes(electionCandidates,数据);
    printResults(electionCandidates);
    FCLOSE(数据);
    返回0;
}


解决方案

在使用 scanf函数,您必须提供的地址的变量的那要扫描的结果之中。通过使用&放大器提供的地址; 运营商。此外,它是检查 scanf函数的结果,以确保它成功扫描你问什么是一个好主意。 scanf函数总是会返回成功扫描,除非发生I / O错误元素的数量,在这种情况下,它会返回一个负数。

这是你的程序的固定,注释版本:

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;typedef结构
{
 INT票;
 焦炭名[20];
}候选人;//指定一个新的类型来举行选举结果数据
typedef结构
{
  INT赢家;
  INT maxVotes;
  INT撒娇;
} electionResult;void初始化(候选人* electionCandidates,FILE *数据)
{
    INT I;
    对于(I = 0; I&下; 7;我+ +)
    {
        的fscanf(数据,%[^ \\ n]的%* C,electionCandidates [我]。名称);
        ([I]。名称%S \\ n,electionCandidates)printf的;
        electionCandidates [I] .votes = 0;
    }}//这个函数现在可以返回多个值,因为我们已经包裹
//相关信息到一个名为electionResult结构
electionResult processVotes(候选人* electionCandidates,FILE *数据)
{
    //此处(我们用数据填充)宣布选举结果结构
    //我们最初设定的所有值设置为0    electionResult呃= {0,0,0};
    INT I; //计算票数
    INT选民;
    对于(i = 0; I< 365;我++)
    {
       //通过提供选民的地址扫描票(使用&安培;)
       INT结果=的fscanf(数据,%D,&安培;选民);
       如果(结果== 1)
       {
          如果(选举人&下; = 7和;&放大器;选举人大于0)
             electionCandidates [选举人-1] .votes ++;
          其他
             er.spoilt ++;
       }
    }    er.maxVotes = electionCandidates [0] .votes;
    对于(I = 0; I&下; 7;我+ +)
    {
        如果(er.maxVotes< electionCandidates [I] .votes)
        {
            //更新选举结果结构中的值
            er.maxVotes = electionCandidates [I] .votes;
            er.winner = I;
        }
    }    返回器;
}//这个函数现在接受一个electionResult结构打印选举结果
无效printResults(候选人* electionCandidates,electionResult ER)
{
    的printf(%S在大选中获胜,共%d个票。\\ n有总共%d个娇生惯养
            electionCandidates [er.winner] .name和er.maxVotes,er.spoilt);}
诠释主(){
    FILE *数据=的fopen(elections.txt,R);
    候选electionCandidates [7];
    electionResult器;    初始化(electionCandidates,数据);
    ER = processVotes(electionCandidates,数据);
    printResults(electionCandidates,ER);
    FCLOSE(数据);
    返回0;
}

一些提示:


  • 您不能访问其他功能声明的变量。你必须从一个函数返回所需的数据并将其提供给其它功能。


  • 避免有,如果你能在文件范围内声明的变量。对于这样简单的程序,这是没有太大的问题,但在一般情况下,使用全局变量往往会变得混乱快。


  • 您不能从一个函数返回多个值,除非你在一个结构包裹起来的价值观,就像上面,或者,你已经接受功能的指针应用于对象将保存的结果,类似于如何的fscanf 接受&放大器;选民,随后填充用适当的数据选民变量(如果可以)。


I have to read in a text file with names and numbers. The names represent candidates in a dummy election (7 in total) and the numbers represent the voters. If the voter number is not in the range of the 7 candidates it gets thrown out but still stored. Finally, I have to print out the results of who won the election and how many spoilt votes there were.

This is my text file:

Robert Bloom 
John Brown 
Michelle Dawn 
Michael Hall 
Sean O’Rielly 
Arthur Smith 
Carl White 

3 8 1 3 1 6 12 9 6 5 0 2 8 4 
6 6 8 3 2 8 0 12 6 1 8 3 2 2 
3 2 5 7 4 11 8 6 11 12 11 7 5 5 
8 9 10 12 1 3 12 12 9 11 7 9 3 1 
2 10 12 7 11 9 6 6 0 1 10 7 11 2 
8   0 12 8 10 11 2 2 8 4 2 12 3 2 
9   1 4 8 8 7 7 4 12 2 10 10 9 4 
12 9 3 12 0 4 8 0 6 5 9 0 5 3 
11  6   0   3   0 

This is where I am stuck about how to scan these in properly

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

FILE * data;
int spoilt=0;

typedef struct
{
 int votes;
 char name[20];
}candidates;

void initialize( candidates *electionCandidates, FILE *data )
{
    int i;
    for( i=0; i<7; i++ )
    {
        fscanf( data, "%[^\n]%*c", electionCandidates[i].name );
        printf( "%s\n", electionCandidates[i].name );
        electionCandidates[i].votes=0;
    }

}

int processVotes( candidates *electionCandidates, FILE *data )
{
    int i;                                           //tallying votes
    int voter;
    for ( i = 0; i< 365; i++ )
    {
       fscanf( data, "%d", voter );
       if ( voter <= 7&& voter > 0 )
        electionCandidates[voter-1].votes++;
       else
        spoilt++;
    }

                                                    //catcher to grab winner
    int maxValue, winner=0;

    maxValue = electionCandidates[0].votes;
    for( i = 0; i < 7; i++ )
    {
        if( maxValue < electionCandidates[i].votes )
        {
            maxValue = electionCandidates[i].votes;
            electionCandidates[winner] = electionCandidates[i];
        }

    }

    return electionCandidates[winner], maxValue;


}

void printResults( candidates *electionCandidates )
{
    printf("%s won the election with a total of %d votes.\n There was a total of %d spoilt"
            electionCandidates[winner].name, maxValue, spoilt);

}


int main() {
    data = fopen( "elections.txt","r" );
    candidates electionCandidates[7];

    initialize( electionCandidates, data );
    processVotes( electionCandidates, data );
    printResults( electionCandidates );


    fclose( data );
    return 0;
}

解决方案

When using scanf, you must provide the address of the variable that you want to scan the result into. Provide the address by using the & operator. Also, it is a good idea to check the result of scanf to ensure that it successfully scanned what you asked for. scanf will always return the number of elements successfully scanned, unless an I/O error occurred, in which case it will return a negative number.

Here's an fixed, annotated version of your program:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct
{
 int votes;
 char name[20];
}candidates;

// specify a new type to hold the election result data
typedef struct
{
  int winner;
  int maxVotes;
  int spoilt;
} electionResult;

void initialize( candidates *electionCandidates, FILE *data )
{
    int i;
    for( i=0; i<7; i++ )
    {
        fscanf( data, "%[^\n]%*c", electionCandidates[i].name );
        printf( "%s\n", electionCandidates[i].name );
        electionCandidates[i].votes=0;
    }

}

// This function can now return more than one value, because we've wrapped
// the relevant info into a structure called "electionResult"
electionResult processVotes( candidates *electionCandidates, FILE *data )
{
    // declare the election result struct here (which we fill with data)
    // we initially set all values to 0

    electionResult er = {0, 0, 0};
    int i;                                           //tallying votes
    int voter;
    for ( i = 0; i< 365; i++ )
    {
       // scan the vote by providing the address of voter (using &)
       int result = fscanf( data, "%d", &voter );
       if (result == 1)
       {
          if ( voter <= 7&& voter > 0 )
             electionCandidates[voter-1].votes++;
          else
             er.spoilt++;
       }
    }

    er.maxVotes = electionCandidates[0].votes;
    for( i = 0; i < 7; i++ )
    {
        if( er.maxVotes < electionCandidates[i].votes )
        {
            // update the values in the election result struct
            er.maxVotes = electionCandidates[i].votes;
            er.winner = i;
        }
    }

    return er;
}

// this function now prints the result of the election by accepting an "electionResult" struct
void printResults( candidates *electionCandidates, electionResult er )
{
    printf("%s won the election with a total of %d votes.\n There was a total of %d spoilt",
            electionCandidates[er.winner].name, er.maxVotes, er.spoilt);

}


int main() {
    FILE *data = fopen( "elections.txt","r" );
    candidates electionCandidates[7];
    electionResult er;

    initialize( electionCandidates, data );
    er = processVotes( electionCandidates, data );
    printResults( electionCandidates, er );


    fclose( data );
    return 0;
}

Some tips:

  • You can't access variables declared in other functions. You must return the data you want from one function and provide it to the other function.

  • Avoid having variables declared at file scope if you can. For simple programs like this, it isn't much of an issue, but in general, using global variables tends to get messy fast.

  • You can't return more than one value from a function unless you wrap up the values in a struct, like the above, or alternatively, have your function accept pointers to the objects that will hold the result, similar to how fscanf accepts &voter and subsequently fills the voter variable with the appropriate data (if it can).

这篇关于什么时候应该使用与scanf函数的地址'和;'符号键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆