我在将具有并行数组的程序转换为需要作为结构的程序时遇到了很多麻烦。
这是原始程序
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main ( void )
{
int num1,num2,sum,total=0,i;
int frequency[13]={0};
double expected[13] = {0,0,2.78,5.56,8.33,11.11,13.89,16.67,13.89,11.11,8.33,5.56,2.78};
double session[13];
int input=0;
int answer=1;
//ALGORITHM STEP 1:
printf("\n This program will roll a die as many times as YOU choose.\n");
//ALGORITHM STEP 2:
while ( answer == 1 )
{
printf("\n Please enter the number of times you would like to roll:");
scanf("%d", &input);
//ALGORITHM STEP 3:
srand(time(NULL));
i=0;
while( i<input )
{
sum=0;
num1=1+rand()%6;
num2=1+rand()%6;
sum=num1+num2;
frequency[sum]++;
i++;
}
//ALGORITHM STEP 4:
i=2;
while( i<13 )
{
session[i]= ((double)frequency[i] / input)*100 ;
i++;
}
//ALGORITHM STEP 5:
printf("\t Roll \t Session \t Expected \n ");
i=2;
while( i<13 )
{
printf("\t%d \t%2.2lf \t\t%2.2lf\n",i,session[i],expected[i]);
i++;
}
//ALGORITHM STEP 6:
printf("\n Would you like to enter another number?");
printf("\n if so press 1 if not press 0: ");
scanf("%d",&answer);
}
printf(" Goodbye! \n");
return ( 0 ) ;
}
我不知所措。这就是我到目前为止所做的,而且我在wazoo中遇到错误
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
struct dice
{
int frequency[13];
double expected[13];
double session[13];
};
int main ( void )
{
strcpy(dice_1.frequency,"0");
strcpy(dice_1.expected,"0,0,2.78,5.56,8.33,11.11,13.89,16.67,13.89,11.11,8.33,5.56,2.78");
strcpy(dice_1.session,"");
struct dice dice_1;
int num1,num2,sum,total=0,i;
int input=0;
int answer=1;
//ALGORITHM STEP 1:
printf("\n This program will roll a die as many times as YOU choose.\n");
//ALGORITHM STEP 2:
while ( answer == 1 )
{
printf("\n Please enter the number of times you would like to roll:");
scanf("%d", &input);
//ALGORITHM STEP 3:
srand(time(NULL));
i=0;
while( i<input )
{
sum=0;
num1=1+rand()%6;
num2=1+rand()%6;
sum=num1+num2;
frequency[sum]++;
i++;
}
//ALGORITHM STEP 4:
i=2;
while( i<13 )
{
session[i]= ((double)frequency[i] / input)*100 ;
i++;
}
//ALGORITHM STEP 5:
printf("\t Roll \t Session \t Expected \n ");
i=2;
while( i<13 )
{
printf("\t%d \t%2.2lf \t\t%2.2lf\n",i,session[i],expected[i]);
i++;
}
//ALGORITHM STEP 6:
printf("\n Would you like to enter another number?");
printf("\n if so press 1 if not press 0: ");
scanf("%d",&answer);
}
printf(" Goodbye! \n");
return ( 0 ) ;
}
答案 0 :(得分:0)
你需要这样做
int i=0;
char *token;
token = strtok(expected, ",");
while (token != NULL)
{
switch(i)
{
case 0:
//structure_object = your data; change according to your requirement
break;
//continue until you have data to convert
}
如果字符串使用int
,请确保数据类型必须与double
,strcpy
匹配。