测试系统
列出10个数学题
输入每道题的答案。
如果用户的答案不正确,显示出正确答案。
给出最终的分数。
用switch语句来控制加或减的运算。
代码稍后~
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ADD 0
#define MINUS 1
void main()
{
int i,a,b,answer,score=0,op,rtanswer
srand((unsigned)time(NULL))
for (i=0i<10i++)
{
a=100*rand()/RAND_MAX
b=100*rand()/RAND_MAX
op=2*rand()/RAND_MAX
switch (op)
{
case ADD:
printf("%d %d+%d=",i+1,a,b)
rtanswer=a+b
break
case MINUS:
printf("%d %d-%d=",i+1,a,b)
rtanswer=a-b
break
}
scanf("%d",&answer)
if (answer!=rtanswer)
{
printf("correct answer=%d\n",rtanswer)
}
else score++
}
printf("Your Score is %d\n",score)
}
////////////////////////////////第一题/*
Question 1 - 15 marks
Write a C program that prints out all dierent possibilities of obtaining $2(2
pounds) using coins of values 2 pence, 5 pence, and 10 pence. Indicate how
many possibilities have been found. The output of your program may look like:
$2 = 100 x 2p
$2 = 90 x 2p + 4 x 5p
$2 = 80 x 2p + 8 x 5p ....
In total, there are ??? possibilities to make $2.
Note that ??? (no. of the possibilities) is calculated by your C program.
*/
#include<stdio.h>
void main()
{
int count2,count5,count10
int count=0
int sum
count=0
for( count2=100count2>=0 count2=count2-1 )
for( count5=40count5>=0count5=count5-1 )
for( count10=20count10>=0count10=count10-1 )
{
sum=count2*2 + count5*5 + count10*10
if( sum==200 )
{
printf("$2 =")
if( count2!=0 )
printf(" +%d X 2p",count2)
if( count5!=0 )
printf(" +%d X 5p",count5)
if( count10!=0 )
printf(" +%d X 10p",count10)
printf("\n")
count++
}
}
printf("In total, there are %d possibilities to make $2.\n",count)
}
///////////////////////////////////////////第二题
/*
Question 2 - 15 marks
Write a C program that reads m x n matrix "A" and p x q matrix "B", checks
whether these matrices are multipliable in either order or not (e.g. whether A
x B or B x A is dened). Further, if A x B or B x A is dened then calculates
the product and prints out the result.
*/
#include<stdio.h>
#define MAX 256
void SetZero( int a[MAX][MAX] )
void Print( int a[MAX][MAX], int r, int c )
void MultiplyMatrix( int a[MAX][MAX],int m,int n, int b[MAX][MAX],int p, int q )
int result[MAX][MAX]//存放结果
/* 矩阵相乘 */
void MultiplyMatrix( int a[MAX][MAX],int m,int n, int b[MAX][MAX],int p, int q )
{
int row,column
SetZero(result)
if( n==p )
{
for( row=0row<mrow++ )
for( column=0column<qcolumn++ )
for( n=0n<pn++ )
result[row][column] += a[row][n] * b[n][column]
}
else if( q==m )
{
for( row=0row<prow++ )
for( column=0column<ncolumn++ )
for( m=0m<qm++ )
result[row][column] += b[row][m] * a[m][column]
}
else
printf("can't multiply.\n")
}
/* 输出矩阵 */
void Print( int a[MAX][MAX], int r, int c )
{
int i,j
for( i=0i<ri++ )
{
for( j=0j<cj++ )
printf("%4d",a[i][j])
printf("\n")
}
}
/*将矩阵元素设为0 */
void SetZero( int a[MAX][MAX] )
{
int i,j
for( i=0i<MAXi++ )
for( j=0j<MAXj++ )
a[i][j]=0
}
void main()
{
int a[MAX][MAX]
int b[MAX][MAX]
int m,n//a[m][n]
int p,q//b[p][q]
int i,j
char f='A'// f=='L' A*B
// F=='R' B*A
SetZero(a)
SetZero(b)
/* 输入矩阵A的元素,需先输入m和n(矩阵的行列) */
printf("Input m*n matrix \"A\" \n")
printf("m=")scanf("%d",&m)
printf("n=")scanf("%d",&n)
printf("matrixA:\n")
for( i=0i<mi++ )
for( j=0j<nj++ )
scanf("%d",&a[i][j])
/* 输入矩阵B的元素,需先输入p和q(矩阵的行列) */
printf("Input p*q matrix \"B\" \n")
printf("p=")scanf("%d",&p)
printf("q=")scanf("%d",&q)
printf("matrixB:\n")
for( i=0i<pi++ )
for( j=0j<qj++ )
scanf("%d",&b[i][j])
if( n==p )
{
MultiplyMatrix( a,m,n, b,p,q )
f='L'
}
else if( q==m )
{
MultiplyMatrix( b,p,q, a,m,n )
f='R'
}
else
{
printf("can't multiply.\n")
f='A'
}
printf(" matrix A\n")
Print( a, m, n )
printf(" matrix B\n")
Print( b, p, q )
if( f=='L' )
{
printf(" A*B\n")
Print( result, m, q )
}
else if( f=='R' )
{
printf(" B*A\n")
Print( result, p, n )
}
}
//////////////////////////////////第三题
/*
Question 3 - 20 marks
Write a C program that initializes an array of integer and then copies the con-
tents of the array into two other arrays. Declare all arrays in the main program.
To make the rst copy, write a function, which uses the array notation (the
square brackets []) to access the elements of the array.
To make the second copy, write a function that uses the pointer notation and
pointer incrementing to access the elements of the arrays.
Each function takes the name of the source array, the name of the target/destination
1
array and the number of elements to be copied as function arguments.
Here is an example showing how the functions should be called giving the fol-
lowing declarations:
int source[4] = {1,2,4,6}
int destination1[4]
int destination2[4]
copy_array(source, destination1, 4)
copy_ptr(source, destination2, 4)
*/
#include<stdio.h>
void Print( int s[], int n )
{
int i
for( i=0i<ni++ )
printf("%4d",s[i])
printf("\n")
}
void copy_ptr( int *s, int *d, int n )
{
for( n--n>=0n-- )
{
*d=*s
d++
s++
}
}
void copy_array( int s[], int d[], int n )
{
int i
for( i=0i<ni++ )
d[i]=s[i]
}
void main()
{
int source[4] = {1,2,4,6}
int destination1[4]
int destination2[4]
copy_array(source, destination1, 4)
copy_ptr(source, destination2, 4)
Print( source, 4 )
printf("source[] = ")
Print( source, 4 )
printf("destination1[] = ")
Print( destination1, 4 )
printf("destination2[] = ")
Print( destination2, 4 )
}
////////////////////////////////第四题
晚上我再写第四题吧 有点长
两种方法我写在一起,可以独立拆开。
#include <stdio.h>
void finda1(char a[3][10])
void finda2(char a[3][10])
void show(char (*p)[10])
int main()
{
char a[3][10]={{"gehajl"},{"788a987a7"},{"ccabbbabbb"}}
printf("原数组内容:\n")
show(a)
printf("\n1、用数组指针的方法(函数finda1):\n")
finda1(a)
printf("执行后:\n")
show(a)
printf("\n---------------------\n")
char b[3][10]={{"gehajl"},{"788a987a7"},{"ccabbbabbb"}}
printf("原数组内容:\n")
show(a)
printf("\n2、用指针数组的方法(函数finda2):\n")
finda2(b)
printf("执行后:\n")
show(b)
return 0
}
void finda1(char a[3][10])
{
int i,j
char (*p)[10]=a
for(i=0i<3i++)
for(j=0j<10j++)
if(p[i][j]=='a')
printf("发现:第%d行第%d个元素是‘a’,已替换\n",i+1,j+1),p[i][j]='1'
}
void finda2(char a[3][10])
{
int i,j
char *p[3]={&a[0][0],&a[1][0],&a[2][0]}
for(i=0i<3i++)
for(j=0j<10j++)
if(p[i][j]=='a')
printf("发现:第%d行第%d个元素是‘a’,已替换\n",i+1,j+1),p[i][j]='1'
}
void show(char (*p)[10])
{
int i,j
for(i=0i<3i++,printf("\n"))
for(j=0j<10j++)
printf("%c ",p[i][j])
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)