如何去掉goto并加入多個函式?


uj5u.com熱心網友回復:
就是新規一個函式,在goto陳述句的地方呼叫函式而已比如:
goto test;
test:
pass:
改為
int test(){
pass
}
test();
uj5u.com熱心網友回復:
那改后的choose函式包含下面的switch 陳述句嘛
uj5u.com熱心網友回復:
建議直接貼代碼而不是貼圖片~uj5u.com熱心網友回復:
#include <stdio.h>#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
//This is a program that will help elementary school pupils practice math.
int main()
{
char fid[10],ID[10],symbol[10],convert[3],answer[10][3];
int j=0,i,op,n,a[10],b[10],result[10],score=0,grade,timeduation;
FILE *data;
time_t tstart, tend;
do{
printf("Please input your four digit ID number such as AB1234: ");
scanf("%s",ID);
if(!(ID[6]=='\0'&&isalpha(ID[0])&&isalpha(ID[1])&&isdigit(ID[2])&&isdigit(ID[3])&&isdigit(ID[4])&&isdigit(ID[5])))
printf("Sorry, invalid ID. ");
}while(!(ID[6]=='\0'&&isalpha(ID[0])&&isalpha(ID[1])&&isdigit(ID[2])&&isdigit(ID[3])&&isdigit(ID[4])&&isdigit(ID[5])));//To check the ID invalidation
choose:
printf("\nPlease type in the number to continue.\n(1) Start a test\n(2) Check scores\n(3) Exit\n");
scanf("%d",&op);
switch(op)//let user to choose what function they want
{
case 1:
getchar();
printf("\n");
srand((unsigned)time(NULL));
time(&tstart);
for(i=0;i<10;i++)//to start the test
{
n=rand()%4;
do{
a[i]=rand()%100;
b[i]=rand()%100;
switch(n){
case 0:symbol[i] = '+';
result[i] = a[i] + b[i];
break;
case 1:symbol[i] = '-';
result[i] = a[i] - b[i];
break;
case 2:symbol[i] = '*';
result[i] = a[i] * b[i];
break;
case 3:symbol[i] = '/';
result[i] = a[i] / b[i];
}
}while(result[i] > 99 || result[i] < 0 || n==3 && a[i] % b[i] != 0);
printf("%2d %c %2d = ", a[i], symbol[i], b[i]);
itoa(result[i], convert, 10);
gets(answer[i]);//gather the user's answers
if (!strcmp(convert, answer[i])) {
score += 10;
}
}
time(&tend);//time ends
printf("\nYou have got %d score(s) in %ld seconds!\n",score,tend-tstart);
printf("Prob. | Correct Answ. | Ur Answ\n"); //print the test information
for(i=0;i<10;i++)
{
printf("(%2d) %2d %c %2d = %3d %s\n",i+1,a[i],symbol[i],b[i],result[i],answer[i]);
}
data = fopen("record.txt", "a+");
fprintf(data,"%s %3d %3ld\n",ID,score,tend-tstart);
fclose(data);
goto choose;
break;
case 2:
data=https://bbs.csdn.net/topics/fopen("record.txt","r");
if(data=https://bbs.csdn.net/topics/=NULL)
{
printf("You have never done the test before.");
goto choose;
}
fscanf(data,"%s %d %d",fid,&grade,&timeduation);//scan the user's information
printf("Your previous records are:\n");
while(!feof(data))
{
if(strncmp(ID,fid,6)==0)//check the user's name
{
printf("%s %d %d \n\n",fid,grade,timeduation);
j=1;
}
fscanf(data,"%s %d %d",fid,&grade,&timeduation);
}
if(j==0)
{
printf("Sorry, there is no previous record.\n");
}
fclose(data);
goto choose;
break;
case 3:
printf("It's about to quit! \n");
break; //quit
default:
printf("Wrong Number! ");
goto choose;
}
return 0;
}
uj5u.com熱心網友回復:
你原來的choose函式是啥功能,新規的函式就是啥功能。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/47642.html
標籤:C語言
上一篇:main.c(199): error C237: 'display': function already has a body
